blob: 9caebdd599f2d0ae3ec57dc9f5402d6c737d3838 [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 Tarreaufbee7132007-10-18 13:53:22 +020051#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020052#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020053#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020054#include <proto/server.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010055#include <proto/stream_interface.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020056#include <proto/stream_sock.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010057#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020058
Simon Horman9bd2c732011-06-15 15:18:44 +090059static int stats_dump_raw_to_buffer(struct stream_interface *si);
60static int stats_dump_full_sess_to_buffer(struct stream_interface *si);
61static int stats_dump_sess_to_buffer(struct stream_interface *si);
62static int stats_dump_errors_to_buffer(struct stream_interface *si);
Simon Hormanc88b8872011-06-15 15:18:49 +090063static int stats_table_request(struct stream_interface *si, bool show);
Simon Horman9bd2c732011-06-15 15:18:44 +090064static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
65static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri);
66
67static struct si_applet cli_applet;
68
69static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020070 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +020071 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +020072 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020073 " help : this message\n"
74 " prompt : toggle interactive mode with prompt\n"
75 " quit : disconnect\n"
76 " show info : report information about the running process\n"
77 " show stat : report counters for each proxy and server\n"
78 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +010079 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +020080 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +020081 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +020082 " set weight : change a server's weight\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +010083 " set timeout : change a timeout setting\n"
Cyril Bontécd19e512010-01-31 22:34:03 +010084 " disable server : set a server in maintenance mode\n"
85 " enable server : re-enable a server that was previously in maintenance mode\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020086 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020087
Simon Horman9bd2c732011-06-15 15:18:44 +090088static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +020089 "Permission denied\n"
90 "";
91
Willy Tarreau295a8372011-03-10 11:25:07 +010092/* data transmission states for the stats responses */
93enum {
94 STAT_ST_INIT = 0,
95 STAT_ST_HEAD,
96 STAT_ST_INFO,
97 STAT_ST_LIST,
98 STAT_ST_END,
99 STAT_ST_FIN,
100};
101
102/* data transmission states for the stats responses inside a proxy */
103enum {
104 STAT_PX_ST_INIT = 0,
105 STAT_PX_ST_TH,
106 STAT_PX_ST_FE,
107 STAT_PX_ST_LI,
108 STAT_PX_ST_SV,
109 STAT_PX_ST_BE,
110 STAT_PX_ST_END,
111 STAT_PX_ST_FIN,
112};
113
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200114/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +0200115 * a new stats socket. It returns a positive value upon success, 0 if the connection
116 * needs to be closed and ignored, or a negative value upon critical failure.
117 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900118static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200119{
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200120 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100121 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200122 copy_target(&s->target, &s->si[1].target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +0100123 s->si[1].applet.private = s;
124 s->si[1].applet.st1 = 0;
125 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200126
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200127 tv_zero(&s->logs.tv_request);
128 s->logs.t_queue = 0;
129 s->logs.t_connect = 0;
130 s->logs.t_data = 0;
131 s->logs.t_close = 0;
132 s->logs.bytes_in = s->logs.bytes_out = 0;
133 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
134 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200135
Willy Tarreaueb472682010-05-28 18:46:57 +0200136 s->req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
137
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200138 if (s->listener->timeout) {
139 s->req->rto = *s->listener->timeout;
140 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200141 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200142 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200143}
144
Willy Tarreau07e9e642010-08-17 21:48:17 +0200145/* allocate a new stats frontend named <name>, and return it
146 * (or NULL in case of lack of memory).
147 */
148static struct proxy *alloc_stats_fe(const char *name)
149{
150 struct proxy *fe;
151
152 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
153 if (!fe)
154 return NULL;
155
Willy Tarreau237250c2011-07-29 01:49:03 +0200156 init_new_proxy(fe);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200157
158 fe->last_change = now.tv_sec;
159 fe->id = strdup("GLOBAL");
160 fe->cap = PR_CAP_FE;
161 return fe;
162}
163
Willy Tarreaufbee7132007-10-18 13:53:22 +0200164/* This function parses a "stats" statement in the "global" section. It returns
165 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
166 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
167 * zero included. The trailing '\n' must not be written. The function must be
168 * called with <args> pointing to the first word after "stats".
169 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200170static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
171 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200172{
Willy Tarreau10522fd2008-07-09 20:12:41 +0200173 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200174 if (!strcmp(args[0], "socket")) {
Andreas Kohn16171e22011-01-19 20:29:32 +0100175 struct sockaddr_un *su;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200176 int cur_arg;
177
178 if (*args[1] == 0) {
179 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
180 return -1;
181 }
182
183 if (global.stats_sock.state != LI_NEW) {
184 snprintf(err, errlen, "'stats socket' already specified in global section");
185 return -1;
186 }
187
Andreas Kohn16171e22011-01-19 20:29:32 +0100188 su = str2sun(args[1]);
189 if (!su) {
190 snprintf(err, errlen, "'stats socket' path would require truncation");
191 return -1;
192 }
193 memcpy(&global.stats_sock.addr, su, sizeof(struct sockaddr_un)); // guaranteed to fit
Willy Tarreaufbee7132007-10-18 13:53:22 +0200194
Willy Tarreau89a63132009-08-16 17:41:45 +0200195 if (!global.stats_fe) {
Willy Tarreau07e9e642010-08-17 21:48:17 +0200196 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau89a63132009-08-16 17:41:45 +0200197 snprintf(err, errlen, "out of memory");
198 return -1;
199 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200200 global.stats_fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreau89a63132009-08-16 17:41:45 +0200201 }
202
Willy Tarreaufbee7132007-10-18 13:53:22 +0200203 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100204 global.stats_sock.options = LI_O_NONE;
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200205 global.stats_sock.accept = session_accept;
206 global.stats_fe->accept = stats_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200207 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200208 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200209 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreaueb472682010-05-28 18:46:57 +0200210 global.stats_sock.frontend = global.stats_fe;
Willy Tarreau6162db22009-10-10 17:13:00 +0200211 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau07e9e642010-08-17 21:48:17 +0200212 global.stats_fe->maxconn = global.stats_sock.maxconn;
Willy Tarreau89a63132009-08-16 17:41:45 +0200213 global.stats_sock.timeout = &global.stats_fe->timeout.client;
214
215 global.stats_sock.next = global.stats_fe->listen;
216 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200217
218 cur_arg = 2;
219 while (*args[cur_arg]) {
220 if (!strcmp(args[cur_arg], "uid")) {
221 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
222 cur_arg += 2;
223 }
224 else if (!strcmp(args[cur_arg], "gid")) {
225 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
226 cur_arg += 2;
227 }
228 else if (!strcmp(args[cur_arg], "mode")) {
229 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
230 cur_arg += 2;
231 }
232 else if (!strcmp(args[cur_arg], "user")) {
233 struct passwd *user;
234 user = getpwnam(args[cur_arg + 1]);
235 if (!user) {
236 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
237 args[cur_arg + 1]);
238 return -1;
239 }
240 global.stats_sock.perm.ux.uid = user->pw_uid;
241 cur_arg += 2;
242 }
243 else if (!strcmp(args[cur_arg], "group")) {
244 struct group *group;
245 group = getgrnam(args[cur_arg + 1]);
246 if (!group) {
247 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
248 args[cur_arg + 1]);
249 return -1;
250 }
251 global.stats_sock.perm.ux.gid = group->gr_gid;
252 cur_arg += 2;
253 }
Willy Tarreau6162db22009-10-10 17:13:00 +0200254 else if (!strcmp(args[cur_arg], "level")) {
255 if (!strcmp(args[cur_arg+1], "user"))
256 global.stats_sock.perm.ux.level = ACCESS_LVL_USER;
257 else if (!strcmp(args[cur_arg+1], "operator"))
258 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER;
259 else if (!strcmp(args[cur_arg+1], "admin"))
260 global.stats_sock.perm.ux.level = ACCESS_LVL_ADMIN;
261 else {
262 snprintf(err, errlen, "'stats socket level' only supports 'user', 'operator', and 'admin'");
263 return -1;
264 }
265 cur_arg += 2;
266 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200267 else {
Willy Tarreau6162db22009-10-10 17:13:00 +0200268 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', 'level', and 'mode'");
Willy Tarreaufbee7132007-10-18 13:53:22 +0200269 return -1;
270 }
271 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100272
Willy Tarreaufbee7132007-10-18 13:53:22 +0200273 uxst_add_listener(&global.stats_sock);
274 global.maxsock++;
275 }
276 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100277 unsigned timeout;
278 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
279
280 if (res) {
281 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
282 return -1;
283 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200284
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100285 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200286 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
287 return -1;
288 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200289 if (!global.stats_fe) {
290 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
291 snprintf(err, errlen, "out of memory");
292 return -1;
293 }
294 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200295 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200296 }
297 else if (!strcmp(args[0], "maxconn")) {
298 int maxconn = atol(args[1]);
299
300 if (maxconn <= 0) {
301 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
302 return -1;
303 }
304 global.maxsock -= global.stats_sock.maxconn;
305 global.stats_sock.maxconn = maxconn;
306 global.maxsock += global.stats_sock.maxconn;
Willy Tarreaueb472682010-05-28 18:46:57 +0200307 if (global.stats_fe)
308 global.stats_fe->maxconn = global.stats_sock.maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200309 }
310 else {
311 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
312 return -1;
313 }
314 return 0;
315}
316
Simon Horman9bd2c732011-06-15 15:18:44 +0900317static int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100318{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200319 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100320 "# pxname,svname,"
321 "qcur,qmax,"
322 "scur,smax,slim,stot,"
323 "bin,bout,"
324 "dreq,dresp,"
325 "ereq,econ,eresp,"
326 "wretr,wredis,"
327 "status,weight,act,bck,"
328 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200329 "pid,iid,sid,throttle,lbtot,tracked,type,"
330 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200331 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100332 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100333 "req_rate,req_rate_max,req_tot,"
334 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100335 "\n");
336}
337
Simon Hormand9366582011-06-15 15:18:45 +0900338/* print a string of text buffer to <out>. The format is :
339 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
340 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
341 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
342 */
343static int dump_text(struct chunk *out, const char *buf, int bsize)
344{
345 unsigned char c;
346 int ptr = 0;
347
348 while (buf[ptr] && ptr < bsize) {
349 c = buf[ptr];
350 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
351 if (out->len > out->size - 1)
352 break;
353 out->str[out->len++] = c;
354 }
355 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
356 if (out->len > out->size - 2)
357 break;
358 out->str[out->len++] = '\\';
359 switch (c) {
360 case ' ': c = ' '; break;
361 case '\t': c = 't'; break;
362 case '\n': c = 'n'; break;
363 case '\r': c = 'r'; break;
364 case '\e': c = 'e'; break;
365 case '\\': c = '\\'; break;
366 }
367 out->str[out->len++] = c;
368 }
369 else {
370 if (out->len > out->size - 4)
371 break;
372 out->str[out->len++] = '\\';
373 out->str[out->len++] = 'x';
374 out->str[out->len++] = hextab[(c >> 4) & 0xF];
375 out->str[out->len++] = hextab[c & 0xF];
376 }
377 ptr++;
378 }
379
380 return ptr;
381}
382
383/* print a buffer in hexa.
384 * Print stopped if <bsize> is reached, or if no more place in the chunk.
385 */
386static int dump_binary(struct chunk *out, const char *buf, int bsize)
387{
388 unsigned char c;
389 int ptr = 0;
390
391 while (ptr < bsize) {
392 c = buf[ptr];
393
394 if (out->len > out->size - 2)
395 break;
396 out->str[out->len++] = hextab[(c >> 4) & 0xF];
397 out->str[out->len++] = hextab[c & 0xF];
398
399 ptr++;
400 }
401 return ptr;
402}
403
404/* Dump the status of a table to a stream interface's
405 * read buffer. It returns 0 if the output buffer is full
406 * and needs to be called again, otherwise non-zero.
407 */
408static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
409 struct proxy *proxy, struct proxy *target)
410{
411 struct session *s = si->applet.private;
412
413 chunk_printf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
414 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
415
416 /* any other information should be dumped here */
417
418 if (target && s->listener->perm.ux.level < ACCESS_LVL_OPER)
419 chunk_printf(msg, "# contents not dumped due to insufficient privileges\n");
420
421 if (buffer_feed_chunk(si->ib, msg) >= 0)
422 return 0;
423
424 return 1;
425}
426
427/* Dump the a table entry to a stream interface's
428 * read buffer. It returns 0 if the output buffer is full
429 * and needs to be called again, otherwise non-zero.
430 */
431static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
432 struct proxy *proxy, struct stksess *entry)
433{
434 int dt;
435
436 chunk_printf(msg, "%p:", entry);
437
438 if (proxy->table.type == STKTABLE_TYPE_IP) {
439 char addr[INET_ADDRSTRLEN];
440 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
441 chunk_printf(msg, " key=%s", addr);
442 }
443 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
444 char addr[INET6_ADDRSTRLEN];
445 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
446 chunk_printf(msg, " key=%s", addr);
447 }
448 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
449 chunk_printf(msg, " key=%u", *(unsigned int *)entry->key.key);
450 }
451 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
452 chunk_printf(msg, " key=");
453 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
454 }
455 else {
456 chunk_printf(msg, " key=");
457 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
458 }
459
460 chunk_printf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
461
462 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
463 void *ptr;
464
465 if (proxy->table.data_ofs[dt] == 0)
466 continue;
467 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
468 chunk_printf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
469 else
470 chunk_printf(msg, " %s=", stktable_data_types[dt].name);
471
472 ptr = stktable_data_ptr(&proxy->table, entry, dt);
473 switch (stktable_data_types[dt].std_type) {
474 case STD_T_SINT:
475 chunk_printf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
476 break;
477 case STD_T_UINT:
478 chunk_printf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
479 break;
480 case STD_T_ULL:
481 chunk_printf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
482 break;
483 case STD_T_FRQP:
484 chunk_printf(msg, "%d",
485 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
486 proxy->table.data_arg[dt].u));
487 break;
488 }
489 }
490 chunk_printf(msg, "\n");
491
492 if (buffer_feed_chunk(si->ib, msg) >= 0)
493 return 0;
494
495 return 1;
496}
497
Simon Horman17bce342011-06-15 15:18:47 +0900498static void stats_sock_table_key_request(struct stream_interface *si, char **args, bool show)
Simon Horman121f3052011-06-15 15:18:46 +0900499{
500 struct session *s = si->applet.private;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900501 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900502 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900503 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900504 unsigned char ip6_key[sizeof(struct in6_addr)];
Simon Horman121f3052011-06-15 15:18:46 +0900505
Simon Hormand5b9fd92011-06-15 15:18:48 +0900506 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900507
508 if (!*args[4]) {
509 si->applet.ctx.cli.msg = "Key value expected\n";
510 si->applet.st0 = STAT_CLI_PRINT;
511 return;
512 }
513
Simon Hormanc5b89f62011-06-15 15:18:50 +0900514 switch (px->table.type) {
515 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900516 uint32_key = htonl(inetaddr_host(args[4]));
517 static_table_key.key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900518 break;
519 case STKTABLE_TYPE_IPV6:
520 inet_pton(AF_INET6, args[4], ip6_key);
521 static_table_key.key = &ip6_key;
522 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900523 case STKTABLE_TYPE_INTEGER:
524 {
525 char *endptr;
526 unsigned long val;
527 errno = 0;
528 val = strtoul(args[4], &endptr, 10);
529 if ((errno == ERANGE && val == ULONG_MAX) ||
530 (errno != 0 && val == 0) || endptr == args[4] ||
531 val > 0xffffffff) {
532 si->applet.ctx.cli.msg = "Invalid key\n";
533 si->applet.st0 = STAT_CLI_PRINT;
534 return;
535 }
536 uint32_key = (uint32_t) val;
537 static_table_key.key = &uint32_key;
538 break;
539 }
540 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900541 case STKTABLE_TYPE_STRING:
542 static_table_key.key = args[4];
543 static_table_key.key_len = strlen(args[4]);
544 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900545 default:
Simon Horman17bce342011-06-15 15:18:47 +0900546 if (show)
Simon Hormancec9a222011-06-15 15:18:51 +0900547 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 +0900548 else
Simon Hormancec9a222011-06-15 15:18:51 +0900549 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 +0900550 si->applet.st0 = STAT_CLI_PRINT;
551 return;
552 }
553
554 /* check permissions */
555 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
556 si->applet.ctx.cli.msg = stats_permission_denied_msg;
557 si->applet.st0 = STAT_CLI_PRINT;
558 return;
559 }
560
561 ts = stktable_lookup_key(&px->table, &static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900562 if (!ts)
563 return;
564
565 if (show) {
566 struct chunk msg;
567 chunk_init(&msg, trash, sizeof(trash));
568 if (!stats_dump_table_head_to_buffer(&msg, si, px, px))
569 return;
570 stats_dump_table_entry_to_buffer(&msg, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900571 return;
572 }
Simon Horman17bce342011-06-15 15:18:47 +0900573
574 if (ts->ref_cnt) {
Simon Horman121f3052011-06-15 15:18:46 +0900575 /* don't delete an entry which is currently referenced */
576 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
577 si->applet.st0 = STAT_CLI_PRINT;
578 return;
579 }
580
581 stksess_kill(&px->table, ts);
582}
583
Simon Hormand5b9fd92011-06-15 15:18:48 +0900584static void stats_sock_table_data_request(struct stream_interface *si, char **args)
585{
586 /* condition on stored data value */
587 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
588 if (si->applet.ctx.table.data_type < 0) {
589 si->applet.ctx.cli.msg = "Unknown data type\n";
590 si->applet.st0 = STAT_CLI_PRINT;
591 return;
592 }
593
594 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
595 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
596 si->applet.st0 = STAT_CLI_PRINT;
597 return;
598 }
599
600 si->applet.ctx.table.data_op = get_std_op(args[4]);
601 if (si->applet.ctx.table.data_op < 0) {
602 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
603 si->applet.st0 = STAT_CLI_PRINT;
604 return;
605 }
606
607 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
608 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
609 si->applet.st0 = STAT_CLI_PRINT;
610 return;
611 }
612}
613
614static void stats_sock_table_request(struct stream_interface *si, char **args, bool show)
615{
616 si->applet.ctx.table.data_type = -1;
617 si->applet.state = STAT_ST_INIT;
618 si->applet.ctx.table.proxy = NULL;
619 si->applet.ctx.table.entry = NULL;
Simon Hormanc88b8872011-06-15 15:18:49 +0900620 if (show)
621 si->applet.st0 = STAT_CLI_O_TAB;
622 else
623 si->applet.st0 = STAT_CLI_O_CLR;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900624
625 if (*args[2]) {
626 si->applet.ctx.table.target = find_stktable(args[2]);
627 if (!si->applet.ctx.table.target) {
628 si->applet.ctx.cli.msg = "No such table\n";
629 si->applet.st0 = STAT_CLI_PRINT;
630 return;
631 }
632 }
633 else {
634 if (!show)
635 goto err_args;
636 return;
637 }
638
639 if (strcmp(args[3], "key") == 0)
640 stats_sock_table_key_request(si, args, show);
Simon Hormanc88b8872011-06-15 15:18:49 +0900641 else if (strncmp(args[3], "data.", 5) == 0)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900642 stats_sock_table_data_request(si, args);
Simon Hormanc88b8872011-06-15 15:18:49 +0900643 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900644 goto err_args;
645
646 return;
647
648err_args:
649 if (show)
650 si->applet.ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
651 else
Simon Hormanc88b8872011-06-15 15:18:49 +0900652 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 +0900653 si->applet.st0 = STAT_CLI_PRINT;
654}
655
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200656/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200657 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100658 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200659 * designating the function which will have to process the request, which can
660 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200661 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900662static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200663{
Willy Tarreaubc4af052011-02-13 13:25:14 +0100664 struct session *s = si->applet.private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200665 char *args[MAX_STATS_ARGS + 1];
666 int arg;
667
668 while (isspace((unsigned char)*line))
669 line++;
670
671 arg = 0;
672 args[arg] = line;
673
674 while (*line && arg < MAX_STATS_ARGS) {
675 if (isspace((unsigned char)*line)) {
676 *line++ = '\0';
677
678 while (isspace((unsigned char)*line))
679 line++;
680
681 args[++arg] = line;
682 continue;
683 }
684
685 line++;
686 }
687
688 while (++arg <= MAX_STATS_ARGS)
689 args[arg] = line;
690
Willy Tarreau295a8372011-03-10 11:25:07 +0100691 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200692 if (strcmp(args[0], "show") == 0) {
693 if (strcmp(args[1], "stat") == 0) {
694 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100695 si->applet.ctx.stats.flags |= STAT_BOUND;
696 si->applet.ctx.stats.iid = atoi(args[2]);
697 si->applet.ctx.stats.type = atoi(args[3]);
698 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200699 }
700
Willy Tarreau295a8372011-03-10 11:25:07 +0100701 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
702 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
703 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100704 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200705 }
706 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100707 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
708 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
709 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100710 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200711 }
712 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100713 si->applet.state = STAT_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200714 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100715 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100716 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200717 return 1;
718 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100719 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100720 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100721 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100722 si->applet.ctx.sess.target = NULL;
723 si->applet.ctx.sess.section = 0; /* start with session status */
724 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100725 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200726 }
727 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200728 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100729 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100730 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200731 return 1;
732 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200733 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100734 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200735 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100736 si->applet.ctx.errors.iid = -1;
737 si->applet.ctx.errors.px = NULL;
738 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100739 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200740 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200741 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900742 stats_sock_table_request(si, args, true);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200743 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200744 else { /* neither "stat" nor "info" nor "sess" nor "errors" no "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200745 return 0;
746 }
747 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200748 else if (strcmp(args[0], "clear") == 0) {
749 if (strcmp(args[1], "counters") == 0) {
750 struct proxy *px;
751 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200752 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200753 int clrall = 0;
754
755 if (strcmp(args[2], "all") == 0)
756 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200757
Willy Tarreau6162db22009-10-10 17:13:00 +0200758 /* check permissions */
759 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
760 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100761 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100762 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200763 return 1;
764 }
765
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200766 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100767 if (clrall) {
768 memset(&px->be_counters, 0, sizeof(px->be_counters));
769 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
770 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200771 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100772 px->be_counters.conn_max = 0;
773 px->be_counters.p.http.rps_max = 0;
774 px->be_counters.sps_max = 0;
775 px->be_counters.cps_max = 0;
776 px->be_counters.nbpend_max = 0;
777
778 px->fe_counters.conn_max = 0;
779 px->fe_counters.p.http.rps_max = 0;
780 px->fe_counters.sps_max = 0;
781 px->fe_counters.cps_max = 0;
782 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200783 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200784
785 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200786 if (clrall)
787 memset(&sv->counters, 0, sizeof(sv->counters));
788 else {
789 sv->counters.cur_sess_max = 0;
790 sv->counters.nbpend_max = 0;
791 sv->counters.sps_max = 0;
792 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200793
794 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200795 if (li->counters) {
796 if (clrall)
797 memset(li->counters, 0, sizeof(*li->counters));
798 else
799 li->counters->conn_max = 0;
800 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200801 }
802
803 return 1;
804 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200805 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900806 stats_sock_table_request(si, args, false);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200807 /* end of processing */
808 return 1;
809 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200810 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200811 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200812 return 0;
813 }
814 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200815 else if (strcmp(args[0], "get") == 0) {
816 if (strcmp(args[1], "weight") == 0) {
817 struct proxy *px;
818 struct server *sv;
819
820 /* split "backend/server" and make <line> point to server */
821 for (line = args[2]; *line; line++)
822 if (*line == '/') {
823 *line++ = '\0';
824 break;
825 }
826
827 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100828 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100829 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200830 return 1;
831 }
832
833 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100834 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100835 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200836 return 1;
837 }
838
839 /* return server's effective weight at the moment */
840 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
841 buffer_feed(si->ib, trash);
842 return 1;
843 }
844 else { /* not "get weight" */
845 return 0;
846 }
847 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200848 else if (strcmp(args[0], "set") == 0) {
849 if (strcmp(args[1], "weight") == 0) {
850 struct proxy *px;
851 struct server *sv;
852 int w;
853
854 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100855 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100856 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200857 return 1;
858 }
859
860 /* split "backend/server" and make <line> point to server */
861 for (line = args[2]; *line; line++)
862 if (*line == '/') {
863 *line++ = '\0';
864 break;
865 }
866
867 if (!*line || !*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100868 si->applet.ctx.cli.msg = "Require 'backend/server' and 'weight' or 'weight%'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100869 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200870 return 1;
871 }
872
873 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100874 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100875 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200876 return 1;
877 }
878
Cyril Bonté613f0df2011-03-03 20:49:04 +0100879 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100880 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100881 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100882 return 1;
883 }
884
Willy Tarreau4483d432009-10-10 19:30:08 +0200885 /* if the weight is terminated with '%', it is set relative to
886 * the initial weight, otherwise it is absolute.
887 */
888 w = atoi(args[3]);
889 if (strchr(args[3], '%') != NULL) {
890 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100891 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100892 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200893 return 1;
894 }
895 w = sv->iweight * w / 100;
896 }
897 else {
898 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100899 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100900 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200901 return 1;
902 }
903 }
904
905 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100906 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 +0100907 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200908 return 1;
909 }
910
911 sv->uweight = w;
912
913 if (px->lbprm.algo & BE_LB_PROP_DYN) {
914 /* we must take care of not pushing the server to full throttle during slow starts */
915 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
916 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
917 else
918 sv->eweight = BE_WEIGHT_SCALE;
919 sv->eweight *= sv->uweight;
920 } else {
921 sv->eweight = sv->uweight;
922 }
923
924 /* static LB algorithms are a bit harder to update */
925 if (px->lbprm.update_server_eweight)
926 px->lbprm.update_server_eweight(sv);
927 else if (sv->eweight)
928 px->lbprm.set_server_status_up(sv);
929 else
930 px->lbprm.set_server_status_down(sv);
931
932 return 1;
933 }
Willy Tarreau7aabd112010-01-26 10:59:06 +0100934 else if (strcmp(args[1], "timeout") == 0) {
935 if (strcmp(args[2], "cli") == 0) {
936 unsigned timeout;
937 const char *res;
938
939 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100940 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100941 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100942 return 1;
943 }
944
945 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
946 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100947 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100948 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100949 return 1;
950 }
951
952 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
953 return 1;
954 }
955 else {
Willy Tarreau295a8372011-03-10 11:25:07 +0100956 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100957 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100958 return 1;
959 }
960 }
961 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +0200962 return 0;
963 }
964 }
Cyril Bontécd19e512010-01-31 22:34:03 +0100965 else if (strcmp(args[0], "enable") == 0) {
966 if (strcmp(args[1], "server") == 0) {
967 struct proxy *px;
968 struct server *sv;
969
970 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100971 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100972 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100973 return 1;
974 }
975
976 /* split "backend/server" and make <line> point to server */
977 for (line = args[2]; *line; line++)
978 if (*line == '/') {
979 *line++ = '\0';
980 break;
981 }
982
983 if (!*line || !*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100984 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100985 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100986 return 1;
987 }
988
989 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100990 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100991 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100992 return 1;
993 }
994
Cyril Bonté613f0df2011-03-03 20:49:04 +0100995 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100996 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100997 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100998 return 1;
999 }
1000
Cyril Bontécd19e512010-01-31 22:34:03 +01001001 if (sv->state & SRV_MAINTAIN) {
1002 /* The server is really in maintenance, we can change the server state */
1003 if (sv->tracked) {
1004 /* If this server tracks the status of another one,
1005 * we must restore the good status.
1006 */
1007 if (sv->tracked->state & SRV_RUNNING) {
1008 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001009 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001010 } else {
1011 sv->state &= ~SRV_MAINTAIN;
1012 set_server_down(sv);
1013 }
1014 } else {
1015 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001016 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001017 }
1018 }
1019
1020 return 1;
1021 }
1022 else { /* unknown "enable" parameter */
1023 return 0;
1024 }
1025 }
1026 else if (strcmp(args[0], "disable") == 0) {
1027 if (strcmp(args[1], "server") == 0) {
1028 struct proxy *px;
1029 struct server *sv;
1030
1031 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001032 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001033 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001034 return 1;
1035 }
1036
1037 /* split "backend/server" and make <line> point to server */
1038 for (line = args[2]; *line; line++)
1039 if (*line == '/') {
1040 *line++ = '\0';
1041 break;
1042 }
1043
1044 if (!*line || !*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001045 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001046 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001047 return 1;
1048 }
1049
1050 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001051 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001052 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001053 return 1;
1054 }
1055
Cyril Bonté613f0df2011-03-03 20:49:04 +01001056 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001057 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001058 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001059 return 1;
1060 }
1061
Cyril Bontécd19e512010-01-31 22:34:03 +01001062 if (! (sv->state & SRV_MAINTAIN)) {
1063 /* Not already in maintenance, we can change the server state */
1064 sv->state |= SRV_MAINTAIN;
1065 set_server_down(sv);
1066 }
1067
1068 return 1;
1069 }
1070 else { /* unknown "disable" parameter */
1071 return 0;
1072 }
1073 }
1074 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001075 return 0;
1076 }
1077 return 1;
1078}
1079
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001080/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001081 * used to processes I/O from/to the stats unix socket. The system relies on a
1082 * state machine handling requests and various responses. We read a request,
1083 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001084 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1085 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001086 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001087 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001088static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001089{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001090 struct buffer *req = si->ob;
1091 struct buffer *res = si->ib;
1092 int reql;
1093 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001094
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001095 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1096 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001097
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001098 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001099 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001100 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001101 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001102 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001103 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001104 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001105 /* Let's close for real now. We just close the request
1106 * side, the conditions below will complete if needed.
1107 */
1108 si->shutw(si);
1109 break;
1110 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001111 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001112 /* ensure we have some output room left in the event we
1113 * would want to return some info right after parsing.
1114 */
1115 if (buffer_almost_full(si->ib))
1116 break;
1117
Willy Tarreau74b08c92010-09-08 17:04:31 +02001118 reql = buffer_get_line(si->ob, trash, sizeof(trash));
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001119 if (reql <= 0) { /* closed or EOL not found */
1120 if (reql == 0)
1121 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001122 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001123 continue;
1124 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001125
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001126 /* seek for a possible semi-colon. If we find one, we
1127 * replace it with an LF and skip only this part.
1128 */
1129 for (len = 0; len < reql; len++)
1130 if (trash[len] == ';') {
1131 trash[len] = '\n';
1132 reql = len + 1;
1133 break;
1134 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001135
Willy Tarreau816fc222009-10-04 07:36:58 +02001136 /* now it is time to check that we have a full line,
1137 * remove the trailing \n and possibly \r, then cut the
1138 * line.
1139 */
1140 len = reql - 1;
1141 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001142 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001143 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001144 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001145
Willy Tarreau816fc222009-10-04 07:36:58 +02001146 if (len && trash[len-1] == '\r')
1147 len--;
1148
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001149 trash[len] = '\0';
1150
Willy Tarreaubc4af052011-02-13 13:25:14 +01001151 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001152 if (len) {
1153 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001154 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001155 continue;
1156 }
1157 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001158 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001159 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001160 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001161 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001162 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001163 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001164 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001165 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001166 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001167 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001168 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001169 /* if prompt is disabled, print help on empty lines,
1170 * so that the user at least knows how to enable
1171 * prompt and find help.
1172 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001173 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001174 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001175 }
1176
1177 /* re-adjust req buffer */
1178 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001179 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001180 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001181 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001182 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001183 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001184 continue;
1185 }
1186
Willy Tarreaubc4af052011-02-13 13:25:14 +01001187 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001188 case STAT_CLI_PRINT:
Willy Tarreau295a8372011-03-10 11:25:07 +01001189 if (buffer_feed(si->ib, si->applet.ctx.cli.msg) < 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001190 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001191 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001192 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001193 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001194 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001195 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001196 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001197 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001198 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001199 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001200 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001201 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001202 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001203 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001204 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001205 if (stats_table_request(si, true))
1206 si->applet.st0 = STAT_CLI_PROMPT;
1207 break;
1208 case STAT_CLI_O_CLR:
1209 if (stats_table_request(si, false))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001210 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001211 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001212 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001213 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001214 break;
1215 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001216
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001217 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001218 if (si->applet.st0 == STAT_CLI_PROMPT) {
1219 if (buffer_feed(si->ib, si->applet.st1 ? "\n> " : "\n") < 0)
1220 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001221 }
1222
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001223 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001224 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001225 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001226
1227 /* Now we close the output if one of the writers did so,
1228 * or if we're not in interactive mode and the request
1229 * buffer is empty. This still allows pipelined requests
1230 * to be sent in non-interactive mode.
1231 */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001232 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->applet.st1 && !req->send_max)) {
1233 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001234 continue;
1235 }
1236
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001237 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001238 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001239 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001240 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001241
Willy Tarreaubc4af052011-02-13 13:25:14 +01001242 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001243 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1244 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1245 /* Other size has closed, let's abort if we have no more processing to do
1246 * and nothing more to consume. This is comparable to a broken pipe, so
1247 * we forward the close to the request side so that it flows upstream to
1248 * the client.
1249 */
1250 si->shutw(si);
1251 }
1252
Willy Tarreaubc4af052011-02-13 13:25:14 +01001253 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001254 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1255 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1256 /* We have no more processing to do, and nothing more to send, and
1257 * the client side has closed. So we'll forward this state downstream
1258 * on the response buffer.
1259 */
1260 si->shutr(si);
1261 res->flags |= BF_READ_NULL;
1262 }
1263
1264 /* update all other flags and resync with the other side */
1265 si->update(si);
1266
1267 /* we don't want to expire timeouts while we're processing requests */
1268 si->ib->rex = TICK_ETERNITY;
1269 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001270
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001271 out:
1272 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
1273 __FUNCTION__, __LINE__,
1274 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
1275
1276 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1277 /* check that we have released everything then unregister */
1278 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001279 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001280}
1281
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001282/* This function dumps statistics onto the stream interface's read buffer.
1283 * The data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001284 * It returns 0 as long as it does not complete, non-zero upon completion.
1285 * Some states are not used but it makes the code more similar to other
1286 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001287 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001288static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001289{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001290 struct proxy *px;
1291 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001292 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001293
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001294 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +02001295
Willy Tarreau295a8372011-03-10 11:25:07 +01001296 switch (si->applet.state) {
1297 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001298 /* the function had not been called yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001299 si->applet.state = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001300 /* fall through */
1301
Willy Tarreau295a8372011-03-10 11:25:07 +01001302 case STAT_ST_HEAD:
1303 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001304 print_csv_header(&msg);
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001305 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001306 return 0;
1307 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001308
Willy Tarreau295a8372011-03-10 11:25:07 +01001309 si->applet.state = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001310 /* fall through */
1311
Willy Tarreau295a8372011-03-10 11:25:07 +01001312 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001313 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001314 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001315 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001316 "Name: " PRODUCT_NAME "\n"
1317 "Version: " HAPROXY_VERSION "\n"
1318 "Release_date: " HAPROXY_DATE "\n"
1319 "Nbproc: %d\n"
1320 "Process_num: %d\n"
1321 "Pid: %d\n"
1322 "Uptime: %dd %dh%02dm%02ds\n"
1323 "Uptime_sec: %d\n"
1324 "Memmax_MB: %d\n"
1325 "Ulimit-n: %d\n"
1326 "Maxsock: %d\n"
1327 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001328 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001329 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001330 "PipesUsed: %d\n"
1331 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001332 "Tasks: %d\n"
1333 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001334 "node: %s\n"
1335 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001336 "",
1337 global.nbproc,
1338 relative_pid,
1339 pid,
1340 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1341 up,
1342 global.rlimit_memmax,
1343 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001344 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001345 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001346 nb_tasks_cur, run_queue_cur,
1347 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001348 );
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001349 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001350 return 0;
1351 }
1352
Willy Tarreau295a8372011-03-10 11:25:07 +01001353 si->applet.ctx.stats.px = proxy;
1354 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1355 si->applet.ctx.stats.sv = NULL;
1356 si->applet.state = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001357 /* fall through */
1358
Willy Tarreau295a8372011-03-10 11:25:07 +01001359 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001360 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001361 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1362 while (si->applet.ctx.stats.px) {
1363 px = si->applet.ctx.stats.px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001364 /* skip the disabled proxies and non-networked ones */
1365 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001366 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001367 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001368 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001369 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001370
Willy Tarreau295a8372011-03-10 11:25:07 +01001371 si->applet.ctx.stats.px = px->next;
1372 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001373 }
1374 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001375 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001376
Willy Tarreau295a8372011-03-10 11:25:07 +01001377 si->applet.state = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001378 /* fall through */
1379
Willy Tarreau295a8372011-03-10 11:25:07 +01001380 case STAT_ST_END:
1381 si->applet.state = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001382 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001383
Willy Tarreau295a8372011-03-10 11:25:07 +01001384 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001385 return 1;
1386
1387 default:
1388 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001389 si->applet.state = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001390 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001391 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001392}
1393
1394
Cyril Bonté70be45d2010-10-12 00:14:35 +02001395/* We don't want to land on the posted stats page because a refresh will
1396 * repost the data. We don't want this to happen on accident so we redirect
1397 * the browse to the stats page with a GET.
1398 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001399static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001400{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001401 struct session *s = si->applet.private;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001402 struct chunk msg;
1403
1404 chunk_init(&msg, trash, sizeof(trash));
1405
Willy Tarreau295a8372011-03-10 11:25:07 +01001406 switch (si->applet.state) {
1407 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001408 chunk_printf(&msg,
1409 "HTTP/1.0 303 See Other\r\n"
1410 "Cache-Control: no-cache\r\n"
1411 "Content-Type: text/plain\r\n"
1412 "Connection: close\r\n"
1413 "Location: %s;st=%s",
Willy Tarreau295a8372011-03-10 11:25:07 +01001414 uri->uri_prefix, si->applet.ctx.stats.st_code);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001415 chunk_printf(&msg, "\r\n\r\n");
1416
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001417 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001418 return 0;
1419
1420 s->txn.status = 303;
1421
1422 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1423 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1424 if (!(s->flags & SN_FINST_MASK))
1425 s->flags |= SN_FINST_R;
1426
Willy Tarreau295a8372011-03-10 11:25:07 +01001427 si->applet.state = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001428 return 1;
1429 }
1430 return 1;
1431}
1432
1433
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001434/* This I/O handler runs as an applet embedded in a stream interface. It is
1435 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001436 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001437 * automatically unregisters itself once transfer is complete.
1438 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001439static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001440{
Willy Tarreaubc4af052011-02-13 13:25:14 +01001441 struct session *s = si->applet.private;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001442 struct buffer *req = si->ob;
1443 struct buffer *res = si->ib;
1444
1445 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1446 goto out;
1447
1448 /* check that the output is not closed */
1449 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001450 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001451
Willy Tarreaubc4af052011-02-13 13:25:14 +01001452 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001453 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001454 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001455 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001456 si->shutw(si);
1457 }
1458 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001459 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001460 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001461 si->shutw(si);
1462 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001463 }
1464 }
1465
1466 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
1467 si->shutw(si);
1468
Willy Tarreaubc4af052011-02-13 13:25:14 +01001469 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001470 si->shutr(si);
1471 res->flags |= BF_READ_NULL;
1472 }
1473
1474 /* update all other flags and resync with the other side */
1475 si->update(si);
1476
1477 /* we don't want to expire timeouts while we're processing requests */
1478 si->ib->rex = TICK_ETERNITY;
1479 si->ob->wex = TICK_ETERNITY;
1480
1481 out:
1482 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1483 /* check that we have released everything then unregister */
1484 stream_int_unregister_handler(si);
1485 }
1486}
1487
1488
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001489/* This function dumps statistics in HTTP format onto the stream interface's
1490 * read buffer. The data_ctx must have been zeroed first, and the flags
1491 * properly set. It returns 0 if it had to stop writing data and an I/O is
1492 * needed, 1 if the dump is finished and the session must be closed, or -1
1493 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001494 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001495static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001496{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001497 struct session *s = si->applet.private;
1498 struct buffer *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001499 struct proxy *px;
1500 struct chunk msg;
1501 unsigned int up;
1502
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001503 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001504
Willy Tarreau295a8372011-03-10 11:25:07 +01001505 switch (si->applet.state) {
1506 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001507 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001508 "HTTP/1.0 200 OK\r\n"
1509 "Cache-Control: no-cache\r\n"
1510 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001511 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001512 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001513
Willy Tarreau295a8372011-03-10 11:25:07 +01001514 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001515 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001516 uri->refresh);
1517
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001518 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001519
1520 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001521 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001522 return 0;
1523
Willy Tarreau91861262007-10-17 17:06:05 +02001524 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1525 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1526 if (!(s->flags & SN_FINST_MASK))
1527 s->flags |= SN_FINST_R;
1528
1529 if (s->txn.meth == HTTP_METH_HEAD) {
1530 /* that's all we return in case of HEAD request */
Willy Tarreau295a8372011-03-10 11:25:07 +01001531 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001532 return 1;
1533 }
1534
Willy Tarreau295a8372011-03-10 11:25:07 +01001535 si->applet.state = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001536 /* fall through */
1537
Willy Tarreau295a8372011-03-10 11:25:07 +01001538 case STAT_ST_HEAD:
1539 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001540 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001541 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001542 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1543 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001544 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001545 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1546 "<style type=\"text/css\"><!--\n"
1547 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001548 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001549 " font-size: 12px;"
1550 " font-weight: normal;"
1551 " color: black;"
1552 " background: white;"
1553 "}\n"
1554 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001555 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001556 "}\n"
1557 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001558 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001559 " margin-bottom: 0.5em;"
1560 "}\n"
1561 "h2 {"
1562 " font-family: helvetica, arial;"
1563 " font-size: x-large;"
1564 " font-weight: bold;"
1565 " font-style: italic;"
1566 " color: #6020a0;"
1567 " margin-top: 0em;"
1568 " margin-bottom: 0em;"
1569 "}\n"
1570 "h3 {"
1571 " font-family: helvetica, arial;"
1572 " font-size: 16px;"
1573 " font-weight: bold;"
1574 " color: #b00040;"
1575 " background: #e8e8d0;"
1576 " margin-top: 0em;"
1577 " margin-bottom: 0em;"
1578 "}\n"
1579 "li {"
1580 " margin-top: 0.25em;"
1581 " margin-right: 2em;"
1582 "}\n"
1583 ".hr {margin-top: 0.25em;"
1584 " border-color: black;"
1585 " border-bottom-style: solid;"
1586 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001587 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001588 ".total {background: #20D0D0;color: #ffff80;}\n"
1589 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001590 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001591 ".backend {background: #e8e8d0;}\n"
1592 ".active0 {background: #ff9090;}\n"
1593 ".active1 {background: #ffd020;}\n"
1594 ".active2 {background: #ffffa0;}\n"
1595 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001596 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1597 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1598 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001599 ".backup0 {background: #ff9090;}\n"
1600 ".backup1 {background: #ff80ff;}\n"
1601 ".backup2 {background: #c060ff;}\n"
1602 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001603 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1604 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1605 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001606 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001607 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001608 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001609 "a.px:link {color: #ffff40; text-decoration: none;}"
1610 "a.px:visited {color: #ffff40; text-decoration: none;}"
1611 "a.px:hover {color: #ffffff; text-decoration: none;}"
1612 "a.lfsb:link {color: #000000; text-decoration: none;}"
1613 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1614 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1615 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001616 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001617 "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"
1618 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001619 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001620 "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 +02001621 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001622 "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 +02001623 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001624 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1625 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1626 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001627 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001628 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001629 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001630 (uri->flags&ST_SHNODE) ? " on " : "",
1631 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001632 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001633 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001634 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001635 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001636 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001637 return 0;
1638
Willy Tarreau295a8372011-03-10 11:25:07 +01001639 si->applet.state = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02001640 /* fall through */
1641
Willy Tarreau295a8372011-03-10 11:25:07 +01001642 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02001643 up = (now.tv_sec - start_date.tv_sec);
1644
1645 /* WARNING! this has to fit the first packet too.
1646 * We are around 3.5 kB, add adding entries will
1647 * become tricky if we want to support 4kB buffers !
1648 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001649 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001650 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001651 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1652 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001653 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001654 "<hr width=\"100%%\" class=\"hr\">\n"
1655 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001656 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001657 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001658 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001659 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1660 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
1661 "current conns = %d; current pipes = %d/%d<br>\n"
1662 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001663 "</td><td align=\"center\" nowrap>\n"
1664 "<table class=\"lgd\"><tr>\n"
1665 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1666 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1667 "</tr><tr>\n"
1668 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1669 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1670 "</tr><tr>\n"
1671 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1672 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1673 "</tr><tr>\n"
1674 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001675 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001676 "</tr><tr>\n"
1677 "<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 +02001678 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001679 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001680 "</td>"
1681 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1682 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1683 "",
1684 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001685 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1686 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1687 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001688 up / 86400, (up % 86400) / 3600,
1689 (up % 3600) / 60, (up % 60),
1690 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1691 global.rlimit_memmax ? " MB" : "",
1692 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001693 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001694 actconn, pipes_used, pipes_used+pipes_free,
1695 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +02001696 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001697
Willy Tarreau295a8372011-03-10 11:25:07 +01001698 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001699 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001700 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1701 uri->uri_prefix,
1702 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01001703 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001704 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001705 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001706 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1707 uri->uri_prefix,
1708 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01001709 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001710
Willy Tarreau55bb8452007-10-17 18:44:57 +02001711 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001712 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001713 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001714 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1715 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001716 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001717 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001718 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001719 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001720 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1721 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001722 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001723 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001724 }
Willy Tarreau91861262007-10-17 17:06:05 +02001725
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001726 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001727 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
1728 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001729 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
1730 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001731
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001732 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02001733 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
1734 uri->uri_prefix,
1735 (uri->refresh > 0) ? ";norefresh" : "");
1736
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001737 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001738 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001739 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1740 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
1741 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
1742 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
1743 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
1744 "</ul>"
1745 "</td>"
1746 "</tr></table>\n"
1747 ""
1748 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001749
Willy Tarreau295a8372011-03-10 11:25:07 +01001750 if (si->applet.ctx.stats.st_code) {
1751 if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001752 chunk_printf(&msg,
1753 "<p><div class=active3>"
1754 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1755 "Action processed successfully."
1756 "</div>\n", uri->uri_prefix);
1757 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001758 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_NONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001759 chunk_printf(&msg,
1760 "<p><div class=active2>"
1761 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1762 "Nothing has changed."
1763 "</div>\n", uri->uri_prefix);
1764 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001765 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_EXCD) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001766 chunk_printf(&msg,
1767 "<p><div class=active0>"
1768 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1769 "<b>Action not processed : the buffer couldn't store all the data.<br>"
1770 "You should retry with less servers at a time.</b>"
1771 "</div>\n", uri->uri_prefix);
1772 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001773 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DENY) == 0) {
Cyril Bonté474be412010-10-12 00:14:36 +02001774 chunk_printf(&msg,
1775 "<p><div class=active0>"
1776 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1777 "<b>Action denied.</b>"
1778 "</div>\n", uri->uri_prefix);
1779 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001780 else {
1781 chunk_printf(&msg,
1782 "<p><div class=active6>"
1783 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1784 "Unexpected result."
1785 "</div>\n", uri->uri_prefix);
1786 }
1787 chunk_printf(&msg,"<p>\n");
1788 }
1789
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001790 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001791 return 0;
1792 }
Willy Tarreau91861262007-10-17 17:06:05 +02001793
Willy Tarreau295a8372011-03-10 11:25:07 +01001794 si->applet.ctx.stats.px = proxy;
1795 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1796 si->applet.state = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02001797 /* fall through */
1798
Willy Tarreau295a8372011-03-10 11:25:07 +01001799 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02001800 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001801 while (si->applet.ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001802 if (buffer_almost_full(rep))
1803 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01001804 px = si->applet.ctx.stats.px;
Willy Tarreau91861262007-10-17 17:06:05 +02001805 /* skip the disabled proxies and non-networked ones */
1806 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001807 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001808 return 0;
1809
Willy Tarreau295a8372011-03-10 11:25:07 +01001810 si->applet.ctx.stats.px = px->next;
1811 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02001812 }
1813 /* here, we just have reached the last proxy */
1814
Willy Tarreau295a8372011-03-10 11:25:07 +01001815 si->applet.state = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02001816 /* fall through */
1817
Willy Tarreau295a8372011-03-10 11:25:07 +01001818 case STAT_ST_END:
1819 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001820 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001821 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001822 return 0;
1823 }
Willy Tarreau91861262007-10-17 17:06:05 +02001824
Willy Tarreau295a8372011-03-10 11:25:07 +01001825 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001826 /* fall through */
1827
Willy Tarreau295a8372011-03-10 11:25:07 +01001828 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02001829 return 1;
1830
1831 default:
1832 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001833 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001834 return -1;
1835 }
1836}
1837
1838
1839/*
1840 * Dumps statistics for a proxy.
1841 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1842 * ot non-zero if everything completed.
1843 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001844static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001845{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001846 struct session *s = si->applet.private;
1847 struct buffer *rep = si->ib;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001848 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001849 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02001850 struct chunk msg;
1851
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001852 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001853
Willy Tarreau295a8372011-03-10 11:25:07 +01001854 switch (si->applet.ctx.stats.px_st) {
1855 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02001856 /* we are on a new proxy */
1857
1858 if (uri && uri->scope) {
1859 /* we have a limited scope, we have to check the proxy name */
1860 struct stat_scope *scope;
1861 int len;
1862
1863 len = strlen(px->id);
1864 scope = uri->scope;
1865
1866 while (scope) {
1867 /* match exact proxy name */
1868 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1869 break;
1870
1871 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001872 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001873 break;
1874 scope = scope->next;
1875 }
1876
1877 /* proxy name not found : don't dump anything */
1878 if (scope == NULL)
1879 return 1;
1880 }
1881
Willy Tarreau295a8372011-03-10 11:25:07 +01001882 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
1883 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001884 return 1;
1885
Willy Tarreau295a8372011-03-10 11:25:07 +01001886 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02001887 /* fall through */
1888
Willy Tarreau295a8372011-03-10 11:25:07 +01001889 case STAT_PX_ST_TH:
1890 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
1891 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001892 /* A form to enable/disable this proxy servers */
1893 chunk_printf(&msg,
1894 "<form action=\"%s\" method=\"post\">",
1895 uri->uri_prefix);
1896 }
1897
Willy Tarreau55bb8452007-10-17 18:44:57 +02001898 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001899 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001900 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001901 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001902 "<th class=\"pxname\" width=\"10%%\"");
1903
1904 if (uri->flags&ST_SHLGNDS) {
1905 /* cap, mode, id */
1906 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
1907 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1908 px->uuid);
1909
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001910 chunk_printf(&msg, "\"");
1911 }
1912
1913 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001914 ">%s<a name=\"%s\"></a>"
1915 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001916 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001917 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001918 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001919 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001920 "<tr class=\"titre\">",
1921 (uri->flags & ST_SHLGNDS)?"<u>":"",
1922 px->id, px->id, px->id,
1923 (uri->flags & ST_SHLGNDS)?"</u>":"",
1924 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1925
Willy Tarreau295a8372011-03-10 11:25:07 +01001926 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001927 /* Column heading for Enable or Disable server */
1928 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
1929 }
1930
1931 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001932 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001933 "<th colspan=3>Queue</th>"
1934 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001935 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001936 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001937 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001938 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001939 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001940 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001941 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001942 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001943 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001944 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001945 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001946 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1947 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001948 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001949
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001950 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001951 return 0;
1952 }
Willy Tarreau91861262007-10-17 17:06:05 +02001953
Willy Tarreau295a8372011-03-10 11:25:07 +01001954 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02001955 /* fall through */
1956
Willy Tarreau295a8372011-03-10 11:25:07 +01001957 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02001958 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001959 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01001960 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
1961 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001962 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001963 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02001964 "<tr class=\"frontend\">");
1965
Willy Tarreau295a8372011-03-10 11:25:07 +01001966 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001967 /* Column sub-heading for Enable or Disable server */
1968 chunk_printf(&msg, "<td></td>");
1969 }
1970
1971 chunk_printf(&msg,
1972 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001973 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001974 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
1975 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01001976 "",
1977 px->id, px->id);
1978
1979 if (px->mode == PR_MODE_HTTP) {
1980 chunk_printf(&msg,
1981 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01001982 "<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 +01001983 "",
1984 read_freq_ctr(&px->fe_req_per_sec),
1985 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001986 px->fe_counters.p.http.rps_max,
1987 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01001988 LIM2A2(px->fe_sps_lim, "-"));
1989 } else {
1990 chunk_printf(&msg,
1991 /* sessions rate : current, max, limit */
1992 "<td>%s</td><td>%s</td><td>%s</td>"
1993 "",
1994 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001995 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01001996 }
1997
1998 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001999 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002000 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002001 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002002 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002003 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002004
2005 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2006 if (px->mode == PR_MODE_HTTP) {
2007 int i;
2008
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002009 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002010
2011 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002012 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002013
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002014 chunk_printf(&msg, " other=%lld\"", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002015 }
2016
2017 chunk_printf(&msg,
2018 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002019 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002020 /* bytes : in, out */
2021 "<td>%s</td><td>%s</td>"
2022 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002023 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002024 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002025 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002026 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002027
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002028 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002029 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002030 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002031 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002032 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002033 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002034 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002035 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002036 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002037 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002038 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002039 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002040 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2041 U2H2(px->fe_counters.failed_req),
Willy Tarreau562515c2011-07-25 08:11:52 +02002042 px->state == PR_STREADY ? "OPEN" :
2043 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002044 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002045 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002046 /* pxid, name, queue cur, queue max, */
2047 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002048 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002049 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002050 /* bytes : in, out */
2051 "%lld,%lld,"
2052 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002053 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002054 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002055 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002056 /* warnings: retries, redispatches */
2057 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002058 /* server status : reflect frontend status */
2059 "%s,"
2060 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002061 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002062 /* pid, iid, sid, throttle, lbtot, tracked, type */
2063 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002064 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002065 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002066 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002067 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002068 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002069 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2070 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2071 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2072 px->fe_counters.failed_req,
Willy Tarreau562515c2011-07-25 08:11:52 +02002073 px->state == PR_STREADY ? "OPEN" :
2074 px->state == PR_STFULL ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002075 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002076 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002077 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002078
2079 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2080 if (px->mode == PR_MODE_HTTP) {
2081 int i;
2082
2083 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002084 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002085
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002086 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002087 } else {
2088 chunk_printf(&msg, ",,,,,,");
2089 }
2090
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002091 /* failed health analyses */
2092 chunk_printf(&msg, ",");
2093
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002094 /* requests : req_rate, req_rate_max, req_tot, */
2095 chunk_printf(&msg, "%u,%u,%lld,",
2096 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002097 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002098
Willy Tarreauae526782010-03-04 20:34:23 +01002099 /* errors: cli_aborts, srv_aborts */
2100 chunk_printf(&msg, ",,");
2101
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002102 /* finish with EOL */
2103 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002104 }
Willy Tarreau91861262007-10-17 17:06:05 +02002105
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002106 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002107 return 0;
2108 }
2109
Willy Tarreau295a8372011-03-10 11:25:07 +01002110 si->applet.ctx.stats.l = px->listen; /* may be NULL */
2111 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002112 /* fall through */
2113
Willy Tarreau295a8372011-03-10 11:25:07 +01002114 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002115 /* stats.l has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002116 for (; si->applet.ctx.stats.l != NULL; si->applet.ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002117 if (buffer_almost_full(rep))
2118 return 0;
2119
Willy Tarreau295a8372011-03-10 11:25:07 +01002120 l = si->applet.ctx.stats.l;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002121 if (!l->counters)
2122 continue;
2123
Willy Tarreau295a8372011-03-10 11:25:07 +01002124 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2125 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002126 break;
2127
Willy Tarreau295a8372011-03-10 11:25:07 +01002128 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002129 continue;
2130 }
2131
Willy Tarreau295a8372011-03-10 11:25:07 +01002132 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002133 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002134 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002135 /* Column sub-heading for Enable or Disable server */
2136 chunk_printf(&msg, "<td></td>");
2137 }
2138 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002139
2140 if (uri->flags&ST_SHLGNDS) {
2141 char str[INET6_ADDRSTRLEN], *fmt = NULL;
2142 int port;
2143
2144 chunk_printf(&msg, " title=\"IP: ");
2145
2146 port = (l->addr.ss_family == AF_INET6)
2147 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
2148 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
2149
2150 if (l->addr.ss_family == AF_INET) {
2151 if (inet_ntop(AF_INET,
2152 (const void *)&((struct sockaddr_in *)&l->addr)->sin_addr,
2153 str, sizeof(str)))
2154 fmt = "%s:%d";
2155 } else {
2156 if (inet_ntop(AF_INET6,
2157 (const void *)&((struct sockaddr_in6 *)(&l->addr))->sin6_addr,
2158 str, sizeof(str)))
2159 fmt = "[%s]:%d";
2160 }
2161
2162 if (fmt)
2163 chunk_printf(&msg, fmt, str, port);
2164 else
2165 chunk_printf(&msg, "(%s)", strerror(errno));
2166
2167 /* id */
2168 chunk_printf(&msg, ", id: %d", l->luid);
2169
2170 chunk_printf(&msg, "\"");
2171 }
2172
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002173 chunk_printf(&msg,
2174 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002175 ">%s<a name=\"%s/+%s\"></a>"
2176 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002177 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002178 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002179 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002180 "<td>%s</td><td>%s</td><td>%s</td>"
2181 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002182 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002183 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002184 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002185 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002186 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002187 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002188 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2189 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2190
2191 chunk_printf(&msg,
2192 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002193 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002194 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002195 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002196 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002197 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002198 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002199 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002200 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002201 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002202 "",
2203 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2204 U2H2(l->counters->failed_req),
Willy Tarreaua17c2d92011-07-25 08:16:20 +02002205 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002206 } else {
2207 chunk_printf(&msg,
2208 /* pxid, name, queue cur, queue max, */
2209 "%s,%s,,,"
2210 /* sessions: current, max, limit, total */
2211 "%d,%d,%d,%lld,"
2212 /* bytes: in, out */
2213 "%lld,%lld,"
2214 /* denied: req, resp */
2215 "%lld,%lld,"
2216 /* errors: request, connect, response */
2217 "%lld,,,"
2218 /* warnings: retries, redispatches */
2219 ",,"
2220 /* server status: reflect listener status */
2221 "%s,"
2222 /* rest of server: nothing */
2223 ",,,,,,,,"
2224 /* pid, iid, sid, throttle, lbtot, tracked, type */
2225 "%d,%d,%d,,,,%d,"
2226 /* rate, rate_lim, rate_max */
2227 ",,,"
2228 /* check_status, check_code, check_duration */
2229 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002230 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2231 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002232 /* failed health analyses */
2233 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002234 /* requests : req_rate, req_rate_max, req_tot, */
2235 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002236 /* errors: cli_aborts, srv_aborts */
2237 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002238 "\n",
2239 px->id, l->name,
2240 l->nbconn, l->counters->conn_max,
2241 l->maxconn, l->counters->cum_conn,
2242 l->counters->bytes_in, l->counters->bytes_out,
2243 l->counters->denied_req, l->counters->denied_resp,
2244 l->counters->failed_req,
2245 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2246 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2247 }
2248
2249 if (buffer_feed_chunk(rep, &msg) >= 0)
2250 return 0;
2251 }
2252
Willy Tarreau295a8372011-03-10 11:25:07 +01002253 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2254 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002255 /* fall through */
2256
Willy Tarreau295a8372011-03-10 11:25:07 +01002257 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002258 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002259 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002260 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 +02002261
Willy Tarreau4e33d862009-10-11 23:35:10 +02002262 if (buffer_almost_full(rep))
2263 return 0;
2264
Willy Tarreau295a8372011-03-10 11:25:07 +01002265 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002266
Willy Tarreau295a8372011-03-10 11:25:07 +01002267 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2268 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002269 break;
2270
Willy Tarreau295a8372011-03-10 11:25:07 +01002271 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002272 continue;
2273 }
2274
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002275 if (sv->tracked)
2276 svs = sv->tracked;
2277 else
2278 svs = sv;
2279
Willy Tarreau91861262007-10-17 17:06:05 +02002280 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002281 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002282 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002283 else if (svs->state & SRV_RUNNING) {
2284 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002285 sv_state = 3; /* UP */
2286 else
2287 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002288
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002289 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002290 sv_state += 2;
2291 }
Willy Tarreau91861262007-10-17 17:06:05 +02002292 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002293 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002294 sv_state = 1; /* going up */
2295 else
2296 sv_state = 0; /* DOWN */
2297
Willy Tarreau295a8372011-03-10 11:25:07 +01002298 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002299 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002300 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002301 continue;
2302 }
2303
Willy Tarreau295a8372011-03-10 11:25:07 +01002304 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002305 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2306 "UP %d/%d &darr;", "UP",
2307 "NOLB %d/%d &darr;", "NOLB",
2308 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002309 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002310 chunk_printf(&msg,
2311 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002312 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002313 );
2314 }
2315 else {
2316 chunk_printf(&msg,
2317 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002318 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002319 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2320 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002321
Willy Tarreau295a8372011-03-10 11:25:07 +01002322 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002323 chunk_printf(&msg,
2324 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2325 sv->id);
2326 }
2327
2328 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002329
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002330 if (uri->flags&ST_SHLGNDS) {
2331 char str[INET6_ADDRSTRLEN];
2332
2333 chunk_printf(&msg, " title=\"IP: ");
2334
2335 /* IP */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002336 switch (sv->addr.ss_family) {
2337 case AF_INET:
2338 if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&sv->addr)->sin_addr, str, sizeof(str)))
2339 chunk_printf(&msg, "%s:%d", str, htons(((struct sockaddr_in *)&sv->addr)->sin_port));
2340 else
2341 chunk_printf(&msg, "(%s)", strerror(errno));
2342 break;
2343 case AF_INET6:
2344 if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&sv->addr)->sin6_addr, str, sizeof(str)))
2345 chunk_printf(&msg, "%s:%d", str, htons(((struct sockaddr_in6 *)&sv->addr)->sin6_port));
2346 else
2347 chunk_printf(&msg, "(%s)", strerror(errno));
2348 break;
2349 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002350
2351 /* id */
2352 chunk_printf(&msg, ", id: %d", sv->puid);
2353
2354 /* cookie */
2355 if (sv->cookie) {
2356 struct chunk src;
2357
2358 chunk_printf(&msg, ", cookie: '");
2359
2360 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2361 chunk_htmlencode(&msg, &src);
2362
2363 chunk_printf(&msg, "'");
2364 }
2365
2366 chunk_printf(&msg, "\"");
2367 }
2368
2369 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002370 ">%s<a name=\"%s/%s\"></a>"
2371 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002372 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002373 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002374 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002375 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002376 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002377 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002378 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002379 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002380 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002381 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002382 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002383 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2384 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002385 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2386
2387 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2388 if (px->mode == PR_MODE_HTTP) {
2389 int i;
2390
2391 chunk_printf(&msg, " title=\"rsp codes:");
2392
2393 for (i = 1; i < 6; i++)
2394 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2395
2396 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2397 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002398
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002399 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002400 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002401 ">%s%s%s</td><td>%s</td>",
2402 (px->mode == PR_MODE_HTTP)?"<u>":"",
2403 U2H0(sv->counters.cum_sess),
2404 (px->mode == PR_MODE_HTTP)?"</u>":"",
2405 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002406
2407 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002408 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002409 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002410 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002411 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002412 /* errors : request, connect */
2413 "<td></td><td>%s</td>"
2414 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002415 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002416 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002417 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002418 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002419 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2420 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002421 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002422 sv->counters.cli_aborts,
2423 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002424 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002425 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002426
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002427 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002428 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002429
Cyril Bontécd19e512010-01-31 22:34:03 +01002430 if (sv->state & SRV_MAINTAIN) {
2431 chunk_printf(&msg, "%s ",
2432 human_time(now.tv_sec - sv->last_change, 1));
2433 chunk_printf(&msg, "MAINT");
2434 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002435 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2436 chunk_printf(&msg, "%s ",
2437 human_time(now.tv_sec - svs->last_change, 1));
2438 chunk_printf(&msg, "MAINT(via)");
2439 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002440 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002441 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002442 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002443
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002444 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002445 srv_hlt_st[sv_state],
2446 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2447 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002448 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002449
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002450 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002451 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002452 get_check_status_description(sv->check_status));
2453
2454 if (*sv->check_desc) {
2455 struct chunk src;
2456
2457 chunk_printf(&msg, ": ");
2458
2459 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2460 chunk_htmlencode(&msg, &src);
2461 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002462
Willy Tarreaue0454092010-02-26 12:29:07 +01002463 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002464 tv_iszero(&sv->check_start)?"":"* ",
2465 get_check_status_info(sv->check_status));
2466
2467 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002468 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002469
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002470 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002471 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002472 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002473 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002474
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002475 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002476 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002477 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002478 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002479 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002480 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002481 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002482 (sv->state & SRV_BACKUP) ? "-" : "Y",
2483 (sv->state & SRV_BACKUP) ? "Y" : "-");
2484
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002485 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002486 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002487 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002488 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2489
2490 if (svs->observe)
2491 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2492
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002493 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002494 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002495 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002496 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002497 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002498 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002499 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002500 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2501 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002502 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002503 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002504 "<td colspan=3></td>");
2505
2506 /* throttle */
2507 if ((sv->state & SRV_WARMINGUP) &&
2508 now.tv_sec < sv->last_change + sv->slowstart &&
2509 now.tv_sec >= sv->last_change) {
2510 unsigned int ratio;
2511 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002512 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002513 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002514 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002515 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002516 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002517 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002518 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002519 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2520 "UP %d/%d,", "UP,",
2521 "NOLB %d/%d,", "NOLB,",
2522 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002523 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002524 /* pxid, name */
2525 "%s,%s,"
2526 /* queue : current, max */
2527 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002528 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002529 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002530 /* bytes : in, out */
2531 "%lld,%lld,"
2532 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002533 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002534 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002535 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002536 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002537 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002538 "",
2539 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002540 sv->nbpend, sv->counters.nbpend_max,
2541 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002542 sv->counters.bytes_in, sv->counters.bytes_out,
2543 sv->counters.failed_secu,
2544 sv->counters.failed_conns, sv->counters.failed_resp,
2545 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002546
Willy Tarreau55bb8452007-10-17 18:44:57 +02002547 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002548 if (sv->state & SRV_MAINTAIN) {
2549 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002550 }
2551 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2552 chunk_printf(&msg, "MAINT(via),");
2553 }
2554 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002555 chunk_printf(&msg,
2556 srv_hlt_st[sv_state],
2557 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2558 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2559 }
Willy Tarreau91861262007-10-17 17:06:05 +02002560
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002561 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002562 /* weight, active, backup */
2563 "%d,%d,%d,"
2564 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002565 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002566 (sv->state & SRV_BACKUP) ? 0 : 1,
2567 (sv->state & SRV_BACKUP) ? 1 : 0);
2568
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002569 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002570 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002571 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002572 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002573 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002574 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002575 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002576 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002577 ",,,,");
2578
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002579 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002580 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002581 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002582 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002583 LIM2A0(sv->maxqueue, ""),
2584 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002585
2586 /* throttle */
2587 if ((sv->state & SRV_WARMINGUP) &&
2588 now.tv_sec < sv->last_change + sv->slowstart &&
2589 now.tv_sec >= sv->last_change) {
2590 unsigned int ratio;
2591 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002592 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002593 }
2594
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002595 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002596 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002597
2598 /* tracked */
2599 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002600 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002601 sv->tracked->proxy->id, sv->tracked->id);
2602 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002603 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002604
Willy Tarreau7f062c42009-03-05 18:43:00 +01002605 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002606 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002607
2608 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002609 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002610 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002611 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002612
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002613 if (sv->state & SRV_CHECKED) {
2614 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002615 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002616
2617 /* check_code */
2618 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002619 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002620 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002621 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002622
2623 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002624 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002625 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002626 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002627 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002628
2629 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002630 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002631 }
2632
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002633 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2634 if (px->mode == PR_MODE_HTTP) {
2635 int i;
2636
2637 for (i=1; i<6; i++)
2638 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2639
2640 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2641 } else {
2642 chunk_printf(&msg, ",,,,,,");
2643 }
2644
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002645 /* failed health analyses */
2646 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2647
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002648 /* requests : req_rate, req_rate_max, req_tot, */
2649 chunk_printf(&msg, ",,,");
2650
Willy Tarreauae526782010-03-04 20:34:23 +01002651 /* errors: cli_aborts, srv_aborts */
2652 chunk_printf(&msg, "%lld,%lld,",
2653 sv->counters.cli_aborts, sv->counters.srv_aborts);
2654
Willy Tarreau7f062c42009-03-05 18:43:00 +01002655 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002656 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002657 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002658 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002659 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002660 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002661
Willy Tarreau295a8372011-03-10 11:25:07 +01002662 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02002663 /* fall through */
2664
Willy Tarreau295a8372011-03-10 11:25:07 +01002665 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02002666 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002667 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002668 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2669 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002670 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01002671 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002672 /* Column sub-heading for Enable or Disable server */
2673 chunk_printf(&msg, "<td></td>");
2674 }
2675 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002676
2677 if (uri->flags&ST_SHLGNDS) {
2678 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002679 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002680 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002681
2682 /* cookie */
2683 if (px->cookie_name) {
2684 struct chunk src;
2685
2686 chunk_printf(&msg, ", cookie: '");
2687
2688 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2689 chunk_htmlencode(&msg, &src);
2690
2691 chunk_printf(&msg, "'");
2692 }
2693
2694 chunk_printf(&msg, "\"");
2695
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002696 }
2697
2698 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002699 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01002700 ">%s<a name=\"%s/Backend\"></a>"
2701 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002702 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002703 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002704 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002705 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002706 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002707 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002708 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002709 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002710 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
2711 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02002712
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002713 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002714 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002715 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002716 "<td"
2717 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002718 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002719
2720 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2721 if (px->mode == PR_MODE_HTTP) {
2722 int i;
2723
2724 chunk_printf(&msg, " title=\"rsp codes:");
2725
2726 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002727 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002728
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002729 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002730 }
2731
2732 chunk_printf(&msg,
2733 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002734 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002735 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002736 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002737 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002738 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002739 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01002740 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002741 U2H7(px->be_counters.cum_lbconn),
2742 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002743
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002744 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002745 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002746 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002747 /* errors : request, connect */
2748 "<td></td><td>%s</td>"
2749 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002750 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002751 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002752 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002753 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02002754 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002755 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02002756 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002757 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2758 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002759 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002760 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
2761 U2H2(px->be_counters.failed_conns),
2762 px->be_counters.cli_aborts,
2763 px->be_counters.srv_aborts,
2764 U2H5(px->be_counters.failed_resp),
2765 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002766 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01002767 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2768 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01002769 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002770 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002771
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002772 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002773 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002774 "<td class=ac>&nbsp;</td><td>%d</td>"
2775 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002776 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002777 "</tr>",
2778 px->down_trans,
2779 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002780 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002781 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002782 /* pxid, name */
2783 "%s,BACKEND,"
2784 /* queue : current, max */
2785 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002786 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002787 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002788 /* bytes : in, out */
2789 "%lld,%lld,"
2790 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002791 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002792 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002793 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002794 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002795 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002796 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02002797 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002798 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02002799 * active and backups. */
2800 "%s,"
2801 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002802 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002803 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002804 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002805 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002806 /* rate, rate_lim, rate_max, */
2807 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002808 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002809 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002810 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002811 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2812 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2813 px->be_counters.bytes_in, px->be_counters.bytes_out,
2814 px->be_counters.denied_req, px->be_counters.denied_resp,
2815 px->be_counters.failed_conns, px->be_counters.failed_resp,
2816 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01002817 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01002818 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002819 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002820 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01002821 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002822 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002823 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002824 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002825 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002826
2827 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2828 if (px->mode == PR_MODE_HTTP) {
2829 int i;
2830
2831 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002832 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002833
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002834 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002835 } else {
2836 chunk_printf(&msg, ",,,,,,");
2837 }
2838
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002839 /* failed health analyses */
2840 chunk_printf(&msg, ",");
2841
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002842 /* requests : req_rate, req_rate_max, req_tot, */
2843 chunk_printf(&msg, ",,,");
2844
Willy Tarreauae526782010-03-04 20:34:23 +01002845 /* errors: cli_aborts, srv_aborts */
2846 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002847 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01002848
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002849 /* finish with EOL */
2850 chunk_printf(&msg, "\n");
2851
Willy Tarreau55bb8452007-10-17 18:44:57 +02002852 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002853 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002854 return 0;
2855 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002856
Willy Tarreau295a8372011-03-10 11:25:07 +01002857 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002858 /* fall through */
2859
Willy Tarreau295a8372011-03-10 11:25:07 +01002860 case STAT_PX_ST_END:
2861 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002862 chunk_printf(&msg, "</table>");
2863
Willy Tarreau295a8372011-03-10 11:25:07 +01002864 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002865 /* close the form used to enable/disable this proxy servers */
2866 chunk_printf(&msg,
2867 "Choose the action to perform on the checked servers : "
2868 "<select name=action>"
2869 "<option value=\"\"></option>"
2870 "<option value=\"disable\">Disable</option>"
2871 "<option value=\"enable\">Enable</option>"
2872 "</select>"
2873 "<input type=\"hidden\" name=\"b\" value=\"%s\">"
2874 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2875 "</form>",
2876 px->id);
2877 }
2878
2879 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02002880
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002881 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002882 return 0;
2883 }
Willy Tarreau91861262007-10-17 17:06:05 +02002884
Willy Tarreau295a8372011-03-10 11:25:07 +01002885 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002886 /* fall through */
2887
Willy Tarreau295a8372011-03-10 11:25:07 +01002888 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002889 return 1;
2890
2891 default:
2892 /* unknown state, we should put an abort() here ! */
2893 return 1;
2894 }
2895}
2896
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002897/* This function dumps a complete session state onto the stream intreface's
2898 * read buffer. The data_ctx must have been zeroed first, and the flags
2899 * properly set. The session has to be set in data_ctx.sess.target. It returns
2900 * 0 if the output buffer is full and it needs to be called again, otherwise
2901 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002902 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002903static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002904{
2905 struct tm tm;
2906 struct chunk msg;
2907 struct session *sess;
2908 extern const char *monthname[12];
2909 char pn[INET6_ADDRSTRLEN];
2910
2911 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau295a8372011-03-10 11:25:07 +01002912 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002913
Willy Tarreau295a8372011-03-10 11:25:07 +01002914 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002915 /* session changed, no need to go any further */
2916 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002917 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002918 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002919 si->applet.ctx.sess.target = NULL;
2920 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002921 return 1;
2922 }
2923
Willy Tarreau295a8372011-03-10 11:25:07 +01002924 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002925 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01002926 si->applet.ctx.sess.uid = sess->uniq_id;
2927 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002928 /* fall through */
2929
2930 case 1:
2931 chunk_printf(&msg,
2932 "%p: id=%u, proto=%s",
2933 sess,
2934 sess->uniq_id,
2935 sess->listener->proto->name);
2936
2937 switch (sess->listener->proto->sock_family) {
2938 case AF_INET:
2939 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002940 (const void *)&((struct sockaddr_in *)&sess->si[0].addr.c.from)->sin_addr,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002941 pn, sizeof(pn));
2942
2943 chunk_printf(&msg,
2944 " source=%s:%d\n",
2945 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002946 ntohs(((struct sockaddr_in *)&sess->si[0].addr.c.from)->sin_port));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002947 break;
2948 case AF_INET6:
2949 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002950 (const void *)&((struct sockaddr_in6 *)(&sess->si[0].addr.c.from))->sin6_addr,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002951 pn, sizeof(pn));
2952
2953 chunk_printf(&msg,
2954 " source=%s:%d\n",
2955 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002956 ntohs(((struct sockaddr_in6 *)&sess->si[0].addr.c.from)->sin6_port));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002957 break;
2958 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02002959 chunk_printf(&msg,
2960 " source=unix:%d\n", sess->listener->luid);
2961 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002962 default:
2963 /* no more information to print right now */
2964 chunk_printf(&msg, "\n");
2965 break;
2966 }
2967
2968 chunk_printf(&msg,
2969 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02002970 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002971
2972 chunk_printf(&msg,
2973 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)\n",
2974 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
2975 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
2976 sess->listener ? sess->listener->luid : 0);
2977
2978 chunk_printf(&msg,
2979 " backend=%s (id=%u mode=%s) server=%s (id=%u)\n",
2980 sess->be->id, sess->be->uuid, sess->be->mode ? "http" : "tcp",
Willy Tarreau827aee92011-03-10 16:55:02 +01002981 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
2982 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002983
2984 chunk_printf(&msg,
2985 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
2986 sess->task,
2987 sess->task->state,
2988 sess->task->nice, sess->task->calls,
2989 sess->task->expire ?
2990 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
2991 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
2992 TICKS_TO_MS(1000)) : "<NEVER>",
2993 task_in_rq(sess->task) ? ", running" : "");
2994
2995 get_localtime(sess->logs.accept_date.tv_sec, &tm);
2996 chunk_printf(&msg,
2997 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
2998 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2999 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3000 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3001
3002 chunk_printf(&msg,
3003 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3004 &sess->si[0],
3005 sess->si[0].state,
3006 sess->si[0].flags,
3007 sess->si[0].fd,
3008 sess->si[0].exp ?
3009 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3010 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3011 TICKS_TO_MS(1000)) : "<NEVER>",
3012 sess->si[0].err_type);
3013
3014 chunk_printf(&msg,
3015 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3016 &sess->si[1],
3017 sess->si[1].state,
3018 sess->si[1].flags,
3019 sess->si[1].fd,
3020 sess->si[1].exp ?
3021 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3022 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3023 TICKS_TO_MS(1000)) : "<NEVER>",
3024 sess->si[1].err_type);
3025
3026 chunk_printf(&msg,
3027 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3028 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3029 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3030
3031
3032 chunk_printf(&msg,
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02003033 " req=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003034 " an_exp=%s",
3035 sess->req,
3036 sess->req->flags, sess->req->analysers,
3037 sess->req->l, sess->req->send_max,
3038 sess->req->pipe ? sess->req->pipe->data : 0,
3039 sess->req->to_forward,
3040 sess->req->analyse_exp ?
3041 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3042 TICKS_TO_MS(1000)) : "<NEVER>");
3043
3044 chunk_printf(&msg,
3045 " rex=%s",
3046 sess->req->rex ?
3047 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3048 TICKS_TO_MS(1000)) : "<NEVER>");
3049
3050 chunk_printf(&msg,
3051 " wex=%s\n"
3052 " data=%p r=%d w=%d lr=%d total=%lld\n",
3053 sess->req->wex ?
3054 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3055 TICKS_TO_MS(1000)) : "<NEVER>",
3056 sess->req->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01003057 (int)(sess->req->r - sess->req->data),
3058 (int)(sess->req->w - sess->req->data),
3059 (int)(sess->req->lr - sess->req->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003060 sess->req->total);
3061
3062 chunk_printf(&msg,
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02003063 " res=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003064 " an_exp=%s",
3065 sess->rep,
3066 sess->rep->flags, sess->rep->analysers,
3067 sess->rep->l, sess->rep->send_max,
3068 sess->rep->pipe ? sess->rep->pipe->data : 0,
3069 sess->rep->to_forward,
3070 sess->rep->analyse_exp ?
3071 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3072 TICKS_TO_MS(1000)) : "<NEVER>");
3073
3074 chunk_printf(&msg,
3075 " rex=%s",
3076 sess->rep->rex ?
3077 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3078 TICKS_TO_MS(1000)) : "<NEVER>");
3079
3080 chunk_printf(&msg,
3081 " wex=%s\n"
3082 " data=%p r=%d w=%d lr=%d total=%lld\n",
3083 sess->rep->wex ?
3084 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3085 TICKS_TO_MS(1000)) : "<NEVER>",
3086 sess->rep->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01003087 (int)(sess->rep->r - sess->rep->data),
3088 (int)(sess->rep->w - sess->rep->data),
3089 (int)(sess->rep->lr - sess->rep->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003090 sess->rep->total);
3091
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003092 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003093 return 0;
3094
3095 /* use other states to dump the contents */
3096 }
3097 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003098 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003099 return 1;
3100}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003101
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003102/* This function dumps all sessions' states onto the stream intreface's
3103 * read buffer. The data_ctx must have been zeroed first, and the flags
3104 * properly set. It returns 0 if the output buffer is full and it needs
3105 * to be called again, otherwise non-zero. It is designed to be called
3106 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003107 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003108static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003109{
3110 struct chunk msg;
3111
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003112 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003113 /* If we're forced to shut down, we might have to remove our
3114 * reference to the last session being dumped.
3115 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003116 if (si->applet.state == STAT_ST_LIST) {
3117 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3118 LIST_DEL(&si->applet.ctx.sess.bref.users);
3119 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003120 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003121 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003122 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003123 }
3124
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003125 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003126
Willy Tarreau295a8372011-03-10 11:25:07 +01003127 switch (si->applet.state) {
3128 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003129 /* the function had not been called yet, let's prepare the
3130 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003131 * pointer to the first in the global list. When a target
3132 * session is being destroyed, it is responsible for updating
3133 * this pointer. We know we have reached the end when this
3134 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003135 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003136 LIST_INIT(&si->applet.ctx.sess.bref.users);
3137 si->applet.ctx.sess.bref.ref = sessions.n;
3138 si->applet.state = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003139 /* fall through */
3140
Willy Tarreau295a8372011-03-10 11:25:07 +01003141 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003142 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003143 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3144 LIST_DEL(&si->applet.ctx.sess.bref.users);
3145 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003146 }
3147
3148 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003149 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003150 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003151 struct session *curr_sess;
3152
Willy Tarreau295a8372011-03-10 11:25:07 +01003153 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003154
Willy Tarreau295a8372011-03-10 11:25:07 +01003155 if (si->applet.ctx.sess.target) {
3156 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003157 goto next_sess;
3158
Willy Tarreau295a8372011-03-10 11:25:07 +01003159 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003160 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003161 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003162 return 0;
3163
3164 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003165 LIST_DEL(&si->applet.ctx.sess.bref.users);
3166 LIST_INIT(&si->applet.ctx.sess.bref.users);
3167 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003168 break;
3169 }
3170
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003171 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003172 "%p: proto=%s",
3173 curr_sess,
3174 curr_sess->listener->proto->name);
3175
3176 switch (curr_sess->listener->proto->sock_family) {
3177 case AF_INET:
3178 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003179 (const void *)&((struct sockaddr_in *)&curr_sess->si[0].addr.c.from)->sin_addr,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003180 pn, sizeof(pn));
3181
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003182 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003183 " src=%s:%d fe=%s be=%s srv=%s",
3184 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003185 ntohs(((struct sockaddr_in *)&curr_sess->si[0].addr.c.from)->sin_port),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003186 curr_sess->fe->id,
3187 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01003188 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003189 );
3190 break;
3191 case AF_INET6:
3192 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003193 (const void *)&((struct sockaddr_in6 *)(&curr_sess->si[0].addr.c.from))->sin6_addr,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003194 pn, sizeof(pn));
3195
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003196 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003197 " src=%s:%d fe=%s be=%s srv=%s",
3198 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003199 ntohs(((struct sockaddr_in6 *)&curr_sess->si[0].addr.c.from)->sin6_port),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003200 curr_sess->fe->id,
3201 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01003202 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003203 );
3204
3205 break;
3206 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02003207 chunk_printf(&msg,
3208 " src=unix:%d fe=%s be=%s srv=%s",
3209 curr_sess->listener->luid,
3210 curr_sess->fe->id,
3211 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01003212 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003213 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003214 break;
3215 }
3216
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003217 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003218 " ts=%02x age=%s calls=%d",
3219 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003220 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3221 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003222
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003223 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003224 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
3225 curr_sess->req->flags,
3226 curr_sess->req->l,
3227 curr_sess->req->analysers,
3228 curr_sess->req->rex ?
3229 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3230 TICKS_TO_MS(1000)) : "");
3231
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003232 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003233 ",wx=%s",
3234 curr_sess->req->wex ?
3235 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3236 TICKS_TO_MS(1000)) : "");
3237
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003238 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003239 ",ax=%s]",
3240 curr_sess->req->analyse_exp ?
3241 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3242 TICKS_TO_MS(1000)) : "");
3243
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003244 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003245 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
3246 curr_sess->rep->flags,
3247 curr_sess->rep->l,
3248 curr_sess->rep->analysers,
3249 curr_sess->rep->rex ?
3250 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3251 TICKS_TO_MS(1000)) : "");
3252
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003253 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003254 ",wx=%s",
3255 curr_sess->rep->wex ?
3256 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3257 TICKS_TO_MS(1000)) : "");
3258
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003259 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003260 ",ax=%s]",
3261 curr_sess->rep->analyse_exp ?
3262 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3263 TICKS_TO_MS(1000)) : "");
3264
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003265 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003266 " s0=[%d,%1xh,fd=%d,ex=%s]",
3267 curr_sess->si[0].state,
3268 curr_sess->si[0].flags,
3269 curr_sess->si[0].fd,
3270 curr_sess->si[0].exp ?
3271 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3272 TICKS_TO_MS(1000)) : "");
3273
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003274 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003275 " s1=[%d,%1xh,fd=%d,ex=%s]",
3276 curr_sess->si[1].state,
3277 curr_sess->si[1].flags,
3278 curr_sess->si[1].fd,
3279 curr_sess->si[1].exp ?
3280 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3281 TICKS_TO_MS(1000)) : "");
3282
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003283 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003284 " exp=%s",
3285 curr_sess->task->expire ?
3286 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3287 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003288 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003289 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003290
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003291 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003292
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003293 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003294 /* let's try again later from this session. We add ourselves into
3295 * this session's users so that it can remove us upon termination.
3296 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003297 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003298 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003299 }
3300
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003301 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003302 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003303 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003304
Willy Tarreau295a8372011-03-10 11:25:07 +01003305 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003306 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003307 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003308 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3309 else
3310 chunk_printf(&msg, "Session not found.\n");
3311
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003312 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003313 return 0;
3314
Willy Tarreau295a8372011-03-10 11:25:07 +01003315 si->applet.ctx.sess.target = NULL;
3316 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003317 return 1;
3318 }
3319
Willy Tarreau295a8372011-03-10 11:25:07 +01003320 si->applet.state = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003321 /* fall through */
3322
3323 default:
Willy Tarreau295a8372011-03-10 11:25:07 +01003324 si->applet.state = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003325 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003326 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003327}
3328
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003329/* This function dumps all tables' states onto the stream intreface's
3330 * read buffer. The data_ctx must have been zeroed first, and the flags
3331 * properly set. It returns 0 if the output buffer is full and it needs
3332 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003333 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003334static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003335{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003336 struct session *s = si->applet.private;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003337 struct chunk msg;
3338 struct ebmb_node *eb;
3339 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003340 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003341
3342 /*
Willy Tarreau295a8372011-03-10 11:25:07 +01003343 * We have 3 possible states in si->applet.state :
3344 * - STAT_ST_INIT : the first call
3345 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003346 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003347 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003348 * and the entry pointer points to the next entry to be dumped,
3349 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003350 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003351 * data though.
3352 */
3353
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003354 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003355 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau295a8372011-03-10 11:25:07 +01003356 if (si->applet.state == STAT_ST_LIST) {
3357 si->applet.ctx.table.entry->ref_cnt--;
3358 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003359 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003360 return 1;
3361 }
3362
3363 chunk_init(&msg, trash, sizeof(trash));
3364
Willy Tarreau295a8372011-03-10 11:25:07 +01003365 while (si->applet.state != STAT_ST_FIN) {
3366 switch (si->applet.state) {
3367 case STAT_ST_INIT:
3368 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3369 if (!si->applet.ctx.table.proxy)
3370 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003371
Willy Tarreau295a8372011-03-10 11:25:07 +01003372 si->applet.ctx.table.entry = NULL;
3373 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003374 break;
3375
Willy Tarreau295a8372011-03-10 11:25:07 +01003376 case STAT_ST_INFO:
3377 if (!si->applet.ctx.table.proxy ||
3378 (si->applet.ctx.table.target &&
3379 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
3380 si->applet.state = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003381 break;
3382 }
3383
Willy Tarreau295a8372011-03-10 11:25:07 +01003384 if (si->applet.ctx.table.proxy->table.size) {
Simon Hormanc88b8872011-06-15 15:18:49 +09003385 if (show && !stats_dump_table_head_to_buffer(&msg, si, si->applet.ctx.table.proxy,
3386 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003387 return 0;
3388
Willy Tarreau295a8372011-03-10 11:25:07 +01003389 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003390 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
3391 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003392 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003393 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003394 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3395 si->applet.ctx.table.entry->ref_cnt++;
3396 si->applet.state = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003397 break;
3398 }
3399 }
3400 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003401 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003402 break;
3403
Willy Tarreau295a8372011-03-10 11:25:07 +01003404 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003405 skip_entry = false;
3406
Willy Tarreau295a8372011-03-10 11:25:07 +01003407 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003408 /* we're filtering on some data contents */
3409 void *ptr;
3410 long long data;
3411
Willy Tarreau295a8372011-03-10 11:25:07 +01003412 dt = si->applet.ctx.table.data_type;
3413 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3414 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003415 dt);
3416
3417 data = 0;
3418 switch (stktable_data_types[dt].std_type) {
3419 case STD_T_SINT:
3420 data = stktable_data_cast(ptr, std_t_sint);
3421 break;
3422 case STD_T_UINT:
3423 data = stktable_data_cast(ptr, std_t_uint);
3424 break;
3425 case STD_T_ULL:
3426 data = stktable_data_cast(ptr, std_t_ull);
3427 break;
3428 case STD_T_FRQP:
3429 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003430 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003431 break;
3432 }
3433
3434 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003435 if ((data < si->applet.ctx.table.value &&
3436 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3437 si->applet.ctx.table.data_op == STD_OP_GT ||
3438 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3439 (data == si->applet.ctx.table.value &&
3440 (si->applet.ctx.table.data_op == STD_OP_NE ||
3441 si->applet.ctx.table.data_op == STD_OP_GT ||
3442 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3443 (data > si->applet.ctx.table.value &&
3444 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3445 si->applet.ctx.table.data_op == STD_OP_LT ||
3446 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003447 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003448 }
3449
Simon Hormanc88b8872011-06-15 15:18:49 +09003450 if (show && !skip_entry &&
3451 !stats_dump_table_entry_to_buffer(&msg, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003452 si->applet.ctx.table.entry))
3453 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003454
Willy Tarreau295a8372011-03-10 11:25:07 +01003455 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003456
Willy Tarreau295a8372011-03-10 11:25:07 +01003457 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003458 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003459 struct stksess *old = si->applet.ctx.table.entry;
3460 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3461 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3462 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003463 break;
3464 }
3465
Simon Hormanc88b8872011-06-15 15:18:49 +09003466
3467 if (show)
3468 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3469 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3470 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3471
Willy Tarreau295a8372011-03-10 11:25:07 +01003472 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
3473 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003474 break;
3475
Willy Tarreau295a8372011-03-10 11:25:07 +01003476 case STAT_ST_END:
3477 si->applet.state = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003478 break;
3479 }
3480 }
3481 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003482}
3483
Willy Tarreaud426a182010-03-05 14:58:26 +01003484/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003485 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3486 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3487 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3488 * lines are respected within the limit of 70 output chars. Lines that are
3489 * continuation of a previous truncated line begin with "+" instead of " "
3490 * after the offset. The new pointer is returned.
3491 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003492static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3493 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003494{
3495 int end;
3496 unsigned char c;
3497
3498 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003499 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003500 return ptr;
3501
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003502 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003503
Willy Tarreaud426a182010-03-05 14:58:26 +01003504 while (ptr < len && ptr < bsize) {
3505 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003506 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003507 if (out->len > end - 2)
3508 break;
3509 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003510 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003511 if (out->len > end - 3)
3512 break;
3513 out->str[out->len++] = '\\';
3514 switch (c) {
3515 case '\t': c = 't'; break;
3516 case '\n': c = 'n'; break;
3517 case '\r': c = 'r'; break;
3518 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003519 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003520 }
3521 out->str[out->len++] = c;
3522 } else {
3523 if (out->len > end - 5)
3524 break;
3525 out->str[out->len++] = '\\';
3526 out->str[out->len++] = 'x';
3527 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3528 out->str[out->len++] = hextab[c & 0xF];
3529 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003530 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003531 /* we had a line break, let's return now */
3532 out->str[out->len++] = '\n';
3533 *line = ptr;
3534 return ptr;
3535 }
3536 }
3537 /* we have an incomplete line, we return it as-is */
3538 out->str[out->len++] = '\n';
3539 return ptr;
3540}
3541
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003542/* This function dumps all captured errors onto the stream intreface's
3543 * read buffer. The data_ctx must have been zeroed first, and the flags
3544 * properly set. It returns 0 if the output buffer is full and it needs
3545 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003546 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003547static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003548{
3549 extern const char *monthname[12];
3550 struct chunk msg;
3551
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003552 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003553 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003554
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003555 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003556
Willy Tarreau295a8372011-03-10 11:25:07 +01003557 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003558 /* the function had not been called yet, let's prepare the
3559 * buffer for a response.
3560 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003561 struct tm tm;
3562
3563 get_localtime(date.tv_sec, &tm);
3564 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3565 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3566 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3567 error_snapshot_id);
3568
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003569 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003570 /* Socket buffer full. Let's try again later from the same point */
3571 return 0;
3572 }
3573
Willy Tarreau295a8372011-03-10 11:25:07 +01003574 si->applet.ctx.errors.px = proxy;
3575 si->applet.ctx.errors.buf = 0;
3576 si->applet.ctx.errors.bol = 0;
3577 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003578 }
3579
3580 /* we have two inner loops here, one for the proxy, the other one for
3581 * the buffer.
3582 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003583 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003584 struct error_snapshot *es;
3585
Willy Tarreau295a8372011-03-10 11:25:07 +01003586 if (si->applet.ctx.errors.buf == 0)
3587 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003588 else
Willy Tarreau295a8372011-03-10 11:25:07 +01003589 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003590
3591 if (!es->when.tv_sec)
3592 goto next;
3593
Willy Tarreau295a8372011-03-10 11:25:07 +01003594 if (si->applet.ctx.errors.iid >= 0 &&
3595 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
3596 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003597 goto next;
3598
Willy Tarreau295a8372011-03-10 11:25:07 +01003599 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003600 /* just print headers now */
3601
3602 char pn[INET6_ADDRSTRLEN];
3603 struct tm tm;
3604
3605 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003606 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003607 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003608 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003609
3610
3611 if (es->src.ss_family == AF_INET)
3612 inet_ntop(AF_INET,
3613 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
3614 pn, sizeof(pn));
3615 else
3616 inet_ntop(AF_INET6,
3617 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
3618 pn, sizeof(pn));
3619
Willy Tarreau295a8372011-03-10 11:25:07 +01003620 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003621 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003622 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003623 " frontend %s (#%d): invalid request\n"
3624 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003625 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003626 " request length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003627 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau3fd6cec2010-03-25 06:45:07 +01003628 pn, es->sid, (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3629 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003630 es->srv ? es->srv->id : "<NONE>",
3631 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003632 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003633 es->len, es->pos);
3634 break;
3635 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003636 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003637 " backend %s (#%d) : invalid response\n"
3638 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003639 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003640 " response length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003641 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003642 pn, es->sid, es->oe->id, es->oe->uuid,
3643 es->srv ? es->srv->id : "<NONE>",
3644 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003645 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003646 es->len, es->pos);
3647 break;
3648 }
3649
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003650 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003651 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003652 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003653 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003654 si->applet.ctx.errors.ptr = 0;
3655 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003656 }
3657
Willy Tarreau295a8372011-03-10 11:25:07 +01003658 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003659 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003660 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003661 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003662 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau61b34732009-10-03 23:49:35 +02003663 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003664 goto next;
3665 }
3666
3667 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01003668 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003669 int newptr;
3670 int newline;
3671
Willy Tarreau295a8372011-03-10 11:25:07 +01003672 newline = si->applet.ctx.errors.bol;
3673 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
3674 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02003675 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003676
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003677 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003678 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003679 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003680 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003681 si->applet.ctx.errors.ptr = newptr;
3682 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003683 };
3684 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01003685 si->applet.ctx.errors.bol = 0;
3686 si->applet.ctx.errors.ptr = -1;
3687 si->applet.ctx.errors.buf++;
3688 if (si->applet.ctx.errors.buf > 1) {
3689 si->applet.ctx.errors.buf = 0;
3690 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003691 }
3692 }
3693
3694 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02003695 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003696}
3697
Willy Tarreaub24281b2011-02-13 13:16:36 +01003698struct si_applet http_stats_applet = {
3699 .name = "<STATS>", /* used for logging */
3700 .fct = http_stats_io_handler,
3701};
3702
Simon Horman9bd2c732011-06-15 15:18:44 +09003703static struct si_applet cli_applet = {
Willy Tarreaub24281b2011-02-13 13:16:36 +01003704 .name = "<CLI>", /* used for logging */
3705 .fct = cli_io_handler,
3706};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003707
Willy Tarreau10522fd2008-07-09 20:12:41 +02003708static struct cfg_kw_list cfg_kws = {{ },{
3709 { CFG_GLOBAL, "stats", stats_parse_global },
3710 { 0, NULL, NULL },
3711}};
3712
3713__attribute__((constructor))
3714static void __dumpstats_module_init(void)
3715{
3716 cfg_register_keywords(&cfg_kws);
3717}
3718
Willy Tarreau91861262007-10-17 17:06:05 +02003719/*
3720 * Local variables:
3721 * c-indent-level: 8
3722 * c-basic-offset: 8
3723 * End:
3724 */