blob: 96999852c8b867cbfb09cd238ee35c5ffea12705 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaueb472682010-05-28 18:46:57 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
Simon Horman17bce342011-06-15 15:18:47 +090017#include <stdbool.h>
Simon Hormancec9a222011-06-15 15:18:51 +090018#include <stdint.h>
Willy Tarreau91861262007-10-17 17:06:05 +020019#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020022#include <pwd.h>
23#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020024
25#include <sys/socket.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28
Willy Tarreau10522fd2008-07-09 20:12:41 +020029#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020030#include <common/compat.h>
31#include <common/config.h>
32#include <common/debug.h>
33#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
40
Willy Tarreau91861262007-10-17 17:06:05 +020041#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020042
43#include <proto/backend.h>
44#include <proto/buffers.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020045#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020046#include <proto/dumpstats.h>
47#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010048#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020049#include <proto/log.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010050#include <proto/pipe.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020051#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020052#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020053#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020054#include <proto/server.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010055#include <proto/stream_interface.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020056#include <proto/stream_sock.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010057#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020058
Simon Horman9bd2c732011-06-15 15:18:44 +090059static int stats_dump_raw_to_buffer(struct stream_interface *si);
60static int stats_dump_full_sess_to_buffer(struct stream_interface *si);
61static int stats_dump_sess_to_buffer(struct stream_interface *si);
62static int stats_dump_errors_to_buffer(struct stream_interface *si);
Simon Hormanc88b8872011-06-15 15:18:49 +090063static int stats_table_request(struct stream_interface *si, bool show);
Simon Horman9bd2c732011-06-15 15:18:44 +090064static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
65static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri);
66
67static struct si_applet cli_applet;
68
69static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020070 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +020071 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +020072 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020073 " help : this message\n"
74 " prompt : toggle interactive mode with prompt\n"
75 " quit : disconnect\n"
76 " show info : report information about the running process\n"
77 " show stat : report counters for each proxy and server\n"
78 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +010079 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +020080 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +020081 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +020082 " set weight : change a server's weight\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +010083 " set timeout : change a timeout setting\n"
Cyril Bontécd19e512010-01-31 22:34:03 +010084 " disable server : set a server in maintenance mode\n"
85 " enable server : re-enable a server that was previously in maintenance mode\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020086 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020087
Simon Horman9bd2c732011-06-15 15:18:44 +090088static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +020089 "Permission denied\n"
90 "";
91
Willy Tarreau295a8372011-03-10 11:25:07 +010092/* data transmission states for the stats responses */
93enum {
94 STAT_ST_INIT = 0,
95 STAT_ST_HEAD,
96 STAT_ST_INFO,
97 STAT_ST_LIST,
98 STAT_ST_END,
99 STAT_ST_FIN,
100};
101
102/* data transmission states for the stats responses inside a proxy */
103enum {
104 STAT_PX_ST_INIT = 0,
105 STAT_PX_ST_TH,
106 STAT_PX_ST_FE,
107 STAT_PX_ST_LI,
108 STAT_PX_ST_SV,
109 STAT_PX_ST_BE,
110 STAT_PX_ST_END,
111 STAT_PX_ST_FIN,
112};
113
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200114/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +0200115 * a new stats socket. It returns a positive value upon success, 0 if the connection
116 * needs to be closed and ignored, or a negative value upon critical failure.
117 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900118static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200119{
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200120 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100121 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200122 copy_target(&s->target, &s->si[1].target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +0100123 s->si[1].applet.private = s;
124 s->si[1].applet.st1 = 0;
125 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200126
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200127 tv_zero(&s->logs.tv_request);
128 s->logs.t_queue = 0;
129 s->logs.t_connect = 0;
130 s->logs.t_data = 0;
131 s->logs.t_close = 0;
132 s->logs.bytes_in = s->logs.bytes_out = 0;
133 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
134 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200135
Willy Tarreaueb472682010-05-28 18:46:57 +0200136 s->req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
137
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200138 if (s->listener->timeout) {
139 s->req->rto = *s->listener->timeout;
140 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200141 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200142 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200143}
144
Willy Tarreau07e9e642010-08-17 21:48:17 +0200145/* allocate a new stats frontend named <name>, and return it
146 * (or NULL in case of lack of memory).
147 */
148static struct proxy *alloc_stats_fe(const char *name)
149{
150 struct proxy *fe;
151
152 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
153 if (!fe)
154 return NULL;
155
156 LIST_INIT(&fe->pendconns);
157 LIST_INIT(&fe->acl);
158 LIST_INIT(&fe->block_cond);
159 LIST_INIT(&fe->redirect_rules);
160 LIST_INIT(&fe->mon_fail_cond);
161 LIST_INIT(&fe->switching_rules);
162 LIST_INIT(&fe->tcp_req.inspect_rules);
163
164 /* Timeouts are defined as -1, so we cannot use the zeroed area
165 * as a default value.
166 */
167 proxy_reset_timeouts(fe);
168
169 fe->last_change = now.tv_sec;
170 fe->id = strdup("GLOBAL");
171 fe->cap = PR_CAP_FE;
172 return fe;
173}
174
Willy Tarreaufbee7132007-10-18 13:53:22 +0200175/* This function parses a "stats" statement in the "global" section. It returns
176 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
177 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
178 * zero included. The trailing '\n' must not be written. The function must be
179 * called with <args> pointing to the first word after "stats".
180 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200181static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
182 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200183{
Willy Tarreau10522fd2008-07-09 20:12:41 +0200184 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200185 if (!strcmp(args[0], "socket")) {
Andreas Kohn16171e22011-01-19 20:29:32 +0100186 struct sockaddr_un *su;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200187 int cur_arg;
188
189 if (*args[1] == 0) {
190 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
191 return -1;
192 }
193
194 if (global.stats_sock.state != LI_NEW) {
195 snprintf(err, errlen, "'stats socket' already specified in global section");
196 return -1;
197 }
198
Andreas Kohn16171e22011-01-19 20:29:32 +0100199 su = str2sun(args[1]);
200 if (!su) {
201 snprintf(err, errlen, "'stats socket' path would require truncation");
202 return -1;
203 }
204 memcpy(&global.stats_sock.addr, su, sizeof(struct sockaddr_un)); // guaranteed to fit
Willy Tarreaufbee7132007-10-18 13:53:22 +0200205
Willy Tarreau89a63132009-08-16 17:41:45 +0200206 if (!global.stats_fe) {
Willy Tarreau07e9e642010-08-17 21:48:17 +0200207 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau89a63132009-08-16 17:41:45 +0200208 snprintf(err, errlen, "out of memory");
209 return -1;
210 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200211 global.stats_fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreau89a63132009-08-16 17:41:45 +0200212 }
213
Willy Tarreaufbee7132007-10-18 13:53:22 +0200214 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100215 global.stats_sock.options = LI_O_NONE;
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200216 global.stats_sock.accept = session_accept;
217 global.stats_fe->accept = stats_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200218 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200219 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200220 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreaueb472682010-05-28 18:46:57 +0200221 global.stats_sock.frontend = global.stats_fe;
Willy Tarreau6162db22009-10-10 17:13:00 +0200222 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau07e9e642010-08-17 21:48:17 +0200223 global.stats_fe->maxconn = global.stats_sock.maxconn;
Willy Tarreau89a63132009-08-16 17:41:45 +0200224 global.stats_sock.timeout = &global.stats_fe->timeout.client;
225
226 global.stats_sock.next = global.stats_fe->listen;
227 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200228
229 cur_arg = 2;
230 while (*args[cur_arg]) {
231 if (!strcmp(args[cur_arg], "uid")) {
232 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
233 cur_arg += 2;
234 }
235 else if (!strcmp(args[cur_arg], "gid")) {
236 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
237 cur_arg += 2;
238 }
239 else if (!strcmp(args[cur_arg], "mode")) {
240 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
241 cur_arg += 2;
242 }
243 else if (!strcmp(args[cur_arg], "user")) {
244 struct passwd *user;
245 user = getpwnam(args[cur_arg + 1]);
246 if (!user) {
247 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
248 args[cur_arg + 1]);
249 return -1;
250 }
251 global.stats_sock.perm.ux.uid = user->pw_uid;
252 cur_arg += 2;
253 }
254 else if (!strcmp(args[cur_arg], "group")) {
255 struct group *group;
256 group = getgrnam(args[cur_arg + 1]);
257 if (!group) {
258 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
259 args[cur_arg + 1]);
260 return -1;
261 }
262 global.stats_sock.perm.ux.gid = group->gr_gid;
263 cur_arg += 2;
264 }
Willy Tarreau6162db22009-10-10 17:13:00 +0200265 else if (!strcmp(args[cur_arg], "level")) {
266 if (!strcmp(args[cur_arg+1], "user"))
267 global.stats_sock.perm.ux.level = ACCESS_LVL_USER;
268 else if (!strcmp(args[cur_arg+1], "operator"))
269 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER;
270 else if (!strcmp(args[cur_arg+1], "admin"))
271 global.stats_sock.perm.ux.level = ACCESS_LVL_ADMIN;
272 else {
273 snprintf(err, errlen, "'stats socket level' only supports 'user', 'operator', and 'admin'");
274 return -1;
275 }
276 cur_arg += 2;
277 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200278 else {
Willy Tarreau6162db22009-10-10 17:13:00 +0200279 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', 'level', and 'mode'");
Willy Tarreaufbee7132007-10-18 13:53:22 +0200280 return -1;
281 }
282 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100283
Willy Tarreaufbee7132007-10-18 13:53:22 +0200284 uxst_add_listener(&global.stats_sock);
285 global.maxsock++;
286 }
287 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100288 unsigned timeout;
289 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
290
291 if (res) {
292 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
293 return -1;
294 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200295
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100296 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200297 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
298 return -1;
299 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200300 if (!global.stats_fe) {
301 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
302 snprintf(err, errlen, "out of memory");
303 return -1;
304 }
305 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200306 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200307 }
308 else if (!strcmp(args[0], "maxconn")) {
309 int maxconn = atol(args[1]);
310
311 if (maxconn <= 0) {
312 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
313 return -1;
314 }
315 global.maxsock -= global.stats_sock.maxconn;
316 global.stats_sock.maxconn = maxconn;
317 global.maxsock += global.stats_sock.maxconn;
Willy Tarreaueb472682010-05-28 18:46:57 +0200318 if (global.stats_fe)
319 global.stats_fe->maxconn = global.stats_sock.maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200320 }
321 else {
322 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
323 return -1;
324 }
325 return 0;
326}
327
Simon Horman9bd2c732011-06-15 15:18:44 +0900328static int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100329{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200330 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100331 "# pxname,svname,"
332 "qcur,qmax,"
333 "scur,smax,slim,stot,"
334 "bin,bout,"
335 "dreq,dresp,"
336 "ereq,econ,eresp,"
337 "wretr,wredis,"
338 "status,weight,act,bck,"
339 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200340 "pid,iid,sid,throttle,lbtot,tracked,type,"
341 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200342 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100343 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100344 "req_rate,req_rate_max,req_tot,"
345 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100346 "\n");
347}
348
Simon Hormand9366582011-06-15 15:18:45 +0900349/* print a string of text buffer to <out>. The format is :
350 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
351 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
352 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
353 */
354static int dump_text(struct chunk *out, const char *buf, int bsize)
355{
356 unsigned char c;
357 int ptr = 0;
358
359 while (buf[ptr] && ptr < bsize) {
360 c = buf[ptr];
361 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
362 if (out->len > out->size - 1)
363 break;
364 out->str[out->len++] = c;
365 }
366 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
367 if (out->len > out->size - 2)
368 break;
369 out->str[out->len++] = '\\';
370 switch (c) {
371 case ' ': c = ' '; break;
372 case '\t': c = 't'; break;
373 case '\n': c = 'n'; break;
374 case '\r': c = 'r'; break;
375 case '\e': c = 'e'; break;
376 case '\\': c = '\\'; break;
377 }
378 out->str[out->len++] = c;
379 }
380 else {
381 if (out->len > out->size - 4)
382 break;
383 out->str[out->len++] = '\\';
384 out->str[out->len++] = 'x';
385 out->str[out->len++] = hextab[(c >> 4) & 0xF];
386 out->str[out->len++] = hextab[c & 0xF];
387 }
388 ptr++;
389 }
390
391 return ptr;
392}
393
394/* print a buffer in hexa.
395 * Print stopped if <bsize> is reached, or if no more place in the chunk.
396 */
397static int dump_binary(struct chunk *out, const char *buf, int bsize)
398{
399 unsigned char c;
400 int ptr = 0;
401
402 while (ptr < bsize) {
403 c = buf[ptr];
404
405 if (out->len > out->size - 2)
406 break;
407 out->str[out->len++] = hextab[(c >> 4) & 0xF];
408 out->str[out->len++] = hextab[c & 0xF];
409
410 ptr++;
411 }
412 return ptr;
413}
414
415/* Dump the status of a table to a stream interface's
416 * read buffer. It returns 0 if the output buffer is full
417 * and needs to be called again, otherwise non-zero.
418 */
419static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
420 struct proxy *proxy, struct proxy *target)
421{
422 struct session *s = si->applet.private;
423
424 chunk_printf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
425 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
426
427 /* any other information should be dumped here */
428
429 if (target && s->listener->perm.ux.level < ACCESS_LVL_OPER)
430 chunk_printf(msg, "# contents not dumped due to insufficient privileges\n");
431
432 if (buffer_feed_chunk(si->ib, msg) >= 0)
433 return 0;
434
435 return 1;
436}
437
438/* Dump the a table entry to a stream interface's
439 * read buffer. It returns 0 if the output buffer is full
440 * and needs to be called again, otherwise non-zero.
441 */
442static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
443 struct proxy *proxy, struct stksess *entry)
444{
445 int dt;
446
447 chunk_printf(msg, "%p:", entry);
448
449 if (proxy->table.type == STKTABLE_TYPE_IP) {
450 char addr[INET_ADDRSTRLEN];
451 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
452 chunk_printf(msg, " key=%s", addr);
453 }
454 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
455 char addr[INET6_ADDRSTRLEN];
456 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
457 chunk_printf(msg, " key=%s", addr);
458 }
459 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
460 chunk_printf(msg, " key=%u", *(unsigned int *)entry->key.key);
461 }
462 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
463 chunk_printf(msg, " key=");
464 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
465 }
466 else {
467 chunk_printf(msg, " key=");
468 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
469 }
470
471 chunk_printf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
472
473 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
474 void *ptr;
475
476 if (proxy->table.data_ofs[dt] == 0)
477 continue;
478 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
479 chunk_printf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
480 else
481 chunk_printf(msg, " %s=", stktable_data_types[dt].name);
482
483 ptr = stktable_data_ptr(&proxy->table, entry, dt);
484 switch (stktable_data_types[dt].std_type) {
485 case STD_T_SINT:
486 chunk_printf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
487 break;
488 case STD_T_UINT:
489 chunk_printf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
490 break;
491 case STD_T_ULL:
492 chunk_printf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
493 break;
494 case STD_T_FRQP:
495 chunk_printf(msg, "%d",
496 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
497 proxy->table.data_arg[dt].u));
498 break;
499 }
500 }
501 chunk_printf(msg, "\n");
502
503 if (buffer_feed_chunk(si->ib, msg) >= 0)
504 return 0;
505
506 return 1;
507}
508
Simon Horman17bce342011-06-15 15:18:47 +0900509static void stats_sock_table_key_request(struct stream_interface *si, char **args, bool show)
Simon Horman121f3052011-06-15 15:18:46 +0900510{
511 struct session *s = si->applet.private;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900512 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900513 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900514 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900515 unsigned char ip6_key[sizeof(struct in6_addr)];
Simon Horman121f3052011-06-15 15:18:46 +0900516
Simon Hormand5b9fd92011-06-15 15:18:48 +0900517 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900518
519 if (!*args[4]) {
520 si->applet.ctx.cli.msg = "Key value expected\n";
521 si->applet.st0 = STAT_CLI_PRINT;
522 return;
523 }
524
Simon Hormanc5b89f62011-06-15 15:18:50 +0900525 switch (px->table.type) {
526 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900527 uint32_key = htonl(inetaddr_host(args[4]));
528 static_table_key.key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900529 break;
530 case STKTABLE_TYPE_IPV6:
531 inet_pton(AF_INET6, args[4], ip6_key);
532 static_table_key.key = &ip6_key;
533 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900534 case STKTABLE_TYPE_INTEGER:
535 {
536 char *endptr;
537 unsigned long val;
538 errno = 0;
539 val = strtoul(args[4], &endptr, 10);
540 if ((errno == ERANGE && val == ULONG_MAX) ||
541 (errno != 0 && val == 0) || endptr == args[4] ||
542 val > 0xffffffff) {
543 si->applet.ctx.cli.msg = "Invalid key\n";
544 si->applet.st0 = STAT_CLI_PRINT;
545 return;
546 }
547 uint32_key = (uint32_t) val;
548 static_table_key.key = &uint32_key;
549 break;
550 }
551 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900552 case STKTABLE_TYPE_STRING:
553 static_table_key.key = args[4];
554 static_table_key.key_len = strlen(args[4]);
555 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900556 default:
Simon Horman17bce342011-06-15 15:18:47 +0900557 if (show)
Simon Hormancec9a222011-06-15 15:18:51 +0900558 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 +0900559 else
Simon Hormancec9a222011-06-15 15:18:51 +0900560 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 +0900561 si->applet.st0 = STAT_CLI_PRINT;
562 return;
563 }
564
565 /* check permissions */
566 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
567 si->applet.ctx.cli.msg = stats_permission_denied_msg;
568 si->applet.st0 = STAT_CLI_PRINT;
569 return;
570 }
571
572 ts = stktable_lookup_key(&px->table, &static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900573 if (!ts)
574 return;
575
576 if (show) {
577 struct chunk msg;
578 chunk_init(&msg, trash, sizeof(trash));
579 if (!stats_dump_table_head_to_buffer(&msg, si, px, px))
580 return;
581 stats_dump_table_entry_to_buffer(&msg, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900582 return;
583 }
Simon Horman17bce342011-06-15 15:18:47 +0900584
585 if (ts->ref_cnt) {
Simon Horman121f3052011-06-15 15:18:46 +0900586 /* don't delete an entry which is currently referenced */
587 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
588 si->applet.st0 = STAT_CLI_PRINT;
589 return;
590 }
591
592 stksess_kill(&px->table, ts);
593}
594
Simon Hormand5b9fd92011-06-15 15:18:48 +0900595static void stats_sock_table_data_request(struct stream_interface *si, char **args)
596{
597 /* condition on stored data value */
598 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
599 if (si->applet.ctx.table.data_type < 0) {
600 si->applet.ctx.cli.msg = "Unknown data type\n";
601 si->applet.st0 = STAT_CLI_PRINT;
602 return;
603 }
604
605 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
606 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
607 si->applet.st0 = STAT_CLI_PRINT;
608 return;
609 }
610
611 si->applet.ctx.table.data_op = get_std_op(args[4]);
612 if (si->applet.ctx.table.data_op < 0) {
613 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
614 si->applet.st0 = STAT_CLI_PRINT;
615 return;
616 }
617
618 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
619 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
620 si->applet.st0 = STAT_CLI_PRINT;
621 return;
622 }
623}
624
625static void stats_sock_table_request(struct stream_interface *si, char **args, bool show)
626{
627 si->applet.ctx.table.data_type = -1;
628 si->applet.state = STAT_ST_INIT;
629 si->applet.ctx.table.proxy = NULL;
630 si->applet.ctx.table.entry = NULL;
Simon Hormanc88b8872011-06-15 15:18:49 +0900631 if (show)
632 si->applet.st0 = STAT_CLI_O_TAB;
633 else
634 si->applet.st0 = STAT_CLI_O_CLR;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900635
636 if (*args[2]) {
637 si->applet.ctx.table.target = find_stktable(args[2]);
638 if (!si->applet.ctx.table.target) {
639 si->applet.ctx.cli.msg = "No such table\n";
640 si->applet.st0 = STAT_CLI_PRINT;
641 return;
642 }
643 }
644 else {
645 if (!show)
646 goto err_args;
647 return;
648 }
649
650 if (strcmp(args[3], "key") == 0)
651 stats_sock_table_key_request(si, args, show);
Simon Hormanc88b8872011-06-15 15:18:49 +0900652 else if (strncmp(args[3], "data.", 5) == 0)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900653 stats_sock_table_data_request(si, args);
Simon Hormanc88b8872011-06-15 15:18:49 +0900654 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900655 goto err_args;
656
657 return;
658
659err_args:
660 if (show)
661 si->applet.ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
662 else
Simon Hormanc88b8872011-06-15 15:18:49 +0900663 si->applet.ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
Simon Hormand5b9fd92011-06-15 15:18:48 +0900664 si->applet.st0 = STAT_CLI_PRINT;
665}
666
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200667/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200668 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100669 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200670 * designating the function which will have to process the request, which can
671 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200672 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900673static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200674{
Willy Tarreaubc4af052011-02-13 13:25:14 +0100675 struct session *s = si->applet.private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200676 char *args[MAX_STATS_ARGS + 1];
677 int arg;
678
679 while (isspace((unsigned char)*line))
680 line++;
681
682 arg = 0;
683 args[arg] = line;
684
685 while (*line && arg < MAX_STATS_ARGS) {
686 if (isspace((unsigned char)*line)) {
687 *line++ = '\0';
688
689 while (isspace((unsigned char)*line))
690 line++;
691
692 args[++arg] = line;
693 continue;
694 }
695
696 line++;
697 }
698
699 while (++arg <= MAX_STATS_ARGS)
700 args[arg] = line;
701
Willy Tarreau295a8372011-03-10 11:25:07 +0100702 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200703 if (strcmp(args[0], "show") == 0) {
704 if (strcmp(args[1], "stat") == 0) {
705 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100706 si->applet.ctx.stats.flags |= STAT_BOUND;
707 si->applet.ctx.stats.iid = atoi(args[2]);
708 si->applet.ctx.stats.type = atoi(args[3]);
709 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200710 }
711
Willy Tarreau295a8372011-03-10 11:25:07 +0100712 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
713 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
714 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100715 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200716 }
717 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100718 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
719 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
720 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100721 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200722 }
723 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100724 si->applet.state = STAT_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200725 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100726 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100727 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200728 return 1;
729 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100730 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100731 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100732 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100733 si->applet.ctx.sess.target = NULL;
734 si->applet.ctx.sess.section = 0; /* start with session status */
735 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100736 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200737 }
738 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200739 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100740 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100741 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200742 return 1;
743 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200744 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100745 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200746 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100747 si->applet.ctx.errors.iid = -1;
748 si->applet.ctx.errors.px = NULL;
749 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100750 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200751 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200752 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900753 stats_sock_table_request(si, args, true);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200754 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200755 else { /* neither "stat" nor "info" nor "sess" nor "errors" no "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200756 return 0;
757 }
758 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200759 else if (strcmp(args[0], "clear") == 0) {
760 if (strcmp(args[1], "counters") == 0) {
761 struct proxy *px;
762 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200763 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200764 int clrall = 0;
765
766 if (strcmp(args[2], "all") == 0)
767 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200768
Willy Tarreau6162db22009-10-10 17:13:00 +0200769 /* check permissions */
770 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
771 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100772 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100773 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200774 return 1;
775 }
776
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200777 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100778 if (clrall) {
779 memset(&px->be_counters, 0, sizeof(px->be_counters));
780 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
781 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200782 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100783 px->be_counters.conn_max = 0;
784 px->be_counters.p.http.rps_max = 0;
785 px->be_counters.sps_max = 0;
786 px->be_counters.cps_max = 0;
787 px->be_counters.nbpend_max = 0;
788
789 px->fe_counters.conn_max = 0;
790 px->fe_counters.p.http.rps_max = 0;
791 px->fe_counters.sps_max = 0;
792 px->fe_counters.cps_max = 0;
793 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200794 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200795
796 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200797 if (clrall)
798 memset(&sv->counters, 0, sizeof(sv->counters));
799 else {
800 sv->counters.cur_sess_max = 0;
801 sv->counters.nbpend_max = 0;
802 sv->counters.sps_max = 0;
803 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200804
805 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200806 if (li->counters) {
807 if (clrall)
808 memset(li->counters, 0, sizeof(*li->counters));
809 else
810 li->counters->conn_max = 0;
811 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200812 }
813
814 return 1;
815 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200816 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900817 stats_sock_table_request(si, args, false);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200818 /* end of processing */
819 return 1;
820 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200821 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200822 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200823 return 0;
824 }
825 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200826 else if (strcmp(args[0], "get") == 0) {
827 if (strcmp(args[1], "weight") == 0) {
828 struct proxy *px;
829 struct server *sv;
830
831 /* split "backend/server" and make <line> point to server */
832 for (line = args[2]; *line; line++)
833 if (*line == '/') {
834 *line++ = '\0';
835 break;
836 }
837
838 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100839 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100840 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200841 return 1;
842 }
843
844 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100845 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100846 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200847 return 1;
848 }
849
850 /* return server's effective weight at the moment */
851 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
852 buffer_feed(si->ib, trash);
853 return 1;
854 }
855 else { /* not "get weight" */
856 return 0;
857 }
858 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200859 else if (strcmp(args[0], "set") == 0) {
860 if (strcmp(args[1], "weight") == 0) {
861 struct proxy *px;
862 struct server *sv;
863 int w;
864
865 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100866 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100867 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200868 return 1;
869 }
870
871 /* split "backend/server" and make <line> point to server */
872 for (line = args[2]; *line; line++)
873 if (*line == '/') {
874 *line++ = '\0';
875 break;
876 }
877
878 if (!*line || !*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100879 si->applet.ctx.cli.msg = "Require 'backend/server' and 'weight' or 'weight%'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100880 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200881 return 1;
882 }
883
884 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100885 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100886 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200887 return 1;
888 }
889
Cyril Bonté613f0df2011-03-03 20:49:04 +0100890 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100891 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100892 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100893 return 1;
894 }
895
Willy Tarreau4483d432009-10-10 19:30:08 +0200896 /* if the weight is terminated with '%', it is set relative to
897 * the initial weight, otherwise it is absolute.
898 */
899 w = atoi(args[3]);
900 if (strchr(args[3], '%') != NULL) {
901 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100902 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100903 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200904 return 1;
905 }
906 w = sv->iweight * w / 100;
907 }
908 else {
909 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100910 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100911 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200912 return 1;
913 }
914 }
915
916 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100917 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 +0100918 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200919 return 1;
920 }
921
922 sv->uweight = w;
923
924 if (px->lbprm.algo & BE_LB_PROP_DYN) {
925 /* we must take care of not pushing the server to full throttle during slow starts */
926 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
927 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
928 else
929 sv->eweight = BE_WEIGHT_SCALE;
930 sv->eweight *= sv->uweight;
931 } else {
932 sv->eweight = sv->uweight;
933 }
934
935 /* static LB algorithms are a bit harder to update */
936 if (px->lbprm.update_server_eweight)
937 px->lbprm.update_server_eweight(sv);
938 else if (sv->eweight)
939 px->lbprm.set_server_status_up(sv);
940 else
941 px->lbprm.set_server_status_down(sv);
942
943 return 1;
944 }
Willy Tarreau7aabd112010-01-26 10:59:06 +0100945 else if (strcmp(args[1], "timeout") == 0) {
946 if (strcmp(args[2], "cli") == 0) {
947 unsigned timeout;
948 const char *res;
949
950 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100951 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100952 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100953 return 1;
954 }
955
956 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
957 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100958 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100959 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100960 return 1;
961 }
962
963 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
964 return 1;
965 }
966 else {
Willy Tarreau295a8372011-03-10 11:25:07 +0100967 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100968 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100969 return 1;
970 }
971 }
972 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +0200973 return 0;
974 }
975 }
Cyril Bontécd19e512010-01-31 22:34:03 +0100976 else if (strcmp(args[0], "enable") == 0) {
977 if (strcmp(args[1], "server") == 0) {
978 struct proxy *px;
979 struct server *sv;
980
981 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100982 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100983 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100984 return 1;
985 }
986
987 /* split "backend/server" and make <line> point to server */
988 for (line = args[2]; *line; line++)
989 if (*line == '/') {
990 *line++ = '\0';
991 break;
992 }
993
994 if (!*line || !*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100995 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100996 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100997 return 1;
998 }
999
1000 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001001 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001002 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001003 return 1;
1004 }
1005
Cyril Bonté613f0df2011-03-03 20:49:04 +01001006 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001007 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001008 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001009 return 1;
1010 }
1011
Cyril Bontécd19e512010-01-31 22:34:03 +01001012 if (sv->state & SRV_MAINTAIN) {
1013 /* The server is really in maintenance, we can change the server state */
1014 if (sv->tracked) {
1015 /* If this server tracks the status of another one,
1016 * we must restore the good status.
1017 */
1018 if (sv->tracked->state & SRV_RUNNING) {
1019 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001020 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001021 } else {
1022 sv->state &= ~SRV_MAINTAIN;
1023 set_server_down(sv);
1024 }
1025 } else {
1026 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001027 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001028 }
1029 }
1030
1031 return 1;
1032 }
1033 else { /* unknown "enable" parameter */
1034 return 0;
1035 }
1036 }
1037 else if (strcmp(args[0], "disable") == 0) {
1038 if (strcmp(args[1], "server") == 0) {
1039 struct proxy *px;
1040 struct server *sv;
1041
1042 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001043 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001044 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001045 return 1;
1046 }
1047
1048 /* split "backend/server" and make <line> point to server */
1049 for (line = args[2]; *line; line++)
1050 if (*line == '/') {
1051 *line++ = '\0';
1052 break;
1053 }
1054
1055 if (!*line || !*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001056 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001057 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001058 return 1;
1059 }
1060
1061 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001062 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001063 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001064 return 1;
1065 }
1066
Cyril Bonté613f0df2011-03-03 20:49:04 +01001067 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001068 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001069 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001070 return 1;
1071 }
1072
Cyril Bontécd19e512010-01-31 22:34:03 +01001073 if (! (sv->state & SRV_MAINTAIN)) {
1074 /* Not already in maintenance, we can change the server state */
1075 sv->state |= SRV_MAINTAIN;
1076 set_server_down(sv);
1077 }
1078
1079 return 1;
1080 }
1081 else { /* unknown "disable" parameter */
1082 return 0;
1083 }
1084 }
1085 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001086 return 0;
1087 }
1088 return 1;
1089}
1090
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001091/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001092 * used to processes I/O from/to the stats unix socket. The system relies on a
1093 * state machine handling requests and various responses. We read a request,
1094 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001095 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1096 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001097 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001098 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001099static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001100{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001101 struct buffer *req = si->ob;
1102 struct buffer *res = si->ib;
1103 int reql;
1104 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001105
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001106 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1107 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001108
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001109 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001110 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001111 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001112 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001113 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001114 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001115 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001116 /* Let's close for real now. We just close the request
1117 * side, the conditions below will complete if needed.
1118 */
1119 si->shutw(si);
1120 break;
1121 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001122 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001123 /* ensure we have some output room left in the event we
1124 * would want to return some info right after parsing.
1125 */
1126 if (buffer_almost_full(si->ib))
1127 break;
1128
Willy Tarreau74b08c92010-09-08 17:04:31 +02001129 reql = buffer_get_line(si->ob, trash, sizeof(trash));
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001130 if (reql <= 0) { /* closed or EOL not found */
1131 if (reql == 0)
1132 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001133 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001134 continue;
1135 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001136
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001137 /* seek for a possible semi-colon. If we find one, we
1138 * replace it with an LF and skip only this part.
1139 */
1140 for (len = 0; len < reql; len++)
1141 if (trash[len] == ';') {
1142 trash[len] = '\n';
1143 reql = len + 1;
1144 break;
1145 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001146
Willy Tarreau816fc222009-10-04 07:36:58 +02001147 /* now it is time to check that we have a full line,
1148 * remove the trailing \n and possibly \r, then cut the
1149 * line.
1150 */
1151 len = reql - 1;
1152 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001153 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001154 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001155 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001156
Willy Tarreau816fc222009-10-04 07:36:58 +02001157 if (len && trash[len-1] == '\r')
1158 len--;
1159
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001160 trash[len] = '\0';
1161
Willy Tarreaubc4af052011-02-13 13:25:14 +01001162 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001163 if (len) {
1164 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001165 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001166 continue;
1167 }
1168 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001169 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001170 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001171 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001172 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001173 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001174 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001175 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001176 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001177 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001178 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001179 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001180 /* if prompt is disabled, print help on empty lines,
1181 * so that the user at least knows how to enable
1182 * prompt and find help.
1183 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001184 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001185 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001186 }
1187
1188 /* re-adjust req buffer */
1189 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001190 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001191 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001192 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001193 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001194 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001195 continue;
1196 }
1197
Willy Tarreaubc4af052011-02-13 13:25:14 +01001198 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001199 case STAT_CLI_PRINT:
Willy Tarreau295a8372011-03-10 11:25:07 +01001200 if (buffer_feed(si->ib, si->applet.ctx.cli.msg) < 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001201 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001202 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001203 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001204 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001205 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001206 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001207 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001208 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001209 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001210 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001211 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001212 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001213 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001214 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001215 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001216 if (stats_table_request(si, true))
1217 si->applet.st0 = STAT_CLI_PROMPT;
1218 break;
1219 case STAT_CLI_O_CLR:
1220 if (stats_table_request(si, false))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001221 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001222 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001223 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001224 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001225 break;
1226 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001227
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001228 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001229 if (si->applet.st0 == STAT_CLI_PROMPT) {
1230 if (buffer_feed(si->ib, si->applet.st1 ? "\n> " : "\n") < 0)
1231 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001232 }
1233
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001234 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001235 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001236 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001237
1238 /* Now we close the output if one of the writers did so,
1239 * or if we're not in interactive mode and the request
1240 * buffer is empty. This still allows pipelined requests
1241 * to be sent in non-interactive mode.
1242 */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001243 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->applet.st1 && !req->send_max)) {
1244 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001245 continue;
1246 }
1247
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001248 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001249 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001250 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001251 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001252
Willy Tarreaubc4af052011-02-13 13:25:14 +01001253 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001254 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1255 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1256 /* Other size has closed, let's abort if we have no more processing to do
1257 * and nothing more to consume. This is comparable to a broken pipe, so
1258 * we forward the close to the request side so that it flows upstream to
1259 * the client.
1260 */
1261 si->shutw(si);
1262 }
1263
Willy Tarreaubc4af052011-02-13 13:25:14 +01001264 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001265 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1266 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1267 /* We have no more processing to do, and nothing more to send, and
1268 * the client side has closed. So we'll forward this state downstream
1269 * on the response buffer.
1270 */
1271 si->shutr(si);
1272 res->flags |= BF_READ_NULL;
1273 }
1274
1275 /* update all other flags and resync with the other side */
1276 si->update(si);
1277
1278 /* we don't want to expire timeouts while we're processing requests */
1279 si->ib->rex = TICK_ETERNITY;
1280 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001281
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001282 out:
1283 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
1284 __FUNCTION__, __LINE__,
1285 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
1286
1287 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1288 /* check that we have released everything then unregister */
1289 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001290 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001291}
1292
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001293/* This function dumps statistics onto the stream interface's read buffer.
1294 * The data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001295 * It returns 0 as long as it does not complete, non-zero upon completion.
1296 * Some states are not used but it makes the code more similar to other
1297 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001298 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001299static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001300{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001301 struct proxy *px;
1302 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001303 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001304
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001305 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +02001306
Willy Tarreau295a8372011-03-10 11:25:07 +01001307 switch (si->applet.state) {
1308 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001309 /* the function had not been called yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001310 si->applet.state = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001311 /* fall through */
1312
Willy Tarreau295a8372011-03-10 11:25:07 +01001313 case STAT_ST_HEAD:
1314 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001315 print_csv_header(&msg);
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001316 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001317 return 0;
1318 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001319
Willy Tarreau295a8372011-03-10 11:25:07 +01001320 si->applet.state = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001321 /* fall through */
1322
Willy Tarreau295a8372011-03-10 11:25:07 +01001323 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001324 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001325 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001326 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001327 "Name: " PRODUCT_NAME "\n"
1328 "Version: " HAPROXY_VERSION "\n"
1329 "Release_date: " HAPROXY_DATE "\n"
1330 "Nbproc: %d\n"
1331 "Process_num: %d\n"
1332 "Pid: %d\n"
1333 "Uptime: %dd %dh%02dm%02ds\n"
1334 "Uptime_sec: %d\n"
1335 "Memmax_MB: %d\n"
1336 "Ulimit-n: %d\n"
1337 "Maxsock: %d\n"
1338 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001339 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001340 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001341 "PipesUsed: %d\n"
1342 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001343 "Tasks: %d\n"
1344 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001345 "node: %s\n"
1346 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001347 "",
1348 global.nbproc,
1349 relative_pid,
1350 pid,
1351 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1352 up,
1353 global.rlimit_memmax,
1354 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001355 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001356 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001357 nb_tasks_cur, run_queue_cur,
1358 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001359 );
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001360 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001361 return 0;
1362 }
1363
Willy Tarreau295a8372011-03-10 11:25:07 +01001364 si->applet.ctx.stats.px = proxy;
1365 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1366 si->applet.ctx.stats.sv = NULL;
1367 si->applet.state = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001368 /* fall through */
1369
Willy Tarreau295a8372011-03-10 11:25:07 +01001370 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001371 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001372 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1373 while (si->applet.ctx.stats.px) {
1374 px = si->applet.ctx.stats.px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001375 /* skip the disabled proxies and non-networked ones */
1376 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001377 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001378 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001379 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001380 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001381
Willy Tarreau295a8372011-03-10 11:25:07 +01001382 si->applet.ctx.stats.px = px->next;
1383 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001384 }
1385 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001386 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001387
Willy Tarreau295a8372011-03-10 11:25:07 +01001388 si->applet.state = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001389 /* fall through */
1390
Willy Tarreau295a8372011-03-10 11:25:07 +01001391 case STAT_ST_END:
1392 si->applet.state = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001393 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001394
Willy Tarreau295a8372011-03-10 11:25:07 +01001395 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001396 return 1;
1397
1398 default:
1399 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001400 si->applet.state = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001401 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001402 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001403}
1404
1405
Cyril Bonté70be45d2010-10-12 00:14:35 +02001406/* We don't want to land on the posted stats page because a refresh will
1407 * repost the data. We don't want this to happen on accident so we redirect
1408 * the browse to the stats page with a GET.
1409 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001410static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001411{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001412 struct session *s = si->applet.private;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001413 struct chunk msg;
1414
1415 chunk_init(&msg, trash, sizeof(trash));
1416
Willy Tarreau295a8372011-03-10 11:25:07 +01001417 switch (si->applet.state) {
1418 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001419 chunk_printf(&msg,
1420 "HTTP/1.0 303 See Other\r\n"
1421 "Cache-Control: no-cache\r\n"
1422 "Content-Type: text/plain\r\n"
1423 "Connection: close\r\n"
1424 "Location: %s;st=%s",
Willy Tarreau295a8372011-03-10 11:25:07 +01001425 uri->uri_prefix, si->applet.ctx.stats.st_code);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001426 chunk_printf(&msg, "\r\n\r\n");
1427
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001428 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001429 return 0;
1430
1431 s->txn.status = 303;
1432
1433 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1434 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1435 if (!(s->flags & SN_FINST_MASK))
1436 s->flags |= SN_FINST_R;
1437
Willy Tarreau295a8372011-03-10 11:25:07 +01001438 si->applet.state = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001439 return 1;
1440 }
1441 return 1;
1442}
1443
1444
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001445/* This I/O handler runs as an applet embedded in a stream interface. It is
1446 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001447 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001448 * automatically unregisters itself once transfer is complete.
1449 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001450static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001451{
Willy Tarreaubc4af052011-02-13 13:25:14 +01001452 struct session *s = si->applet.private;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001453 struct buffer *req = si->ob;
1454 struct buffer *res = si->ib;
1455
1456 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1457 goto out;
1458
1459 /* check that the output is not closed */
1460 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001461 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001462
Willy Tarreaubc4af052011-02-13 13:25:14 +01001463 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001464 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001465 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001466 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001467 si->shutw(si);
1468 }
1469 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001470 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001471 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001472 si->shutw(si);
1473 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001474 }
1475 }
1476
1477 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
1478 si->shutw(si);
1479
Willy Tarreaubc4af052011-02-13 13:25:14 +01001480 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001481 si->shutr(si);
1482 res->flags |= BF_READ_NULL;
1483 }
1484
1485 /* update all other flags and resync with the other side */
1486 si->update(si);
1487
1488 /* we don't want to expire timeouts while we're processing requests */
1489 si->ib->rex = TICK_ETERNITY;
1490 si->ob->wex = TICK_ETERNITY;
1491
1492 out:
1493 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1494 /* check that we have released everything then unregister */
1495 stream_int_unregister_handler(si);
1496 }
1497}
1498
1499
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001500/* This function dumps statistics in HTTP format onto the stream interface's
1501 * read buffer. The data_ctx must have been zeroed first, and the flags
1502 * properly set. It returns 0 if it had to stop writing data and an I/O is
1503 * needed, 1 if the dump is finished and the session must be closed, or -1
1504 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001505 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001506static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001507{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001508 struct session *s = si->applet.private;
1509 struct buffer *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001510 struct proxy *px;
1511 struct chunk msg;
1512 unsigned int up;
1513
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001514 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001515
Willy Tarreau295a8372011-03-10 11:25:07 +01001516 switch (si->applet.state) {
1517 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001518 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001519 "HTTP/1.0 200 OK\r\n"
1520 "Cache-Control: no-cache\r\n"
1521 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001522 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001523 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001524
Willy Tarreau295a8372011-03-10 11:25:07 +01001525 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001526 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001527 uri->refresh);
1528
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001529 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001530
1531 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001532 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001533 return 0;
1534
Willy Tarreau91861262007-10-17 17:06:05 +02001535 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1536 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1537 if (!(s->flags & SN_FINST_MASK))
1538 s->flags |= SN_FINST_R;
1539
1540 if (s->txn.meth == HTTP_METH_HEAD) {
1541 /* that's all we return in case of HEAD request */
Willy Tarreau295a8372011-03-10 11:25:07 +01001542 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001543 return 1;
1544 }
1545
Willy Tarreau295a8372011-03-10 11:25:07 +01001546 si->applet.state = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001547 /* fall through */
1548
Willy Tarreau295a8372011-03-10 11:25:07 +01001549 case STAT_ST_HEAD:
1550 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001551 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001552 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001553 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1554 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001555 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001556 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1557 "<style type=\"text/css\"><!--\n"
1558 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001559 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001560 " font-size: 12px;"
1561 " font-weight: normal;"
1562 " color: black;"
1563 " background: white;"
1564 "}\n"
1565 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001566 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001567 "}\n"
1568 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001569 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001570 " margin-bottom: 0.5em;"
1571 "}\n"
1572 "h2 {"
1573 " font-family: helvetica, arial;"
1574 " font-size: x-large;"
1575 " font-weight: bold;"
1576 " font-style: italic;"
1577 " color: #6020a0;"
1578 " margin-top: 0em;"
1579 " margin-bottom: 0em;"
1580 "}\n"
1581 "h3 {"
1582 " font-family: helvetica, arial;"
1583 " font-size: 16px;"
1584 " font-weight: bold;"
1585 " color: #b00040;"
1586 " background: #e8e8d0;"
1587 " margin-top: 0em;"
1588 " margin-bottom: 0em;"
1589 "}\n"
1590 "li {"
1591 " margin-top: 0.25em;"
1592 " margin-right: 2em;"
1593 "}\n"
1594 ".hr {margin-top: 0.25em;"
1595 " border-color: black;"
1596 " border-bottom-style: solid;"
1597 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001598 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001599 ".total {background: #20D0D0;color: #ffff80;}\n"
1600 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001601 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001602 ".backend {background: #e8e8d0;}\n"
1603 ".active0 {background: #ff9090;}\n"
1604 ".active1 {background: #ffd020;}\n"
1605 ".active2 {background: #ffffa0;}\n"
1606 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001607 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1608 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1609 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001610 ".backup0 {background: #ff9090;}\n"
1611 ".backup1 {background: #ff80ff;}\n"
1612 ".backup2 {background: #c060ff;}\n"
1613 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001614 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1615 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1616 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001617 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001618 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001619 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001620 "a.px:link {color: #ffff40; text-decoration: none;}"
1621 "a.px:visited {color: #ffff40; text-decoration: none;}"
1622 "a.px:hover {color: #ffffff; text-decoration: none;}"
1623 "a.lfsb:link {color: #000000; text-decoration: none;}"
1624 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1625 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1626 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001627 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001628 "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"
1629 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001630 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001631 "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 +02001632 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001633 "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 +02001634 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001635 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1636 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1637 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001638 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001639 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001640 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001641 (uri->flags&ST_SHNODE) ? " on " : "",
1642 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001643 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001644 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001645 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001646 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001647 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001648 return 0;
1649
Willy Tarreau295a8372011-03-10 11:25:07 +01001650 si->applet.state = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02001651 /* fall through */
1652
Willy Tarreau295a8372011-03-10 11:25:07 +01001653 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02001654 up = (now.tv_sec - start_date.tv_sec);
1655
1656 /* WARNING! this has to fit the first packet too.
1657 * We are around 3.5 kB, add adding entries will
1658 * become tricky if we want to support 4kB buffers !
1659 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001660 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001661 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001662 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1663 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001664 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001665 "<hr width=\"100%%\" class=\"hr\">\n"
1666 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001667 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001668 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001669 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001670 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1671 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
1672 "current conns = %d; current pipes = %d/%d<br>\n"
1673 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001674 "</td><td align=\"center\" nowrap>\n"
1675 "<table class=\"lgd\"><tr>\n"
1676 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1677 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1678 "</tr><tr>\n"
1679 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1680 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1681 "</tr><tr>\n"
1682 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1683 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1684 "</tr><tr>\n"
1685 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001686 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001687 "</tr><tr>\n"
1688 "<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 +02001689 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001690 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001691 "</td>"
1692 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1693 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1694 "",
1695 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001696 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1697 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1698 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001699 up / 86400, (up % 86400) / 3600,
1700 (up % 3600) / 60, (up % 60),
1701 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1702 global.rlimit_memmax ? " MB" : "",
1703 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001704 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001705 actconn, pipes_used, pipes_used+pipes_free,
1706 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +02001707 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001708
Willy Tarreau295a8372011-03-10 11:25:07 +01001709 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001710 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001711 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1712 uri->uri_prefix,
1713 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01001714 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001715 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001716 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001717 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1718 uri->uri_prefix,
1719 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01001720 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001721
Willy Tarreau55bb8452007-10-17 18:44:57 +02001722 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001723 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001724 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001725 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1726 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001727 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001728 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001729 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001730 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001731 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1732 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001733 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001734 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001735 }
Willy Tarreau91861262007-10-17 17:06:05 +02001736
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001737 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001738 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
1739 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001740 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
1741 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001742
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001743 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02001744 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
1745 uri->uri_prefix,
1746 (uri->refresh > 0) ? ";norefresh" : "");
1747
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001748 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001749 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001750 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1751 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
1752 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
1753 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
1754 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
1755 "</ul>"
1756 "</td>"
1757 "</tr></table>\n"
1758 ""
1759 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001760
Willy Tarreau295a8372011-03-10 11:25:07 +01001761 if (si->applet.ctx.stats.st_code) {
1762 if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001763 chunk_printf(&msg,
1764 "<p><div class=active3>"
1765 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1766 "Action processed successfully."
1767 "</div>\n", uri->uri_prefix);
1768 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001769 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_NONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001770 chunk_printf(&msg,
1771 "<p><div class=active2>"
1772 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1773 "Nothing has changed."
1774 "</div>\n", uri->uri_prefix);
1775 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001776 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_EXCD) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001777 chunk_printf(&msg,
1778 "<p><div class=active0>"
1779 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1780 "<b>Action not processed : the buffer couldn't store all the data.<br>"
1781 "You should retry with less servers at a time.</b>"
1782 "</div>\n", uri->uri_prefix);
1783 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001784 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DENY) == 0) {
Cyril Bonté474be412010-10-12 00:14:36 +02001785 chunk_printf(&msg,
1786 "<p><div class=active0>"
1787 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1788 "<b>Action denied.</b>"
1789 "</div>\n", uri->uri_prefix);
1790 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001791 else {
1792 chunk_printf(&msg,
1793 "<p><div class=active6>"
1794 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1795 "Unexpected result."
1796 "</div>\n", uri->uri_prefix);
1797 }
1798 chunk_printf(&msg,"<p>\n");
1799 }
1800
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001801 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001802 return 0;
1803 }
Willy Tarreau91861262007-10-17 17:06:05 +02001804
Willy Tarreau295a8372011-03-10 11:25:07 +01001805 si->applet.ctx.stats.px = proxy;
1806 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1807 si->applet.state = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02001808 /* fall through */
1809
Willy Tarreau295a8372011-03-10 11:25:07 +01001810 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02001811 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001812 while (si->applet.ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001813 if (buffer_almost_full(rep))
1814 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01001815 px = si->applet.ctx.stats.px;
Willy Tarreau91861262007-10-17 17:06:05 +02001816 /* skip the disabled proxies and non-networked ones */
1817 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001818 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001819 return 0;
1820
Willy Tarreau295a8372011-03-10 11:25:07 +01001821 si->applet.ctx.stats.px = px->next;
1822 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02001823 }
1824 /* here, we just have reached the last proxy */
1825
Willy Tarreau295a8372011-03-10 11:25:07 +01001826 si->applet.state = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02001827 /* fall through */
1828
Willy Tarreau295a8372011-03-10 11:25:07 +01001829 case STAT_ST_END:
1830 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001831 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001832 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001833 return 0;
1834 }
Willy Tarreau91861262007-10-17 17:06:05 +02001835
Willy Tarreau295a8372011-03-10 11:25:07 +01001836 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001837 /* fall through */
1838
Willy Tarreau295a8372011-03-10 11:25:07 +01001839 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02001840 return 1;
1841
1842 default:
1843 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001844 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001845 return -1;
1846 }
1847}
1848
1849
1850/*
1851 * Dumps statistics for a proxy.
1852 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1853 * ot non-zero if everything completed.
1854 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001855static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001856{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001857 struct session *s = si->applet.private;
1858 struct buffer *rep = si->ib;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001859 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001860 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02001861 struct chunk msg;
1862
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001863 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001864
Willy Tarreau295a8372011-03-10 11:25:07 +01001865 switch (si->applet.ctx.stats.px_st) {
1866 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02001867 /* we are on a new proxy */
1868
1869 if (uri && uri->scope) {
1870 /* we have a limited scope, we have to check the proxy name */
1871 struct stat_scope *scope;
1872 int len;
1873
1874 len = strlen(px->id);
1875 scope = uri->scope;
1876
1877 while (scope) {
1878 /* match exact proxy name */
1879 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1880 break;
1881
1882 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001883 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001884 break;
1885 scope = scope->next;
1886 }
1887
1888 /* proxy name not found : don't dump anything */
1889 if (scope == NULL)
1890 return 1;
1891 }
1892
Willy Tarreau295a8372011-03-10 11:25:07 +01001893 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
1894 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001895 return 1;
1896
Willy Tarreau295a8372011-03-10 11:25:07 +01001897 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02001898 /* fall through */
1899
Willy Tarreau295a8372011-03-10 11:25:07 +01001900 case STAT_PX_ST_TH:
1901 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
1902 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001903 /* A form to enable/disable this proxy servers */
1904 chunk_printf(&msg,
1905 "<form action=\"%s\" method=\"post\">",
1906 uri->uri_prefix);
1907 }
1908
Willy Tarreau55bb8452007-10-17 18:44:57 +02001909 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001910 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001911 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001912 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001913 "<th class=\"pxname\" width=\"10%%\"");
1914
1915 if (uri->flags&ST_SHLGNDS) {
1916 /* cap, mode, id */
1917 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
1918 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1919 px->uuid);
1920
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001921 chunk_printf(&msg, "\"");
1922 }
1923
1924 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001925 ">%s<a name=\"%s\"></a>"
1926 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001927 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001928 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001929 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001930 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001931 "<tr class=\"titre\">",
1932 (uri->flags & ST_SHLGNDS)?"<u>":"",
1933 px->id, px->id, px->id,
1934 (uri->flags & ST_SHLGNDS)?"</u>":"",
1935 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1936
Willy Tarreau295a8372011-03-10 11:25:07 +01001937 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001938 /* Column heading for Enable or Disable server */
1939 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
1940 }
1941
1942 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001943 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001944 "<th colspan=3>Queue</th>"
1945 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001946 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001947 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001948 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001949 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001950 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001951 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001952 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001953 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001954 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001955 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001956 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001957 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1958 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001959 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001960
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001961 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001962 return 0;
1963 }
Willy Tarreau91861262007-10-17 17:06:05 +02001964
Willy Tarreau295a8372011-03-10 11:25:07 +01001965 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02001966 /* fall through */
1967
Willy Tarreau295a8372011-03-10 11:25:07 +01001968 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02001969 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001970 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01001971 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
1972 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001973 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001974 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02001975 "<tr class=\"frontend\">");
1976
Willy Tarreau295a8372011-03-10 11:25:07 +01001977 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001978 /* Column sub-heading for Enable or Disable server */
1979 chunk_printf(&msg, "<td></td>");
1980 }
1981
1982 chunk_printf(&msg,
1983 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001984 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001985 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
1986 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01001987 "",
1988 px->id, px->id);
1989
1990 if (px->mode == PR_MODE_HTTP) {
1991 chunk_printf(&msg,
1992 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01001993 "<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 +01001994 "",
1995 read_freq_ctr(&px->fe_req_per_sec),
1996 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001997 px->fe_counters.p.http.rps_max,
1998 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01001999 LIM2A2(px->fe_sps_lim, "-"));
2000 } else {
2001 chunk_printf(&msg,
2002 /* sessions rate : current, max, limit */
2003 "<td>%s</td><td>%s</td><td>%s</td>"
2004 "",
2005 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002006 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01002007 }
2008
2009 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002010 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002011 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002012 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002013 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002014 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002015
2016 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2017 if (px->mode == PR_MODE_HTTP) {
2018 int i;
2019
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002020 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002021
2022 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002023 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002024
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002025 chunk_printf(&msg, " other=%lld\"", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002026 }
2027
2028 chunk_printf(&msg,
2029 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002030 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002031 /* bytes : in, out */
2032 "<td>%s</td><td>%s</td>"
2033 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002034 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002035 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002036 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002037 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002038
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002039 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002040 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002041 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002042 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002043 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002044 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002045 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002046 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002047 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002048 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002049 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002050 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002051 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2052 U2H2(px->fe_counters.failed_req),
Willy Tarreau562515c2011-07-25 08:11:52 +02002053 px->state == PR_STREADY ? "OPEN" :
2054 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002055 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002056 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002057 /* pxid, name, queue cur, queue max, */
2058 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002059 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002060 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002061 /* bytes : in, out */
2062 "%lld,%lld,"
2063 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002064 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002065 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002066 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002067 /* warnings: retries, redispatches */
2068 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002069 /* server status : reflect frontend status */
2070 "%s,"
2071 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002072 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002073 /* pid, iid, sid, throttle, lbtot, tracked, type */
2074 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002075 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002076 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002077 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002078 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002079 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002080 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2081 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2082 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2083 px->fe_counters.failed_req,
Willy Tarreau562515c2011-07-25 08:11:52 +02002084 px->state == PR_STREADY ? "OPEN" :
2085 px->state == PR_STFULL ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002086 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002087 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002088 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002089
2090 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2091 if (px->mode == PR_MODE_HTTP) {
2092 int i;
2093
2094 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002095 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002096
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002097 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002098 } else {
2099 chunk_printf(&msg, ",,,,,,");
2100 }
2101
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002102 /* failed health analyses */
2103 chunk_printf(&msg, ",");
2104
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002105 /* requests : req_rate, req_rate_max, req_tot, */
2106 chunk_printf(&msg, "%u,%u,%lld,",
2107 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002108 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002109
Willy Tarreauae526782010-03-04 20:34:23 +01002110 /* errors: cli_aborts, srv_aborts */
2111 chunk_printf(&msg, ",,");
2112
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002113 /* finish with EOL */
2114 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002115 }
Willy Tarreau91861262007-10-17 17:06:05 +02002116
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002117 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002118 return 0;
2119 }
2120
Willy Tarreau295a8372011-03-10 11:25:07 +01002121 si->applet.ctx.stats.l = px->listen; /* may be NULL */
2122 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002123 /* fall through */
2124
Willy Tarreau295a8372011-03-10 11:25:07 +01002125 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002126 /* stats.l has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002127 for (; si->applet.ctx.stats.l != NULL; si->applet.ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002128 if (buffer_almost_full(rep))
2129 return 0;
2130
Willy Tarreau295a8372011-03-10 11:25:07 +01002131 l = si->applet.ctx.stats.l;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002132 if (!l->counters)
2133 continue;
2134
Willy Tarreau295a8372011-03-10 11:25:07 +01002135 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2136 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002137 break;
2138
Willy Tarreau295a8372011-03-10 11:25:07 +01002139 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002140 continue;
2141 }
2142
Willy Tarreau295a8372011-03-10 11:25:07 +01002143 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002144 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002145 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002146 /* Column sub-heading for Enable or Disable server */
2147 chunk_printf(&msg, "<td></td>");
2148 }
2149 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002150
2151 if (uri->flags&ST_SHLGNDS) {
2152 char str[INET6_ADDRSTRLEN], *fmt = NULL;
2153 int port;
2154
2155 chunk_printf(&msg, " title=\"IP: ");
2156
2157 port = (l->addr.ss_family == AF_INET6)
2158 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
2159 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
2160
2161 if (l->addr.ss_family == AF_INET) {
2162 if (inet_ntop(AF_INET,
2163 (const void *)&((struct sockaddr_in *)&l->addr)->sin_addr,
2164 str, sizeof(str)))
2165 fmt = "%s:%d";
2166 } else {
2167 if (inet_ntop(AF_INET6,
2168 (const void *)&((struct sockaddr_in6 *)(&l->addr))->sin6_addr,
2169 str, sizeof(str)))
2170 fmt = "[%s]:%d";
2171 }
2172
2173 if (fmt)
2174 chunk_printf(&msg, fmt, str, port);
2175 else
2176 chunk_printf(&msg, "(%s)", strerror(errno));
2177
2178 /* id */
2179 chunk_printf(&msg, ", id: %d", l->luid);
2180
2181 chunk_printf(&msg, "\"");
2182 }
2183
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002184 chunk_printf(&msg,
2185 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002186 ">%s<a name=\"%s/+%s\"></a>"
2187 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002188 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002189 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002190 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002191 "<td>%s</td><td>%s</td><td>%s</td>"
2192 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002193 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002194 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002195 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002196 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002197 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002198 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002199 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2200 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2201
2202 chunk_printf(&msg,
2203 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002204 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002205 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002206 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002207 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002208 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002209 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002210 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002211 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002212 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002213 "",
2214 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2215 U2H2(l->counters->failed_req),
Willy Tarreaua17c2d92011-07-25 08:16:20 +02002216 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002217 } else {
2218 chunk_printf(&msg,
2219 /* pxid, name, queue cur, queue max, */
2220 "%s,%s,,,"
2221 /* sessions: current, max, limit, total */
2222 "%d,%d,%d,%lld,"
2223 /* bytes: in, out */
2224 "%lld,%lld,"
2225 /* denied: req, resp */
2226 "%lld,%lld,"
2227 /* errors: request, connect, response */
2228 "%lld,,,"
2229 /* warnings: retries, redispatches */
2230 ",,"
2231 /* server status: reflect listener status */
2232 "%s,"
2233 /* rest of server: nothing */
2234 ",,,,,,,,"
2235 /* pid, iid, sid, throttle, lbtot, tracked, type */
2236 "%d,%d,%d,,,,%d,"
2237 /* rate, rate_lim, rate_max */
2238 ",,,"
2239 /* check_status, check_code, check_duration */
2240 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002241 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2242 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002243 /* failed health analyses */
2244 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002245 /* requests : req_rate, req_rate_max, req_tot, */
2246 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002247 /* errors: cli_aborts, srv_aborts */
2248 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002249 "\n",
2250 px->id, l->name,
2251 l->nbconn, l->counters->conn_max,
2252 l->maxconn, l->counters->cum_conn,
2253 l->counters->bytes_in, l->counters->bytes_out,
2254 l->counters->denied_req, l->counters->denied_resp,
2255 l->counters->failed_req,
2256 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2257 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2258 }
2259
2260 if (buffer_feed_chunk(rep, &msg) >= 0)
2261 return 0;
2262 }
2263
Willy Tarreau295a8372011-03-10 11:25:07 +01002264 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2265 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002266 /* fall through */
2267
Willy Tarreau295a8372011-03-10 11:25:07 +01002268 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002269 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002270 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002271 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 +02002272
Willy Tarreau4e33d862009-10-11 23:35:10 +02002273 if (buffer_almost_full(rep))
2274 return 0;
2275
Willy Tarreau295a8372011-03-10 11:25:07 +01002276 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002277
Willy Tarreau295a8372011-03-10 11:25:07 +01002278 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2279 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002280 break;
2281
Willy Tarreau295a8372011-03-10 11:25:07 +01002282 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002283 continue;
2284 }
2285
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002286 if (sv->tracked)
2287 svs = sv->tracked;
2288 else
2289 svs = sv;
2290
Willy Tarreau91861262007-10-17 17:06:05 +02002291 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002292 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002293 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002294 else if (svs->state & SRV_RUNNING) {
2295 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002296 sv_state = 3; /* UP */
2297 else
2298 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002299
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002300 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002301 sv_state += 2;
2302 }
Willy Tarreau91861262007-10-17 17:06:05 +02002303 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002304 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002305 sv_state = 1; /* going up */
2306 else
2307 sv_state = 0; /* DOWN */
2308
Willy Tarreau295a8372011-03-10 11:25:07 +01002309 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002310 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002311 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002312 continue;
2313 }
2314
Willy Tarreau295a8372011-03-10 11:25:07 +01002315 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002316 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2317 "UP %d/%d &darr;", "UP",
2318 "NOLB %d/%d &darr;", "NOLB",
2319 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002320 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002321 chunk_printf(&msg,
2322 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002323 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002324 );
2325 }
2326 else {
2327 chunk_printf(&msg,
2328 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002329 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002330 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2331 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002332
Willy Tarreau295a8372011-03-10 11:25:07 +01002333 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002334 chunk_printf(&msg,
2335 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2336 sv->id);
2337 }
2338
2339 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002340
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002341 if (uri->flags&ST_SHLGNDS) {
2342 char str[INET6_ADDRSTRLEN];
2343
2344 chunk_printf(&msg, " title=\"IP: ");
2345
2346 /* IP */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002347 switch (sv->addr.ss_family) {
2348 case AF_INET:
2349 if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&sv->addr)->sin_addr, str, sizeof(str)))
2350 chunk_printf(&msg, "%s:%d", str, htons(((struct sockaddr_in *)&sv->addr)->sin_port));
2351 else
2352 chunk_printf(&msg, "(%s)", strerror(errno));
2353 break;
2354 case AF_INET6:
2355 if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&sv->addr)->sin6_addr, str, sizeof(str)))
2356 chunk_printf(&msg, "%s:%d", str, htons(((struct sockaddr_in6 *)&sv->addr)->sin6_port));
2357 else
2358 chunk_printf(&msg, "(%s)", strerror(errno));
2359 break;
2360 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002361
2362 /* id */
2363 chunk_printf(&msg, ", id: %d", sv->puid);
2364
2365 /* cookie */
2366 if (sv->cookie) {
2367 struct chunk src;
2368
2369 chunk_printf(&msg, ", cookie: '");
2370
2371 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2372 chunk_htmlencode(&msg, &src);
2373
2374 chunk_printf(&msg, "'");
2375 }
2376
2377 chunk_printf(&msg, "\"");
2378 }
2379
2380 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002381 ">%s<a name=\"%s/%s\"></a>"
2382 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002383 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002384 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002385 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002386 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002387 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002388 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002389 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002390 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002391 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002392 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002393 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002394 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2395 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002396 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2397
2398 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2399 if (px->mode == PR_MODE_HTTP) {
2400 int i;
2401
2402 chunk_printf(&msg, " title=\"rsp codes:");
2403
2404 for (i = 1; i < 6; i++)
2405 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2406
2407 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2408 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002409
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002410 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002411 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002412 ">%s%s%s</td><td>%s</td>",
2413 (px->mode == PR_MODE_HTTP)?"<u>":"",
2414 U2H0(sv->counters.cum_sess),
2415 (px->mode == PR_MODE_HTTP)?"</u>":"",
2416 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002417
2418 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002419 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002420 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002421 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002422 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002423 /* errors : request, connect */
2424 "<td></td><td>%s</td>"
2425 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002426 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002427 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002428 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002429 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002430 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2431 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002432 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002433 sv->counters.cli_aborts,
2434 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002435 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002436 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002437
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002438 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002439 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002440
Cyril Bontécd19e512010-01-31 22:34:03 +01002441 if (sv->state & SRV_MAINTAIN) {
2442 chunk_printf(&msg, "%s ",
2443 human_time(now.tv_sec - sv->last_change, 1));
2444 chunk_printf(&msg, "MAINT");
2445 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002446 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2447 chunk_printf(&msg, "%s ",
2448 human_time(now.tv_sec - svs->last_change, 1));
2449 chunk_printf(&msg, "MAINT(via)");
2450 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002451 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002452 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002453 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002454
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002455 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002456 srv_hlt_st[sv_state],
2457 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2458 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002459 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002460
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002461 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002462 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002463 get_check_status_description(sv->check_status));
2464
2465 if (*sv->check_desc) {
2466 struct chunk src;
2467
2468 chunk_printf(&msg, ": ");
2469
2470 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2471 chunk_htmlencode(&msg, &src);
2472 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002473
Willy Tarreaue0454092010-02-26 12:29:07 +01002474 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002475 tv_iszero(&sv->check_start)?"":"* ",
2476 get_check_status_info(sv->check_status));
2477
2478 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002479 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002480
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002481 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002482 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002483 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002484 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002485
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002486 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002487 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002488 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002489 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002490 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002491 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002492 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002493 (sv->state & SRV_BACKUP) ? "-" : "Y",
2494 (sv->state & SRV_BACKUP) ? "Y" : "-");
2495
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002496 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002497 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002498 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002499 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2500
2501 if (svs->observe)
2502 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2503
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002504 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002505 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002506 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002507 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002508 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002509 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002510 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002511 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2512 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002513 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002514 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002515 "<td colspan=3></td>");
2516
2517 /* throttle */
2518 if ((sv->state & SRV_WARMINGUP) &&
2519 now.tv_sec < sv->last_change + sv->slowstart &&
2520 now.tv_sec >= sv->last_change) {
2521 unsigned int ratio;
2522 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002523 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002524 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002525 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002526 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002527 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002528 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002529 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002530 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2531 "UP %d/%d,", "UP,",
2532 "NOLB %d/%d,", "NOLB,",
2533 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002534 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002535 /* pxid, name */
2536 "%s,%s,"
2537 /* queue : current, max */
2538 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002539 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002540 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002541 /* bytes : in, out */
2542 "%lld,%lld,"
2543 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002544 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002545 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002546 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002547 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002548 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002549 "",
2550 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002551 sv->nbpend, sv->counters.nbpend_max,
2552 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002553 sv->counters.bytes_in, sv->counters.bytes_out,
2554 sv->counters.failed_secu,
2555 sv->counters.failed_conns, sv->counters.failed_resp,
2556 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002557
Willy Tarreau55bb8452007-10-17 18:44:57 +02002558 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002559 if (sv->state & SRV_MAINTAIN) {
2560 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002561 }
2562 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2563 chunk_printf(&msg, "MAINT(via),");
2564 }
2565 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002566 chunk_printf(&msg,
2567 srv_hlt_st[sv_state],
2568 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2569 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2570 }
Willy Tarreau91861262007-10-17 17:06:05 +02002571
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002572 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002573 /* weight, active, backup */
2574 "%d,%d,%d,"
2575 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002576 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002577 (sv->state & SRV_BACKUP) ? 0 : 1,
2578 (sv->state & SRV_BACKUP) ? 1 : 0);
2579
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002580 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002581 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002582 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002583 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002584 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002585 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002586 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002587 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002588 ",,,,");
2589
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002590 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002591 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002592 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002593 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002594 LIM2A0(sv->maxqueue, ""),
2595 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002596
2597 /* throttle */
2598 if ((sv->state & SRV_WARMINGUP) &&
2599 now.tv_sec < sv->last_change + sv->slowstart &&
2600 now.tv_sec >= sv->last_change) {
2601 unsigned int ratio;
2602 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002603 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002604 }
2605
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002606 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002607 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002608
2609 /* tracked */
2610 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002611 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002612 sv->tracked->proxy->id, sv->tracked->id);
2613 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002614 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002615
Willy Tarreau7f062c42009-03-05 18:43:00 +01002616 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002617 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002618
2619 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002620 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002621 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002622 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002623
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002624 if (sv->state & SRV_CHECKED) {
2625 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002626 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002627
2628 /* check_code */
2629 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002630 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002631 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002632 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002633
2634 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002635 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002636 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002637 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002638 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002639
2640 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002641 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002642 }
2643
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002644 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2645 if (px->mode == PR_MODE_HTTP) {
2646 int i;
2647
2648 for (i=1; i<6; i++)
2649 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2650
2651 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2652 } else {
2653 chunk_printf(&msg, ",,,,,,");
2654 }
2655
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002656 /* failed health analyses */
2657 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2658
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002659 /* requests : req_rate, req_rate_max, req_tot, */
2660 chunk_printf(&msg, ",,,");
2661
Willy Tarreauae526782010-03-04 20:34:23 +01002662 /* errors: cli_aborts, srv_aborts */
2663 chunk_printf(&msg, "%lld,%lld,",
2664 sv->counters.cli_aborts, sv->counters.srv_aborts);
2665
Willy Tarreau7f062c42009-03-05 18:43:00 +01002666 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002667 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002668 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002669 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002670 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002671 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002672
Willy Tarreau295a8372011-03-10 11:25:07 +01002673 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02002674 /* fall through */
2675
Willy Tarreau295a8372011-03-10 11:25:07 +01002676 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02002677 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002678 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002679 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2680 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002681 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01002682 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002683 /* Column sub-heading for Enable or Disable server */
2684 chunk_printf(&msg, "<td></td>");
2685 }
2686 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002687
2688 if (uri->flags&ST_SHLGNDS) {
2689 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002690 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002691 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002692
2693 /* cookie */
2694 if (px->cookie_name) {
2695 struct chunk src;
2696
2697 chunk_printf(&msg, ", cookie: '");
2698
2699 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2700 chunk_htmlencode(&msg, &src);
2701
2702 chunk_printf(&msg, "'");
2703 }
2704
2705 chunk_printf(&msg, "\"");
2706
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002707 }
2708
2709 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002710 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01002711 ">%s<a name=\"%s/Backend\"></a>"
2712 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002713 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002714 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002715 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002716 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002717 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002718 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002719 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002720 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002721 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
2722 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02002723
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002724 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002725 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002726 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002727 "<td"
2728 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002729 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002730
2731 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2732 if (px->mode == PR_MODE_HTTP) {
2733 int i;
2734
2735 chunk_printf(&msg, " title=\"rsp codes:");
2736
2737 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002738 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002739
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002740 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002741 }
2742
2743 chunk_printf(&msg,
2744 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002745 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002746 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002747 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002748 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002749 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002750 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01002751 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002752 U2H7(px->be_counters.cum_lbconn),
2753 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002754
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002755 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002756 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002757 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002758 /* errors : request, connect */
2759 "<td></td><td>%s</td>"
2760 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002761 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002762 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002763 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002764 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02002765 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002766 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02002767 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002768 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2769 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002770 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002771 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
2772 U2H2(px->be_counters.failed_conns),
2773 px->be_counters.cli_aborts,
2774 px->be_counters.srv_aborts,
2775 U2H5(px->be_counters.failed_resp),
2776 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002777 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01002778 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2779 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01002780 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002781 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002782
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002783 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002784 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002785 "<td class=ac>&nbsp;</td><td>%d</td>"
2786 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002787 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002788 "</tr>",
2789 px->down_trans,
2790 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002791 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002792 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002793 /* pxid, name */
2794 "%s,BACKEND,"
2795 /* queue : current, max */
2796 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002797 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002798 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002799 /* bytes : in, out */
2800 "%lld,%lld,"
2801 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002802 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002803 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002804 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002805 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002806 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002807 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02002808 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002809 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02002810 * active and backups. */
2811 "%s,"
2812 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002813 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002814 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002815 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002816 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002817 /* rate, rate_lim, rate_max, */
2818 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002819 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002820 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002821 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002822 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2823 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2824 px->be_counters.bytes_in, px->be_counters.bytes_out,
2825 px->be_counters.denied_req, px->be_counters.denied_resp,
2826 px->be_counters.failed_conns, px->be_counters.failed_resp,
2827 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01002828 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01002829 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002830 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002831 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01002832 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002833 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002834 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002835 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002836 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002837
2838 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2839 if (px->mode == PR_MODE_HTTP) {
2840 int i;
2841
2842 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002843 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002844
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002845 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002846 } else {
2847 chunk_printf(&msg, ",,,,,,");
2848 }
2849
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002850 /* failed health analyses */
2851 chunk_printf(&msg, ",");
2852
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002853 /* requests : req_rate, req_rate_max, req_tot, */
2854 chunk_printf(&msg, ",,,");
2855
Willy Tarreauae526782010-03-04 20:34:23 +01002856 /* errors: cli_aborts, srv_aborts */
2857 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002858 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01002859
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002860 /* finish with EOL */
2861 chunk_printf(&msg, "\n");
2862
Willy Tarreau55bb8452007-10-17 18:44:57 +02002863 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002864 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002865 return 0;
2866 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002867
Willy Tarreau295a8372011-03-10 11:25:07 +01002868 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002869 /* fall through */
2870
Willy Tarreau295a8372011-03-10 11:25:07 +01002871 case STAT_PX_ST_END:
2872 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002873 chunk_printf(&msg, "</table>");
2874
Willy Tarreau295a8372011-03-10 11:25:07 +01002875 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002876 /* close the form used to enable/disable this proxy servers */
2877 chunk_printf(&msg,
2878 "Choose the action to perform on the checked servers : "
2879 "<select name=action>"
2880 "<option value=\"\"></option>"
2881 "<option value=\"disable\">Disable</option>"
2882 "<option value=\"enable\">Enable</option>"
2883 "</select>"
2884 "<input type=\"hidden\" name=\"b\" value=\"%s\">"
2885 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2886 "</form>",
2887 px->id);
2888 }
2889
2890 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02002891
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002892 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002893 return 0;
2894 }
Willy Tarreau91861262007-10-17 17:06:05 +02002895
Willy Tarreau295a8372011-03-10 11:25:07 +01002896 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002897 /* fall through */
2898
Willy Tarreau295a8372011-03-10 11:25:07 +01002899 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002900 return 1;
2901
2902 default:
2903 /* unknown state, we should put an abort() here ! */
2904 return 1;
2905 }
2906}
2907
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002908/* This function dumps a complete session state onto the stream intreface's
2909 * read buffer. The data_ctx must have been zeroed first, and the flags
2910 * properly set. The session has to be set in data_ctx.sess.target. It returns
2911 * 0 if the output buffer is full and it needs to be called again, otherwise
2912 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002913 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002914static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002915{
2916 struct tm tm;
2917 struct chunk msg;
2918 struct session *sess;
2919 extern const char *monthname[12];
2920 char pn[INET6_ADDRSTRLEN];
2921
2922 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau295a8372011-03-10 11:25:07 +01002923 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002924
Willy Tarreau295a8372011-03-10 11:25:07 +01002925 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002926 /* session changed, no need to go any further */
2927 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002928 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002929 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002930 si->applet.ctx.sess.target = NULL;
2931 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002932 return 1;
2933 }
2934
Willy Tarreau295a8372011-03-10 11:25:07 +01002935 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002936 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01002937 si->applet.ctx.sess.uid = sess->uniq_id;
2938 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002939 /* fall through */
2940
2941 case 1:
2942 chunk_printf(&msg,
2943 "%p: id=%u, proto=%s",
2944 sess,
2945 sess->uniq_id,
2946 sess->listener->proto->name);
2947
2948 switch (sess->listener->proto->sock_family) {
2949 case AF_INET:
2950 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002951 (const void *)&((struct sockaddr_in *)&sess->si[0].addr.c.from)->sin_addr,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002952 pn, sizeof(pn));
2953
2954 chunk_printf(&msg,
2955 " source=%s:%d\n",
2956 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002957 ntohs(((struct sockaddr_in *)&sess->si[0].addr.c.from)->sin_port));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002958 break;
2959 case AF_INET6:
2960 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002961 (const void *)&((struct sockaddr_in6 *)(&sess->si[0].addr.c.from))->sin6_addr,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002962 pn, sizeof(pn));
2963
2964 chunk_printf(&msg,
2965 " source=%s:%d\n",
2966 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002967 ntohs(((struct sockaddr_in6 *)&sess->si[0].addr.c.from)->sin6_port));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002968 break;
2969 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02002970 chunk_printf(&msg,
2971 " source=unix:%d\n", sess->listener->luid);
2972 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002973 default:
2974 /* no more information to print right now */
2975 chunk_printf(&msg, "\n");
2976 break;
2977 }
2978
2979 chunk_printf(&msg,
2980 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02002981 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002982
2983 chunk_printf(&msg,
2984 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)\n",
2985 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
2986 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
2987 sess->listener ? sess->listener->luid : 0);
2988
2989 chunk_printf(&msg,
2990 " backend=%s (id=%u mode=%s) server=%s (id=%u)\n",
2991 sess->be->id, sess->be->uuid, sess->be->mode ? "http" : "tcp",
Willy Tarreau827aee92011-03-10 16:55:02 +01002992 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
2993 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002994
2995 chunk_printf(&msg,
2996 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
2997 sess->task,
2998 sess->task->state,
2999 sess->task->nice, sess->task->calls,
3000 sess->task->expire ?
3001 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3002 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3003 TICKS_TO_MS(1000)) : "<NEVER>",
3004 task_in_rq(sess->task) ? ", running" : "");
3005
3006 get_localtime(sess->logs.accept_date.tv_sec, &tm);
3007 chunk_printf(&msg,
3008 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
3009 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3010 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3011 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3012
3013 chunk_printf(&msg,
3014 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3015 &sess->si[0],
3016 sess->si[0].state,
3017 sess->si[0].flags,
3018 sess->si[0].fd,
3019 sess->si[0].exp ?
3020 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3021 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3022 TICKS_TO_MS(1000)) : "<NEVER>",
3023 sess->si[0].err_type);
3024
3025 chunk_printf(&msg,
3026 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3027 &sess->si[1],
3028 sess->si[1].state,
3029 sess->si[1].flags,
3030 sess->si[1].fd,
3031 sess->si[1].exp ?
3032 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3033 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3034 TICKS_TO_MS(1000)) : "<NEVER>",
3035 sess->si[1].err_type);
3036
3037 chunk_printf(&msg,
3038 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3039 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3040 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3041
3042
3043 chunk_printf(&msg,
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02003044 " req=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003045 " an_exp=%s",
3046 sess->req,
3047 sess->req->flags, sess->req->analysers,
3048 sess->req->l, sess->req->send_max,
3049 sess->req->pipe ? sess->req->pipe->data : 0,
3050 sess->req->to_forward,
3051 sess->req->analyse_exp ?
3052 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3053 TICKS_TO_MS(1000)) : "<NEVER>");
3054
3055 chunk_printf(&msg,
3056 " rex=%s",
3057 sess->req->rex ?
3058 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3059 TICKS_TO_MS(1000)) : "<NEVER>");
3060
3061 chunk_printf(&msg,
3062 " wex=%s\n"
3063 " data=%p r=%d w=%d lr=%d total=%lld\n",
3064 sess->req->wex ?
3065 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3066 TICKS_TO_MS(1000)) : "<NEVER>",
3067 sess->req->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01003068 (int)(sess->req->r - sess->req->data),
3069 (int)(sess->req->w - sess->req->data),
3070 (int)(sess->req->lr - sess->req->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003071 sess->req->total);
3072
3073 chunk_printf(&msg,
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02003074 " res=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003075 " an_exp=%s",
3076 sess->rep,
3077 sess->rep->flags, sess->rep->analysers,
3078 sess->rep->l, sess->rep->send_max,
3079 sess->rep->pipe ? sess->rep->pipe->data : 0,
3080 sess->rep->to_forward,
3081 sess->rep->analyse_exp ?
3082 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3083 TICKS_TO_MS(1000)) : "<NEVER>");
3084
3085 chunk_printf(&msg,
3086 " rex=%s",
3087 sess->rep->rex ?
3088 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3089 TICKS_TO_MS(1000)) : "<NEVER>");
3090
3091 chunk_printf(&msg,
3092 " wex=%s\n"
3093 " data=%p r=%d w=%d lr=%d total=%lld\n",
3094 sess->rep->wex ?
3095 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3096 TICKS_TO_MS(1000)) : "<NEVER>",
3097 sess->rep->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01003098 (int)(sess->rep->r - sess->rep->data),
3099 (int)(sess->rep->w - sess->rep->data),
3100 (int)(sess->rep->lr - sess->rep->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003101 sess->rep->total);
3102
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003103 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003104 return 0;
3105
3106 /* use other states to dump the contents */
3107 }
3108 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003109 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003110 return 1;
3111}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003112
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003113/* This function dumps all sessions' states onto the stream intreface's
3114 * read buffer. The data_ctx must have been zeroed first, and the flags
3115 * properly set. It returns 0 if the output buffer is full and it needs
3116 * to be called again, otherwise non-zero. It is designed to be called
3117 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003118 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003119static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003120{
3121 struct chunk msg;
3122
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003123 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003124 /* If we're forced to shut down, we might have to remove our
3125 * reference to the last session being dumped.
3126 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003127 if (si->applet.state == STAT_ST_LIST) {
3128 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3129 LIST_DEL(&si->applet.ctx.sess.bref.users);
3130 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003131 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003132 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003133 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003134 }
3135
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003136 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003137
Willy Tarreau295a8372011-03-10 11:25:07 +01003138 switch (si->applet.state) {
3139 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003140 /* the function had not been called yet, let's prepare the
3141 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003142 * pointer to the first in the global list. When a target
3143 * session is being destroyed, it is responsible for updating
3144 * this pointer. We know we have reached the end when this
3145 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003146 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003147 LIST_INIT(&si->applet.ctx.sess.bref.users);
3148 si->applet.ctx.sess.bref.ref = sessions.n;
3149 si->applet.state = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003150 /* fall through */
3151
Willy Tarreau295a8372011-03-10 11:25:07 +01003152 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003153 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003154 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3155 LIST_DEL(&si->applet.ctx.sess.bref.users);
3156 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003157 }
3158
3159 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003160 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003161 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003162 struct session *curr_sess;
3163
Willy Tarreau295a8372011-03-10 11:25:07 +01003164 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003165
Willy Tarreau295a8372011-03-10 11:25:07 +01003166 if (si->applet.ctx.sess.target) {
3167 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003168 goto next_sess;
3169
Willy Tarreau295a8372011-03-10 11:25:07 +01003170 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003171 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003172 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003173 return 0;
3174
3175 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003176 LIST_DEL(&si->applet.ctx.sess.bref.users);
3177 LIST_INIT(&si->applet.ctx.sess.bref.users);
3178 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003179 break;
3180 }
3181
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003182 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003183 "%p: proto=%s",
3184 curr_sess,
3185 curr_sess->listener->proto->name);
3186
3187 switch (curr_sess->listener->proto->sock_family) {
3188 case AF_INET:
3189 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003190 (const void *)&((struct sockaddr_in *)&curr_sess->si[0].addr.c.from)->sin_addr,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003191 pn, sizeof(pn));
3192
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003193 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003194 " src=%s:%d fe=%s be=%s srv=%s",
3195 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003196 ntohs(((struct sockaddr_in *)&curr_sess->si[0].addr.c.from)->sin_port),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003197 curr_sess->fe->id,
3198 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01003199 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003200 );
3201 break;
3202 case AF_INET6:
3203 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003204 (const void *)&((struct sockaddr_in6 *)(&curr_sess->si[0].addr.c.from))->sin6_addr,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003205 pn, sizeof(pn));
3206
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003207 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003208 " src=%s:%d fe=%s be=%s srv=%s",
3209 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003210 ntohs(((struct sockaddr_in6 *)&curr_sess->si[0].addr.c.from)->sin6_port),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003211 curr_sess->fe->id,
3212 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01003213 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003214 );
3215
3216 break;
3217 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02003218 chunk_printf(&msg,
3219 " src=unix:%d fe=%s be=%s srv=%s",
3220 curr_sess->listener->luid,
3221 curr_sess->fe->id,
3222 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01003223 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003224 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003225 break;
3226 }
3227
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003228 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003229 " ts=%02x age=%s calls=%d",
3230 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003231 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3232 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003233
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003234 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003235 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
3236 curr_sess->req->flags,
3237 curr_sess->req->l,
3238 curr_sess->req->analysers,
3239 curr_sess->req->rex ?
3240 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3241 TICKS_TO_MS(1000)) : "");
3242
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003243 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003244 ",wx=%s",
3245 curr_sess->req->wex ?
3246 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3247 TICKS_TO_MS(1000)) : "");
3248
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003249 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003250 ",ax=%s]",
3251 curr_sess->req->analyse_exp ?
3252 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3253 TICKS_TO_MS(1000)) : "");
3254
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003255 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003256 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
3257 curr_sess->rep->flags,
3258 curr_sess->rep->l,
3259 curr_sess->rep->analysers,
3260 curr_sess->rep->rex ?
3261 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3262 TICKS_TO_MS(1000)) : "");
3263
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003264 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003265 ",wx=%s",
3266 curr_sess->rep->wex ?
3267 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3268 TICKS_TO_MS(1000)) : "");
3269
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003270 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003271 ",ax=%s]",
3272 curr_sess->rep->analyse_exp ?
3273 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3274 TICKS_TO_MS(1000)) : "");
3275
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003276 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003277 " s0=[%d,%1xh,fd=%d,ex=%s]",
3278 curr_sess->si[0].state,
3279 curr_sess->si[0].flags,
3280 curr_sess->si[0].fd,
3281 curr_sess->si[0].exp ?
3282 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3283 TICKS_TO_MS(1000)) : "");
3284
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003285 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003286 " s1=[%d,%1xh,fd=%d,ex=%s]",
3287 curr_sess->si[1].state,
3288 curr_sess->si[1].flags,
3289 curr_sess->si[1].fd,
3290 curr_sess->si[1].exp ?
3291 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3292 TICKS_TO_MS(1000)) : "");
3293
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003294 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003295 " exp=%s",
3296 curr_sess->task->expire ?
3297 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3298 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003299 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003300 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003301
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003302 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003303
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003304 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003305 /* let's try again later from this session. We add ourselves into
3306 * this session's users so that it can remove us upon termination.
3307 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003308 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003309 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003310 }
3311
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003312 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003313 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003314 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003315
Willy Tarreau295a8372011-03-10 11:25:07 +01003316 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003317 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003318 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003319 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3320 else
3321 chunk_printf(&msg, "Session not found.\n");
3322
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003323 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003324 return 0;
3325
Willy Tarreau295a8372011-03-10 11:25:07 +01003326 si->applet.ctx.sess.target = NULL;
3327 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003328 return 1;
3329 }
3330
Willy Tarreau295a8372011-03-10 11:25:07 +01003331 si->applet.state = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003332 /* fall through */
3333
3334 default:
Willy Tarreau295a8372011-03-10 11:25:07 +01003335 si->applet.state = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003336 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003337 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003338}
3339
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003340/* This function dumps all tables' states onto the stream intreface's
3341 * read buffer. The data_ctx must have been zeroed first, and the flags
3342 * properly set. It returns 0 if the output buffer is full and it needs
3343 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003344 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003345static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003346{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003347 struct session *s = si->applet.private;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003348 struct chunk msg;
3349 struct ebmb_node *eb;
3350 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003351 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003352
3353 /*
Willy Tarreau295a8372011-03-10 11:25:07 +01003354 * We have 3 possible states in si->applet.state :
3355 * - STAT_ST_INIT : the first call
3356 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003357 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003358 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003359 * and the entry pointer points to the next entry to be dumped,
3360 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003361 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003362 * data though.
3363 */
3364
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003365 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003366 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau295a8372011-03-10 11:25:07 +01003367 if (si->applet.state == STAT_ST_LIST) {
3368 si->applet.ctx.table.entry->ref_cnt--;
3369 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003370 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003371 return 1;
3372 }
3373
3374 chunk_init(&msg, trash, sizeof(trash));
3375
Willy Tarreau295a8372011-03-10 11:25:07 +01003376 while (si->applet.state != STAT_ST_FIN) {
3377 switch (si->applet.state) {
3378 case STAT_ST_INIT:
3379 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3380 if (!si->applet.ctx.table.proxy)
3381 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003382
Willy Tarreau295a8372011-03-10 11:25:07 +01003383 si->applet.ctx.table.entry = NULL;
3384 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003385 break;
3386
Willy Tarreau295a8372011-03-10 11:25:07 +01003387 case STAT_ST_INFO:
3388 if (!si->applet.ctx.table.proxy ||
3389 (si->applet.ctx.table.target &&
3390 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
3391 si->applet.state = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003392 break;
3393 }
3394
Willy Tarreau295a8372011-03-10 11:25:07 +01003395 if (si->applet.ctx.table.proxy->table.size) {
Simon Hormanc88b8872011-06-15 15:18:49 +09003396 if (show && !stats_dump_table_head_to_buffer(&msg, si, si->applet.ctx.table.proxy,
3397 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003398 return 0;
3399
Willy Tarreau295a8372011-03-10 11:25:07 +01003400 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003401 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
3402 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003403 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003404 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003405 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3406 si->applet.ctx.table.entry->ref_cnt++;
3407 si->applet.state = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003408 break;
3409 }
3410 }
3411 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003412 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003413 break;
3414
Willy Tarreau295a8372011-03-10 11:25:07 +01003415 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003416 skip_entry = false;
3417
Willy Tarreau295a8372011-03-10 11:25:07 +01003418 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003419 /* we're filtering on some data contents */
3420 void *ptr;
3421 long long data;
3422
Willy Tarreau295a8372011-03-10 11:25:07 +01003423 dt = si->applet.ctx.table.data_type;
3424 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3425 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003426 dt);
3427
3428 data = 0;
3429 switch (stktable_data_types[dt].std_type) {
3430 case STD_T_SINT:
3431 data = stktable_data_cast(ptr, std_t_sint);
3432 break;
3433 case STD_T_UINT:
3434 data = stktable_data_cast(ptr, std_t_uint);
3435 break;
3436 case STD_T_ULL:
3437 data = stktable_data_cast(ptr, std_t_ull);
3438 break;
3439 case STD_T_FRQP:
3440 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003441 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003442 break;
3443 }
3444
3445 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003446 if ((data < si->applet.ctx.table.value &&
3447 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3448 si->applet.ctx.table.data_op == STD_OP_GT ||
3449 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3450 (data == si->applet.ctx.table.value &&
3451 (si->applet.ctx.table.data_op == STD_OP_NE ||
3452 si->applet.ctx.table.data_op == STD_OP_GT ||
3453 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3454 (data > si->applet.ctx.table.value &&
3455 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3456 si->applet.ctx.table.data_op == STD_OP_LT ||
3457 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003458 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003459 }
3460
Simon Hormanc88b8872011-06-15 15:18:49 +09003461 if (show && !skip_entry &&
3462 !stats_dump_table_entry_to_buffer(&msg, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003463 si->applet.ctx.table.entry))
3464 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003465
Willy Tarreau295a8372011-03-10 11:25:07 +01003466 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003467
Willy Tarreau295a8372011-03-10 11:25:07 +01003468 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003469 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003470 struct stksess *old = si->applet.ctx.table.entry;
3471 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3472 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3473 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003474 break;
3475 }
3476
Simon Hormanc88b8872011-06-15 15:18:49 +09003477
3478 if (show)
3479 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3480 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3481 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3482
Willy Tarreau295a8372011-03-10 11:25:07 +01003483 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
3484 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003485 break;
3486
Willy Tarreau295a8372011-03-10 11:25:07 +01003487 case STAT_ST_END:
3488 si->applet.state = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003489 break;
3490 }
3491 }
3492 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003493}
3494
Willy Tarreaud426a182010-03-05 14:58:26 +01003495/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003496 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3497 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3498 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3499 * lines are respected within the limit of 70 output chars. Lines that are
3500 * continuation of a previous truncated line begin with "+" instead of " "
3501 * after the offset. The new pointer is returned.
3502 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003503static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3504 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003505{
3506 int end;
3507 unsigned char c;
3508
3509 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003510 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003511 return ptr;
3512
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003513 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003514
Willy Tarreaud426a182010-03-05 14:58:26 +01003515 while (ptr < len && ptr < bsize) {
3516 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003517 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003518 if (out->len > end - 2)
3519 break;
3520 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003521 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003522 if (out->len > end - 3)
3523 break;
3524 out->str[out->len++] = '\\';
3525 switch (c) {
3526 case '\t': c = 't'; break;
3527 case '\n': c = 'n'; break;
3528 case '\r': c = 'r'; break;
3529 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003530 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003531 }
3532 out->str[out->len++] = c;
3533 } else {
3534 if (out->len > end - 5)
3535 break;
3536 out->str[out->len++] = '\\';
3537 out->str[out->len++] = 'x';
3538 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3539 out->str[out->len++] = hextab[c & 0xF];
3540 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003541 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003542 /* we had a line break, let's return now */
3543 out->str[out->len++] = '\n';
3544 *line = ptr;
3545 return ptr;
3546 }
3547 }
3548 /* we have an incomplete line, we return it as-is */
3549 out->str[out->len++] = '\n';
3550 return ptr;
3551}
3552
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003553/* This function dumps all captured errors onto the stream intreface's
3554 * read buffer. The data_ctx must have been zeroed first, and the flags
3555 * properly set. It returns 0 if the output buffer is full and it needs
3556 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003557 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003558static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003559{
3560 extern const char *monthname[12];
3561 struct chunk msg;
3562
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003563 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003564 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003565
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003566 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003567
Willy Tarreau295a8372011-03-10 11:25:07 +01003568 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003569 /* the function had not been called yet, let's prepare the
3570 * buffer for a response.
3571 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003572 struct tm tm;
3573
3574 get_localtime(date.tv_sec, &tm);
3575 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3576 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3577 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3578 error_snapshot_id);
3579
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003580 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003581 /* Socket buffer full. Let's try again later from the same point */
3582 return 0;
3583 }
3584
Willy Tarreau295a8372011-03-10 11:25:07 +01003585 si->applet.ctx.errors.px = proxy;
3586 si->applet.ctx.errors.buf = 0;
3587 si->applet.ctx.errors.bol = 0;
3588 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003589 }
3590
3591 /* we have two inner loops here, one for the proxy, the other one for
3592 * the buffer.
3593 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003594 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003595 struct error_snapshot *es;
3596
Willy Tarreau295a8372011-03-10 11:25:07 +01003597 if (si->applet.ctx.errors.buf == 0)
3598 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003599 else
Willy Tarreau295a8372011-03-10 11:25:07 +01003600 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003601
3602 if (!es->when.tv_sec)
3603 goto next;
3604
Willy Tarreau295a8372011-03-10 11:25:07 +01003605 if (si->applet.ctx.errors.iid >= 0 &&
3606 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
3607 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003608 goto next;
3609
Willy Tarreau295a8372011-03-10 11:25:07 +01003610 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003611 /* just print headers now */
3612
3613 char pn[INET6_ADDRSTRLEN];
3614 struct tm tm;
3615
3616 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003617 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003618 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003619 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003620
3621
3622 if (es->src.ss_family == AF_INET)
3623 inet_ntop(AF_INET,
3624 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
3625 pn, sizeof(pn));
3626 else
3627 inet_ntop(AF_INET6,
3628 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
3629 pn, sizeof(pn));
3630
Willy Tarreau295a8372011-03-10 11:25:07 +01003631 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003632 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003633 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003634 " frontend %s (#%d): invalid request\n"
3635 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003636 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003637 " request length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003638 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau3fd6cec2010-03-25 06:45:07 +01003639 pn, es->sid, (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3640 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003641 es->srv ? es->srv->id : "<NONE>",
3642 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003643 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003644 es->len, es->pos);
3645 break;
3646 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003647 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003648 " backend %s (#%d) : invalid response\n"
3649 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003650 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003651 " response length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003652 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003653 pn, es->sid, es->oe->id, es->oe->uuid,
3654 es->srv ? es->srv->id : "<NONE>",
3655 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003656 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003657 es->len, es->pos);
3658 break;
3659 }
3660
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003661 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003662 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003663 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003664 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003665 si->applet.ctx.errors.ptr = 0;
3666 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003667 }
3668
Willy Tarreau295a8372011-03-10 11:25:07 +01003669 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003670 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003671 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003672 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003673 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau61b34732009-10-03 23:49:35 +02003674 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003675 goto next;
3676 }
3677
3678 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01003679 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003680 int newptr;
3681 int newline;
3682
Willy Tarreau295a8372011-03-10 11:25:07 +01003683 newline = si->applet.ctx.errors.bol;
3684 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
3685 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02003686 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003687
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003688 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003689 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003690 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003691 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003692 si->applet.ctx.errors.ptr = newptr;
3693 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003694 };
3695 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01003696 si->applet.ctx.errors.bol = 0;
3697 si->applet.ctx.errors.ptr = -1;
3698 si->applet.ctx.errors.buf++;
3699 if (si->applet.ctx.errors.buf > 1) {
3700 si->applet.ctx.errors.buf = 0;
3701 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003702 }
3703 }
3704
3705 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02003706 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003707}
3708
Willy Tarreaub24281b2011-02-13 13:16:36 +01003709struct si_applet http_stats_applet = {
3710 .name = "<STATS>", /* used for logging */
3711 .fct = http_stats_io_handler,
3712};
3713
Simon Horman9bd2c732011-06-15 15:18:44 +09003714static struct si_applet cli_applet = {
Willy Tarreaub24281b2011-02-13 13:16:36 +01003715 .name = "<CLI>", /* used for logging */
3716 .fct = cli_io_handler,
3717};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003718
Willy Tarreau10522fd2008-07-09 20:12:41 +02003719static struct cfg_kw_list cfg_kws = {{ },{
3720 { CFG_GLOBAL, "stats", stats_parse_global },
3721 { 0, NULL, NULL },
3722}};
3723
3724__attribute__((constructor))
3725static void __dumpstats_module_init(void)
3726{
3727 cfg_register_keywords(&cfg_kws);
3728}
3729
Willy Tarreau91861262007-10-17 17:06:05 +02003730/*
3731 * Local variables:
3732 * c-indent-level: 8
3733 * c-basic-offset: 8
3734 * End:
3735 */