blob: a15b15b06ee8fde9e3975f4e2d7de87a48426330 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaueb472682010-05-28 18:46:57 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
Simon Horman17bce342011-06-15 15:18:47 +090017#include <stdbool.h>
Willy Tarreau91861262007-10-17 17:06:05 +020018#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020021#include <pwd.h>
22#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020023
24#include <sys/socket.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27
Willy Tarreau10522fd2008-07-09 20:12:41 +020028#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020029#include <common/compat.h>
30#include <common/config.h>
31#include <common/debug.h>
32#include <common/memory.h>
33#include <common/mini-clist.h>
34#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020035#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020036#include <common/time.h>
37#include <common/uri_auth.h>
38#include <common/version.h>
39
Willy Tarreau91861262007-10-17 17:06:05 +020040#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020041
42#include <proto/backend.h>
43#include <proto/buffers.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020044#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020045#include <proto/dumpstats.h>
46#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010047#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020048#include <proto/log.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010049#include <proto/pipe.h>
Willy Tarreau2a0f4d22011-08-02 11:49:05 +020050#include <proto/protocols.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020051#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020052#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020053#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020054#include <proto/server.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010055#include <proto/stream_interface.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020056#include <proto/stream_sock.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010057#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020058
Simon Horman9bd2c732011-06-15 15:18:44 +090059static int stats_dump_raw_to_buffer(struct stream_interface *si);
60static int stats_dump_full_sess_to_buffer(struct stream_interface *si);
61static int stats_dump_sess_to_buffer(struct stream_interface *si);
62static int stats_dump_errors_to_buffer(struct stream_interface *si);
Simon Hormanc88b8872011-06-15 15:18:49 +090063static int stats_table_request(struct stream_interface *si, bool show);
Simon Horman9bd2c732011-06-15 15:18:44 +090064static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
65static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri);
66
67static struct si_applet cli_applet;
68
69static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020070 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +020071 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +020072 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020073 " help : this message\n"
74 " prompt : toggle interactive mode with prompt\n"
75 " quit : disconnect\n"
76 " show info : report information about the running process\n"
77 " show stat : report counters for each proxy and server\n"
78 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +010079 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +020080 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +020081 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +020082 " set weight : change a server's weight\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +010083 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +020084 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +020085 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +020086 " disable : put a server or frontend in maintenance mode\n"
87 " enable : re-enable a server or frontend which is in maintenance mode\n"
88 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020089 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020090
Simon Horman9bd2c732011-06-15 15:18:44 +090091static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +020092 "Permission denied\n"
93 "";
94
Willy Tarreau295a8372011-03-10 11:25:07 +010095/* data transmission states for the stats responses */
96enum {
97 STAT_ST_INIT = 0,
98 STAT_ST_HEAD,
99 STAT_ST_INFO,
100 STAT_ST_LIST,
101 STAT_ST_END,
102 STAT_ST_FIN,
103};
104
105/* data transmission states for the stats responses inside a proxy */
106enum {
107 STAT_PX_ST_INIT = 0,
108 STAT_PX_ST_TH,
109 STAT_PX_ST_FE,
110 STAT_PX_ST_LI,
111 STAT_PX_ST_SV,
112 STAT_PX_ST_BE,
113 STAT_PX_ST_END,
114 STAT_PX_ST_FIN,
115};
116
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 Tarreau532a4502011-09-07 22:37:44 +0200665/* Expects to find a frontend named <arg> and returns it, otherwise displays various
666 * adequate error messages and returns NULL. This function also expects the session
667 * level to be admin.
668 */
669static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
670{
671 struct proxy *px;
672
673 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
674 si->applet.ctx.cli.msg = stats_permission_denied_msg;
675 si->applet.st0 = STAT_CLI_PRINT;
676 return NULL;
677 }
678
679 if (!*arg) {
680 si->applet.ctx.cli.msg = "A frontend name is expected.\n";
681 si->applet.st0 = STAT_CLI_PRINT;
682 return NULL;
683 }
684
685 px = findproxy(arg, PR_CAP_FE);
686 if (!px) {
687 si->applet.ctx.cli.msg = "No such frontend.\n";
688 si->applet.st0 = STAT_CLI_PRINT;
689 return NULL;
690 }
691 return px;
692}
693
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200694/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
695 * and returns the pointer to the server. Otherwise, display adequate error messages
696 * and returns NULL. This function also expects the session level to be admin. Note:
697 * the <arg> is modified to remove the '/'.
698 */
699static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
700{
701 struct proxy *px;
702 struct server *sv;
703 char *line;
704
705 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
706 si->applet.ctx.cli.msg = stats_permission_denied_msg;
707 si->applet.st0 = STAT_CLI_PRINT;
708 return NULL;
709 }
710
711 /* split "backend/server" and make <line> point to server */
712 for (line = arg; *line; line++)
713 if (*line == '/') {
714 *line++ = '\0';
715 break;
716 }
717
718 if (!*line || !*arg) {
719 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
720 si->applet.st0 = STAT_CLI_PRINT;
721 return NULL;
722 }
723
724 if (!get_backend_server(arg, line, &px, &sv)) {
725 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
726 si->applet.st0 = STAT_CLI_PRINT;
727 return NULL;
728 }
729
730 if (px->state == PR_STSTOPPED) {
731 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
732 si->applet.st0 = STAT_CLI_PRINT;
733 return NULL;
734 }
735
736 return sv;
737}
738
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200739/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200740 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100741 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200742 * designating the function which will have to process the request, which can
743 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200744 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900745static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200746{
Willy Tarreaubc4af052011-02-13 13:25:14 +0100747 struct session *s = si->applet.private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200748 char *args[MAX_STATS_ARGS + 1];
749 int arg;
750
751 while (isspace((unsigned char)*line))
752 line++;
753
754 arg = 0;
755 args[arg] = line;
756
757 while (*line && arg < MAX_STATS_ARGS) {
758 if (isspace((unsigned char)*line)) {
759 *line++ = '\0';
760
761 while (isspace((unsigned char)*line))
762 line++;
763
764 args[++arg] = line;
765 continue;
766 }
767
768 line++;
769 }
770
771 while (++arg <= MAX_STATS_ARGS)
772 args[arg] = line;
773
Willy Tarreau295a8372011-03-10 11:25:07 +0100774 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200775 if (strcmp(args[0], "show") == 0) {
776 if (strcmp(args[1], "stat") == 0) {
777 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100778 si->applet.ctx.stats.flags |= STAT_BOUND;
779 si->applet.ctx.stats.iid = atoi(args[2]);
780 si->applet.ctx.stats.type = atoi(args[3]);
781 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200782 }
783
Willy Tarreau295a8372011-03-10 11:25:07 +0100784 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
785 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
786 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100787 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200788 }
789 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100790 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
791 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
792 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100793 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200794 }
795 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100796 si->applet.state = STAT_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200797 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100798 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100799 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200800 return 1;
801 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100802 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100803 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100804 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100805 si->applet.ctx.sess.target = NULL;
806 si->applet.ctx.sess.section = 0; /* start with session status */
807 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100808 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200809 }
810 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200811 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100812 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100813 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200814 return 1;
815 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200816 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100817 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200818 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100819 si->applet.ctx.errors.iid = -1;
820 si->applet.ctx.errors.px = NULL;
821 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100822 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200823 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200824 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900825 stats_sock_table_request(si, args, true);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200826 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200827 else { /* neither "stat" nor "info" nor "sess" nor "errors" no "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200828 return 0;
829 }
830 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200831 else if (strcmp(args[0], "clear") == 0) {
832 if (strcmp(args[1], "counters") == 0) {
833 struct proxy *px;
834 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200835 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200836 int clrall = 0;
837
838 if (strcmp(args[2], "all") == 0)
839 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200840
Willy Tarreau6162db22009-10-10 17:13:00 +0200841 /* check permissions */
842 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
843 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100844 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100845 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200846 return 1;
847 }
848
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200849 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100850 if (clrall) {
851 memset(&px->be_counters, 0, sizeof(px->be_counters));
852 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
853 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200854 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100855 px->be_counters.conn_max = 0;
856 px->be_counters.p.http.rps_max = 0;
857 px->be_counters.sps_max = 0;
858 px->be_counters.cps_max = 0;
859 px->be_counters.nbpend_max = 0;
860
861 px->fe_counters.conn_max = 0;
862 px->fe_counters.p.http.rps_max = 0;
863 px->fe_counters.sps_max = 0;
864 px->fe_counters.cps_max = 0;
865 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200866 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200867
868 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200869 if (clrall)
870 memset(&sv->counters, 0, sizeof(sv->counters));
871 else {
872 sv->counters.cur_sess_max = 0;
873 sv->counters.nbpend_max = 0;
874 sv->counters.sps_max = 0;
875 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200876
877 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200878 if (li->counters) {
879 if (clrall)
880 memset(li->counters, 0, sizeof(*li->counters));
881 else
882 li->counters->conn_max = 0;
883 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200884 }
885
Willy Tarreau81c25d02011-09-07 15:17:21 +0200886 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200887 return 1;
888 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200889 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900890 stats_sock_table_request(si, args, false);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200891 /* end of processing */
892 return 1;
893 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200894 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200895 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200896 return 0;
897 }
898 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200899 else if (strcmp(args[0], "get") == 0) {
900 if (strcmp(args[1], "weight") == 0) {
901 struct proxy *px;
902 struct server *sv;
903
904 /* split "backend/server" and make <line> point to server */
905 for (line = args[2]; *line; line++)
906 if (*line == '/') {
907 *line++ = '\0';
908 break;
909 }
910
911 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100912 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100913 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200914 return 1;
915 }
916
917 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100918 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100919 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200920 return 1;
921 }
922
923 /* return server's effective weight at the moment */
924 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
925 buffer_feed(si->ib, trash);
926 return 1;
927 }
928 else { /* not "get weight" */
929 return 0;
930 }
931 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200932 else if (strcmp(args[0], "set") == 0) {
933 if (strcmp(args[1], "weight") == 0) {
934 struct proxy *px;
935 struct server *sv;
936 int w;
937
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200938 sv = expect_server_admin(s, si, args[2]);
939 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +0200940 return 1;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200941 px = sv->proxy;
Willy Tarreau4483d432009-10-10 19:30:08 +0200942
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200943 /* if the weight is terminated with '%', it is set relative to
944 * the initial weight, otherwise it is absolute.
945 */
946 if (!*args[3]) {
947 si->applet.ctx.cli.msg = "Require <weight> or <weight%>.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100948 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100949 return 1;
950 }
951
Willy Tarreau4483d432009-10-10 19:30:08 +0200952 w = atoi(args[3]);
953 if (strchr(args[3], '%') != NULL) {
954 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100955 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100956 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200957 return 1;
958 }
959 w = sv->iweight * w / 100;
960 }
961 else {
962 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100963 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100964 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200965 return 1;
966 }
967 }
968
969 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100970 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 +0100971 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200972 return 1;
973 }
974
975 sv->uweight = w;
976
977 if (px->lbprm.algo & BE_LB_PROP_DYN) {
978 /* we must take care of not pushing the server to full throttle during slow starts */
979 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
980 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
981 else
982 sv->eweight = BE_WEIGHT_SCALE;
983 sv->eweight *= sv->uweight;
984 } else {
985 sv->eweight = sv->uweight;
986 }
987
988 /* static LB algorithms are a bit harder to update */
989 if (px->lbprm.update_server_eweight)
990 px->lbprm.update_server_eweight(sv);
991 else if (sv->eweight)
992 px->lbprm.set_server_status_up(sv);
993 else
994 px->lbprm.set_server_status_down(sv);
995
996 return 1;
997 }
Willy Tarreau7aabd112010-01-26 10:59:06 +0100998 else if (strcmp(args[1], "timeout") == 0) {
999 if (strcmp(args[2], "cli") == 0) {
1000 unsigned timeout;
1001 const char *res;
1002
1003 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001004 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001005 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001006 return 1;
1007 }
1008
1009 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1010 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001011 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001012 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001013 return 1;
1014 }
1015
1016 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1017 return 1;
1018 }
1019 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001020 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001021 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001022 return 1;
1023 }
1024 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001025 else if (strcmp(args[1], "maxconn") == 0) {
1026 if (strcmp(args[2], "frontend") == 0) {
1027 struct proxy *px;
1028 struct listener *l;
1029 int v;
1030
Willy Tarreau532a4502011-09-07 22:37:44 +02001031 px = expect_frontend_admin(s, si, args[3]);
1032 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001033 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001034
1035 if (!*args[4]) {
1036 si->applet.ctx.cli.msg = "Integer value expected.\n";
1037 si->applet.st0 = STAT_CLI_PRINT;
1038 return 1;
1039 }
1040
1041 v = atoi(args[4]);
1042 /* check for unlimited values, we restore default setting (cfg_maxpconn) */
1043 if (v < 1) {
1044 si->applet.ctx.cli.msg = "Value out of range.\n";
1045 si->applet.st0 = STAT_CLI_PRINT;
1046 return 1;
1047 }
1048
1049 /* OK, the value is fine, so we assign it to the proxy and to all of
1050 * its listeners. The blocked ones will be dequeued.
1051 */
1052 px->maxconn = v;
1053 for (l = px->listen; l != NULL; l = l->next) {
1054 l->maxconn = v;
1055 if (l->state == LI_FULL)
1056 resume_listener(l);
1057 }
1058
1059 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1060 dequeue_all_listeners(&s->fe->listener_queue);
1061
1062 return 1;
1063 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001064 else if (strcmp(args[2], "global") == 0) {
1065 int v;
1066
1067 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1068 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1069 si->applet.st0 = STAT_CLI_PRINT;
1070 return 1;
1071 }
1072
1073 if (!*args[3]) {
1074 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1075 si->applet.st0 = STAT_CLI_PRINT;
1076 return 1;
1077 }
1078
1079 v = atoi(args[3]);
1080 if (v > global.hardmaxconn) {
1081 si->applet.ctx.cli.msg = "Value out of range.\n";
1082 si->applet.st0 = STAT_CLI_PRINT;
1083 return 1;
1084 }
1085
1086 /* check for unlimited values */
1087 if (v <= 0)
1088 v = global.hardmaxconn;
1089
1090 global.maxconn = v;
1091
1092 /* Dequeues all of the listeners waiting for a resource */
1093 if (!LIST_ISEMPTY(&global_listener_queue))
1094 dequeue_all_listeners(&global_listener_queue);
1095
1096 return 1;
1097 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001098 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001099 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001100 si->applet.st0 = STAT_CLI_PRINT;
1101 return 1;
1102 }
1103 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001104 else if (strcmp(args[1], "rate-limit") == 0) {
1105 if (strcmp(args[2], "connections") == 0) {
1106 if (strcmp(args[3], "global") == 0) {
1107 int v;
1108
1109 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1110 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1111 si->applet.st0 = STAT_CLI_PRINT;
1112 return 1;
1113 }
1114
1115 if (!*args[4]) {
1116 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1117 si->applet.st0 = STAT_CLI_PRINT;
1118 return 1;
1119 }
1120
1121 v = atoi(args[4]);
1122 if (v < 0) {
1123 si->applet.ctx.cli.msg = "Value out of range.\n";
1124 si->applet.st0 = STAT_CLI_PRINT;
1125 return 1;
1126 }
1127
1128 global.cps_lim = v;
1129
1130 /* Dequeues all of the listeners waiting for a resource */
1131 if (!LIST_ISEMPTY(&global_listener_queue))
1132 dequeue_all_listeners(&global_listener_queue);
1133
1134 return 1;
1135 }
1136 else {
1137 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1138 si->applet.st0 = STAT_CLI_PRINT;
1139 return 1;
1140 }
1141 }
1142 else {
1143 si->applet.ctx.cli.msg = "'set rate-limit' only supports 'connections'.\n";
1144 si->applet.st0 = STAT_CLI_PRINT;
1145 return 1;
1146 }
1147 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001148 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001149 return 0;
1150 }
1151 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001152 else if (strcmp(args[0], "enable") == 0) {
1153 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001154 struct server *sv;
1155
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001156 sv = expect_server_admin(s, si, args[2]);
1157 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001158 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001159
Cyril Bontécd19e512010-01-31 22:34:03 +01001160 if (sv->state & SRV_MAINTAIN) {
1161 /* The server is really in maintenance, we can change the server state */
1162 if (sv->tracked) {
1163 /* If this server tracks the status of another one,
1164 * we must restore the good status.
1165 */
1166 if (sv->tracked->state & SRV_RUNNING) {
1167 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001168 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001169 } else {
1170 sv->state &= ~SRV_MAINTAIN;
1171 set_server_down(sv);
1172 }
1173 } else {
1174 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001175 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001176 }
1177 }
1178
Willy Tarreau532a4502011-09-07 22:37:44 +02001179 return 1;
1180 }
1181 else if (strcmp(args[1], "frontend") == 0) {
1182 struct proxy *px;
1183
1184 px = expect_frontend_admin(s, si, args[2]);
1185 if (!px)
1186 return 1;
1187
1188 if (px->state == PR_STSTOPPED) {
1189 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1190 si->applet.st0 = STAT_CLI_PRINT;
1191 return 1;
1192 }
1193
1194 if (px->state != PR_STPAUSED) {
1195 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1196 si->applet.st0 = STAT_CLI_PRINT;
1197 return 1;
1198 }
1199
1200 if (!resume_proxy(px)) {
1201 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1202 si->applet.st0 = STAT_CLI_PRINT;
1203 return 1;
1204 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001205 return 1;
1206 }
1207 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001208 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1209 si->applet.st0 = STAT_CLI_PRINT;
1210 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001211 }
1212 }
1213 else if (strcmp(args[0], "disable") == 0) {
1214 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001215 struct server *sv;
1216
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001217 sv = expect_server_admin(s, si, args[2]);
1218 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001219 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001220
Cyril Bontécd19e512010-01-31 22:34:03 +01001221 if (! (sv->state & SRV_MAINTAIN)) {
1222 /* Not already in maintenance, we can change the server state */
1223 sv->state |= SRV_MAINTAIN;
1224 set_server_down(sv);
1225 }
1226
Willy Tarreau532a4502011-09-07 22:37:44 +02001227 return 1;
1228 }
1229 else if (strcmp(args[1], "frontend") == 0) {
1230 struct proxy *px;
1231
1232 px = expect_frontend_admin(s, si, args[2]);
1233 if (!px)
1234 return 1;
1235
1236 if (px->state == PR_STSTOPPED) {
1237 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1238 si->applet.st0 = STAT_CLI_PRINT;
1239 return 1;
1240 }
1241
1242 if (px->state == PR_STPAUSED) {
1243 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1244 si->applet.st0 = STAT_CLI_PRINT;
1245 return 1;
1246 }
1247
1248 if (!pause_proxy(px)) {
1249 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1250 si->applet.st0 = STAT_CLI_PRINT;
1251 return 1;
1252 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001253 return 1;
1254 }
1255 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001256 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1257 si->applet.st0 = STAT_CLI_PRINT;
1258 return 1;
1259 }
1260 }
1261 else if (strcmp(args[0], "shutdown") == 0) {
1262 if (strcmp(args[1], "frontend") == 0) {
1263 struct proxy *px;
1264
1265 px = expect_frontend_admin(s, si, args[2]);
1266 if (!px)
1267 return 1;
1268
1269 if (px->state == PR_STSTOPPED) {
1270 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1271 si->applet.st0 = STAT_CLI_PRINT;
1272 return 1;
1273 }
1274
1275 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1276 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1277 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1278 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1279 stop_proxy(px);
1280 return 1;
1281 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001282 else if (strcmp(args[1], "session") == 0) {
1283 struct session *sess, *ptr;
1284
1285 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1286 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1287 si->applet.st0 = STAT_CLI_PRINT;
1288 return 1;
1289 }
1290
1291 if (!*args[2]) {
1292 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1293 si->applet.st0 = STAT_CLI_PRINT;
1294 return 1;
1295 }
1296
1297 ptr = (void *)strtoul(args[2], NULL, 0);
1298
1299 /* first, look for the requested session in the session table */
1300 list_for_each_entry(sess, &sessions, list) {
1301 if (sess == ptr)
1302 break;
1303 }
1304
1305 /* do we have the session ? */
1306 if (sess != ptr) {
1307 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1308 si->applet.st0 = STAT_CLI_PRINT;
1309 return 1;
1310 }
1311
1312 session_shutdown(sess, SN_ERR_KILLED);
1313 return 1;
1314 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001315 else if (strcmp(args[1], "sessions") == 0) {
1316 if (strcmp(args[2], "server") == 0) {
1317 struct server *sv;
1318 struct session *sess, *sess_bck;
1319
1320 sv = expect_server_admin(s, si, args[3]);
1321 if (!sv)
1322 return 1;
1323
1324 /* kill all the session that are on this server */
1325 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1326 if (sess->srv_conn == sv)
1327 session_shutdown(sess, SN_ERR_KILLED);
1328
1329 return 1;
1330 }
1331 else {
1332 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1333 si->applet.st0 = STAT_CLI_PRINT;
1334 return 1;
1335 }
1336 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001337 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001338 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001339 si->applet.st0 = STAT_CLI_PRINT;
1340 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001341 }
1342 }
1343 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001344 return 0;
1345 }
1346 return 1;
1347}
1348
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001349/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001350 * used to processes I/O from/to the stats unix socket. The system relies on a
1351 * state machine handling requests and various responses. We read a request,
1352 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001353 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1354 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001355 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001356 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001357static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001358{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001359 struct buffer *req = si->ob;
1360 struct buffer *res = si->ib;
1361 int reql;
1362 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001363
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001364 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1365 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001366
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001367 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001368 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001369 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001370 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001371 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001372 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001373 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001374 /* Let's close for real now. We just close the request
1375 * side, the conditions below will complete if needed.
1376 */
1377 si->shutw(si);
1378 break;
1379 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001380 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001381 /* ensure we have some output room left in the event we
1382 * would want to return some info right after parsing.
1383 */
1384 if (buffer_almost_full(si->ib))
1385 break;
1386
Willy Tarreau74b08c92010-09-08 17:04:31 +02001387 reql = buffer_get_line(si->ob, trash, sizeof(trash));
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001388 if (reql <= 0) { /* closed or EOL not found */
1389 if (reql == 0)
1390 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001391 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001392 continue;
1393 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001394
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001395 /* seek for a possible semi-colon. If we find one, we
1396 * replace it with an LF and skip only this part.
1397 */
1398 for (len = 0; len < reql; len++)
1399 if (trash[len] == ';') {
1400 trash[len] = '\n';
1401 reql = len + 1;
1402 break;
1403 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001404
Willy Tarreau816fc222009-10-04 07:36:58 +02001405 /* now it is time to check that we have a full line,
1406 * remove the trailing \n and possibly \r, then cut the
1407 * line.
1408 */
1409 len = reql - 1;
1410 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001411 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001412 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001413 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001414
Willy Tarreau816fc222009-10-04 07:36:58 +02001415 if (len && trash[len-1] == '\r')
1416 len--;
1417
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001418 trash[len] = '\0';
1419
Willy Tarreaubc4af052011-02-13 13:25:14 +01001420 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001421 if (len) {
1422 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001423 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001424 continue;
1425 }
1426 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001427 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001428 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001429 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001430 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001431 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001432 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001433 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001434 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001435 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001436 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001437 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001438 /* if prompt is disabled, print help on empty lines,
1439 * so that the user at least knows how to enable
1440 * prompt and find help.
1441 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001442 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001443 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001444 }
1445
1446 /* re-adjust req buffer */
1447 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001448 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001449 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001450 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001451 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001452 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001453 continue;
1454 }
1455
Willy Tarreaubc4af052011-02-13 13:25:14 +01001456 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001457 case STAT_CLI_PRINT:
Willy Tarreau295a8372011-03-10 11:25:07 +01001458 if (buffer_feed(si->ib, si->applet.ctx.cli.msg) < 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001459 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001460 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001461 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001462 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001463 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001464 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001465 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001466 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001467 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001468 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001469 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001470 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001471 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001472 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001473 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001474 if (stats_table_request(si, true))
1475 si->applet.st0 = STAT_CLI_PROMPT;
1476 break;
1477 case STAT_CLI_O_CLR:
1478 if (stats_table_request(si, false))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001479 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001480 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001481 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001482 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001483 break;
1484 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001485
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001486 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001487 if (si->applet.st0 == STAT_CLI_PROMPT) {
1488 if (buffer_feed(si->ib, si->applet.st1 ? "\n> " : "\n") < 0)
1489 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001490 }
1491
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001492 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001493 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001494 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001495
1496 /* Now we close the output if one of the writers did so,
1497 * or if we're not in interactive mode and the request
1498 * buffer is empty. This still allows pipelined requests
1499 * to be sent in non-interactive mode.
1500 */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001501 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->applet.st1 && !req->send_max)) {
1502 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001503 continue;
1504 }
1505
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001506 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001507 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001508 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001509 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001510
Willy Tarreaubc4af052011-02-13 13:25:14 +01001511 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001512 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1513 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1514 /* Other size has closed, let's abort if we have no more processing to do
1515 * and nothing more to consume. This is comparable to a broken pipe, so
1516 * we forward the close to the request side so that it flows upstream to
1517 * the client.
1518 */
1519 si->shutw(si);
1520 }
1521
Willy Tarreaubc4af052011-02-13 13:25:14 +01001522 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001523 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1524 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1525 /* We have no more processing to do, and nothing more to send, and
1526 * the client side has closed. So we'll forward this state downstream
1527 * on the response buffer.
1528 */
1529 si->shutr(si);
1530 res->flags |= BF_READ_NULL;
1531 }
1532
1533 /* update all other flags and resync with the other side */
1534 si->update(si);
1535
1536 /* we don't want to expire timeouts while we're processing requests */
1537 si->ib->rex = TICK_ETERNITY;
1538 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001539
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001540 out:
1541 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
1542 __FUNCTION__, __LINE__,
1543 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
1544
1545 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1546 /* check that we have released everything then unregister */
1547 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001548 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001549}
1550
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001551/* This function dumps statistics onto the stream interface's read buffer.
1552 * The data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001553 * It returns 0 as long as it does not complete, non-zero upon completion.
1554 * Some states are not used but it makes the code more similar to other
1555 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001556 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001557static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001558{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001559 struct proxy *px;
1560 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001561 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001562
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001563 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +02001564
Willy Tarreau295a8372011-03-10 11:25:07 +01001565 switch (si->applet.state) {
1566 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001567 /* the function had not been called yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001568 si->applet.state = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001569 /* fall through */
1570
Willy Tarreau295a8372011-03-10 11:25:07 +01001571 case STAT_ST_HEAD:
1572 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001573 print_csv_header(&msg);
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001574 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001575 return 0;
1576 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001577
Willy Tarreau295a8372011-03-10 11:25:07 +01001578 si->applet.state = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001579 /* fall through */
1580
Willy Tarreau295a8372011-03-10 11:25:07 +01001581 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001582 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001583 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001584 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001585 "Name: " PRODUCT_NAME "\n"
1586 "Version: " HAPROXY_VERSION "\n"
1587 "Release_date: " HAPROXY_DATE "\n"
1588 "Nbproc: %d\n"
1589 "Process_num: %d\n"
1590 "Pid: %d\n"
1591 "Uptime: %dd %dh%02dm%02ds\n"
1592 "Uptime_sec: %d\n"
1593 "Memmax_MB: %d\n"
1594 "Ulimit-n: %d\n"
1595 "Maxsock: %d\n"
1596 "Maxconn: %d\n"
Willy Tarreau91886b62011-09-07 14:38:31 +02001597 "Hard_maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001598 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001599 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001600 "PipesUsed: %d\n"
1601 "PipesFree: %d\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001602 "ConnRate: %d\n"
1603 "ConnRateLimit: %d\n"
1604 "MaxConnRate: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001605 "Tasks: %d\n"
1606 "Run_queue: %d\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02001607 "Idle_pct: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001608 "node: %s\n"
1609 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001610 "",
1611 global.nbproc,
1612 relative_pid,
1613 pid,
1614 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1615 up,
1616 global.rlimit_memmax,
1617 global.rlimit_nofile,
Willy Tarreau91886b62011-09-07 14:38:31 +02001618 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001619 actconn, pipes_used, pipes_free,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001620 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau45a12512011-09-10 16:56:42 +02001621 nb_tasks_cur, run_queue_cur, idle_pct,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001622 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001623 );
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001624 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001625 return 0;
1626 }
1627
Willy Tarreau295a8372011-03-10 11:25:07 +01001628 si->applet.ctx.stats.px = proxy;
1629 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1630 si->applet.ctx.stats.sv = NULL;
1631 si->applet.state = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001632 /* fall through */
1633
Willy Tarreau295a8372011-03-10 11:25:07 +01001634 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001635 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001636 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1637 while (si->applet.ctx.stats.px) {
1638 px = si->applet.ctx.stats.px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001639 /* skip the disabled proxies and non-networked ones */
1640 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001641 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001642 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001643 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001644 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001645
Willy Tarreau295a8372011-03-10 11:25:07 +01001646 si->applet.ctx.stats.px = px->next;
1647 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001648 }
1649 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001650 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001651
Willy Tarreau295a8372011-03-10 11:25:07 +01001652 si->applet.state = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001653 /* fall through */
1654
Willy Tarreau295a8372011-03-10 11:25:07 +01001655 case STAT_ST_END:
1656 si->applet.state = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001657 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001658
Willy Tarreau295a8372011-03-10 11:25:07 +01001659 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001660 return 1;
1661
1662 default:
1663 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001664 si->applet.state = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001665 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001666 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001667}
1668
1669
Cyril Bonté70be45d2010-10-12 00:14:35 +02001670/* We don't want to land on the posted stats page because a refresh will
1671 * repost the data. We don't want this to happen on accident so we redirect
1672 * the browse to the stats page with a GET.
1673 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001674static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001675{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001676 struct session *s = si->applet.private;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001677 struct chunk msg;
1678
1679 chunk_init(&msg, trash, sizeof(trash));
1680
Willy Tarreau295a8372011-03-10 11:25:07 +01001681 switch (si->applet.state) {
1682 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001683 chunk_printf(&msg,
1684 "HTTP/1.0 303 See Other\r\n"
1685 "Cache-Control: no-cache\r\n"
1686 "Content-Type: text/plain\r\n"
1687 "Connection: close\r\n"
1688 "Location: %s;st=%s",
Willy Tarreau295a8372011-03-10 11:25:07 +01001689 uri->uri_prefix, si->applet.ctx.stats.st_code);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001690 chunk_printf(&msg, "\r\n\r\n");
1691
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001692 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001693 return 0;
1694
1695 s->txn.status = 303;
1696
1697 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1698 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1699 if (!(s->flags & SN_FINST_MASK))
1700 s->flags |= SN_FINST_R;
1701
Willy Tarreau295a8372011-03-10 11:25:07 +01001702 si->applet.state = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001703 return 1;
1704 }
1705 return 1;
1706}
1707
1708
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001709/* This I/O handler runs as an applet embedded in a stream interface. It is
1710 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001711 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001712 * automatically unregisters itself once transfer is complete.
1713 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001714static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001715{
Willy Tarreaubc4af052011-02-13 13:25:14 +01001716 struct session *s = si->applet.private;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001717 struct buffer *req = si->ob;
1718 struct buffer *res = si->ib;
1719
1720 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1721 goto out;
1722
1723 /* check that the output is not closed */
1724 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001725 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001726
Willy Tarreaubc4af052011-02-13 13:25:14 +01001727 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001728 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001729 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001730 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001731 si->shutw(si);
1732 }
1733 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001734 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001735 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001736 si->shutw(si);
1737 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001738 }
1739 }
1740
1741 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
1742 si->shutw(si);
1743
Willy Tarreaubc4af052011-02-13 13:25:14 +01001744 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001745 si->shutr(si);
1746 res->flags |= BF_READ_NULL;
1747 }
1748
1749 /* update all other flags and resync with the other side */
1750 si->update(si);
1751
1752 /* we don't want to expire timeouts while we're processing requests */
1753 si->ib->rex = TICK_ETERNITY;
1754 si->ob->wex = TICK_ETERNITY;
1755
1756 out:
1757 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1758 /* check that we have released everything then unregister */
1759 stream_int_unregister_handler(si);
1760 }
1761}
1762
1763
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001764/* This function dumps statistics in HTTP format onto the stream interface's
1765 * read buffer. The data_ctx must have been zeroed first, and the flags
1766 * properly set. It returns 0 if it had to stop writing data and an I/O is
1767 * needed, 1 if the dump is finished and the session must be closed, or -1
1768 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001769 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001770static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001771{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001772 struct session *s = si->applet.private;
1773 struct buffer *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001774 struct proxy *px;
1775 struct chunk msg;
1776 unsigned int up;
1777
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001778 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001779
Willy Tarreau295a8372011-03-10 11:25:07 +01001780 switch (si->applet.state) {
1781 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001782 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001783 "HTTP/1.0 200 OK\r\n"
1784 "Cache-Control: no-cache\r\n"
1785 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001786 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001787 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001788
Willy Tarreau295a8372011-03-10 11:25:07 +01001789 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001790 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001791 uri->refresh);
1792
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001793 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001794
1795 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001796 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001797 return 0;
1798
Willy Tarreau91861262007-10-17 17:06:05 +02001799 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1800 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1801 if (!(s->flags & SN_FINST_MASK))
1802 s->flags |= SN_FINST_R;
1803
1804 if (s->txn.meth == HTTP_METH_HEAD) {
1805 /* that's all we return in case of HEAD request */
Willy Tarreau295a8372011-03-10 11:25:07 +01001806 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001807 return 1;
1808 }
1809
Willy Tarreau295a8372011-03-10 11:25:07 +01001810 si->applet.state = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001811 /* fall through */
1812
Willy Tarreau295a8372011-03-10 11:25:07 +01001813 case STAT_ST_HEAD:
1814 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001815 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001816 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001817 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1818 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001819 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001820 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1821 "<style type=\"text/css\"><!--\n"
1822 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001823 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001824 " font-size: 12px;"
1825 " font-weight: normal;"
1826 " color: black;"
1827 " background: white;"
1828 "}\n"
1829 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001830 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001831 "}\n"
1832 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001833 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001834 " margin-bottom: 0.5em;"
1835 "}\n"
1836 "h2 {"
1837 " font-family: helvetica, arial;"
1838 " font-size: x-large;"
1839 " font-weight: bold;"
1840 " font-style: italic;"
1841 " color: #6020a0;"
1842 " margin-top: 0em;"
1843 " margin-bottom: 0em;"
1844 "}\n"
1845 "h3 {"
1846 " font-family: helvetica, arial;"
1847 " font-size: 16px;"
1848 " font-weight: bold;"
1849 " color: #b00040;"
1850 " background: #e8e8d0;"
1851 " margin-top: 0em;"
1852 " margin-bottom: 0em;"
1853 "}\n"
1854 "li {"
1855 " margin-top: 0.25em;"
1856 " margin-right: 2em;"
1857 "}\n"
1858 ".hr {margin-top: 0.25em;"
1859 " border-color: black;"
1860 " border-bottom-style: solid;"
1861 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001862 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001863 ".total {background: #20D0D0;color: #ffff80;}\n"
1864 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001865 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001866 ".backend {background: #e8e8d0;}\n"
1867 ".active0 {background: #ff9090;}\n"
1868 ".active1 {background: #ffd020;}\n"
1869 ".active2 {background: #ffffa0;}\n"
1870 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001871 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1872 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1873 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001874 ".backup0 {background: #ff9090;}\n"
1875 ".backup1 {background: #ff80ff;}\n"
1876 ".backup2 {background: #c060ff;}\n"
1877 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001878 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1879 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1880 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001881 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001882 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001883 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001884 "a.px:link {color: #ffff40; text-decoration: none;}"
1885 "a.px:visited {color: #ffff40; text-decoration: none;}"
1886 "a.px:hover {color: #ffffff; text-decoration: none;}"
1887 "a.lfsb:link {color: #000000; text-decoration: none;}"
1888 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1889 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1890 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001891 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001892 "table.tbl td { text-align: right; border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray; white-space: nowrap;}\n"
1893 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001894 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001895 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001896 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001897 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001898 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001899 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1900 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1901 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001902 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001903 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001904 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001905 (uri->flags&ST_SHNODE) ? " on " : "",
1906 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001907 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001908 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001909 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001910 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001911 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001912 return 0;
1913
Willy Tarreau295a8372011-03-10 11:25:07 +01001914 si->applet.state = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02001915 /* fall through */
1916
Willy Tarreau295a8372011-03-10 11:25:07 +01001917 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02001918 up = (now.tv_sec - start_date.tv_sec);
1919
1920 /* WARNING! this has to fit the first packet too.
1921 * We are around 3.5 kB, add adding entries will
1922 * become tricky if we want to support 4kB buffers !
1923 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001924 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001925 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001926 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1927 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001928 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001929 "<hr width=\"100%%\" class=\"hr\">\n"
1930 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001931 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001932 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001933 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001934 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1935 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001936 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02001937 "Running tasks: %d/%d; idle = %d %%<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001938 "</td><td align=\"center\" nowrap>\n"
1939 "<table class=\"lgd\"><tr>\n"
1940 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1941 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1942 "</tr><tr>\n"
1943 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1944 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1945 "</tr><tr>\n"
1946 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1947 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1948 "</tr><tr>\n"
1949 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001950 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001951 "</tr><tr>\n"
1952 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001953 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001954 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001955 "</td>"
1956 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1957 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1958 "",
1959 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001960 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1961 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1962 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001963 up / 86400, (up % 86400) / 3600,
1964 (up % 3600) / 60, (up % 60),
1965 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1966 global.rlimit_memmax ? " MB" : "",
1967 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001968 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001969 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau45a12512011-09-10 16:56:42 +02001970 run_queue_cur, nb_tasks_cur, idle_pct
Willy Tarreau91861262007-10-17 17:06:05 +02001971 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001972
Willy Tarreau295a8372011-03-10 11:25:07 +01001973 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001974 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001975 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1976 uri->uri_prefix,
1977 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01001978 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001979 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001980 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001981 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1982 uri->uri_prefix,
1983 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01001984 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001985
Willy Tarreau55bb8452007-10-17 18:44:57 +02001986 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001987 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001988 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001989 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1990 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001991 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001992 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001993 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001994 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001995 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1996 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001997 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001998 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001999 }
Willy Tarreau91861262007-10-17 17:06:05 +02002000
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002001 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002002 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
2003 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002004 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2005 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002006
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002007 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002008 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
2009 uri->uri_prefix,
2010 (uri->refresh > 0) ? ";norefresh" : "");
2011
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002012 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002013 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002014 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2015 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
2016 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2017 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2018 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2019 "</ul>"
2020 "</td>"
2021 "</tr></table>\n"
2022 ""
2023 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002024
Willy Tarreau295a8372011-03-10 11:25:07 +01002025 if (si->applet.ctx.stats.st_code) {
2026 if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002027 chunk_printf(&msg,
2028 "<p><div class=active3>"
2029 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2030 "Action processed successfully."
2031 "</div>\n", uri->uri_prefix);
2032 }
Willy Tarreau295a8372011-03-10 11:25:07 +01002033 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_NONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002034 chunk_printf(&msg,
2035 "<p><div class=active2>"
2036 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2037 "Nothing has changed."
2038 "</div>\n", uri->uri_prefix);
2039 }
Willy Tarreau295a8372011-03-10 11:25:07 +01002040 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_EXCD) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002041 chunk_printf(&msg,
2042 "<p><div class=active0>"
2043 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2044 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2045 "You should retry with less servers at a time.</b>"
2046 "</div>\n", uri->uri_prefix);
2047 }
Willy Tarreau295a8372011-03-10 11:25:07 +01002048 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DENY) == 0) {
Cyril Bonté474be412010-10-12 00:14:36 +02002049 chunk_printf(&msg,
2050 "<p><div class=active0>"
2051 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2052 "<b>Action denied.</b>"
2053 "</div>\n", uri->uri_prefix);
2054 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002055 else {
2056 chunk_printf(&msg,
2057 "<p><div class=active6>"
2058 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2059 "Unexpected result."
2060 "</div>\n", uri->uri_prefix);
2061 }
2062 chunk_printf(&msg,"<p>\n");
2063 }
2064
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002065 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002066 return 0;
2067 }
Willy Tarreau91861262007-10-17 17:06:05 +02002068
Willy Tarreau295a8372011-03-10 11:25:07 +01002069 si->applet.ctx.stats.px = proxy;
2070 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
2071 si->applet.state = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02002072 /* fall through */
2073
Willy Tarreau295a8372011-03-10 11:25:07 +01002074 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02002075 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01002076 while (si->applet.ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002077 if (buffer_almost_full(rep))
2078 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002079 px = si->applet.ctx.stats.px;
Willy Tarreau91861262007-10-17 17:06:05 +02002080 /* skip the disabled proxies and non-networked ones */
2081 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002082 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002083 return 0;
2084
Willy Tarreau295a8372011-03-10 11:25:07 +01002085 si->applet.ctx.stats.px = px->next;
2086 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02002087 }
2088 /* here, we just have reached the last proxy */
2089
Willy Tarreau295a8372011-03-10 11:25:07 +01002090 si->applet.state = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002091 /* fall through */
2092
Willy Tarreau295a8372011-03-10 11:25:07 +01002093 case STAT_ST_END:
2094 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002095 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002096 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002097 return 0;
2098 }
Willy Tarreau91861262007-10-17 17:06:05 +02002099
Willy Tarreau295a8372011-03-10 11:25:07 +01002100 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002101 /* fall through */
2102
Willy Tarreau295a8372011-03-10 11:25:07 +01002103 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002104 return 1;
2105
2106 default:
2107 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01002108 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002109 return -1;
2110 }
2111}
2112
2113
2114/*
2115 * Dumps statistics for a proxy.
2116 * Returns 0 if it had to stop dumping data because of lack of buffer space,
2117 * ot non-zero if everything completed.
2118 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002119static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002120{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002121 struct session *s = si->applet.private;
2122 struct buffer *rep = si->ib;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002123 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002124 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002125 struct chunk msg;
2126
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002127 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02002128
Willy Tarreau295a8372011-03-10 11:25:07 +01002129 switch (si->applet.ctx.stats.px_st) {
2130 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002131 /* we are on a new proxy */
2132
2133 if (uri && uri->scope) {
2134 /* we have a limited scope, we have to check the proxy name */
2135 struct stat_scope *scope;
2136 int len;
2137
2138 len = strlen(px->id);
2139 scope = uri->scope;
2140
2141 while (scope) {
2142 /* match exact proxy name */
2143 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2144 break;
2145
2146 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002147 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002148 break;
2149 scope = scope->next;
2150 }
2151
2152 /* proxy name not found : don't dump anything */
2153 if (scope == NULL)
2154 return 1;
2155 }
2156
Willy Tarreau295a8372011-03-10 11:25:07 +01002157 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
2158 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002159 return 1;
2160
Willy Tarreau295a8372011-03-10 11:25:07 +01002161 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002162 /* fall through */
2163
Willy Tarreau295a8372011-03-10 11:25:07 +01002164 case STAT_PX_ST_TH:
2165 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
2166 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002167 /* A form to enable/disable this proxy servers */
2168 chunk_printf(&msg,
2169 "<form action=\"%s\" method=\"post\">",
2170 uri->uri_prefix);
2171 }
2172
Willy Tarreau55bb8452007-10-17 18:44:57 +02002173 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002174 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002175 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002176 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002177 "<th class=\"pxname\" width=\"10%%\"");
2178
2179 if (uri->flags&ST_SHLGNDS) {
2180 /* cap, mode, id */
2181 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
2182 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2183 px->uuid);
2184
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002185 chunk_printf(&msg, "\"");
2186 }
2187
2188 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002189 ">%s<a name=\"%s\"></a>"
2190 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002191 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002192 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002193 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002194 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002195 "<tr class=\"titre\">",
2196 (uri->flags & ST_SHLGNDS)?"<u>":"",
2197 px->id, px->id, px->id,
2198 (uri->flags & ST_SHLGNDS)?"</u>":"",
2199 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2200
Willy Tarreau295a8372011-03-10 11:25:07 +01002201 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002202 /* Column heading for Enable or Disable server */
2203 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
2204 }
2205
2206 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002207 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002208 "<th colspan=3>Queue</th>"
2209 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002210 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002211 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002212 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002213 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002214 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002215 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002216 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002217 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002218 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002219 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002220 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002221 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2222 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002223 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002224
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002225 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002226 return 0;
2227 }
Willy Tarreau91861262007-10-17 17:06:05 +02002228
Willy Tarreau295a8372011-03-10 11:25:07 +01002229 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002230 /* fall through */
2231
Willy Tarreau295a8372011-03-10 11:25:07 +01002232 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002233 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002234 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002235 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
2236 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002237 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002238 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002239 "<tr class=\"frontend\">");
2240
Willy Tarreau295a8372011-03-10 11:25:07 +01002241 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002242 /* Column sub-heading for Enable or Disable server */
2243 chunk_printf(&msg, "<td></td>");
2244 }
2245
2246 chunk_printf(&msg,
2247 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002248 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002249 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2250 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002251 "",
2252 px->id, px->id);
2253
2254 if (px->mode == PR_MODE_HTTP) {
2255 chunk_printf(&msg,
2256 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01002257 "<td title=\"Cur: %u req/s\"><u>%s</u></td><td title=\"Max: %u req/s\"><u>%s</u></td><td>%s</td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002258 "",
2259 read_freq_ctr(&px->fe_req_per_sec),
2260 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002261 px->fe_counters.p.http.rps_max,
2262 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01002263 LIM2A2(px->fe_sps_lim, "-"));
2264 } else {
2265 chunk_printf(&msg,
2266 /* sessions rate : current, max, limit */
2267 "<td>%s</td><td>%s</td><td>%s</td>"
2268 "",
2269 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002270 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01002271 }
2272
2273 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002274 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002275 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002276 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002277 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002278 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002279
2280 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2281 if (px->mode == PR_MODE_HTTP) {
2282 int i;
2283
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002284 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002285
2286 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002287 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002288
Willy Tarreaueabea072011-09-10 23:29:44 +02002289 chunk_printf(&msg, " other=%lld,", px->fe_counters.p.http.rsp[0]);
2290 chunk_printf(&msg, " intercepted=%lld\"", px->fe_counters.intercepted_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002291 }
2292
2293 chunk_printf(&msg,
2294 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002295 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002296 /* bytes : in, out */
2297 "<td>%s</td><td>%s</td>"
2298 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002299 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002300 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002301 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002302 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002303
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002304 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002305 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002306 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002307 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002308 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002309 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002310 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002311 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002312 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002313 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002314 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002315 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002316 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2317 U2H2(px->fe_counters.failed_req),
Willy Tarreau562515c2011-07-25 08:11:52 +02002318 px->state == PR_STREADY ? "OPEN" :
2319 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002320 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002321 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002322 /* pxid, name, queue cur, queue max, */
2323 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002324 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002325 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002326 /* bytes : in, out */
2327 "%lld,%lld,"
2328 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002329 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002330 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002331 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002332 /* warnings: retries, redispatches */
2333 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002334 /* server status : reflect frontend status */
2335 "%s,"
2336 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002337 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002338 /* pid, iid, sid, throttle, lbtot, tracked, type */
2339 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002340 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002341 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002342 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002343 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002344 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002345 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2346 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2347 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2348 px->fe_counters.failed_req,
Willy Tarreau562515c2011-07-25 08:11:52 +02002349 px->state == PR_STREADY ? "OPEN" :
2350 px->state == PR_STFULL ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002351 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002352 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002353 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002354
2355 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2356 if (px->mode == PR_MODE_HTTP) {
2357 int i;
2358
2359 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002360 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002361
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002362 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002363 } else {
2364 chunk_printf(&msg, ",,,,,,");
2365 }
2366
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002367 /* failed health analyses */
2368 chunk_printf(&msg, ",");
2369
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002370 /* requests : req_rate, req_rate_max, req_tot, */
2371 chunk_printf(&msg, "%u,%u,%lld,",
2372 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002373 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002374
Willy Tarreauae526782010-03-04 20:34:23 +01002375 /* errors: cli_aborts, srv_aborts */
2376 chunk_printf(&msg, ",,");
2377
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002378 /* finish with EOL */
2379 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002380 }
Willy Tarreau91861262007-10-17 17:06:05 +02002381
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002382 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002383 return 0;
2384 }
2385
Willy Tarreau295a8372011-03-10 11:25:07 +01002386 si->applet.ctx.stats.l = px->listen; /* may be NULL */
2387 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002388 /* fall through */
2389
Willy Tarreau295a8372011-03-10 11:25:07 +01002390 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002391 /* stats.l has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002392 for (; si->applet.ctx.stats.l != NULL; si->applet.ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002393 if (buffer_almost_full(rep))
2394 return 0;
2395
Willy Tarreau295a8372011-03-10 11:25:07 +01002396 l = si->applet.ctx.stats.l;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002397 if (!l->counters)
2398 continue;
2399
Willy Tarreau295a8372011-03-10 11:25:07 +01002400 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2401 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002402 break;
2403
Willy Tarreau295a8372011-03-10 11:25:07 +01002404 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002405 continue;
2406 }
2407
Willy Tarreau295a8372011-03-10 11:25:07 +01002408 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002409 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002410 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002411 /* Column sub-heading for Enable or Disable server */
2412 chunk_printf(&msg, "<td></td>");
2413 }
2414 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002415
2416 if (uri->flags&ST_SHLGNDS) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02002417 char str[INET6_ADDRSTRLEN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002418 int port;
2419
Willy Tarreau631f01c2011-09-05 00:36:48 +02002420 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002421
Willy Tarreau631f01c2011-09-05 00:36:48 +02002422 port = get_host_port(&l->addr);
2423 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2424 case AF_INET:
2425 chunk_printf(&msg, "IPv4: %s:%d, ", str, port);
2426 break;
2427 case AF_INET6:
2428 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, port);
2429 break;
2430 case AF_UNIX:
2431 chunk_printf(&msg, "unix, ");
2432 break;
2433 case -1:
2434 chunk_printf(&msg, "(%s), ", strerror(errno));
2435 break;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002436 }
2437
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002438 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002439 chunk_printf(&msg, "id: %d\"", l->luid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002440 }
2441
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002442 chunk_printf(&msg,
2443 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002444 ">%s<a name=\"%s/+%s\"></a>"
2445 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002446 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002447 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002448 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002449 "<td>%s</td><td>%s</td><td>%s</td>"
2450 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002451 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002452 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002453 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002454 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002455 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002456 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002457 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2458 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2459
2460 chunk_printf(&msg,
2461 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002462 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002463 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002464 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002465 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002466 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002467 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002468 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002469 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002470 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002471 "",
2472 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2473 U2H2(l->counters->failed_req),
Willy Tarreaua17c2d92011-07-25 08:16:20 +02002474 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002475 } else {
2476 chunk_printf(&msg,
2477 /* pxid, name, queue cur, queue max, */
2478 "%s,%s,,,"
2479 /* sessions: current, max, limit, total */
2480 "%d,%d,%d,%lld,"
2481 /* bytes: in, out */
2482 "%lld,%lld,"
2483 /* denied: req, resp */
2484 "%lld,%lld,"
2485 /* errors: request, connect, response */
2486 "%lld,,,"
2487 /* warnings: retries, redispatches */
2488 ",,"
2489 /* server status: reflect listener status */
2490 "%s,"
2491 /* rest of server: nothing */
2492 ",,,,,,,,"
2493 /* pid, iid, sid, throttle, lbtot, tracked, type */
2494 "%d,%d,%d,,,,%d,"
2495 /* rate, rate_lim, rate_max */
2496 ",,,"
2497 /* check_status, check_code, check_duration */
2498 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002499 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2500 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002501 /* failed health analyses */
2502 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002503 /* requests : req_rate, req_rate_max, req_tot, */
2504 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002505 /* errors: cli_aborts, srv_aborts */
2506 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002507 "\n",
2508 px->id, l->name,
2509 l->nbconn, l->counters->conn_max,
2510 l->maxconn, l->counters->cum_conn,
2511 l->counters->bytes_in, l->counters->bytes_out,
2512 l->counters->denied_req, l->counters->denied_resp,
2513 l->counters->failed_req,
2514 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2515 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2516 }
2517
2518 if (buffer_feed_chunk(rep, &msg) >= 0)
2519 return 0;
2520 }
2521
Willy Tarreau295a8372011-03-10 11:25:07 +01002522 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2523 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002524 /* fall through */
2525
Willy Tarreau295a8372011-03-10 11:25:07 +01002526 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002527 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002528 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002529 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 +02002530
Willy Tarreau4e33d862009-10-11 23:35:10 +02002531 if (buffer_almost_full(rep))
2532 return 0;
2533
Willy Tarreau295a8372011-03-10 11:25:07 +01002534 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002535
Willy Tarreau295a8372011-03-10 11:25:07 +01002536 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2537 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002538 break;
2539
Willy Tarreau295a8372011-03-10 11:25:07 +01002540 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002541 continue;
2542 }
2543
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002544 if (sv->tracked)
2545 svs = sv->tracked;
2546 else
2547 svs = sv;
2548
Willy Tarreau91861262007-10-17 17:06:05 +02002549 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002550 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002551 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002552 else if (svs->state & SRV_RUNNING) {
2553 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002554 sv_state = 3; /* UP */
2555 else
2556 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002557
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002558 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002559 sv_state += 2;
2560 }
Willy Tarreau91861262007-10-17 17:06:05 +02002561 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002562 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002563 sv_state = 1; /* going up */
2564 else
2565 sv_state = 0; /* DOWN */
2566
Willy Tarreau295a8372011-03-10 11:25:07 +01002567 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002568 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002569 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002570 continue;
2571 }
2572
Willy Tarreau295a8372011-03-10 11:25:07 +01002573 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002574 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2575 "UP %d/%d &darr;", "UP",
2576 "NOLB %d/%d &darr;", "NOLB",
2577 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002578 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002579 chunk_printf(&msg,
2580 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002581 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002582 );
2583 }
2584 else {
2585 chunk_printf(&msg,
2586 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002587 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002588 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2589 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002590
Willy Tarreau295a8372011-03-10 11:25:07 +01002591 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002592 chunk_printf(&msg,
2593 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2594 sv->id);
2595 }
2596
2597 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002598
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002599 if (uri->flags&ST_SHLGNDS) {
2600 char str[INET6_ADDRSTRLEN];
2601
Willy Tarreau631f01c2011-09-05 00:36:48 +02002602 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002603
Willy Tarreau631f01c2011-09-05 00:36:48 +02002604 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01002605 case AF_INET:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002606 chunk_printf(&msg, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
David du Colombier6f5ccb12011-03-10 22:26:24 +01002607 break;
2608 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002609 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2610 break;
2611 case AF_UNIX:
2612 chunk_printf(&msg, "unix, ");
2613 break;
2614 case -1:
2615 chunk_printf(&msg, "(%s), ", strerror(errno));
2616 break;
2617 default: /* address family not supported */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002618 break;
2619 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002620
2621 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002622 chunk_printf(&msg, "id: %d", sv->puid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002623
2624 /* cookie */
2625 if (sv->cookie) {
2626 struct chunk src;
2627
2628 chunk_printf(&msg, ", cookie: '");
2629
2630 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2631 chunk_htmlencode(&msg, &src);
2632
2633 chunk_printf(&msg, "'");
2634 }
2635
2636 chunk_printf(&msg, "\"");
2637 }
2638
2639 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002640 ">%s<a name=\"%s/%s\"></a>"
2641 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002642 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002643 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002644 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002645 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002646 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002647 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002648 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002649 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002650 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002651 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002652 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002653 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2654 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002655 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2656
2657 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2658 if (px->mode == PR_MODE_HTTP) {
2659 int i;
2660
2661 chunk_printf(&msg, " title=\"rsp codes:");
2662
2663 for (i = 1; i < 6; i++)
2664 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2665
2666 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2667 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002668
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002669 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002670 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002671 ">%s%s%s</td><td>%s</td>",
2672 (px->mode == PR_MODE_HTTP)?"<u>":"",
2673 U2H0(sv->counters.cum_sess),
2674 (px->mode == PR_MODE_HTTP)?"</u>":"",
2675 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002676
2677 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002678 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002679 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002680 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002681 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002682 /* errors : request, connect */
2683 "<td></td><td>%s</td>"
2684 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002685 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002686 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002687 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002688 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002689 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2690 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002691 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002692 sv->counters.cli_aborts,
2693 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002694 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002695 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002696
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002697 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002698 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002699
Cyril Bontécd19e512010-01-31 22:34:03 +01002700 if (sv->state & SRV_MAINTAIN) {
2701 chunk_printf(&msg, "%s ",
2702 human_time(now.tv_sec - sv->last_change, 1));
2703 chunk_printf(&msg, "MAINT");
2704 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002705 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2706 chunk_printf(&msg, "%s ",
2707 human_time(now.tv_sec - svs->last_change, 1));
2708 chunk_printf(&msg, "MAINT(via)");
2709 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002710 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002711 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002712 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002713
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002714 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002715 srv_hlt_st[sv_state],
2716 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2717 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002718 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002719
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002720 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002721 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002722 get_check_status_description(sv->check_status));
2723
2724 if (*sv->check_desc) {
2725 struct chunk src;
2726
2727 chunk_printf(&msg, ": ");
2728
2729 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2730 chunk_htmlencode(&msg, &src);
2731 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002732
Willy Tarreaue0454092010-02-26 12:29:07 +01002733 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002734 tv_iszero(&sv->check_start)?"":"* ",
2735 get_check_status_info(sv->check_status));
2736
2737 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002738 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002739
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002740 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002741 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002742 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002743 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002744
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002745 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002746 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002747 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002748 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002749 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002750 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002751 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002752 (sv->state & SRV_BACKUP) ? "-" : "Y",
2753 (sv->state & SRV_BACKUP) ? "Y" : "-");
2754
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002755 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002756 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002757 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002758 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2759
2760 if (svs->observe)
2761 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2762
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002763 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002764 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002765 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002766 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002767 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002768 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002769 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002770 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2771 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002772 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002773 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002774 "<td colspan=3></td>");
2775
2776 /* throttle */
2777 if ((sv->state & SRV_WARMINGUP) &&
2778 now.tv_sec < sv->last_change + sv->slowstart &&
2779 now.tv_sec >= sv->last_change) {
2780 unsigned int ratio;
2781 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002782 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002783 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002784 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002785 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002786 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002787 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002788 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002789 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2790 "UP %d/%d,", "UP,",
2791 "NOLB %d/%d,", "NOLB,",
2792 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002793 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002794 /* pxid, name */
2795 "%s,%s,"
2796 /* queue : current, max */
2797 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002798 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002799 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002800 /* bytes : in, out */
2801 "%lld,%lld,"
2802 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002803 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002804 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002805 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002806 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002807 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002808 "",
2809 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002810 sv->nbpend, sv->counters.nbpend_max,
2811 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002812 sv->counters.bytes_in, sv->counters.bytes_out,
2813 sv->counters.failed_secu,
2814 sv->counters.failed_conns, sv->counters.failed_resp,
2815 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002816
Willy Tarreau55bb8452007-10-17 18:44:57 +02002817 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002818 if (sv->state & SRV_MAINTAIN) {
2819 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002820 }
2821 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2822 chunk_printf(&msg, "MAINT(via),");
2823 }
2824 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002825 chunk_printf(&msg,
2826 srv_hlt_st[sv_state],
2827 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2828 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2829 }
Willy Tarreau91861262007-10-17 17:06:05 +02002830
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002831 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002832 /* weight, active, backup */
2833 "%d,%d,%d,"
2834 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002835 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002836 (sv->state & SRV_BACKUP) ? 0 : 1,
2837 (sv->state & SRV_BACKUP) ? 1 : 0);
2838
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002839 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002840 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002841 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002842 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002843 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002844 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002845 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002846 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002847 ",,,,");
2848
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002849 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002850 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002851 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002852 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002853 LIM2A0(sv->maxqueue, ""),
2854 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002855
2856 /* throttle */
2857 if ((sv->state & SRV_WARMINGUP) &&
2858 now.tv_sec < sv->last_change + sv->slowstart &&
2859 now.tv_sec >= sv->last_change) {
2860 unsigned int ratio;
2861 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002862 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002863 }
2864
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002865 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002866 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002867
2868 /* tracked */
2869 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002870 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002871 sv->tracked->proxy->id, sv->tracked->id);
2872 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002873 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002874
Willy Tarreau7f062c42009-03-05 18:43:00 +01002875 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002876 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002877
2878 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002879 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002880 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002881 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002882
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002883 if (sv->state & SRV_CHECKED) {
2884 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002885 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002886
2887 /* check_code */
2888 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002889 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002890 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002891 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002892
2893 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002894 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002895 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002896 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002897 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002898
2899 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002900 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002901 }
2902
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002903 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2904 if (px->mode == PR_MODE_HTTP) {
2905 int i;
2906
2907 for (i=1; i<6; i++)
2908 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2909
2910 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2911 } else {
2912 chunk_printf(&msg, ",,,,,,");
2913 }
2914
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002915 /* failed health analyses */
2916 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2917
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002918 /* requests : req_rate, req_rate_max, req_tot, */
2919 chunk_printf(&msg, ",,,");
2920
Willy Tarreauae526782010-03-04 20:34:23 +01002921 /* errors: cli_aborts, srv_aborts */
2922 chunk_printf(&msg, "%lld,%lld,",
2923 sv->counters.cli_aborts, sv->counters.srv_aborts);
2924
Willy Tarreau7f062c42009-03-05 18:43:00 +01002925 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002926 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002927 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002928 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002929 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002930 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002931
Willy Tarreau295a8372011-03-10 11:25:07 +01002932 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02002933 /* fall through */
2934
Willy Tarreau295a8372011-03-10 11:25:07 +01002935 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02002936 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002937 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002938 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2939 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002940 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01002941 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002942 /* Column sub-heading for Enable or Disable server */
2943 chunk_printf(&msg, "<td></td>");
2944 }
2945 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002946
2947 if (uri->flags&ST_SHLGNDS) {
2948 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002949 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002950 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002951
2952 /* cookie */
2953 if (px->cookie_name) {
2954 struct chunk src;
2955
2956 chunk_printf(&msg, ", cookie: '");
2957
2958 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2959 chunk_htmlencode(&msg, &src);
2960
2961 chunk_printf(&msg, "'");
2962 }
2963
2964 chunk_printf(&msg, "\"");
2965
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002966 }
2967
2968 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002969 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01002970 ">%s<a name=\"%s/Backend\"></a>"
2971 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002972 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002973 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002974 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002975 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002976 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002977 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002978 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002979 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002980 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
2981 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02002982
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002983 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002984 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002985 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002986 "<td"
2987 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002988 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002989
2990 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2991 if (px->mode == PR_MODE_HTTP) {
2992 int i;
2993
2994 chunk_printf(&msg, " title=\"rsp codes:");
2995
2996 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002997 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002998
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002999 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003000 }
3001
3002 chunk_printf(&msg,
3003 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01003004 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003005 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003006 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003007 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003008 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003009 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01003010 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003011 U2H7(px->be_counters.cum_lbconn),
3012 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003013
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003014 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02003015 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003016 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01003017 /* errors : request, connect */
3018 "<td></td><td>%s</td>"
3019 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01003020 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003021 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003022 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003023 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02003024 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003025 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02003026 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003027 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3028 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003029 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003030 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
3031 U2H2(px->be_counters.failed_conns),
3032 px->be_counters.cli_aborts,
3033 px->be_counters.srv_aborts,
3034 U2H5(px->be_counters.failed_resp),
3035 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003036 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01003037 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3038 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01003039 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003040 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003041
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003042 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003043 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003044 "<td class=ac>&nbsp;</td><td>%d</td>"
3045 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003046 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003047 "</tr>",
3048 px->down_trans,
3049 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003050 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003051 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02003052 /* pxid, name */
3053 "%s,BACKEND,"
3054 /* queue : current, max */
3055 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003056 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003057 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003058 /* bytes : in, out */
3059 "%lld,%lld,"
3060 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003061 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003062 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003063 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003064 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003065 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003066 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02003067 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003068 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02003069 * active and backups. */
3070 "%s,"
3071 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003072 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003073 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003074 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003075 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003076 /* rate, rate_lim, rate_max, */
3077 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003078 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003079 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02003080 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003081 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3082 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3083 px->be_counters.bytes_in, px->be_counters.bytes_out,
3084 px->be_counters.denied_req, px->be_counters.denied_resp,
3085 px->be_counters.failed_conns, px->be_counters.failed_resp,
3086 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01003087 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01003088 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003089 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003090 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01003091 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003092 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003093 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003094 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003095 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003096
3097 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3098 if (px->mode == PR_MODE_HTTP) {
3099 int i;
3100
3101 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003102 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003103
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003104 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003105 } else {
3106 chunk_printf(&msg, ",,,,,,");
3107 }
3108
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003109 /* failed health analyses */
3110 chunk_printf(&msg, ",");
3111
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003112 /* requests : req_rate, req_rate_max, req_tot, */
3113 chunk_printf(&msg, ",,,");
3114
Willy Tarreauae526782010-03-04 20:34:23 +01003115 /* errors: cli_aborts, srv_aborts */
3116 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003117 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01003118
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003119 /* finish with EOL */
3120 chunk_printf(&msg, "\n");
3121
Willy Tarreau55bb8452007-10-17 18:44:57 +02003122 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003123 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02003124 return 0;
3125 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003126
Willy Tarreau295a8372011-03-10 11:25:07 +01003127 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02003128 /* fall through */
3129
Willy Tarreau295a8372011-03-10 11:25:07 +01003130 case STAT_PX_ST_END:
3131 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003132 chunk_printf(&msg, "</table>");
3133
Willy Tarreau295a8372011-03-10 11:25:07 +01003134 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003135 /* close the form used to enable/disable this proxy servers */
3136 chunk_printf(&msg,
3137 "Choose the action to perform on the checked servers : "
3138 "<select name=action>"
3139 "<option value=\"\"></option>"
3140 "<option value=\"disable\">Disable</option>"
3141 "<option value=\"enable\">Enable</option>"
3142 "</select>"
3143 "<input type=\"hidden\" name=\"b\" value=\"%s\">"
3144 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3145 "</form>",
3146 px->id);
3147 }
3148
3149 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02003150
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003151 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003152 return 0;
3153 }
Willy Tarreau91861262007-10-17 17:06:05 +02003154
Willy Tarreau295a8372011-03-10 11:25:07 +01003155 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02003156 /* fall through */
3157
Willy Tarreau295a8372011-03-10 11:25:07 +01003158 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02003159 return 1;
3160
3161 default:
3162 /* unknown state, we should put an abort() here ! */
3163 return 1;
3164 }
3165}
3166
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003167/* This function dumps a complete session state onto the stream intreface's
3168 * read buffer. The data_ctx must have been zeroed first, and the flags
3169 * properly set. The session has to be set in data_ctx.sess.target. It returns
3170 * 0 if the output buffer is full and it needs to be called again, otherwise
3171 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003172 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003173static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003174{
3175 struct tm tm;
3176 struct chunk msg;
3177 struct session *sess;
3178 extern const char *monthname[12];
3179 char pn[INET6_ADDRSTRLEN];
3180
3181 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau295a8372011-03-10 11:25:07 +01003182 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003183
Willy Tarreau295a8372011-03-10 11:25:07 +01003184 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003185 /* session changed, no need to go any further */
3186 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003187 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003188 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003189 si->applet.ctx.sess.target = NULL;
3190 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003191 return 1;
3192 }
3193
Willy Tarreau295a8372011-03-10 11:25:07 +01003194 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003195 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003196 si->applet.ctx.sess.uid = sess->uniq_id;
3197 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003198 /* fall through */
3199
3200 case 1:
3201 chunk_printf(&msg,
3202 "%p: id=%u, proto=%s",
3203 sess,
3204 sess->uniq_id,
3205 sess->listener->proto->name);
3206
Willy Tarreau6471afb2011-09-23 10:54:59 +02003207 switch (addr_to_str(&sess->si[0].addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003208 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003209 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003210 chunk_printf(&msg, " source=%s:%d\n",
Willy Tarreau6471afb2011-09-23 10:54:59 +02003211 pn, get_host_port(&sess->si[0].addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003212 break;
3213 case AF_UNIX:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003214 chunk_printf(&msg, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003215 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003216 default:
3217 /* no more information to print right now */
3218 chunk_printf(&msg, "\n");
3219 break;
3220 }
3221
3222 chunk_printf(&msg,
3223 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003224 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003225
3226 chunk_printf(&msg,
3227 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)\n",
3228 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3229 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3230 sess->listener ? sess->listener->luid : 0);
3231
Willy Tarreau50943332011-09-02 17:33:05 +02003232 if (sess->be->cap & PR_CAP_BE)
3233 chunk_printf(&msg,
3234 " backend=%s (id=%u mode=%s) server=%s (id=%u)\n",
3235 sess->be->id,
3236 sess->be->uuid, sess->be->mode ? "http" : "tcp",
3237 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
3238 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
3239 else
3240 chunk_printf(&msg, " backend=<NONE> (id=-1 mode=-) server=<NONE> (id=-1)\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003241
3242 chunk_printf(&msg,
3243 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
3244 sess->task,
3245 sess->task->state,
3246 sess->task->nice, sess->task->calls,
3247 sess->task->expire ?
3248 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3249 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3250 TICKS_TO_MS(1000)) : "<NEVER>",
3251 task_in_rq(sess->task) ? ", running" : "");
3252
3253 get_localtime(sess->logs.accept_date.tv_sec, &tm);
3254 chunk_printf(&msg,
3255 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
3256 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3257 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3258 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3259
3260 chunk_printf(&msg,
3261 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3262 &sess->si[0],
3263 sess->si[0].state,
3264 sess->si[0].flags,
3265 sess->si[0].fd,
3266 sess->si[0].exp ?
3267 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3268 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3269 TICKS_TO_MS(1000)) : "<NEVER>",
3270 sess->si[0].err_type);
3271
3272 chunk_printf(&msg,
3273 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3274 &sess->si[1],
3275 sess->si[1].state,
3276 sess->si[1].flags,
3277 sess->si[1].fd,
3278 sess->si[1].exp ?
3279 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3280 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3281 TICKS_TO_MS(1000)) : "<NEVER>",
3282 sess->si[1].err_type);
3283
3284 chunk_printf(&msg,
3285 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3286 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3287 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3288
3289
3290 chunk_printf(&msg,
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02003291 " req=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003292 " an_exp=%s",
3293 sess->req,
3294 sess->req->flags, sess->req->analysers,
3295 sess->req->l, sess->req->send_max,
3296 sess->req->pipe ? sess->req->pipe->data : 0,
3297 sess->req->to_forward,
3298 sess->req->analyse_exp ?
3299 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3300 TICKS_TO_MS(1000)) : "<NEVER>");
3301
3302 chunk_printf(&msg,
3303 " rex=%s",
3304 sess->req->rex ?
3305 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3306 TICKS_TO_MS(1000)) : "<NEVER>");
3307
3308 chunk_printf(&msg,
3309 " wex=%s\n"
3310 " data=%p r=%d w=%d lr=%d total=%lld\n",
3311 sess->req->wex ?
3312 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3313 TICKS_TO_MS(1000)) : "<NEVER>",
3314 sess->req->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01003315 (int)(sess->req->r - sess->req->data),
3316 (int)(sess->req->w - sess->req->data),
3317 (int)(sess->req->lr - sess->req->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003318 sess->req->total);
3319
3320 chunk_printf(&msg,
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02003321 " res=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003322 " an_exp=%s",
3323 sess->rep,
3324 sess->rep->flags, sess->rep->analysers,
3325 sess->rep->l, sess->rep->send_max,
3326 sess->rep->pipe ? sess->rep->pipe->data : 0,
3327 sess->rep->to_forward,
3328 sess->rep->analyse_exp ?
3329 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3330 TICKS_TO_MS(1000)) : "<NEVER>");
3331
3332 chunk_printf(&msg,
3333 " rex=%s",
3334 sess->rep->rex ?
3335 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3336 TICKS_TO_MS(1000)) : "<NEVER>");
3337
3338 chunk_printf(&msg,
3339 " wex=%s\n"
3340 " data=%p r=%d w=%d lr=%d total=%lld\n",
3341 sess->rep->wex ?
3342 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3343 TICKS_TO_MS(1000)) : "<NEVER>",
3344 sess->rep->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01003345 (int)(sess->rep->r - sess->rep->data),
3346 (int)(sess->rep->w - sess->rep->data),
3347 (int)(sess->rep->lr - sess->rep->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003348 sess->rep->total);
3349
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003350 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003351 return 0;
3352
3353 /* use other states to dump the contents */
3354 }
3355 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003356 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003357 return 1;
3358}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003359
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003360/* This function dumps all sessions' states onto the stream intreface's
3361 * read buffer. The data_ctx must have been zeroed first, and the flags
3362 * properly set. It returns 0 if the output buffer is full and it needs
3363 * to be called again, otherwise non-zero. It is designed to be called
3364 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003365 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003366static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003367{
3368 struct chunk msg;
3369
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003370 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003371 /* If we're forced to shut down, we might have to remove our
3372 * reference to the last session being dumped.
3373 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003374 if (si->applet.state == STAT_ST_LIST) {
3375 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3376 LIST_DEL(&si->applet.ctx.sess.bref.users);
3377 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003378 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003379 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003380 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003381 }
3382
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003383 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003384
Willy Tarreau295a8372011-03-10 11:25:07 +01003385 switch (si->applet.state) {
3386 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003387 /* the function had not been called yet, let's prepare the
3388 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003389 * pointer to the first in the global list. When a target
3390 * session is being destroyed, it is responsible for updating
3391 * this pointer. We know we have reached the end when this
3392 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003393 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003394 LIST_INIT(&si->applet.ctx.sess.bref.users);
3395 si->applet.ctx.sess.bref.ref = sessions.n;
3396 si->applet.state = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003397 /* fall through */
3398
Willy Tarreau295a8372011-03-10 11:25:07 +01003399 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003400 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003401 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3402 LIST_DEL(&si->applet.ctx.sess.bref.users);
3403 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003404 }
3405
3406 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003407 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003408 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003409 struct session *curr_sess;
3410
Willy Tarreau295a8372011-03-10 11:25:07 +01003411 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003412
Willy Tarreau295a8372011-03-10 11:25:07 +01003413 if (si->applet.ctx.sess.target) {
3414 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003415 goto next_sess;
3416
Willy Tarreau295a8372011-03-10 11:25:07 +01003417 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003418 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003419 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003420 return 0;
3421
3422 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003423 LIST_DEL(&si->applet.ctx.sess.bref.users);
3424 LIST_INIT(&si->applet.ctx.sess.bref.users);
3425 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003426 break;
3427 }
3428
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003429 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003430 "%p: proto=%s",
3431 curr_sess,
3432 curr_sess->listener->proto->name);
3433
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003434
Willy Tarreau6471afb2011-09-23 10:54:59 +02003435 switch (addr_to_str(&curr_sess->si[0].addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003436 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003437 case AF_INET6:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003438 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003439 " src=%s:%d fe=%s be=%s srv=%s",
3440 pn,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003441 get_host_port(&curr_sess->si[0].addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003442 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003443 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003444 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003445 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003446 break;
3447 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02003448 chunk_printf(&msg,
3449 " src=unix:%d fe=%s be=%s srv=%s",
3450 curr_sess->listener->luid,
3451 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003452 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003453 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003454 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003455 break;
3456 }
3457
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003458 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003459 " ts=%02x age=%s calls=%d",
3460 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003461 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3462 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003463
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003464 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003465 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
3466 curr_sess->req->flags,
3467 curr_sess->req->l,
3468 curr_sess->req->analysers,
3469 curr_sess->req->rex ?
3470 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3471 TICKS_TO_MS(1000)) : "");
3472
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003473 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003474 ",wx=%s",
3475 curr_sess->req->wex ?
3476 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3477 TICKS_TO_MS(1000)) : "");
3478
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003479 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003480 ",ax=%s]",
3481 curr_sess->req->analyse_exp ?
3482 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3483 TICKS_TO_MS(1000)) : "");
3484
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003485 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003486 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
3487 curr_sess->rep->flags,
3488 curr_sess->rep->l,
3489 curr_sess->rep->analysers,
3490 curr_sess->rep->rex ?
3491 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3492 TICKS_TO_MS(1000)) : "");
3493
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003494 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003495 ",wx=%s",
3496 curr_sess->rep->wex ?
3497 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3498 TICKS_TO_MS(1000)) : "");
3499
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003500 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003501 ",ax=%s]",
3502 curr_sess->rep->analyse_exp ?
3503 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3504 TICKS_TO_MS(1000)) : "");
3505
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003506 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003507 " s0=[%d,%1xh,fd=%d,ex=%s]",
3508 curr_sess->si[0].state,
3509 curr_sess->si[0].flags,
3510 curr_sess->si[0].fd,
3511 curr_sess->si[0].exp ?
3512 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3513 TICKS_TO_MS(1000)) : "");
3514
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003515 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003516 " s1=[%d,%1xh,fd=%d,ex=%s]",
3517 curr_sess->si[1].state,
3518 curr_sess->si[1].flags,
3519 curr_sess->si[1].fd,
3520 curr_sess->si[1].exp ?
3521 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3522 TICKS_TO_MS(1000)) : "");
3523
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003524 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003525 " exp=%s",
3526 curr_sess->task->expire ?
3527 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3528 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003529 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003530 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003531
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003532 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003533
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003534 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003535 /* let's try again later from this session. We add ourselves into
3536 * this session's users so that it can remove us upon termination.
3537 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003538 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003539 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003540 }
3541
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003542 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003543 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003544 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003545
Willy Tarreau295a8372011-03-10 11:25:07 +01003546 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003547 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003548 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003549 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3550 else
3551 chunk_printf(&msg, "Session not found.\n");
3552
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003553 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003554 return 0;
3555
Willy Tarreau295a8372011-03-10 11:25:07 +01003556 si->applet.ctx.sess.target = NULL;
3557 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003558 return 1;
3559 }
3560
Willy Tarreau295a8372011-03-10 11:25:07 +01003561 si->applet.state = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003562 /* fall through */
3563
3564 default:
Willy Tarreau295a8372011-03-10 11:25:07 +01003565 si->applet.state = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003566 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003567 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003568}
3569
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003570/* This function dumps all tables' states onto the stream intreface's
3571 * read buffer. The data_ctx must have been zeroed first, and the flags
3572 * properly set. It returns 0 if the output buffer is full and it needs
3573 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003574 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003575static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003576{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003577 struct session *s = si->applet.private;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003578 struct chunk msg;
3579 struct ebmb_node *eb;
3580 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003581 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003582
3583 /*
Willy Tarreau295a8372011-03-10 11:25:07 +01003584 * We have 3 possible states in si->applet.state :
3585 * - STAT_ST_INIT : the first call
3586 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003587 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003588 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003589 * and the entry pointer points to the next entry to be dumped,
3590 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003591 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003592 * data though.
3593 */
3594
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003595 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003596 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau295a8372011-03-10 11:25:07 +01003597 if (si->applet.state == STAT_ST_LIST) {
3598 si->applet.ctx.table.entry->ref_cnt--;
3599 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003600 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003601 return 1;
3602 }
3603
3604 chunk_init(&msg, trash, sizeof(trash));
3605
Willy Tarreau295a8372011-03-10 11:25:07 +01003606 while (si->applet.state != STAT_ST_FIN) {
3607 switch (si->applet.state) {
3608 case STAT_ST_INIT:
3609 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3610 if (!si->applet.ctx.table.proxy)
3611 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003612
Willy Tarreau295a8372011-03-10 11:25:07 +01003613 si->applet.ctx.table.entry = NULL;
3614 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003615 break;
3616
Willy Tarreau295a8372011-03-10 11:25:07 +01003617 case STAT_ST_INFO:
3618 if (!si->applet.ctx.table.proxy ||
3619 (si->applet.ctx.table.target &&
3620 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
3621 si->applet.state = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003622 break;
3623 }
3624
Willy Tarreau295a8372011-03-10 11:25:07 +01003625 if (si->applet.ctx.table.proxy->table.size) {
Simon Hormanc88b8872011-06-15 15:18:49 +09003626 if (show && !stats_dump_table_head_to_buffer(&msg, si, si->applet.ctx.table.proxy,
3627 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003628 return 0;
3629
Willy Tarreau295a8372011-03-10 11:25:07 +01003630 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003631 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
3632 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003633 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003634 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003635 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3636 si->applet.ctx.table.entry->ref_cnt++;
3637 si->applet.state = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003638 break;
3639 }
3640 }
3641 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003642 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003643 break;
3644
Willy Tarreau295a8372011-03-10 11:25:07 +01003645 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003646 skip_entry = false;
3647
Willy Tarreau295a8372011-03-10 11:25:07 +01003648 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003649 /* we're filtering on some data contents */
3650 void *ptr;
3651 long long data;
3652
Willy Tarreau295a8372011-03-10 11:25:07 +01003653 dt = si->applet.ctx.table.data_type;
3654 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3655 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003656 dt);
3657
3658 data = 0;
3659 switch (stktable_data_types[dt].std_type) {
3660 case STD_T_SINT:
3661 data = stktable_data_cast(ptr, std_t_sint);
3662 break;
3663 case STD_T_UINT:
3664 data = stktable_data_cast(ptr, std_t_uint);
3665 break;
3666 case STD_T_ULL:
3667 data = stktable_data_cast(ptr, std_t_ull);
3668 break;
3669 case STD_T_FRQP:
3670 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003671 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003672 break;
3673 }
3674
3675 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003676 if ((data < si->applet.ctx.table.value &&
3677 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3678 si->applet.ctx.table.data_op == STD_OP_GT ||
3679 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3680 (data == si->applet.ctx.table.value &&
3681 (si->applet.ctx.table.data_op == STD_OP_NE ||
3682 si->applet.ctx.table.data_op == STD_OP_GT ||
3683 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3684 (data > si->applet.ctx.table.value &&
3685 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3686 si->applet.ctx.table.data_op == STD_OP_LT ||
3687 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003688 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003689 }
3690
Simon Hormanc88b8872011-06-15 15:18:49 +09003691 if (show && !skip_entry &&
3692 !stats_dump_table_entry_to_buffer(&msg, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003693 si->applet.ctx.table.entry))
3694 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003695
Willy Tarreau295a8372011-03-10 11:25:07 +01003696 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003697
Willy Tarreau295a8372011-03-10 11:25:07 +01003698 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003699 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003700 struct stksess *old = si->applet.ctx.table.entry;
3701 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3702 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3703 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003704 break;
3705 }
3706
Simon Hormanc88b8872011-06-15 15:18:49 +09003707
3708 if (show)
3709 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3710 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3711 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3712
Willy Tarreau295a8372011-03-10 11:25:07 +01003713 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
3714 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003715 break;
3716
Willy Tarreau295a8372011-03-10 11:25:07 +01003717 case STAT_ST_END:
3718 si->applet.state = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003719 break;
3720 }
3721 }
3722 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003723}
3724
Willy Tarreaud426a182010-03-05 14:58:26 +01003725/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003726 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3727 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3728 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3729 * lines are respected within the limit of 70 output chars. Lines that are
3730 * continuation of a previous truncated line begin with "+" instead of " "
3731 * after the offset. The new pointer is returned.
3732 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003733static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3734 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003735{
3736 int end;
3737 unsigned char c;
3738
3739 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003740 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003741 return ptr;
3742
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003743 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003744
Willy Tarreaud426a182010-03-05 14:58:26 +01003745 while (ptr < len && ptr < bsize) {
3746 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003747 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003748 if (out->len > end - 2)
3749 break;
3750 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003751 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003752 if (out->len > end - 3)
3753 break;
3754 out->str[out->len++] = '\\';
3755 switch (c) {
3756 case '\t': c = 't'; break;
3757 case '\n': c = 'n'; break;
3758 case '\r': c = 'r'; break;
3759 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003760 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003761 }
3762 out->str[out->len++] = c;
3763 } else {
3764 if (out->len > end - 5)
3765 break;
3766 out->str[out->len++] = '\\';
3767 out->str[out->len++] = 'x';
3768 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3769 out->str[out->len++] = hextab[c & 0xF];
3770 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003771 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003772 /* we had a line break, let's return now */
3773 out->str[out->len++] = '\n';
3774 *line = ptr;
3775 return ptr;
3776 }
3777 }
3778 /* we have an incomplete line, we return it as-is */
3779 out->str[out->len++] = '\n';
3780 return ptr;
3781}
3782
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003783/* This function dumps all captured errors onto the stream intreface's
3784 * read buffer. The data_ctx must have been zeroed first, and the flags
3785 * properly set. It returns 0 if the output buffer is full and it needs
3786 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003787 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003788static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003789{
3790 extern const char *monthname[12];
3791 struct chunk msg;
3792
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003793 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003794 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003795
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003796 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003797
Willy Tarreau295a8372011-03-10 11:25:07 +01003798 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003799 /* the function had not been called yet, let's prepare the
3800 * buffer for a response.
3801 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003802 struct tm tm;
3803
3804 get_localtime(date.tv_sec, &tm);
3805 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3806 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3807 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3808 error_snapshot_id);
3809
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003810 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003811 /* Socket buffer full. Let's try again later from the same point */
3812 return 0;
3813 }
3814
Willy Tarreau295a8372011-03-10 11:25:07 +01003815 si->applet.ctx.errors.px = proxy;
3816 si->applet.ctx.errors.buf = 0;
3817 si->applet.ctx.errors.bol = 0;
3818 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003819 }
3820
3821 /* we have two inner loops here, one for the proxy, the other one for
3822 * the buffer.
3823 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003824 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003825 struct error_snapshot *es;
3826
Willy Tarreau295a8372011-03-10 11:25:07 +01003827 if (si->applet.ctx.errors.buf == 0)
3828 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003829 else
Willy Tarreau295a8372011-03-10 11:25:07 +01003830 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003831
3832 if (!es->when.tv_sec)
3833 goto next;
3834
Willy Tarreau295a8372011-03-10 11:25:07 +01003835 if (si->applet.ctx.errors.iid >= 0 &&
3836 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
3837 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003838 goto next;
3839
Willy Tarreau295a8372011-03-10 11:25:07 +01003840 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003841 /* just print headers now */
3842
3843 char pn[INET6_ADDRSTRLEN];
3844 struct tm tm;
3845
3846 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003847 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003848 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003849 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003850
Willy Tarreau631f01c2011-09-05 00:36:48 +02003851 addr_to_str(&es->src, pn, sizeof(pn));
Willy Tarreau295a8372011-03-10 11:25:07 +01003852 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003853 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003854 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003855 " frontend %s (#%d): invalid request\n"
3856 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003857 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003858 " request length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003859 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau3fd6cec2010-03-25 06:45:07 +01003860 pn, es->sid, (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3861 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003862 es->srv ? es->srv->id : "<NONE>",
3863 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003864 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003865 es->len, es->pos);
3866 break;
3867 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003868 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003869 " backend %s (#%d) : invalid response\n"
3870 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003871 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003872 " response length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003873 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003874 pn, es->sid, es->oe->id, es->oe->uuid,
3875 es->srv ? es->srv->id : "<NONE>",
3876 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003877 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003878 es->len, es->pos);
3879 break;
3880 }
3881
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003882 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003883 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003884 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003885 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003886 si->applet.ctx.errors.ptr = 0;
3887 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003888 }
3889
Willy Tarreau295a8372011-03-10 11:25:07 +01003890 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003891 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003892 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003893 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003894 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau61b34732009-10-03 23:49:35 +02003895 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003896 goto next;
3897 }
3898
3899 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01003900 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003901 int newptr;
3902 int newline;
3903
Willy Tarreau295a8372011-03-10 11:25:07 +01003904 newline = si->applet.ctx.errors.bol;
3905 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
3906 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02003907 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003908
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003909 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003910 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003911 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003912 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003913 si->applet.ctx.errors.ptr = newptr;
3914 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003915 };
3916 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01003917 si->applet.ctx.errors.bol = 0;
3918 si->applet.ctx.errors.ptr = -1;
3919 si->applet.ctx.errors.buf++;
3920 if (si->applet.ctx.errors.buf > 1) {
3921 si->applet.ctx.errors.buf = 0;
3922 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003923 }
3924 }
3925
3926 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02003927 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003928}
3929
Willy Tarreaub24281b2011-02-13 13:16:36 +01003930struct si_applet http_stats_applet = {
3931 .name = "<STATS>", /* used for logging */
3932 .fct = http_stats_io_handler,
3933};
3934
Simon Horman9bd2c732011-06-15 15:18:44 +09003935static struct si_applet cli_applet = {
Willy Tarreaub24281b2011-02-13 13:16:36 +01003936 .name = "<CLI>", /* used for logging */
3937 .fct = cli_io_handler,
3938};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003939
Willy Tarreau10522fd2008-07-09 20:12:41 +02003940static struct cfg_kw_list cfg_kws = {{ },{
3941 { CFG_GLOBAL, "stats", stats_parse_global },
3942 { 0, NULL, NULL },
3943}};
3944
3945__attribute__((constructor))
3946static void __dumpstats_module_init(void)
3947{
3948 cfg_register_keywords(&cfg_kws);
3949}
3950
Willy Tarreau91861262007-10-17 17:06:05 +02003951/*
3952 * Local variables:
3953 * c-indent-level: 8
3954 * c-basic-offset: 8
3955 * End:
3956 */