blob: 964ba83b3629f6d94aa7d4c21fc21ad5239d0392 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaueb472682010-05-28 18:46:57 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
Simon Horman17bce342011-06-15 15:18:47 +090017#include <stdbool.h>
Simon Hormancec9a222011-06-15 15:18:51 +090018#include <stdint.h>
Willy Tarreau91861262007-10-17 17:06:05 +020019#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020022#include <pwd.h>
23#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020024
25#include <sys/socket.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28
Willy Tarreau10522fd2008-07-09 20:12:41 +020029#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020030#include <common/compat.h>
31#include <common/config.h>
32#include <common/debug.h>
33#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
40
Willy Tarreau91861262007-10-17 17:06:05 +020041#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020042
43#include <proto/backend.h>
44#include <proto/buffers.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020045#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020046#include <proto/dumpstats.h>
47#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010048#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020049#include <proto/log.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010050#include <proto/pipe.h>
Willy Tarreau2a0f4d22011-08-02 11:49:05 +020051#include <proto/protocols.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020052#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020053#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020054#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020055#include <proto/server.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010056#include <proto/stream_interface.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020057#include <proto/stream_sock.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010058#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020059
Simon Horman9bd2c732011-06-15 15:18:44 +090060static int stats_dump_raw_to_buffer(struct stream_interface *si);
61static int stats_dump_full_sess_to_buffer(struct stream_interface *si);
62static int stats_dump_sess_to_buffer(struct stream_interface *si);
63static int stats_dump_errors_to_buffer(struct stream_interface *si);
Simon Hormanc88b8872011-06-15 15:18:49 +090064static int stats_table_request(struct stream_interface *si, bool show);
Simon Horman9bd2c732011-06-15 15:18:44 +090065static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
66static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri);
67
68static struct si_applet cli_applet;
69
70static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020071 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +020072 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +020073 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020074 " help : this message\n"
75 " prompt : toggle interactive mode with prompt\n"
76 " quit : disconnect\n"
77 " show info : report information about the running process\n"
78 " show stat : report counters for each proxy and server\n"
79 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +010080 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +020081 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +020082 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +020083 " set weight : change a server's weight\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +010084 " set timeout : change a timeout setting\n"
Cyril Bontécd19e512010-01-31 22:34:03 +010085 " disable server : set a server in maintenance mode\n"
86 " enable server : re-enable a server that was previously in maintenance mode\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +020087 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +020088 " set rate-limit : change a rate limiting value\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020089 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020090
Simon Horman9bd2c732011-06-15 15:18:44 +090091static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +020092 "Permission denied\n"
93 "";
94
Willy Tarreau295a8372011-03-10 11:25:07 +010095/* data transmission states for the stats responses */
96enum {
97 STAT_ST_INIT = 0,
98 STAT_ST_HEAD,
99 STAT_ST_INFO,
100 STAT_ST_LIST,
101 STAT_ST_END,
102 STAT_ST_FIN,
103};
104
105/* data transmission states for the stats responses inside a proxy */
106enum {
107 STAT_PX_ST_INIT = 0,
108 STAT_PX_ST_TH,
109 STAT_PX_ST_FE,
110 STAT_PX_ST_LI,
111 STAT_PX_ST_SV,
112 STAT_PX_ST_BE,
113 STAT_PX_ST_END,
114 STAT_PX_ST_FIN,
115};
116
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200117/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +0200118 * a new stats socket. It returns a positive value upon success, 0 if the connection
119 * needs to be closed and ignored, or a negative value upon critical failure.
120 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900121static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200122{
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200123 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100124 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200125 copy_target(&s->target, &s->si[1].target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +0100126 s->si[1].applet.private = s;
127 s->si[1].applet.st1 = 0;
128 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200129
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200130 tv_zero(&s->logs.tv_request);
131 s->logs.t_queue = 0;
132 s->logs.t_connect = 0;
133 s->logs.t_data = 0;
134 s->logs.t_close = 0;
135 s->logs.bytes_in = s->logs.bytes_out = 0;
136 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
137 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200138
Willy Tarreaueb472682010-05-28 18:46:57 +0200139 s->req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
140
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200141 if (s->listener->timeout) {
142 s->req->rto = *s->listener->timeout;
143 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200144 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200145 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200146}
147
Willy Tarreau07e9e642010-08-17 21:48:17 +0200148/* allocate a new stats frontend named <name>, and return it
149 * (or NULL in case of lack of memory).
150 */
151static struct proxy *alloc_stats_fe(const char *name)
152{
153 struct proxy *fe;
154
155 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
156 if (!fe)
157 return NULL;
158
Willy Tarreau237250c2011-07-29 01:49:03 +0200159 init_new_proxy(fe);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200160
161 fe->last_change = now.tv_sec;
162 fe->id = strdup("GLOBAL");
163 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200164 fe->maxconn = 10; /* default to 10 concurrent connections */
165 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
166
Willy Tarreau07e9e642010-08-17 21:48:17 +0200167 return fe;
168}
169
Willy Tarreaufbee7132007-10-18 13:53:22 +0200170/* This function parses a "stats" statement in the "global" section. It returns
171 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
172 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
173 * zero included. The trailing '\n' must not be written. The function must be
174 * called with <args> pointing to the first word after "stats".
175 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200176static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
177 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200178{
Willy Tarreau10522fd2008-07-09 20:12:41 +0200179 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200180 if (!strcmp(args[0], "socket")) {
Andreas Kohn16171e22011-01-19 20:29:32 +0100181 struct sockaddr_un *su;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200182 int cur_arg;
183
184 if (*args[1] == 0) {
185 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
186 return -1;
187 }
188
189 if (global.stats_sock.state != LI_NEW) {
190 snprintf(err, errlen, "'stats socket' already specified in global section");
191 return -1;
192 }
193
Andreas Kohn16171e22011-01-19 20:29:32 +0100194 su = str2sun(args[1]);
195 if (!su) {
196 snprintf(err, errlen, "'stats socket' path would require truncation");
197 return -1;
198 }
199 memcpy(&global.stats_sock.addr, su, sizeof(struct sockaddr_un)); // guaranteed to fit
Willy Tarreaufbee7132007-10-18 13:53:22 +0200200
Willy Tarreau89a63132009-08-16 17:41:45 +0200201 if (!global.stats_fe) {
Willy Tarreau07e9e642010-08-17 21:48:17 +0200202 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau89a63132009-08-16 17:41:45 +0200203 snprintf(err, errlen, "out of memory");
204 return -1;
205 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200206 }
207
Willy Tarreaufbee7132007-10-18 13:53:22 +0200208 global.stats_sock.state = LI_INIT;
Willy Tarreau3c63fd82011-09-07 18:00:47 +0200209 global.stats_sock.options = LI_O_UNLIMITED;
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200210 global.stats_sock.accept = session_accept;
211 global.stats_fe->accept = stats_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200212 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200213 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200214 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreaueb472682010-05-28 18:46:57 +0200215 global.stats_sock.frontend = global.stats_fe;
Willy Tarreau6162db22009-10-10 17:13:00 +0200216 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200217 global.stats_sock.maxconn = global.stats_fe->maxconn;
Willy Tarreau89a63132009-08-16 17:41:45 +0200218 global.stats_sock.timeout = &global.stats_fe->timeout.client;
219
220 global.stats_sock.next = global.stats_fe->listen;
221 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200222
223 cur_arg = 2;
224 while (*args[cur_arg]) {
225 if (!strcmp(args[cur_arg], "uid")) {
226 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
227 cur_arg += 2;
228 }
229 else if (!strcmp(args[cur_arg], "gid")) {
230 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
231 cur_arg += 2;
232 }
233 else if (!strcmp(args[cur_arg], "mode")) {
234 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
235 cur_arg += 2;
236 }
237 else if (!strcmp(args[cur_arg], "user")) {
238 struct passwd *user;
239 user = getpwnam(args[cur_arg + 1]);
240 if (!user) {
241 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
242 args[cur_arg + 1]);
243 return -1;
244 }
245 global.stats_sock.perm.ux.uid = user->pw_uid;
246 cur_arg += 2;
247 }
248 else if (!strcmp(args[cur_arg], "group")) {
249 struct group *group;
250 group = getgrnam(args[cur_arg + 1]);
251 if (!group) {
252 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
253 args[cur_arg + 1]);
254 return -1;
255 }
256 global.stats_sock.perm.ux.gid = group->gr_gid;
257 cur_arg += 2;
258 }
Willy Tarreau6162db22009-10-10 17:13:00 +0200259 else if (!strcmp(args[cur_arg], "level")) {
260 if (!strcmp(args[cur_arg+1], "user"))
261 global.stats_sock.perm.ux.level = ACCESS_LVL_USER;
262 else if (!strcmp(args[cur_arg+1], "operator"))
263 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER;
264 else if (!strcmp(args[cur_arg+1], "admin"))
265 global.stats_sock.perm.ux.level = ACCESS_LVL_ADMIN;
266 else {
267 snprintf(err, errlen, "'stats socket level' only supports 'user', 'operator', and 'admin'");
268 return -1;
269 }
270 cur_arg += 2;
271 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200272 else {
Willy Tarreau6162db22009-10-10 17:13:00 +0200273 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', 'level', and 'mode'");
Willy Tarreaufbee7132007-10-18 13:53:22 +0200274 return -1;
275 }
276 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100277
Willy Tarreaufbee7132007-10-18 13:53:22 +0200278 uxst_add_listener(&global.stats_sock);
279 global.maxsock++;
280 }
281 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100282 unsigned timeout;
283 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
284
285 if (res) {
286 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
287 return -1;
288 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200289
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100290 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200291 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
292 return -1;
293 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200294 if (!global.stats_fe) {
295 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
296 snprintf(err, errlen, "out of memory");
297 return -1;
298 }
299 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200300 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200301 }
302 else if (!strcmp(args[0], "maxconn")) {
303 int maxconn = atol(args[1]);
304
305 if (maxconn <= 0) {
306 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
307 return -1;
308 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200309
310 if (!global.stats_fe) {
311 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
312 snprintf(err, errlen, "out of memory");
313 return -1;
314 }
315 }
316 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200317 }
318 else {
319 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
320 return -1;
321 }
322 return 0;
323}
324
Simon Horman9bd2c732011-06-15 15:18:44 +0900325static int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100326{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200327 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100328 "# pxname,svname,"
329 "qcur,qmax,"
330 "scur,smax,slim,stot,"
331 "bin,bout,"
332 "dreq,dresp,"
333 "ereq,econ,eresp,"
334 "wretr,wredis,"
335 "status,weight,act,bck,"
336 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200337 "pid,iid,sid,throttle,lbtot,tracked,type,"
338 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200339 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100340 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100341 "req_rate,req_rate_max,req_tot,"
342 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100343 "\n");
344}
345
Simon Hormand9366582011-06-15 15:18:45 +0900346/* print a string of text buffer to <out>. The format is :
347 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
348 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
349 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
350 */
351static int dump_text(struct chunk *out, const char *buf, int bsize)
352{
353 unsigned char c;
354 int ptr = 0;
355
356 while (buf[ptr] && ptr < bsize) {
357 c = buf[ptr];
358 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
359 if (out->len > out->size - 1)
360 break;
361 out->str[out->len++] = c;
362 }
363 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
364 if (out->len > out->size - 2)
365 break;
366 out->str[out->len++] = '\\';
367 switch (c) {
368 case ' ': c = ' '; break;
369 case '\t': c = 't'; break;
370 case '\n': c = 'n'; break;
371 case '\r': c = 'r'; break;
372 case '\e': c = 'e'; break;
373 case '\\': c = '\\'; break;
374 }
375 out->str[out->len++] = c;
376 }
377 else {
378 if (out->len > out->size - 4)
379 break;
380 out->str[out->len++] = '\\';
381 out->str[out->len++] = 'x';
382 out->str[out->len++] = hextab[(c >> 4) & 0xF];
383 out->str[out->len++] = hextab[c & 0xF];
384 }
385 ptr++;
386 }
387
388 return ptr;
389}
390
391/* print a buffer in hexa.
392 * Print stopped if <bsize> is reached, or if no more place in the chunk.
393 */
394static int dump_binary(struct chunk *out, const char *buf, int bsize)
395{
396 unsigned char c;
397 int ptr = 0;
398
399 while (ptr < bsize) {
400 c = buf[ptr];
401
402 if (out->len > out->size - 2)
403 break;
404 out->str[out->len++] = hextab[(c >> 4) & 0xF];
405 out->str[out->len++] = hextab[c & 0xF];
406
407 ptr++;
408 }
409 return ptr;
410}
411
412/* Dump the status of a table to a stream interface's
413 * read buffer. It returns 0 if the output buffer is full
414 * and needs to be called again, otherwise non-zero.
415 */
416static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
417 struct proxy *proxy, struct proxy *target)
418{
419 struct session *s = si->applet.private;
420
421 chunk_printf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
422 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
423
424 /* any other information should be dumped here */
425
426 if (target && s->listener->perm.ux.level < ACCESS_LVL_OPER)
427 chunk_printf(msg, "# contents not dumped due to insufficient privileges\n");
428
429 if (buffer_feed_chunk(si->ib, msg) >= 0)
430 return 0;
431
432 return 1;
433}
434
435/* Dump the a table entry to a stream interface's
436 * read buffer. It returns 0 if the output buffer is full
437 * and needs to be called again, otherwise non-zero.
438 */
439static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
440 struct proxy *proxy, struct stksess *entry)
441{
442 int dt;
443
444 chunk_printf(msg, "%p:", entry);
445
446 if (proxy->table.type == STKTABLE_TYPE_IP) {
447 char addr[INET_ADDRSTRLEN];
448 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
449 chunk_printf(msg, " key=%s", addr);
450 }
451 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
452 char addr[INET6_ADDRSTRLEN];
453 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
454 chunk_printf(msg, " key=%s", addr);
455 }
456 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
457 chunk_printf(msg, " key=%u", *(unsigned int *)entry->key.key);
458 }
459 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
460 chunk_printf(msg, " key=");
461 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
462 }
463 else {
464 chunk_printf(msg, " key=");
465 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
466 }
467
468 chunk_printf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
469
470 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
471 void *ptr;
472
473 if (proxy->table.data_ofs[dt] == 0)
474 continue;
475 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
476 chunk_printf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
477 else
478 chunk_printf(msg, " %s=", stktable_data_types[dt].name);
479
480 ptr = stktable_data_ptr(&proxy->table, entry, dt);
481 switch (stktable_data_types[dt].std_type) {
482 case STD_T_SINT:
483 chunk_printf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
484 break;
485 case STD_T_UINT:
486 chunk_printf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
487 break;
488 case STD_T_ULL:
489 chunk_printf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
490 break;
491 case STD_T_FRQP:
492 chunk_printf(msg, "%d",
493 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
494 proxy->table.data_arg[dt].u));
495 break;
496 }
497 }
498 chunk_printf(msg, "\n");
499
500 if (buffer_feed_chunk(si->ib, msg) >= 0)
501 return 0;
502
503 return 1;
504}
505
Simon Horman17bce342011-06-15 15:18:47 +0900506static void stats_sock_table_key_request(struct stream_interface *si, char **args, bool show)
Simon Horman121f3052011-06-15 15:18:46 +0900507{
508 struct session *s = si->applet.private;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900509 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900510 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900511 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900512 unsigned char ip6_key[sizeof(struct in6_addr)];
Simon Horman121f3052011-06-15 15:18:46 +0900513
Simon Hormand5b9fd92011-06-15 15:18:48 +0900514 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900515
516 if (!*args[4]) {
517 si->applet.ctx.cli.msg = "Key value expected\n";
518 si->applet.st0 = STAT_CLI_PRINT;
519 return;
520 }
521
Simon Hormanc5b89f62011-06-15 15:18:50 +0900522 switch (px->table.type) {
523 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900524 uint32_key = htonl(inetaddr_host(args[4]));
525 static_table_key.key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900526 break;
527 case STKTABLE_TYPE_IPV6:
528 inet_pton(AF_INET6, args[4], ip6_key);
529 static_table_key.key = &ip6_key;
530 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900531 case STKTABLE_TYPE_INTEGER:
532 {
533 char *endptr;
534 unsigned long val;
535 errno = 0;
536 val = strtoul(args[4], &endptr, 10);
537 if ((errno == ERANGE && val == ULONG_MAX) ||
538 (errno != 0 && val == 0) || endptr == args[4] ||
539 val > 0xffffffff) {
540 si->applet.ctx.cli.msg = "Invalid key\n";
541 si->applet.st0 = STAT_CLI_PRINT;
542 return;
543 }
544 uint32_key = (uint32_t) val;
545 static_table_key.key = &uint32_key;
546 break;
547 }
548 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900549 case STKTABLE_TYPE_STRING:
550 static_table_key.key = args[4];
551 static_table_key.key_len = strlen(args[4]);
552 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900553 default:
Simon Horman17bce342011-06-15 15:18:47 +0900554 if (show)
Simon Hormancec9a222011-06-15 15:18:51 +0900555 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 +0900556 else
Simon Hormancec9a222011-06-15 15:18:51 +0900557 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 +0900558 si->applet.st0 = STAT_CLI_PRINT;
559 return;
560 }
561
562 /* check permissions */
563 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
564 si->applet.ctx.cli.msg = stats_permission_denied_msg;
565 si->applet.st0 = STAT_CLI_PRINT;
566 return;
567 }
568
569 ts = stktable_lookup_key(&px->table, &static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900570 if (!ts)
571 return;
572
573 if (show) {
574 struct chunk msg;
575 chunk_init(&msg, trash, sizeof(trash));
576 if (!stats_dump_table_head_to_buffer(&msg, si, px, px))
577 return;
578 stats_dump_table_entry_to_buffer(&msg, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900579 return;
580 }
Simon Horman17bce342011-06-15 15:18:47 +0900581
582 if (ts->ref_cnt) {
Simon Horman121f3052011-06-15 15:18:46 +0900583 /* don't delete an entry which is currently referenced */
584 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
585 si->applet.st0 = STAT_CLI_PRINT;
586 return;
587 }
588
589 stksess_kill(&px->table, ts);
590}
591
Simon Hormand5b9fd92011-06-15 15:18:48 +0900592static void stats_sock_table_data_request(struct stream_interface *si, char **args)
593{
594 /* condition on stored data value */
595 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
596 if (si->applet.ctx.table.data_type < 0) {
597 si->applet.ctx.cli.msg = "Unknown data type\n";
598 si->applet.st0 = STAT_CLI_PRINT;
599 return;
600 }
601
602 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
603 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
604 si->applet.st0 = STAT_CLI_PRINT;
605 return;
606 }
607
608 si->applet.ctx.table.data_op = get_std_op(args[4]);
609 if (si->applet.ctx.table.data_op < 0) {
610 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
611 si->applet.st0 = STAT_CLI_PRINT;
612 return;
613 }
614
615 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
616 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
617 si->applet.st0 = STAT_CLI_PRINT;
618 return;
619 }
620}
621
622static void stats_sock_table_request(struct stream_interface *si, char **args, bool show)
623{
624 si->applet.ctx.table.data_type = -1;
625 si->applet.state = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200626 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900627 si->applet.ctx.table.proxy = NULL;
628 si->applet.ctx.table.entry = NULL;
Simon Hormanc88b8872011-06-15 15:18:49 +0900629 if (show)
630 si->applet.st0 = STAT_CLI_O_TAB;
631 else
632 si->applet.st0 = STAT_CLI_O_CLR;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900633
634 if (*args[2]) {
635 si->applet.ctx.table.target = find_stktable(args[2]);
636 if (!si->applet.ctx.table.target) {
637 si->applet.ctx.cli.msg = "No such table\n";
638 si->applet.st0 = STAT_CLI_PRINT;
639 return;
640 }
641 }
642 else {
643 if (!show)
644 goto err_args;
645 return;
646 }
647
648 if (strcmp(args[3], "key") == 0)
649 stats_sock_table_key_request(si, args, show);
Simon Hormanc88b8872011-06-15 15:18:49 +0900650 else if (strncmp(args[3], "data.", 5) == 0)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900651 stats_sock_table_data_request(si, args);
Simon Hormanc88b8872011-06-15 15:18:49 +0900652 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900653 goto err_args;
654
655 return;
656
657err_args:
658 if (show)
659 si->applet.ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
660 else
Simon Hormanc88b8872011-06-15 15:18:49 +0900661 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 +0900662 si->applet.st0 = STAT_CLI_PRINT;
663}
664
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200665/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200666 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100667 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200668 * designating the function which will have to process the request, which can
669 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200670 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900671static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200672{
Willy Tarreaubc4af052011-02-13 13:25:14 +0100673 struct session *s = si->applet.private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200674 char *args[MAX_STATS_ARGS + 1];
675 int arg;
676
677 while (isspace((unsigned char)*line))
678 line++;
679
680 arg = 0;
681 args[arg] = line;
682
683 while (*line && arg < MAX_STATS_ARGS) {
684 if (isspace((unsigned char)*line)) {
685 *line++ = '\0';
686
687 while (isspace((unsigned char)*line))
688 line++;
689
690 args[++arg] = line;
691 continue;
692 }
693
694 line++;
695 }
696
697 while (++arg <= MAX_STATS_ARGS)
698 args[arg] = line;
699
Willy Tarreau295a8372011-03-10 11:25:07 +0100700 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200701 if (strcmp(args[0], "show") == 0) {
702 if (strcmp(args[1], "stat") == 0) {
703 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100704 si->applet.ctx.stats.flags |= STAT_BOUND;
705 si->applet.ctx.stats.iid = atoi(args[2]);
706 si->applet.ctx.stats.type = atoi(args[3]);
707 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200708 }
709
Willy Tarreau295a8372011-03-10 11:25:07 +0100710 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
711 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
712 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100713 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200714 }
715 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100716 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
717 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
718 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100719 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200720 }
721 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100722 si->applet.state = STAT_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200723 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100724 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100725 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200726 return 1;
727 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100728 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100729 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100730 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100731 si->applet.ctx.sess.target = NULL;
732 si->applet.ctx.sess.section = 0; /* start with session status */
733 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100734 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200735 }
736 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200737 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100738 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100739 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200740 return 1;
741 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200742 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100743 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200744 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100745 si->applet.ctx.errors.iid = -1;
746 si->applet.ctx.errors.px = NULL;
747 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100748 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200749 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200750 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900751 stats_sock_table_request(si, args, true);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200752 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200753 else { /* neither "stat" nor "info" nor "sess" nor "errors" no "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200754 return 0;
755 }
756 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200757 else if (strcmp(args[0], "clear") == 0) {
758 if (strcmp(args[1], "counters") == 0) {
759 struct proxy *px;
760 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200761 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200762 int clrall = 0;
763
764 if (strcmp(args[2], "all") == 0)
765 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200766
Willy Tarreau6162db22009-10-10 17:13:00 +0200767 /* check permissions */
768 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
769 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100770 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100771 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200772 return 1;
773 }
774
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200775 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100776 if (clrall) {
777 memset(&px->be_counters, 0, sizeof(px->be_counters));
778 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
779 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200780 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100781 px->be_counters.conn_max = 0;
782 px->be_counters.p.http.rps_max = 0;
783 px->be_counters.sps_max = 0;
784 px->be_counters.cps_max = 0;
785 px->be_counters.nbpend_max = 0;
786
787 px->fe_counters.conn_max = 0;
788 px->fe_counters.p.http.rps_max = 0;
789 px->fe_counters.sps_max = 0;
790 px->fe_counters.cps_max = 0;
791 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200792 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200793
794 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200795 if (clrall)
796 memset(&sv->counters, 0, sizeof(sv->counters));
797 else {
798 sv->counters.cur_sess_max = 0;
799 sv->counters.nbpend_max = 0;
800 sv->counters.sps_max = 0;
801 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200802
803 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200804 if (li->counters) {
805 if (clrall)
806 memset(li->counters, 0, sizeof(*li->counters));
807 else
808 li->counters->conn_max = 0;
809 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200810 }
811
Willy Tarreau81c25d02011-09-07 15:17:21 +0200812 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200813 return 1;
814 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200815 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900816 stats_sock_table_request(si, args, false);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200817 /* end of processing */
818 return 1;
819 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200820 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200821 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200822 return 0;
823 }
824 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200825 else if (strcmp(args[0], "get") == 0) {
826 if (strcmp(args[1], "weight") == 0) {
827 struct proxy *px;
828 struct server *sv;
829
830 /* split "backend/server" and make <line> point to server */
831 for (line = args[2]; *line; line++)
832 if (*line == '/') {
833 *line++ = '\0';
834 break;
835 }
836
837 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100838 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100839 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200840 return 1;
841 }
842
843 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100844 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100845 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200846 return 1;
847 }
848
849 /* return server's effective weight at the moment */
850 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
851 buffer_feed(si->ib, trash);
852 return 1;
853 }
854 else { /* not "get weight" */
855 return 0;
856 }
857 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200858 else if (strcmp(args[0], "set") == 0) {
859 if (strcmp(args[1], "weight") == 0) {
860 struct proxy *px;
861 struct server *sv;
862 int w;
863
864 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100865 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100866 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200867 return 1;
868 }
869
870 /* split "backend/server" and make <line> point to server */
871 for (line = args[2]; *line; line++)
872 if (*line == '/') {
873 *line++ = '\0';
874 break;
875 }
876
877 if (!*line || !*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100878 si->applet.ctx.cli.msg = "Require 'backend/server' and 'weight' or 'weight%'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100879 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200880 return 1;
881 }
882
883 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100884 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100885 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200886 return 1;
887 }
888
Cyril Bonté613f0df2011-03-03 20:49:04 +0100889 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100890 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100891 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100892 return 1;
893 }
894
Willy Tarreau4483d432009-10-10 19:30:08 +0200895 /* if the weight is terminated with '%', it is set relative to
896 * the initial weight, otherwise it is absolute.
897 */
898 w = atoi(args[3]);
899 if (strchr(args[3], '%') != NULL) {
900 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100901 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100902 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200903 return 1;
904 }
905 w = sv->iweight * w / 100;
906 }
907 else {
908 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100909 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100910 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200911 return 1;
912 }
913 }
914
915 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100916 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 +0100917 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200918 return 1;
919 }
920
921 sv->uweight = w;
922
923 if (px->lbprm.algo & BE_LB_PROP_DYN) {
924 /* we must take care of not pushing the server to full throttle during slow starts */
925 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
926 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
927 else
928 sv->eweight = BE_WEIGHT_SCALE;
929 sv->eweight *= sv->uweight;
930 } else {
931 sv->eweight = sv->uweight;
932 }
933
934 /* static LB algorithms are a bit harder to update */
935 if (px->lbprm.update_server_eweight)
936 px->lbprm.update_server_eweight(sv);
937 else if (sv->eweight)
938 px->lbprm.set_server_status_up(sv);
939 else
940 px->lbprm.set_server_status_down(sv);
941
942 return 1;
943 }
Willy Tarreau7aabd112010-01-26 10:59:06 +0100944 else if (strcmp(args[1], "timeout") == 0) {
945 if (strcmp(args[2], "cli") == 0) {
946 unsigned timeout;
947 const char *res;
948
949 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100950 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100951 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100952 return 1;
953 }
954
955 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
956 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100957 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100958 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100959 return 1;
960 }
961
962 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
963 return 1;
964 }
965 else {
Willy Tarreau295a8372011-03-10 11:25:07 +0100966 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100967 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100968 return 1;
969 }
970 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200971 else if (strcmp(args[1], "maxconn") == 0) {
972 if (strcmp(args[2], "frontend") == 0) {
973 struct proxy *px;
974 struct listener *l;
975 int v;
976
977 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
978 si->applet.ctx.cli.msg = stats_permission_denied_msg;
979 si->applet.st0 = STAT_CLI_PRINT;
980 return 1;
981 }
982
983 if (!*args[3]) {
984 si->applet.ctx.cli.msg = "Frontend name expected.\n";
985 si->applet.st0 = STAT_CLI_PRINT;
986 return 1;
987 }
988
989 px = findproxy(args[3], PR_CAP_FE);
990 if (!px) {
991 si->applet.ctx.cli.msg = "No such frontend.\n";
992 si->applet.st0 = STAT_CLI_PRINT;
993 return 1;
994 }
995
996 if (!*args[4]) {
997 si->applet.ctx.cli.msg = "Integer value expected.\n";
998 si->applet.st0 = STAT_CLI_PRINT;
999 return 1;
1000 }
1001
1002 v = atoi(args[4]);
1003 /* check for unlimited values, we restore default setting (cfg_maxpconn) */
1004 if (v < 1) {
1005 si->applet.ctx.cli.msg = "Value out of range.\n";
1006 si->applet.st0 = STAT_CLI_PRINT;
1007 return 1;
1008 }
1009
1010 /* OK, the value is fine, so we assign it to the proxy and to all of
1011 * its listeners. The blocked ones will be dequeued.
1012 */
1013 px->maxconn = v;
1014 for (l = px->listen; l != NULL; l = l->next) {
1015 l->maxconn = v;
1016 if (l->state == LI_FULL)
1017 resume_listener(l);
1018 }
1019
1020 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1021 dequeue_all_listeners(&s->fe->listener_queue);
1022
1023 return 1;
1024 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001025 else if (strcmp(args[2], "global") == 0) {
1026 int v;
1027
1028 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1029 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1030 si->applet.st0 = STAT_CLI_PRINT;
1031 return 1;
1032 }
1033
1034 if (!*args[3]) {
1035 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1036 si->applet.st0 = STAT_CLI_PRINT;
1037 return 1;
1038 }
1039
1040 v = atoi(args[3]);
1041 if (v > global.hardmaxconn) {
1042 si->applet.ctx.cli.msg = "Value out of range.\n";
1043 si->applet.st0 = STAT_CLI_PRINT;
1044 return 1;
1045 }
1046
1047 /* check for unlimited values */
1048 if (v <= 0)
1049 v = global.hardmaxconn;
1050
1051 global.maxconn = v;
1052
1053 /* Dequeues all of the listeners waiting for a resource */
1054 if (!LIST_ISEMPTY(&global_listener_queue))
1055 dequeue_all_listeners(&global_listener_queue);
1056
1057 return 1;
1058 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001059 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001060 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001061 si->applet.st0 = STAT_CLI_PRINT;
1062 return 1;
1063 }
1064 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001065 else if (strcmp(args[1], "rate-limit") == 0) {
1066 if (strcmp(args[2], "connections") == 0) {
1067 if (strcmp(args[3], "global") == 0) {
1068 int v;
1069
1070 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1071 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1072 si->applet.st0 = STAT_CLI_PRINT;
1073 return 1;
1074 }
1075
1076 if (!*args[4]) {
1077 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1078 si->applet.st0 = STAT_CLI_PRINT;
1079 return 1;
1080 }
1081
1082 v = atoi(args[4]);
1083 if (v < 0) {
1084 si->applet.ctx.cli.msg = "Value out of range.\n";
1085 si->applet.st0 = STAT_CLI_PRINT;
1086 return 1;
1087 }
1088
1089 global.cps_lim = v;
1090
1091 /* Dequeues all of the listeners waiting for a resource */
1092 if (!LIST_ISEMPTY(&global_listener_queue))
1093 dequeue_all_listeners(&global_listener_queue);
1094
1095 return 1;
1096 }
1097 else {
1098 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1099 si->applet.st0 = STAT_CLI_PRINT;
1100 return 1;
1101 }
1102 }
1103 else {
1104 si->applet.ctx.cli.msg = "'set rate-limit' only supports 'connections'.\n";
1105 si->applet.st0 = STAT_CLI_PRINT;
1106 return 1;
1107 }
1108 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001109 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001110 return 0;
1111 }
1112 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001113 else if (strcmp(args[0], "enable") == 0) {
1114 if (strcmp(args[1], "server") == 0) {
1115 struct proxy *px;
1116 struct server *sv;
1117
1118 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001119 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001120 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001121 return 1;
1122 }
1123
1124 /* split "backend/server" and make <line> point to server */
1125 for (line = args[2]; *line; line++)
1126 if (*line == '/') {
1127 *line++ = '\0';
1128 break;
1129 }
1130
1131 if (!*line || !*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001132 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001133 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001134 return 1;
1135 }
1136
1137 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001138 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001139 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001140 return 1;
1141 }
1142
Cyril Bonté613f0df2011-03-03 20:49:04 +01001143 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001144 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001145 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001146 return 1;
1147 }
1148
Cyril Bontécd19e512010-01-31 22:34:03 +01001149 if (sv->state & SRV_MAINTAIN) {
1150 /* The server is really in maintenance, we can change the server state */
1151 if (sv->tracked) {
1152 /* If this server tracks the status of another one,
1153 * we must restore the good status.
1154 */
1155 if (sv->tracked->state & SRV_RUNNING) {
1156 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001157 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001158 } else {
1159 sv->state &= ~SRV_MAINTAIN;
1160 set_server_down(sv);
1161 }
1162 } else {
1163 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001164 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001165 }
1166 }
1167
1168 return 1;
1169 }
1170 else { /* unknown "enable" parameter */
1171 return 0;
1172 }
1173 }
1174 else if (strcmp(args[0], "disable") == 0) {
1175 if (strcmp(args[1], "server") == 0) {
1176 struct proxy *px;
1177 struct server *sv;
1178
1179 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001180 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001181 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001182 return 1;
1183 }
1184
1185 /* split "backend/server" and make <line> point to server */
1186 for (line = args[2]; *line; line++)
1187 if (*line == '/') {
1188 *line++ = '\0';
1189 break;
1190 }
1191
1192 if (!*line || !*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001193 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001194 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001195 return 1;
1196 }
1197
1198 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001199 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001200 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001201 return 1;
1202 }
1203
Cyril Bonté613f0df2011-03-03 20:49:04 +01001204 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001205 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001206 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001207 return 1;
1208 }
1209
Cyril Bontécd19e512010-01-31 22:34:03 +01001210 if (! (sv->state & SRV_MAINTAIN)) {
1211 /* Not already in maintenance, we can change the server state */
1212 sv->state |= SRV_MAINTAIN;
1213 set_server_down(sv);
1214 }
1215
1216 return 1;
1217 }
1218 else { /* unknown "disable" parameter */
1219 return 0;
1220 }
1221 }
1222 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001223 return 0;
1224 }
1225 return 1;
1226}
1227
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001228/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001229 * used to processes I/O from/to the stats unix socket. The system relies on a
1230 * state machine handling requests and various responses. We read a request,
1231 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001232 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1233 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001234 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001235 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001236static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001237{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001238 struct buffer *req = si->ob;
1239 struct buffer *res = si->ib;
1240 int reql;
1241 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001242
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001243 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1244 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001245
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001246 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001247 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001248 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001249 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001250 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001251 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001252 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001253 /* Let's close for real now. We just close the request
1254 * side, the conditions below will complete if needed.
1255 */
1256 si->shutw(si);
1257 break;
1258 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001259 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001260 /* ensure we have some output room left in the event we
1261 * would want to return some info right after parsing.
1262 */
1263 if (buffer_almost_full(si->ib))
1264 break;
1265
Willy Tarreau74b08c92010-09-08 17:04:31 +02001266 reql = buffer_get_line(si->ob, trash, sizeof(trash));
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001267 if (reql <= 0) { /* closed or EOL not found */
1268 if (reql == 0)
1269 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001270 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001271 continue;
1272 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001273
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001274 /* seek for a possible semi-colon. If we find one, we
1275 * replace it with an LF and skip only this part.
1276 */
1277 for (len = 0; len < reql; len++)
1278 if (trash[len] == ';') {
1279 trash[len] = '\n';
1280 reql = len + 1;
1281 break;
1282 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001283
Willy Tarreau816fc222009-10-04 07:36:58 +02001284 /* now it is time to check that we have a full line,
1285 * remove the trailing \n and possibly \r, then cut the
1286 * line.
1287 */
1288 len = reql - 1;
1289 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001290 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001291 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001292 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001293
Willy Tarreau816fc222009-10-04 07:36:58 +02001294 if (len && trash[len-1] == '\r')
1295 len--;
1296
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001297 trash[len] = '\0';
1298
Willy Tarreaubc4af052011-02-13 13:25:14 +01001299 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001300 if (len) {
1301 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001302 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001303 continue;
1304 }
1305 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001306 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001307 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001308 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001309 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001310 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001311 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001312 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001313 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001314 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001315 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001316 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001317 /* if prompt is disabled, print help on empty lines,
1318 * so that the user at least knows how to enable
1319 * prompt and find help.
1320 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001321 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001322 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001323 }
1324
1325 /* re-adjust req buffer */
1326 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001327 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001328 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001329 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001330 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001331 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001332 continue;
1333 }
1334
Willy Tarreaubc4af052011-02-13 13:25:14 +01001335 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001336 case STAT_CLI_PRINT:
Willy Tarreau295a8372011-03-10 11:25:07 +01001337 if (buffer_feed(si->ib, si->applet.ctx.cli.msg) < 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001338 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001339 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001340 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001341 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001342 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001343 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001344 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001345 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001346 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001347 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001348 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001349 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001350 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001351 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001352 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001353 if (stats_table_request(si, true))
1354 si->applet.st0 = STAT_CLI_PROMPT;
1355 break;
1356 case STAT_CLI_O_CLR:
1357 if (stats_table_request(si, false))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001358 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001359 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001360 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001361 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001362 break;
1363 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001364
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001365 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001366 if (si->applet.st0 == STAT_CLI_PROMPT) {
1367 if (buffer_feed(si->ib, si->applet.st1 ? "\n> " : "\n") < 0)
1368 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001369 }
1370
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001371 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001372 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001373 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001374
1375 /* Now we close the output if one of the writers did so,
1376 * or if we're not in interactive mode and the request
1377 * buffer is empty. This still allows pipelined requests
1378 * to be sent in non-interactive mode.
1379 */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001380 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->applet.st1 && !req->send_max)) {
1381 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001382 continue;
1383 }
1384
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001385 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001386 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001387 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001388 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001389
Willy Tarreaubc4af052011-02-13 13:25:14 +01001390 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001391 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1392 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1393 /* Other size has closed, let's abort if we have no more processing to do
1394 * and nothing more to consume. This is comparable to a broken pipe, so
1395 * we forward the close to the request side so that it flows upstream to
1396 * the client.
1397 */
1398 si->shutw(si);
1399 }
1400
Willy Tarreaubc4af052011-02-13 13:25:14 +01001401 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001402 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1403 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1404 /* We have no more processing to do, and nothing more to send, and
1405 * the client side has closed. So we'll forward this state downstream
1406 * on the response buffer.
1407 */
1408 si->shutr(si);
1409 res->flags |= BF_READ_NULL;
1410 }
1411
1412 /* update all other flags and resync with the other side */
1413 si->update(si);
1414
1415 /* we don't want to expire timeouts while we're processing requests */
1416 si->ib->rex = TICK_ETERNITY;
1417 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001418
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001419 out:
1420 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
1421 __FUNCTION__, __LINE__,
1422 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
1423
1424 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1425 /* check that we have released everything then unregister */
1426 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001427 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001428}
1429
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001430/* This function dumps statistics onto the stream interface's read buffer.
1431 * The data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001432 * It returns 0 as long as it does not complete, non-zero upon completion.
1433 * Some states are not used but it makes the code more similar to other
1434 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001435 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001436static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001437{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001438 struct proxy *px;
1439 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001440 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001441
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001442 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +02001443
Willy Tarreau295a8372011-03-10 11:25:07 +01001444 switch (si->applet.state) {
1445 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001446 /* the function had not been called yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001447 si->applet.state = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001448 /* fall through */
1449
Willy Tarreau295a8372011-03-10 11:25:07 +01001450 case STAT_ST_HEAD:
1451 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001452 print_csv_header(&msg);
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001453 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001454 return 0;
1455 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001456
Willy Tarreau295a8372011-03-10 11:25:07 +01001457 si->applet.state = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001458 /* fall through */
1459
Willy Tarreau295a8372011-03-10 11:25:07 +01001460 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001461 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001462 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001463 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001464 "Name: " PRODUCT_NAME "\n"
1465 "Version: " HAPROXY_VERSION "\n"
1466 "Release_date: " HAPROXY_DATE "\n"
1467 "Nbproc: %d\n"
1468 "Process_num: %d\n"
1469 "Pid: %d\n"
1470 "Uptime: %dd %dh%02dm%02ds\n"
1471 "Uptime_sec: %d\n"
1472 "Memmax_MB: %d\n"
1473 "Ulimit-n: %d\n"
1474 "Maxsock: %d\n"
1475 "Maxconn: %d\n"
Willy Tarreau91886b62011-09-07 14:38:31 +02001476 "Hard_maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001477 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001478 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001479 "PipesUsed: %d\n"
1480 "PipesFree: %d\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001481 "ConnRate: %d\n"
1482 "ConnRateLimit: %d\n"
1483 "MaxConnRate: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001484 "Tasks: %d\n"
1485 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001486 "node: %s\n"
1487 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001488 "",
1489 global.nbproc,
1490 relative_pid,
1491 pid,
1492 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1493 up,
1494 global.rlimit_memmax,
1495 global.rlimit_nofile,
Willy Tarreau91886b62011-09-07 14:38:31 +02001496 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001497 actconn, pipes_used, pipes_free,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001498 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001499 nb_tasks_cur, run_queue_cur,
1500 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001501 );
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001502 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001503 return 0;
1504 }
1505
Willy Tarreau295a8372011-03-10 11:25:07 +01001506 si->applet.ctx.stats.px = proxy;
1507 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1508 si->applet.ctx.stats.sv = NULL;
1509 si->applet.state = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001510 /* fall through */
1511
Willy Tarreau295a8372011-03-10 11:25:07 +01001512 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001513 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001514 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1515 while (si->applet.ctx.stats.px) {
1516 px = si->applet.ctx.stats.px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001517 /* skip the disabled proxies and non-networked ones */
1518 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001519 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001520 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001521 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001522 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001523
Willy Tarreau295a8372011-03-10 11:25:07 +01001524 si->applet.ctx.stats.px = px->next;
1525 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001526 }
1527 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001528 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001529
Willy Tarreau295a8372011-03-10 11:25:07 +01001530 si->applet.state = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001531 /* fall through */
1532
Willy Tarreau295a8372011-03-10 11:25:07 +01001533 case STAT_ST_END:
1534 si->applet.state = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001535 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001536
Willy Tarreau295a8372011-03-10 11:25:07 +01001537 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001538 return 1;
1539
1540 default:
1541 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001542 si->applet.state = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001543 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001544 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001545}
1546
1547
Cyril Bonté70be45d2010-10-12 00:14:35 +02001548/* We don't want to land on the posted stats page because a refresh will
1549 * repost the data. We don't want this to happen on accident so we redirect
1550 * the browse to the stats page with a GET.
1551 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001552static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001553{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001554 struct session *s = si->applet.private;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001555 struct chunk msg;
1556
1557 chunk_init(&msg, trash, sizeof(trash));
1558
Willy Tarreau295a8372011-03-10 11:25:07 +01001559 switch (si->applet.state) {
1560 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001561 chunk_printf(&msg,
1562 "HTTP/1.0 303 See Other\r\n"
1563 "Cache-Control: no-cache\r\n"
1564 "Content-Type: text/plain\r\n"
1565 "Connection: close\r\n"
1566 "Location: %s;st=%s",
Willy Tarreau295a8372011-03-10 11:25:07 +01001567 uri->uri_prefix, si->applet.ctx.stats.st_code);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001568 chunk_printf(&msg, "\r\n\r\n");
1569
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001570 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001571 return 0;
1572
1573 s->txn.status = 303;
1574
1575 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1576 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1577 if (!(s->flags & SN_FINST_MASK))
1578 s->flags |= SN_FINST_R;
1579
Willy Tarreau295a8372011-03-10 11:25:07 +01001580 si->applet.state = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001581 return 1;
1582 }
1583 return 1;
1584}
1585
1586
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001587/* This I/O handler runs as an applet embedded in a stream interface. It is
1588 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001589 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001590 * automatically unregisters itself once transfer is complete.
1591 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001592static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001593{
Willy Tarreaubc4af052011-02-13 13:25:14 +01001594 struct session *s = si->applet.private;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001595 struct buffer *req = si->ob;
1596 struct buffer *res = si->ib;
1597
1598 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1599 goto out;
1600
1601 /* check that the output is not closed */
1602 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001603 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001604
Willy Tarreaubc4af052011-02-13 13:25:14 +01001605 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001606 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001607 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001608 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001609 si->shutw(si);
1610 }
1611 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001612 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001613 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001614 si->shutw(si);
1615 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001616 }
1617 }
1618
1619 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
1620 si->shutw(si);
1621
Willy Tarreaubc4af052011-02-13 13:25:14 +01001622 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001623 si->shutr(si);
1624 res->flags |= BF_READ_NULL;
1625 }
1626
1627 /* update all other flags and resync with the other side */
1628 si->update(si);
1629
1630 /* we don't want to expire timeouts while we're processing requests */
1631 si->ib->rex = TICK_ETERNITY;
1632 si->ob->wex = TICK_ETERNITY;
1633
1634 out:
1635 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1636 /* check that we have released everything then unregister */
1637 stream_int_unregister_handler(si);
1638 }
1639}
1640
1641
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001642/* This function dumps statistics in HTTP format onto the stream interface's
1643 * read buffer. The data_ctx must have been zeroed first, and the flags
1644 * properly set. It returns 0 if it had to stop writing data and an I/O is
1645 * needed, 1 if the dump is finished and the session must be closed, or -1
1646 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001647 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001648static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001649{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001650 struct session *s = si->applet.private;
1651 struct buffer *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001652 struct proxy *px;
1653 struct chunk msg;
1654 unsigned int up;
1655
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001656 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001657
Willy Tarreau295a8372011-03-10 11:25:07 +01001658 switch (si->applet.state) {
1659 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001660 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001661 "HTTP/1.0 200 OK\r\n"
1662 "Cache-Control: no-cache\r\n"
1663 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001664 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001665 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001666
Willy Tarreau295a8372011-03-10 11:25:07 +01001667 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001668 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001669 uri->refresh);
1670
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001671 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001672
1673 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001674 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001675 return 0;
1676
Willy Tarreau91861262007-10-17 17:06:05 +02001677 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1678 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1679 if (!(s->flags & SN_FINST_MASK))
1680 s->flags |= SN_FINST_R;
1681
1682 if (s->txn.meth == HTTP_METH_HEAD) {
1683 /* that's all we return in case of HEAD request */
Willy Tarreau295a8372011-03-10 11:25:07 +01001684 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001685 return 1;
1686 }
1687
Willy Tarreau295a8372011-03-10 11:25:07 +01001688 si->applet.state = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001689 /* fall through */
1690
Willy Tarreau295a8372011-03-10 11:25:07 +01001691 case STAT_ST_HEAD:
1692 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001693 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001694 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001695 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1696 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001697 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001698 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1699 "<style type=\"text/css\"><!--\n"
1700 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001701 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001702 " font-size: 12px;"
1703 " font-weight: normal;"
1704 " color: black;"
1705 " background: white;"
1706 "}\n"
1707 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001708 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001709 "}\n"
1710 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001711 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001712 " margin-bottom: 0.5em;"
1713 "}\n"
1714 "h2 {"
1715 " font-family: helvetica, arial;"
1716 " font-size: x-large;"
1717 " font-weight: bold;"
1718 " font-style: italic;"
1719 " color: #6020a0;"
1720 " margin-top: 0em;"
1721 " margin-bottom: 0em;"
1722 "}\n"
1723 "h3 {"
1724 " font-family: helvetica, arial;"
1725 " font-size: 16px;"
1726 " font-weight: bold;"
1727 " color: #b00040;"
1728 " background: #e8e8d0;"
1729 " margin-top: 0em;"
1730 " margin-bottom: 0em;"
1731 "}\n"
1732 "li {"
1733 " margin-top: 0.25em;"
1734 " margin-right: 2em;"
1735 "}\n"
1736 ".hr {margin-top: 0.25em;"
1737 " border-color: black;"
1738 " border-bottom-style: solid;"
1739 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001740 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001741 ".total {background: #20D0D0;color: #ffff80;}\n"
1742 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001743 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001744 ".backend {background: #e8e8d0;}\n"
1745 ".active0 {background: #ff9090;}\n"
1746 ".active1 {background: #ffd020;}\n"
1747 ".active2 {background: #ffffa0;}\n"
1748 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001749 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1750 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1751 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001752 ".backup0 {background: #ff9090;}\n"
1753 ".backup1 {background: #ff80ff;}\n"
1754 ".backup2 {background: #c060ff;}\n"
1755 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001756 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1757 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1758 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001759 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001760 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001761 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001762 "a.px:link {color: #ffff40; text-decoration: none;}"
1763 "a.px:visited {color: #ffff40; text-decoration: none;}"
1764 "a.px:hover {color: #ffffff; text-decoration: none;}"
1765 "a.lfsb:link {color: #000000; text-decoration: none;}"
1766 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1767 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1768 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001769 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001770 "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"
1771 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001772 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001773 "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 +02001774 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001775 "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 +02001776 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001777 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1778 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1779 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001780 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001781 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001782 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001783 (uri->flags&ST_SHNODE) ? " on " : "",
1784 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001785 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001786 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001787 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001788 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001789 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001790 return 0;
1791
Willy Tarreau295a8372011-03-10 11:25:07 +01001792 si->applet.state = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02001793 /* fall through */
1794
Willy Tarreau295a8372011-03-10 11:25:07 +01001795 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02001796 up = (now.tv_sec - start_date.tv_sec);
1797
1798 /* WARNING! this has to fit the first packet too.
1799 * We are around 3.5 kB, add adding entries will
1800 * become tricky if we want to support 4kB buffers !
1801 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001802 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001803 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001804 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1805 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001806 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001807 "<hr width=\"100%%\" class=\"hr\">\n"
1808 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001809 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001810 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001811 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001812 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1813 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001814 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001815 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001816 "</td><td align=\"center\" nowrap>\n"
1817 "<table class=\"lgd\"><tr>\n"
1818 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1819 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1820 "</tr><tr>\n"
1821 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1822 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1823 "</tr><tr>\n"
1824 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1825 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1826 "</tr><tr>\n"
1827 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001828 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001829 "</tr><tr>\n"
1830 "<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 +02001831 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001832 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001833 "</td>"
1834 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1835 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1836 "",
1837 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001838 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1839 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1840 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001841 up / 86400, (up % 86400) / 3600,
1842 (up % 3600) / 60, (up % 60),
1843 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1844 global.rlimit_memmax ? " MB" : "",
1845 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001846 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001847 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001848 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +02001849 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001850
Willy Tarreau295a8372011-03-10 11:25:07 +01001851 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001852 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001853 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1854 uri->uri_prefix,
1855 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01001856 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001857 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001858 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001859 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1860 uri->uri_prefix,
1861 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01001862 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001863
Willy Tarreau55bb8452007-10-17 18:44:57 +02001864 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001865 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001866 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001867 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1868 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001869 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001870 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001871 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001872 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001873 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1874 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001875 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001876 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001877 }
Willy Tarreau91861262007-10-17 17:06:05 +02001878
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001879 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001880 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
1881 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001882 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
1883 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001884
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001885 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02001886 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
1887 uri->uri_prefix,
1888 (uri->refresh > 0) ? ";norefresh" : "");
1889
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001890 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001891 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001892 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1893 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
1894 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
1895 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
1896 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
1897 "</ul>"
1898 "</td>"
1899 "</tr></table>\n"
1900 ""
1901 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001902
Willy Tarreau295a8372011-03-10 11:25:07 +01001903 if (si->applet.ctx.stats.st_code) {
1904 if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001905 chunk_printf(&msg,
1906 "<p><div class=active3>"
1907 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1908 "Action processed successfully."
1909 "</div>\n", uri->uri_prefix);
1910 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001911 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_NONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001912 chunk_printf(&msg,
1913 "<p><div class=active2>"
1914 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1915 "Nothing has changed."
1916 "</div>\n", uri->uri_prefix);
1917 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001918 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_EXCD) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001919 chunk_printf(&msg,
1920 "<p><div class=active0>"
1921 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1922 "<b>Action not processed : the buffer couldn't store all the data.<br>"
1923 "You should retry with less servers at a time.</b>"
1924 "</div>\n", uri->uri_prefix);
1925 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001926 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DENY) == 0) {
Cyril Bonté474be412010-10-12 00:14:36 +02001927 chunk_printf(&msg,
1928 "<p><div class=active0>"
1929 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1930 "<b>Action denied.</b>"
1931 "</div>\n", uri->uri_prefix);
1932 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001933 else {
1934 chunk_printf(&msg,
1935 "<p><div class=active6>"
1936 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1937 "Unexpected result."
1938 "</div>\n", uri->uri_prefix);
1939 }
1940 chunk_printf(&msg,"<p>\n");
1941 }
1942
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001943 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001944 return 0;
1945 }
Willy Tarreau91861262007-10-17 17:06:05 +02001946
Willy Tarreau295a8372011-03-10 11:25:07 +01001947 si->applet.ctx.stats.px = proxy;
1948 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1949 si->applet.state = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02001950 /* fall through */
1951
Willy Tarreau295a8372011-03-10 11:25:07 +01001952 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02001953 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001954 while (si->applet.ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001955 if (buffer_almost_full(rep))
1956 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01001957 px = si->applet.ctx.stats.px;
Willy Tarreau91861262007-10-17 17:06:05 +02001958 /* skip the disabled proxies and non-networked ones */
1959 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001960 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001961 return 0;
1962
Willy Tarreau295a8372011-03-10 11:25:07 +01001963 si->applet.ctx.stats.px = px->next;
1964 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02001965 }
1966 /* here, we just have reached the last proxy */
1967
Willy Tarreau295a8372011-03-10 11:25:07 +01001968 si->applet.state = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02001969 /* fall through */
1970
Willy Tarreau295a8372011-03-10 11:25:07 +01001971 case STAT_ST_END:
1972 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001973 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001974 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001975 return 0;
1976 }
Willy Tarreau91861262007-10-17 17:06:05 +02001977
Willy Tarreau295a8372011-03-10 11:25:07 +01001978 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001979 /* fall through */
1980
Willy Tarreau295a8372011-03-10 11:25:07 +01001981 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02001982 return 1;
1983
1984 default:
1985 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001986 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001987 return -1;
1988 }
1989}
1990
1991
1992/*
1993 * Dumps statistics for a proxy.
1994 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1995 * ot non-zero if everything completed.
1996 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001997static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001998{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001999 struct session *s = si->applet.private;
2000 struct buffer *rep = si->ib;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002001 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002002 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002003 struct chunk msg;
2004
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002005 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02002006
Willy Tarreau295a8372011-03-10 11:25:07 +01002007 switch (si->applet.ctx.stats.px_st) {
2008 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002009 /* we are on a new proxy */
2010
2011 if (uri && uri->scope) {
2012 /* we have a limited scope, we have to check the proxy name */
2013 struct stat_scope *scope;
2014 int len;
2015
2016 len = strlen(px->id);
2017 scope = uri->scope;
2018
2019 while (scope) {
2020 /* match exact proxy name */
2021 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2022 break;
2023
2024 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002025 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002026 break;
2027 scope = scope->next;
2028 }
2029
2030 /* proxy name not found : don't dump anything */
2031 if (scope == NULL)
2032 return 1;
2033 }
2034
Willy Tarreau295a8372011-03-10 11:25:07 +01002035 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
2036 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002037 return 1;
2038
Willy Tarreau295a8372011-03-10 11:25:07 +01002039 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002040 /* fall through */
2041
Willy Tarreau295a8372011-03-10 11:25:07 +01002042 case STAT_PX_ST_TH:
2043 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
2044 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002045 /* A form to enable/disable this proxy servers */
2046 chunk_printf(&msg,
2047 "<form action=\"%s\" method=\"post\">",
2048 uri->uri_prefix);
2049 }
2050
Willy Tarreau55bb8452007-10-17 18:44:57 +02002051 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002052 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002053 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002054 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002055 "<th class=\"pxname\" width=\"10%%\"");
2056
2057 if (uri->flags&ST_SHLGNDS) {
2058 /* cap, mode, id */
2059 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
2060 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2061 px->uuid);
2062
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002063 chunk_printf(&msg, "\"");
2064 }
2065
2066 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002067 ">%s<a name=\"%s\"></a>"
2068 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002069 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002070 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002071 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002072 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002073 "<tr class=\"titre\">",
2074 (uri->flags & ST_SHLGNDS)?"<u>":"",
2075 px->id, px->id, px->id,
2076 (uri->flags & ST_SHLGNDS)?"</u>":"",
2077 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2078
Willy Tarreau295a8372011-03-10 11:25:07 +01002079 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002080 /* Column heading for Enable or Disable server */
2081 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
2082 }
2083
2084 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002085 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002086 "<th colspan=3>Queue</th>"
2087 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002088 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002089 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002090 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002091 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002092 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002093 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002094 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002095 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002096 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002097 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002098 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002099 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2100 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002101 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002102
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002103 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002104 return 0;
2105 }
Willy Tarreau91861262007-10-17 17:06:05 +02002106
Willy Tarreau295a8372011-03-10 11:25:07 +01002107 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002108 /* fall through */
2109
Willy Tarreau295a8372011-03-10 11:25:07 +01002110 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002111 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002112 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002113 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
2114 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002115 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002116 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002117 "<tr class=\"frontend\">");
2118
Willy Tarreau295a8372011-03-10 11:25:07 +01002119 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002120 /* Column sub-heading for Enable or Disable server */
2121 chunk_printf(&msg, "<td></td>");
2122 }
2123
2124 chunk_printf(&msg,
2125 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002126 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002127 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2128 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002129 "",
2130 px->id, px->id);
2131
2132 if (px->mode == PR_MODE_HTTP) {
2133 chunk_printf(&msg,
2134 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01002135 "<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 +01002136 "",
2137 read_freq_ctr(&px->fe_req_per_sec),
2138 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002139 px->fe_counters.p.http.rps_max,
2140 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01002141 LIM2A2(px->fe_sps_lim, "-"));
2142 } else {
2143 chunk_printf(&msg,
2144 /* sessions rate : current, max, limit */
2145 "<td>%s</td><td>%s</td><td>%s</td>"
2146 "",
2147 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002148 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01002149 }
2150
2151 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002152 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002153 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002154 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002155 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002156 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002157
2158 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2159 if (px->mode == PR_MODE_HTTP) {
2160 int i;
2161
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002162 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002163
2164 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002165 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002166
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002167 chunk_printf(&msg, " other=%lld\"", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002168 }
2169
2170 chunk_printf(&msg,
2171 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002172 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002173 /* bytes : in, out */
2174 "<td>%s</td><td>%s</td>"
2175 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002176 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002177 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002178 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002179 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002180
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002181 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002182 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002183 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002184 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002185 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002186 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002187 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002188 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002189 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002190 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002191 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002192 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002193 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2194 U2H2(px->fe_counters.failed_req),
Willy Tarreau562515c2011-07-25 08:11:52 +02002195 px->state == PR_STREADY ? "OPEN" :
2196 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002197 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002198 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002199 /* pxid, name, queue cur, queue max, */
2200 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002201 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002202 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002203 /* bytes : in, out */
2204 "%lld,%lld,"
2205 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002206 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002207 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002208 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002209 /* warnings: retries, redispatches */
2210 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002211 /* server status : reflect frontend status */
2212 "%s,"
2213 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002214 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002215 /* pid, iid, sid, throttle, lbtot, tracked, type */
2216 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002217 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002218 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002219 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002220 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002221 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002222 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2223 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2224 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2225 px->fe_counters.failed_req,
Willy Tarreau562515c2011-07-25 08:11:52 +02002226 px->state == PR_STREADY ? "OPEN" :
2227 px->state == PR_STFULL ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002228 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002229 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002230 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002231
2232 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2233 if (px->mode == PR_MODE_HTTP) {
2234 int i;
2235
2236 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002237 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002238
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002239 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002240 } else {
2241 chunk_printf(&msg, ",,,,,,");
2242 }
2243
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002244 /* failed health analyses */
2245 chunk_printf(&msg, ",");
2246
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002247 /* requests : req_rate, req_rate_max, req_tot, */
2248 chunk_printf(&msg, "%u,%u,%lld,",
2249 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002250 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002251
Willy Tarreauae526782010-03-04 20:34:23 +01002252 /* errors: cli_aborts, srv_aborts */
2253 chunk_printf(&msg, ",,");
2254
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002255 /* finish with EOL */
2256 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002257 }
Willy Tarreau91861262007-10-17 17:06:05 +02002258
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002259 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002260 return 0;
2261 }
2262
Willy Tarreau295a8372011-03-10 11:25:07 +01002263 si->applet.ctx.stats.l = px->listen; /* may be NULL */
2264 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002265 /* fall through */
2266
Willy Tarreau295a8372011-03-10 11:25:07 +01002267 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002268 /* stats.l has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002269 for (; si->applet.ctx.stats.l != NULL; si->applet.ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002270 if (buffer_almost_full(rep))
2271 return 0;
2272
Willy Tarreau295a8372011-03-10 11:25:07 +01002273 l = si->applet.ctx.stats.l;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002274 if (!l->counters)
2275 continue;
2276
Willy Tarreau295a8372011-03-10 11:25:07 +01002277 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2278 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002279 break;
2280
Willy Tarreau295a8372011-03-10 11:25:07 +01002281 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002282 continue;
2283 }
2284
Willy Tarreau295a8372011-03-10 11:25:07 +01002285 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002286 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002287 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002288 /* Column sub-heading for Enable or Disable server */
2289 chunk_printf(&msg, "<td></td>");
2290 }
2291 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002292
2293 if (uri->flags&ST_SHLGNDS) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02002294 char str[INET6_ADDRSTRLEN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002295 int port;
2296
Willy Tarreau631f01c2011-09-05 00:36:48 +02002297 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002298
Willy Tarreau631f01c2011-09-05 00:36:48 +02002299 port = get_host_port(&l->addr);
2300 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2301 case AF_INET:
2302 chunk_printf(&msg, "IPv4: %s:%d, ", str, port);
2303 break;
2304 case AF_INET6:
2305 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, port);
2306 break;
2307 case AF_UNIX:
2308 chunk_printf(&msg, "unix, ");
2309 break;
2310 case -1:
2311 chunk_printf(&msg, "(%s), ", strerror(errno));
2312 break;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002313 }
2314
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002315 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002316 chunk_printf(&msg, "id: %d\"", l->luid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002317 }
2318
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002319 chunk_printf(&msg,
2320 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002321 ">%s<a name=\"%s/+%s\"></a>"
2322 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002323 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002324 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002325 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002326 "<td>%s</td><td>%s</td><td>%s</td>"
2327 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002328 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002329 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002330 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002331 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002332 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002333 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002334 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2335 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2336
2337 chunk_printf(&msg,
2338 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002339 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002340 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002341 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002342 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002343 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002344 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002345 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002346 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002347 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002348 "",
2349 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2350 U2H2(l->counters->failed_req),
Willy Tarreaua17c2d92011-07-25 08:16:20 +02002351 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002352 } else {
2353 chunk_printf(&msg,
2354 /* pxid, name, queue cur, queue max, */
2355 "%s,%s,,,"
2356 /* sessions: current, max, limit, total */
2357 "%d,%d,%d,%lld,"
2358 /* bytes: in, out */
2359 "%lld,%lld,"
2360 /* denied: req, resp */
2361 "%lld,%lld,"
2362 /* errors: request, connect, response */
2363 "%lld,,,"
2364 /* warnings: retries, redispatches */
2365 ",,"
2366 /* server status: reflect listener status */
2367 "%s,"
2368 /* rest of server: nothing */
2369 ",,,,,,,,"
2370 /* pid, iid, sid, throttle, lbtot, tracked, type */
2371 "%d,%d,%d,,,,%d,"
2372 /* rate, rate_lim, rate_max */
2373 ",,,"
2374 /* check_status, check_code, check_duration */
2375 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002376 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2377 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002378 /* failed health analyses */
2379 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002380 /* requests : req_rate, req_rate_max, req_tot, */
2381 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002382 /* errors: cli_aborts, srv_aborts */
2383 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002384 "\n",
2385 px->id, l->name,
2386 l->nbconn, l->counters->conn_max,
2387 l->maxconn, l->counters->cum_conn,
2388 l->counters->bytes_in, l->counters->bytes_out,
2389 l->counters->denied_req, l->counters->denied_resp,
2390 l->counters->failed_req,
2391 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2392 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2393 }
2394
2395 if (buffer_feed_chunk(rep, &msg) >= 0)
2396 return 0;
2397 }
2398
Willy Tarreau295a8372011-03-10 11:25:07 +01002399 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2400 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002401 /* fall through */
2402
Willy Tarreau295a8372011-03-10 11:25:07 +01002403 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002404 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002405 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002406 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 +02002407
Willy Tarreau4e33d862009-10-11 23:35:10 +02002408 if (buffer_almost_full(rep))
2409 return 0;
2410
Willy Tarreau295a8372011-03-10 11:25:07 +01002411 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002412
Willy Tarreau295a8372011-03-10 11:25:07 +01002413 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2414 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002415 break;
2416
Willy Tarreau295a8372011-03-10 11:25:07 +01002417 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002418 continue;
2419 }
2420
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002421 if (sv->tracked)
2422 svs = sv->tracked;
2423 else
2424 svs = sv;
2425
Willy Tarreau91861262007-10-17 17:06:05 +02002426 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002427 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002428 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002429 else if (svs->state & SRV_RUNNING) {
2430 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002431 sv_state = 3; /* UP */
2432 else
2433 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002434
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002435 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002436 sv_state += 2;
2437 }
Willy Tarreau91861262007-10-17 17:06:05 +02002438 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002439 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002440 sv_state = 1; /* going up */
2441 else
2442 sv_state = 0; /* DOWN */
2443
Willy Tarreau295a8372011-03-10 11:25:07 +01002444 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002445 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002446 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002447 continue;
2448 }
2449
Willy Tarreau295a8372011-03-10 11:25:07 +01002450 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002451 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2452 "UP %d/%d &darr;", "UP",
2453 "NOLB %d/%d &darr;", "NOLB",
2454 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002455 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002456 chunk_printf(&msg,
2457 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002458 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002459 );
2460 }
2461 else {
2462 chunk_printf(&msg,
2463 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002464 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002465 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2466 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002467
Willy Tarreau295a8372011-03-10 11:25:07 +01002468 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002469 chunk_printf(&msg,
2470 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2471 sv->id);
2472 }
2473
2474 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002475
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002476 if (uri->flags&ST_SHLGNDS) {
2477 char str[INET6_ADDRSTRLEN];
2478
Willy Tarreau631f01c2011-09-05 00:36:48 +02002479 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002480
Willy Tarreau631f01c2011-09-05 00:36:48 +02002481 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01002482 case AF_INET:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002483 chunk_printf(&msg, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
David du Colombier6f5ccb12011-03-10 22:26:24 +01002484 break;
2485 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002486 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2487 break;
2488 case AF_UNIX:
2489 chunk_printf(&msg, "unix, ");
2490 break;
2491 case -1:
2492 chunk_printf(&msg, "(%s), ", strerror(errno));
2493 break;
2494 default: /* address family not supported */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002495 break;
2496 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002497
2498 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002499 chunk_printf(&msg, "id: %d", sv->puid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002500
2501 /* cookie */
2502 if (sv->cookie) {
2503 struct chunk src;
2504
2505 chunk_printf(&msg, ", cookie: '");
2506
2507 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2508 chunk_htmlencode(&msg, &src);
2509
2510 chunk_printf(&msg, "'");
2511 }
2512
2513 chunk_printf(&msg, "\"");
2514 }
2515
2516 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002517 ">%s<a name=\"%s/%s\"></a>"
2518 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002519 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002520 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002521 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002522 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002523 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002524 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002525 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002526 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002527 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002528 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002529 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002530 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2531 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002532 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2533
2534 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2535 if (px->mode == PR_MODE_HTTP) {
2536 int i;
2537
2538 chunk_printf(&msg, " title=\"rsp codes:");
2539
2540 for (i = 1; i < 6; i++)
2541 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2542
2543 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2544 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002545
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002546 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002547 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002548 ">%s%s%s</td><td>%s</td>",
2549 (px->mode == PR_MODE_HTTP)?"<u>":"",
2550 U2H0(sv->counters.cum_sess),
2551 (px->mode == PR_MODE_HTTP)?"</u>":"",
2552 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002553
2554 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002555 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002556 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002557 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002558 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002559 /* errors : request, connect */
2560 "<td></td><td>%s</td>"
2561 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002562 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002563 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002564 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002565 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002566 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2567 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002568 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002569 sv->counters.cli_aborts,
2570 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002571 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002572 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002573
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002574 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002575 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002576
Cyril Bontécd19e512010-01-31 22:34:03 +01002577 if (sv->state & SRV_MAINTAIN) {
2578 chunk_printf(&msg, "%s ",
2579 human_time(now.tv_sec - sv->last_change, 1));
2580 chunk_printf(&msg, "MAINT");
2581 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002582 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2583 chunk_printf(&msg, "%s ",
2584 human_time(now.tv_sec - svs->last_change, 1));
2585 chunk_printf(&msg, "MAINT(via)");
2586 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002587 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002588 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002589 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002590
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002591 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002592 srv_hlt_st[sv_state],
2593 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2594 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002595 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002596
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002597 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002598 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002599 get_check_status_description(sv->check_status));
2600
2601 if (*sv->check_desc) {
2602 struct chunk src;
2603
2604 chunk_printf(&msg, ": ");
2605
2606 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2607 chunk_htmlencode(&msg, &src);
2608 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002609
Willy Tarreaue0454092010-02-26 12:29:07 +01002610 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002611 tv_iszero(&sv->check_start)?"":"* ",
2612 get_check_status_info(sv->check_status));
2613
2614 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002615 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002616
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002617 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002618 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002619 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002620 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002621
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002622 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002623 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002624 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002625 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002626 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002627 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002628 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002629 (sv->state & SRV_BACKUP) ? "-" : "Y",
2630 (sv->state & SRV_BACKUP) ? "Y" : "-");
2631
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002632 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002633 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002634 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002635 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2636
2637 if (svs->observe)
2638 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2639
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002640 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002641 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002642 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002643 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002644 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002645 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002646 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002647 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2648 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002649 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002650 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002651 "<td colspan=3></td>");
2652
2653 /* throttle */
2654 if ((sv->state & SRV_WARMINGUP) &&
2655 now.tv_sec < sv->last_change + sv->slowstart &&
2656 now.tv_sec >= sv->last_change) {
2657 unsigned int ratio;
2658 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002659 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002660 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002661 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002662 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002663 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002664 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002665 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002666 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2667 "UP %d/%d,", "UP,",
2668 "NOLB %d/%d,", "NOLB,",
2669 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002670 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002671 /* pxid, name */
2672 "%s,%s,"
2673 /* queue : current, max */
2674 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002675 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002676 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002677 /* bytes : in, out */
2678 "%lld,%lld,"
2679 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002680 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002681 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002682 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002683 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002684 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002685 "",
2686 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002687 sv->nbpend, sv->counters.nbpend_max,
2688 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002689 sv->counters.bytes_in, sv->counters.bytes_out,
2690 sv->counters.failed_secu,
2691 sv->counters.failed_conns, sv->counters.failed_resp,
2692 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002693
Willy Tarreau55bb8452007-10-17 18:44:57 +02002694 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002695 if (sv->state & SRV_MAINTAIN) {
2696 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002697 }
2698 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2699 chunk_printf(&msg, "MAINT(via),");
2700 }
2701 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002702 chunk_printf(&msg,
2703 srv_hlt_st[sv_state],
2704 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2705 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2706 }
Willy Tarreau91861262007-10-17 17:06:05 +02002707
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002708 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002709 /* weight, active, backup */
2710 "%d,%d,%d,"
2711 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002712 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002713 (sv->state & SRV_BACKUP) ? 0 : 1,
2714 (sv->state & SRV_BACKUP) ? 1 : 0);
2715
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002716 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002717 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002718 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002719 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002720 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002721 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002722 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002723 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002724 ",,,,");
2725
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002726 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002727 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002728 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002729 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002730 LIM2A0(sv->maxqueue, ""),
2731 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002732
2733 /* throttle */
2734 if ((sv->state & SRV_WARMINGUP) &&
2735 now.tv_sec < sv->last_change + sv->slowstart &&
2736 now.tv_sec >= sv->last_change) {
2737 unsigned int ratio;
2738 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002739 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002740 }
2741
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002742 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002743 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002744
2745 /* tracked */
2746 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002747 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002748 sv->tracked->proxy->id, sv->tracked->id);
2749 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002750 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002751
Willy Tarreau7f062c42009-03-05 18:43:00 +01002752 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002753 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002754
2755 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002756 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002757 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002758 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002759
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002760 if (sv->state & SRV_CHECKED) {
2761 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002762 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002763
2764 /* check_code */
2765 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002766 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002767 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002768 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002769
2770 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002771 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002772 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002773 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002774 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002775
2776 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002777 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002778 }
2779
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002780 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2781 if (px->mode == PR_MODE_HTTP) {
2782 int i;
2783
2784 for (i=1; i<6; i++)
2785 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2786
2787 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2788 } else {
2789 chunk_printf(&msg, ",,,,,,");
2790 }
2791
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002792 /* failed health analyses */
2793 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2794
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002795 /* requests : req_rate, req_rate_max, req_tot, */
2796 chunk_printf(&msg, ",,,");
2797
Willy Tarreauae526782010-03-04 20:34:23 +01002798 /* errors: cli_aborts, srv_aborts */
2799 chunk_printf(&msg, "%lld,%lld,",
2800 sv->counters.cli_aborts, sv->counters.srv_aborts);
2801
Willy Tarreau7f062c42009-03-05 18:43:00 +01002802 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002803 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002804 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002805 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002806 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002807 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002808
Willy Tarreau295a8372011-03-10 11:25:07 +01002809 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02002810 /* fall through */
2811
Willy Tarreau295a8372011-03-10 11:25:07 +01002812 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02002813 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002814 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002815 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2816 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002817 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01002818 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002819 /* Column sub-heading for Enable or Disable server */
2820 chunk_printf(&msg, "<td></td>");
2821 }
2822 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002823
2824 if (uri->flags&ST_SHLGNDS) {
2825 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002826 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002827 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002828
2829 /* cookie */
2830 if (px->cookie_name) {
2831 struct chunk src;
2832
2833 chunk_printf(&msg, ", cookie: '");
2834
2835 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2836 chunk_htmlencode(&msg, &src);
2837
2838 chunk_printf(&msg, "'");
2839 }
2840
2841 chunk_printf(&msg, "\"");
2842
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002843 }
2844
2845 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002846 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01002847 ">%s<a name=\"%s/Backend\"></a>"
2848 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002849 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002850 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002851 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002852 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002853 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002854 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002855 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002856 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002857 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
2858 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02002859
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002860 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002861 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002862 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002863 "<td"
2864 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002865 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002866
2867 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2868 if (px->mode == PR_MODE_HTTP) {
2869 int i;
2870
2871 chunk_printf(&msg, " title=\"rsp codes:");
2872
2873 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002874 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002875
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002876 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002877 }
2878
2879 chunk_printf(&msg,
2880 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002881 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002882 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002883 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002884 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002885 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002886 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01002887 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002888 U2H7(px->be_counters.cum_lbconn),
2889 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002890
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002891 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002892 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002893 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002894 /* errors : request, connect */
2895 "<td></td><td>%s</td>"
2896 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002897 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002898 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002899 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002900 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02002901 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002902 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02002903 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002904 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2905 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002906 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002907 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
2908 U2H2(px->be_counters.failed_conns),
2909 px->be_counters.cli_aborts,
2910 px->be_counters.srv_aborts,
2911 U2H5(px->be_counters.failed_resp),
2912 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002913 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01002914 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2915 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01002916 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002917 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002918
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002919 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002920 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002921 "<td class=ac>&nbsp;</td><td>%d</td>"
2922 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002923 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002924 "</tr>",
2925 px->down_trans,
2926 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002927 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002928 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002929 /* pxid, name */
2930 "%s,BACKEND,"
2931 /* queue : current, max */
2932 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002933 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002934 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002935 /* bytes : in, out */
2936 "%lld,%lld,"
2937 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002938 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002939 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002940 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002941 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002942 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002943 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02002944 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002945 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02002946 * active and backups. */
2947 "%s,"
2948 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002949 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002950 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002951 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002952 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002953 /* rate, rate_lim, rate_max, */
2954 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002955 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002956 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002957 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002958 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2959 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2960 px->be_counters.bytes_in, px->be_counters.bytes_out,
2961 px->be_counters.denied_req, px->be_counters.denied_resp,
2962 px->be_counters.failed_conns, px->be_counters.failed_resp,
2963 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01002964 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01002965 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002966 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002967 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01002968 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002969 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002970 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002971 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002972 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002973
2974 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2975 if (px->mode == PR_MODE_HTTP) {
2976 int i;
2977
2978 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002979 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002980
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002981 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002982 } else {
2983 chunk_printf(&msg, ",,,,,,");
2984 }
2985
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002986 /* failed health analyses */
2987 chunk_printf(&msg, ",");
2988
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002989 /* requests : req_rate, req_rate_max, req_tot, */
2990 chunk_printf(&msg, ",,,");
2991
Willy Tarreauae526782010-03-04 20:34:23 +01002992 /* errors: cli_aborts, srv_aborts */
2993 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002994 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01002995
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002996 /* finish with EOL */
2997 chunk_printf(&msg, "\n");
2998
Willy Tarreau55bb8452007-10-17 18:44:57 +02002999 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003000 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02003001 return 0;
3002 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003003
Willy Tarreau295a8372011-03-10 11:25:07 +01003004 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02003005 /* fall through */
3006
Willy Tarreau295a8372011-03-10 11:25:07 +01003007 case STAT_PX_ST_END:
3008 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003009 chunk_printf(&msg, "</table>");
3010
Willy Tarreau295a8372011-03-10 11:25:07 +01003011 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003012 /* close the form used to enable/disable this proxy servers */
3013 chunk_printf(&msg,
3014 "Choose the action to perform on the checked servers : "
3015 "<select name=action>"
3016 "<option value=\"\"></option>"
3017 "<option value=\"disable\">Disable</option>"
3018 "<option value=\"enable\">Enable</option>"
3019 "</select>"
3020 "<input type=\"hidden\" name=\"b\" value=\"%s\">"
3021 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3022 "</form>",
3023 px->id);
3024 }
3025
3026 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02003027
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003028 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003029 return 0;
3030 }
Willy Tarreau91861262007-10-17 17:06:05 +02003031
Willy Tarreau295a8372011-03-10 11:25:07 +01003032 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02003033 /* fall through */
3034
Willy Tarreau295a8372011-03-10 11:25:07 +01003035 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02003036 return 1;
3037
3038 default:
3039 /* unknown state, we should put an abort() here ! */
3040 return 1;
3041 }
3042}
3043
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003044/* This function dumps a complete session state onto the stream intreface's
3045 * read buffer. The data_ctx must have been zeroed first, and the flags
3046 * properly set. The session has to be set in data_ctx.sess.target. It returns
3047 * 0 if the output buffer is full and it needs to be called again, otherwise
3048 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003049 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003050static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003051{
3052 struct tm tm;
3053 struct chunk msg;
3054 struct session *sess;
3055 extern const char *monthname[12];
3056 char pn[INET6_ADDRSTRLEN];
3057
3058 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau295a8372011-03-10 11:25:07 +01003059 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003060
Willy Tarreau295a8372011-03-10 11:25:07 +01003061 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003062 /* session changed, no need to go any further */
3063 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003064 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003065 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003066 si->applet.ctx.sess.target = NULL;
3067 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003068 return 1;
3069 }
3070
Willy Tarreau295a8372011-03-10 11:25:07 +01003071 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003072 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003073 si->applet.ctx.sess.uid = sess->uniq_id;
3074 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003075 /* fall through */
3076
3077 case 1:
3078 chunk_printf(&msg,
3079 "%p: id=%u, proto=%s",
3080 sess,
3081 sess->uniq_id,
3082 sess->listener->proto->name);
3083
Willy Tarreau631f01c2011-09-05 00:36:48 +02003084 switch (addr_to_str(&sess->si[0].addr.c.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003085 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003086 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003087 chunk_printf(&msg, " source=%s:%d\n",
3088 pn, get_host_port(&sess->si[0].addr.c.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003089 break;
3090 case AF_UNIX:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003091 chunk_printf(&msg, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003092 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003093 default:
3094 /* no more information to print right now */
3095 chunk_printf(&msg, "\n");
3096 break;
3097 }
3098
3099 chunk_printf(&msg,
3100 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003101 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003102
3103 chunk_printf(&msg,
3104 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)\n",
3105 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3106 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3107 sess->listener ? sess->listener->luid : 0);
3108
Willy Tarreau50943332011-09-02 17:33:05 +02003109 if (sess->be->cap & PR_CAP_BE)
3110 chunk_printf(&msg,
3111 " backend=%s (id=%u mode=%s) server=%s (id=%u)\n",
3112 sess->be->id,
3113 sess->be->uuid, sess->be->mode ? "http" : "tcp",
3114 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
3115 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
3116 else
3117 chunk_printf(&msg, " backend=<NONE> (id=-1 mode=-) server=<NONE> (id=-1)\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003118
3119 chunk_printf(&msg,
3120 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
3121 sess->task,
3122 sess->task->state,
3123 sess->task->nice, sess->task->calls,
3124 sess->task->expire ?
3125 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3126 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3127 TICKS_TO_MS(1000)) : "<NEVER>",
3128 task_in_rq(sess->task) ? ", running" : "");
3129
3130 get_localtime(sess->logs.accept_date.tv_sec, &tm);
3131 chunk_printf(&msg,
3132 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
3133 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3134 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3135 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3136
3137 chunk_printf(&msg,
3138 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3139 &sess->si[0],
3140 sess->si[0].state,
3141 sess->si[0].flags,
3142 sess->si[0].fd,
3143 sess->si[0].exp ?
3144 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3145 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3146 TICKS_TO_MS(1000)) : "<NEVER>",
3147 sess->si[0].err_type);
3148
3149 chunk_printf(&msg,
3150 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3151 &sess->si[1],
3152 sess->si[1].state,
3153 sess->si[1].flags,
3154 sess->si[1].fd,
3155 sess->si[1].exp ?
3156 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3157 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3158 TICKS_TO_MS(1000)) : "<NEVER>",
3159 sess->si[1].err_type);
3160
3161 chunk_printf(&msg,
3162 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3163 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3164 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3165
3166
3167 chunk_printf(&msg,
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02003168 " req=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003169 " an_exp=%s",
3170 sess->req,
3171 sess->req->flags, sess->req->analysers,
3172 sess->req->l, sess->req->send_max,
3173 sess->req->pipe ? sess->req->pipe->data : 0,
3174 sess->req->to_forward,
3175 sess->req->analyse_exp ?
3176 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3177 TICKS_TO_MS(1000)) : "<NEVER>");
3178
3179 chunk_printf(&msg,
3180 " rex=%s",
3181 sess->req->rex ?
3182 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3183 TICKS_TO_MS(1000)) : "<NEVER>");
3184
3185 chunk_printf(&msg,
3186 " wex=%s\n"
3187 " data=%p r=%d w=%d lr=%d total=%lld\n",
3188 sess->req->wex ?
3189 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3190 TICKS_TO_MS(1000)) : "<NEVER>",
3191 sess->req->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01003192 (int)(sess->req->r - sess->req->data),
3193 (int)(sess->req->w - sess->req->data),
3194 (int)(sess->req->lr - sess->req->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003195 sess->req->total);
3196
3197 chunk_printf(&msg,
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02003198 " res=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003199 " an_exp=%s",
3200 sess->rep,
3201 sess->rep->flags, sess->rep->analysers,
3202 sess->rep->l, sess->rep->send_max,
3203 sess->rep->pipe ? sess->rep->pipe->data : 0,
3204 sess->rep->to_forward,
3205 sess->rep->analyse_exp ?
3206 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3207 TICKS_TO_MS(1000)) : "<NEVER>");
3208
3209 chunk_printf(&msg,
3210 " rex=%s",
3211 sess->rep->rex ?
3212 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3213 TICKS_TO_MS(1000)) : "<NEVER>");
3214
3215 chunk_printf(&msg,
3216 " wex=%s\n"
3217 " data=%p r=%d w=%d lr=%d total=%lld\n",
3218 sess->rep->wex ?
3219 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3220 TICKS_TO_MS(1000)) : "<NEVER>",
3221 sess->rep->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01003222 (int)(sess->rep->r - sess->rep->data),
3223 (int)(sess->rep->w - sess->rep->data),
3224 (int)(sess->rep->lr - sess->rep->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003225 sess->rep->total);
3226
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003227 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003228 return 0;
3229
3230 /* use other states to dump the contents */
3231 }
3232 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003233 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003234 return 1;
3235}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003236
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003237/* This function dumps all sessions' states onto the stream intreface's
3238 * read buffer. The data_ctx must have been zeroed first, and the flags
3239 * properly set. It returns 0 if the output buffer is full and it needs
3240 * to be called again, otherwise non-zero. It is designed to be called
3241 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003242 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003243static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003244{
3245 struct chunk msg;
3246
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003247 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003248 /* If we're forced to shut down, we might have to remove our
3249 * reference to the last session being dumped.
3250 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003251 if (si->applet.state == STAT_ST_LIST) {
3252 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3253 LIST_DEL(&si->applet.ctx.sess.bref.users);
3254 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003255 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003256 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003257 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003258 }
3259
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003260 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003261
Willy Tarreau295a8372011-03-10 11:25:07 +01003262 switch (si->applet.state) {
3263 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003264 /* the function had not been called yet, let's prepare the
3265 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003266 * pointer to the first in the global list. When a target
3267 * session is being destroyed, it is responsible for updating
3268 * this pointer. We know we have reached the end when this
3269 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003270 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003271 LIST_INIT(&si->applet.ctx.sess.bref.users);
3272 si->applet.ctx.sess.bref.ref = sessions.n;
3273 si->applet.state = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003274 /* fall through */
3275
Willy Tarreau295a8372011-03-10 11:25:07 +01003276 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003277 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003278 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3279 LIST_DEL(&si->applet.ctx.sess.bref.users);
3280 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003281 }
3282
3283 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003284 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003285 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003286 struct session *curr_sess;
3287
Willy Tarreau295a8372011-03-10 11:25:07 +01003288 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003289
Willy Tarreau295a8372011-03-10 11:25:07 +01003290 if (si->applet.ctx.sess.target) {
3291 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003292 goto next_sess;
3293
Willy Tarreau295a8372011-03-10 11:25:07 +01003294 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003295 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003296 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003297 return 0;
3298
3299 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003300 LIST_DEL(&si->applet.ctx.sess.bref.users);
3301 LIST_INIT(&si->applet.ctx.sess.bref.users);
3302 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003303 break;
3304 }
3305
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003306 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003307 "%p: proto=%s",
3308 curr_sess,
3309 curr_sess->listener->proto->name);
3310
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003311
Willy Tarreau631f01c2011-09-05 00:36:48 +02003312 switch (addr_to_str(&curr_sess->si[0].addr.c.from, pn, sizeof(pn))) {
3313 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003314 case AF_INET6:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003315 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003316 " src=%s:%d fe=%s be=%s srv=%s",
3317 pn,
Willy Tarreau631f01c2011-09-05 00:36:48 +02003318 get_host_port(&curr_sess->si[0].addr.c.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003319 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003320 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003321 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003322 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003323 break;
3324 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02003325 chunk_printf(&msg,
3326 " src=unix:%d fe=%s be=%s srv=%s",
3327 curr_sess->listener->luid,
3328 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003329 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003330 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003331 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003332 break;
3333 }
3334
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003335 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003336 " ts=%02x age=%s calls=%d",
3337 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003338 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3339 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003340
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003341 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003342 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
3343 curr_sess->req->flags,
3344 curr_sess->req->l,
3345 curr_sess->req->analysers,
3346 curr_sess->req->rex ?
3347 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3348 TICKS_TO_MS(1000)) : "");
3349
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003350 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003351 ",wx=%s",
3352 curr_sess->req->wex ?
3353 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3354 TICKS_TO_MS(1000)) : "");
3355
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003356 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003357 ",ax=%s]",
3358 curr_sess->req->analyse_exp ?
3359 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3360 TICKS_TO_MS(1000)) : "");
3361
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003362 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003363 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
3364 curr_sess->rep->flags,
3365 curr_sess->rep->l,
3366 curr_sess->rep->analysers,
3367 curr_sess->rep->rex ?
3368 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3369 TICKS_TO_MS(1000)) : "");
3370
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003371 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003372 ",wx=%s",
3373 curr_sess->rep->wex ?
3374 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3375 TICKS_TO_MS(1000)) : "");
3376
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003377 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003378 ",ax=%s]",
3379 curr_sess->rep->analyse_exp ?
3380 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3381 TICKS_TO_MS(1000)) : "");
3382
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003383 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003384 " s0=[%d,%1xh,fd=%d,ex=%s]",
3385 curr_sess->si[0].state,
3386 curr_sess->si[0].flags,
3387 curr_sess->si[0].fd,
3388 curr_sess->si[0].exp ?
3389 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3390 TICKS_TO_MS(1000)) : "");
3391
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003392 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003393 " s1=[%d,%1xh,fd=%d,ex=%s]",
3394 curr_sess->si[1].state,
3395 curr_sess->si[1].flags,
3396 curr_sess->si[1].fd,
3397 curr_sess->si[1].exp ?
3398 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3399 TICKS_TO_MS(1000)) : "");
3400
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003401 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003402 " exp=%s",
3403 curr_sess->task->expire ?
3404 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3405 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003406 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003407 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003408
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003409 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003410
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003411 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003412 /* let's try again later from this session. We add ourselves into
3413 * this session's users so that it can remove us upon termination.
3414 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003415 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003416 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003417 }
3418
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003419 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003420 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003421 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003422
Willy Tarreau295a8372011-03-10 11:25:07 +01003423 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003424 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003425 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003426 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3427 else
3428 chunk_printf(&msg, "Session not found.\n");
3429
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003430 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003431 return 0;
3432
Willy Tarreau295a8372011-03-10 11:25:07 +01003433 si->applet.ctx.sess.target = NULL;
3434 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003435 return 1;
3436 }
3437
Willy Tarreau295a8372011-03-10 11:25:07 +01003438 si->applet.state = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003439 /* fall through */
3440
3441 default:
Willy Tarreau295a8372011-03-10 11:25:07 +01003442 si->applet.state = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003443 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003444 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003445}
3446
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003447/* This function dumps all tables' states onto the stream intreface's
3448 * read buffer. The data_ctx must have been zeroed first, and the flags
3449 * properly set. It returns 0 if the output buffer is full and it needs
3450 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003451 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003452static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003453{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003454 struct session *s = si->applet.private;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003455 struct chunk msg;
3456 struct ebmb_node *eb;
3457 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003458 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003459
3460 /*
Willy Tarreau295a8372011-03-10 11:25:07 +01003461 * We have 3 possible states in si->applet.state :
3462 * - STAT_ST_INIT : the first call
3463 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003464 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003465 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003466 * and the entry pointer points to the next entry to be dumped,
3467 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003468 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003469 * data though.
3470 */
3471
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003472 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003473 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau295a8372011-03-10 11:25:07 +01003474 if (si->applet.state == STAT_ST_LIST) {
3475 si->applet.ctx.table.entry->ref_cnt--;
3476 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003477 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003478 return 1;
3479 }
3480
3481 chunk_init(&msg, trash, sizeof(trash));
3482
Willy Tarreau295a8372011-03-10 11:25:07 +01003483 while (si->applet.state != STAT_ST_FIN) {
3484 switch (si->applet.state) {
3485 case STAT_ST_INIT:
3486 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3487 if (!si->applet.ctx.table.proxy)
3488 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003489
Willy Tarreau295a8372011-03-10 11:25:07 +01003490 si->applet.ctx.table.entry = NULL;
3491 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003492 break;
3493
Willy Tarreau295a8372011-03-10 11:25:07 +01003494 case STAT_ST_INFO:
3495 if (!si->applet.ctx.table.proxy ||
3496 (si->applet.ctx.table.target &&
3497 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
3498 si->applet.state = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003499 break;
3500 }
3501
Willy Tarreau295a8372011-03-10 11:25:07 +01003502 if (si->applet.ctx.table.proxy->table.size) {
Simon Hormanc88b8872011-06-15 15:18:49 +09003503 if (show && !stats_dump_table_head_to_buffer(&msg, si, si->applet.ctx.table.proxy,
3504 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003505 return 0;
3506
Willy Tarreau295a8372011-03-10 11:25:07 +01003507 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003508 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
3509 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003510 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003511 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003512 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3513 si->applet.ctx.table.entry->ref_cnt++;
3514 si->applet.state = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003515 break;
3516 }
3517 }
3518 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003519 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003520 break;
3521
Willy Tarreau295a8372011-03-10 11:25:07 +01003522 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003523 skip_entry = false;
3524
Willy Tarreau295a8372011-03-10 11:25:07 +01003525 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003526 /* we're filtering on some data contents */
3527 void *ptr;
3528 long long data;
3529
Willy Tarreau295a8372011-03-10 11:25:07 +01003530 dt = si->applet.ctx.table.data_type;
3531 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3532 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003533 dt);
3534
3535 data = 0;
3536 switch (stktable_data_types[dt].std_type) {
3537 case STD_T_SINT:
3538 data = stktable_data_cast(ptr, std_t_sint);
3539 break;
3540 case STD_T_UINT:
3541 data = stktable_data_cast(ptr, std_t_uint);
3542 break;
3543 case STD_T_ULL:
3544 data = stktable_data_cast(ptr, std_t_ull);
3545 break;
3546 case STD_T_FRQP:
3547 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003548 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003549 break;
3550 }
3551
3552 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003553 if ((data < si->applet.ctx.table.value &&
3554 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3555 si->applet.ctx.table.data_op == STD_OP_GT ||
3556 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3557 (data == si->applet.ctx.table.value &&
3558 (si->applet.ctx.table.data_op == STD_OP_NE ||
3559 si->applet.ctx.table.data_op == STD_OP_GT ||
3560 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3561 (data > si->applet.ctx.table.value &&
3562 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3563 si->applet.ctx.table.data_op == STD_OP_LT ||
3564 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003565 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003566 }
3567
Simon Hormanc88b8872011-06-15 15:18:49 +09003568 if (show && !skip_entry &&
3569 !stats_dump_table_entry_to_buffer(&msg, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003570 si->applet.ctx.table.entry))
3571 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003572
Willy Tarreau295a8372011-03-10 11:25:07 +01003573 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003574
Willy Tarreau295a8372011-03-10 11:25:07 +01003575 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003576 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003577 struct stksess *old = si->applet.ctx.table.entry;
3578 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3579 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3580 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003581 break;
3582 }
3583
Simon Hormanc88b8872011-06-15 15:18:49 +09003584
3585 if (show)
3586 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3587 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3588 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3589
Willy Tarreau295a8372011-03-10 11:25:07 +01003590 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
3591 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003592 break;
3593
Willy Tarreau295a8372011-03-10 11:25:07 +01003594 case STAT_ST_END:
3595 si->applet.state = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003596 break;
3597 }
3598 }
3599 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003600}
3601
Willy Tarreaud426a182010-03-05 14:58:26 +01003602/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003603 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3604 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3605 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3606 * lines are respected within the limit of 70 output chars. Lines that are
3607 * continuation of a previous truncated line begin with "+" instead of " "
3608 * after the offset. The new pointer is returned.
3609 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003610static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3611 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003612{
3613 int end;
3614 unsigned char c;
3615
3616 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003617 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003618 return ptr;
3619
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003620 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003621
Willy Tarreaud426a182010-03-05 14:58:26 +01003622 while (ptr < len && ptr < bsize) {
3623 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003624 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003625 if (out->len > end - 2)
3626 break;
3627 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003628 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003629 if (out->len > end - 3)
3630 break;
3631 out->str[out->len++] = '\\';
3632 switch (c) {
3633 case '\t': c = 't'; break;
3634 case '\n': c = 'n'; break;
3635 case '\r': c = 'r'; break;
3636 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003637 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003638 }
3639 out->str[out->len++] = c;
3640 } else {
3641 if (out->len > end - 5)
3642 break;
3643 out->str[out->len++] = '\\';
3644 out->str[out->len++] = 'x';
3645 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3646 out->str[out->len++] = hextab[c & 0xF];
3647 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003648 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003649 /* we had a line break, let's return now */
3650 out->str[out->len++] = '\n';
3651 *line = ptr;
3652 return ptr;
3653 }
3654 }
3655 /* we have an incomplete line, we return it as-is */
3656 out->str[out->len++] = '\n';
3657 return ptr;
3658}
3659
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003660/* This function dumps all captured errors onto the stream intreface's
3661 * read buffer. The data_ctx must have been zeroed first, and the flags
3662 * properly set. It returns 0 if the output buffer is full and it needs
3663 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003664 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003665static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003666{
3667 extern const char *monthname[12];
3668 struct chunk msg;
3669
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003670 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003671 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003672
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003673 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003674
Willy Tarreau295a8372011-03-10 11:25:07 +01003675 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003676 /* the function had not been called yet, let's prepare the
3677 * buffer for a response.
3678 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003679 struct tm tm;
3680
3681 get_localtime(date.tv_sec, &tm);
3682 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3683 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3684 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3685 error_snapshot_id);
3686
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003687 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003688 /* Socket buffer full. Let's try again later from the same point */
3689 return 0;
3690 }
3691
Willy Tarreau295a8372011-03-10 11:25:07 +01003692 si->applet.ctx.errors.px = proxy;
3693 si->applet.ctx.errors.buf = 0;
3694 si->applet.ctx.errors.bol = 0;
3695 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003696 }
3697
3698 /* we have two inner loops here, one for the proxy, the other one for
3699 * the buffer.
3700 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003701 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003702 struct error_snapshot *es;
3703
Willy Tarreau295a8372011-03-10 11:25:07 +01003704 if (si->applet.ctx.errors.buf == 0)
3705 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003706 else
Willy Tarreau295a8372011-03-10 11:25:07 +01003707 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003708
3709 if (!es->when.tv_sec)
3710 goto next;
3711
Willy Tarreau295a8372011-03-10 11:25:07 +01003712 if (si->applet.ctx.errors.iid >= 0 &&
3713 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
3714 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003715 goto next;
3716
Willy Tarreau295a8372011-03-10 11:25:07 +01003717 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003718 /* just print headers now */
3719
3720 char pn[INET6_ADDRSTRLEN];
3721 struct tm tm;
3722
3723 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003724 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003725 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003726 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003727
Willy Tarreau631f01c2011-09-05 00:36:48 +02003728 addr_to_str(&es->src, pn, sizeof(pn));
Willy Tarreau295a8372011-03-10 11:25:07 +01003729 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003730 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003731 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003732 " frontend %s (#%d): invalid request\n"
3733 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003734 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003735 " request length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003736 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau3fd6cec2010-03-25 06:45:07 +01003737 pn, es->sid, (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3738 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003739 es->srv ? es->srv->id : "<NONE>",
3740 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003741 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003742 es->len, es->pos);
3743 break;
3744 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003745 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003746 " backend %s (#%d) : invalid response\n"
3747 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003748 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003749 " response length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003750 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003751 pn, es->sid, es->oe->id, es->oe->uuid,
3752 es->srv ? es->srv->id : "<NONE>",
3753 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003754 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003755 es->len, es->pos);
3756 break;
3757 }
3758
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003759 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003760 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003761 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003762 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003763 si->applet.ctx.errors.ptr = 0;
3764 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003765 }
3766
Willy Tarreau295a8372011-03-10 11:25:07 +01003767 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003768 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003769 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003770 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003771 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau61b34732009-10-03 23:49:35 +02003772 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003773 goto next;
3774 }
3775
3776 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01003777 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003778 int newptr;
3779 int newline;
3780
Willy Tarreau295a8372011-03-10 11:25:07 +01003781 newline = si->applet.ctx.errors.bol;
3782 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
3783 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02003784 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003785
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003786 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003787 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003788 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003789 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003790 si->applet.ctx.errors.ptr = newptr;
3791 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003792 };
3793 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01003794 si->applet.ctx.errors.bol = 0;
3795 si->applet.ctx.errors.ptr = -1;
3796 si->applet.ctx.errors.buf++;
3797 if (si->applet.ctx.errors.buf > 1) {
3798 si->applet.ctx.errors.buf = 0;
3799 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003800 }
3801 }
3802
3803 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02003804 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003805}
3806
Willy Tarreaub24281b2011-02-13 13:16:36 +01003807struct si_applet http_stats_applet = {
3808 .name = "<STATS>", /* used for logging */
3809 .fct = http_stats_io_handler,
3810};
3811
Simon Horman9bd2c732011-06-15 15:18:44 +09003812static struct si_applet cli_applet = {
Willy Tarreaub24281b2011-02-13 13:16:36 +01003813 .name = "<CLI>", /* used for logging */
3814 .fct = cli_io_handler,
3815};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003816
Willy Tarreau10522fd2008-07-09 20:12:41 +02003817static struct cfg_kw_list cfg_kws = {{ },{
3818 { CFG_GLOBAL, "stats", stats_parse_global },
3819 { 0, NULL, NULL },
3820}};
3821
3822__attribute__((constructor))
3823static void __dumpstats_module_init(void)
3824{
3825 cfg_register_keywords(&cfg_kws);
3826}
3827
Willy Tarreau91861262007-10-17 17:06:05 +02003828/*
3829 * Local variables:
3830 * c-indent-level: 8
3831 * c-basic-offset: 8
3832 * End:
3833 */