blob: 45424dfa3cfbcf89b8a7ef23cccedad9aa865d08 [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 Hormanc5b89f62011-06-15 15:18:50 +0900552 default:
Simon Horman17bce342011-06-15 15:18:47 +0900553 if (show)
Simon Hormancec9a222011-06-15 15:18:51 +0900554 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 +0900555 else
Simon Hormancec9a222011-06-15 15:18:51 +0900556 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 +0900557 si->applet.st0 = STAT_CLI_PRINT;
558 return;
559 }
560
561 /* check permissions */
562 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
563 si->applet.ctx.cli.msg = stats_permission_denied_msg;
564 si->applet.st0 = STAT_CLI_PRINT;
565 return;
566 }
567
568 ts = stktable_lookup_key(&px->table, &static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900569 if (!ts)
570 return;
571
572 if (show) {
573 struct chunk msg;
574 chunk_init(&msg, trash, sizeof(trash));
575 if (!stats_dump_table_head_to_buffer(&msg, si, px, px))
576 return;
577 stats_dump_table_entry_to_buffer(&msg, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900578 return;
579 }
Simon Horman17bce342011-06-15 15:18:47 +0900580
581 if (ts->ref_cnt) {
Simon Horman121f3052011-06-15 15:18:46 +0900582 /* don't delete an entry which is currently referenced */
583 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
584 si->applet.st0 = STAT_CLI_PRINT;
585 return;
586 }
587
588 stksess_kill(&px->table, ts);
589}
590
Simon Hormand5b9fd92011-06-15 15:18:48 +0900591static void stats_sock_table_data_request(struct stream_interface *si, char **args)
592{
593 /* condition on stored data value */
594 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
595 if (si->applet.ctx.table.data_type < 0) {
596 si->applet.ctx.cli.msg = "Unknown data type\n";
597 si->applet.st0 = STAT_CLI_PRINT;
598 return;
599 }
600
601 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
602 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
603 si->applet.st0 = STAT_CLI_PRINT;
604 return;
605 }
606
607 si->applet.ctx.table.data_op = get_std_op(args[4]);
608 if (si->applet.ctx.table.data_op < 0) {
609 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
610 si->applet.st0 = STAT_CLI_PRINT;
611 return;
612 }
613
614 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
615 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
616 si->applet.st0 = STAT_CLI_PRINT;
617 return;
618 }
619}
620
621static void stats_sock_table_request(struct stream_interface *si, char **args, bool show)
622{
623 si->applet.ctx.table.data_type = -1;
624 si->applet.state = STAT_ST_INIT;
625 si->applet.ctx.table.proxy = NULL;
626 si->applet.ctx.table.entry = NULL;
Simon Hormanc88b8872011-06-15 15:18:49 +0900627 if (show)
628 si->applet.st0 = STAT_CLI_O_TAB;
629 else
630 si->applet.st0 = STAT_CLI_O_CLR;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900631
632 if (*args[2]) {
633 si->applet.ctx.table.target = find_stktable(args[2]);
634 if (!si->applet.ctx.table.target) {
635 si->applet.ctx.cli.msg = "No such table\n";
636 si->applet.st0 = STAT_CLI_PRINT;
637 return;
638 }
639 }
640 else {
641 if (!show)
642 goto err_args;
643 return;
644 }
645
646 if (strcmp(args[3], "key") == 0)
647 stats_sock_table_key_request(si, args, show);
Simon Hormanc88b8872011-06-15 15:18:49 +0900648 else if (strncmp(args[3], "data.", 5) == 0)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900649 stats_sock_table_data_request(si, args);
Simon Hormanc88b8872011-06-15 15:18:49 +0900650 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900651 goto err_args;
652
653 return;
654
655err_args:
656 if (show)
657 si->applet.ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
658 else
Simon Hormanc88b8872011-06-15 15:18:49 +0900659 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 +0900660 si->applet.st0 = STAT_CLI_PRINT;
661}
662
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200663/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200664 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100665 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200666 * designating the function which will have to process the request, which can
667 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200668 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900669static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200670{
Willy Tarreaubc4af052011-02-13 13:25:14 +0100671 struct session *s = si->applet.private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200672 char *args[MAX_STATS_ARGS + 1];
673 int arg;
674
675 while (isspace((unsigned char)*line))
676 line++;
677
678 arg = 0;
679 args[arg] = line;
680
681 while (*line && arg < MAX_STATS_ARGS) {
682 if (isspace((unsigned char)*line)) {
683 *line++ = '\0';
684
685 while (isspace((unsigned char)*line))
686 line++;
687
688 args[++arg] = line;
689 continue;
690 }
691
692 line++;
693 }
694
695 while (++arg <= MAX_STATS_ARGS)
696 args[arg] = line;
697
Willy Tarreau295a8372011-03-10 11:25:07 +0100698 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200699 if (strcmp(args[0], "show") == 0) {
700 if (strcmp(args[1], "stat") == 0) {
701 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100702 si->applet.ctx.stats.flags |= STAT_BOUND;
703 si->applet.ctx.stats.iid = atoi(args[2]);
704 si->applet.ctx.stats.type = atoi(args[3]);
705 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200706 }
707
Willy Tarreau295a8372011-03-10 11:25:07 +0100708 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
709 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
710 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100711 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200712 }
713 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100714 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
715 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
716 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100717 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200718 }
719 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100720 si->applet.state = STAT_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200721 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100722 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100723 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200724 return 1;
725 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100726 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100727 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100728 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100729 si->applet.ctx.sess.target = NULL;
730 si->applet.ctx.sess.section = 0; /* start with session status */
731 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100732 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200733 }
734 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200735 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100736 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100737 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200738 return 1;
739 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200740 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100741 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200742 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100743 si->applet.ctx.errors.iid = -1;
744 si->applet.ctx.errors.px = NULL;
745 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100746 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200747 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200748 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900749 stats_sock_table_request(si, args, true);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200750 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200751 else { /* neither "stat" nor "info" nor "sess" nor "errors" no "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200752 return 0;
753 }
754 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200755 else if (strcmp(args[0], "clear") == 0) {
756 if (strcmp(args[1], "counters") == 0) {
757 struct proxy *px;
758 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200759 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200760 int clrall = 0;
761
762 if (strcmp(args[2], "all") == 0)
763 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200764
Willy Tarreau6162db22009-10-10 17:13:00 +0200765 /* check permissions */
766 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
767 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100768 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100769 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200770 return 1;
771 }
772
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200773 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100774 if (clrall) {
775 memset(&px->be_counters, 0, sizeof(px->be_counters));
776 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
777 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200778 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100779 px->be_counters.conn_max = 0;
780 px->be_counters.p.http.rps_max = 0;
781 px->be_counters.sps_max = 0;
782 px->be_counters.cps_max = 0;
783 px->be_counters.nbpend_max = 0;
784
785 px->fe_counters.conn_max = 0;
786 px->fe_counters.p.http.rps_max = 0;
787 px->fe_counters.sps_max = 0;
788 px->fe_counters.cps_max = 0;
789 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200790 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200791
792 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200793 if (clrall)
794 memset(&sv->counters, 0, sizeof(sv->counters));
795 else {
796 sv->counters.cur_sess_max = 0;
797 sv->counters.nbpend_max = 0;
798 sv->counters.sps_max = 0;
799 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200800
801 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200802 if (li->counters) {
803 if (clrall)
804 memset(li->counters, 0, sizeof(*li->counters));
805 else
806 li->counters->conn_max = 0;
807 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200808 }
809
810 return 1;
811 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200812 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900813 stats_sock_table_request(si, args, false);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200814 /* end of processing */
815 return 1;
816 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200817 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200818 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200819 return 0;
820 }
821 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200822 else if (strcmp(args[0], "get") == 0) {
823 if (strcmp(args[1], "weight") == 0) {
824 struct proxy *px;
825 struct server *sv;
826
827 /* split "backend/server" and make <line> point to server */
828 for (line = args[2]; *line; line++)
829 if (*line == '/') {
830 *line++ = '\0';
831 break;
832 }
833
834 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100835 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100836 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200837 return 1;
838 }
839
840 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100841 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100842 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200843 return 1;
844 }
845
846 /* return server's effective weight at the moment */
847 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
848 buffer_feed(si->ib, trash);
849 return 1;
850 }
851 else { /* not "get weight" */
852 return 0;
853 }
854 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200855 else if (strcmp(args[0], "set") == 0) {
856 if (strcmp(args[1], "weight") == 0) {
857 struct proxy *px;
858 struct server *sv;
859 int w;
860
861 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100862 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100863 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200864 return 1;
865 }
866
867 /* split "backend/server" and make <line> point to server */
868 for (line = args[2]; *line; line++)
869 if (*line == '/') {
870 *line++ = '\0';
871 break;
872 }
873
874 if (!*line || !*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100875 si->applet.ctx.cli.msg = "Require 'backend/server' and 'weight' or 'weight%'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100876 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200877 return 1;
878 }
879
880 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100881 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100882 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200883 return 1;
884 }
885
Cyril Bonté613f0df2011-03-03 20:49:04 +0100886 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100887 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100888 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100889 return 1;
890 }
891
Willy Tarreau4483d432009-10-10 19:30:08 +0200892 /* if the weight is terminated with '%', it is set relative to
893 * the initial weight, otherwise it is absolute.
894 */
895 w = atoi(args[3]);
896 if (strchr(args[3], '%') != NULL) {
897 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100898 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100899 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200900 return 1;
901 }
902 w = sv->iweight * w / 100;
903 }
904 else {
905 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100906 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100907 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200908 return 1;
909 }
910 }
911
912 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100913 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 +0100914 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200915 return 1;
916 }
917
918 sv->uweight = w;
919
920 if (px->lbprm.algo & BE_LB_PROP_DYN) {
921 /* we must take care of not pushing the server to full throttle during slow starts */
922 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
923 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
924 else
925 sv->eweight = BE_WEIGHT_SCALE;
926 sv->eweight *= sv->uweight;
927 } else {
928 sv->eweight = sv->uweight;
929 }
930
931 /* static LB algorithms are a bit harder to update */
932 if (px->lbprm.update_server_eweight)
933 px->lbprm.update_server_eweight(sv);
934 else if (sv->eweight)
935 px->lbprm.set_server_status_up(sv);
936 else
937 px->lbprm.set_server_status_down(sv);
938
939 return 1;
940 }
Willy Tarreau7aabd112010-01-26 10:59:06 +0100941 else if (strcmp(args[1], "timeout") == 0) {
942 if (strcmp(args[2], "cli") == 0) {
943 unsigned timeout;
944 const char *res;
945
946 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100947 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100948 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100949 return 1;
950 }
951
952 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
953 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100954 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100955 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100956 return 1;
957 }
958
959 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
960 return 1;
961 }
962 else {
Willy Tarreau295a8372011-03-10 11:25:07 +0100963 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100964 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100965 return 1;
966 }
967 }
968 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +0200969 return 0;
970 }
971 }
Cyril Bontécd19e512010-01-31 22:34:03 +0100972 else if (strcmp(args[0], "enable") == 0) {
973 if (strcmp(args[1], "server") == 0) {
974 struct proxy *px;
975 struct server *sv;
976
977 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100978 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100979 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100980 return 1;
981 }
982
983 /* split "backend/server" and make <line> point to server */
984 for (line = args[2]; *line; line++)
985 if (*line == '/') {
986 *line++ = '\0';
987 break;
988 }
989
990 if (!*line || !*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100991 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100992 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100993 return 1;
994 }
995
996 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100997 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100998 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100999 return 1;
1000 }
1001
Cyril Bonté613f0df2011-03-03 20:49:04 +01001002 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001003 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001004 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001005 return 1;
1006 }
1007
Cyril Bontécd19e512010-01-31 22:34:03 +01001008 if (sv->state & SRV_MAINTAIN) {
1009 /* The server is really in maintenance, we can change the server state */
1010 if (sv->tracked) {
1011 /* If this server tracks the status of another one,
1012 * we must restore the good status.
1013 */
1014 if (sv->tracked->state & SRV_RUNNING) {
1015 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001016 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001017 } else {
1018 sv->state &= ~SRV_MAINTAIN;
1019 set_server_down(sv);
1020 }
1021 } else {
1022 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001023 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001024 }
1025 }
1026
1027 return 1;
1028 }
1029 else { /* unknown "enable" parameter */
1030 return 0;
1031 }
1032 }
1033 else if (strcmp(args[0], "disable") == 0) {
1034 if (strcmp(args[1], "server") == 0) {
1035 struct proxy *px;
1036 struct server *sv;
1037
1038 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001039 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001040 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001041 return 1;
1042 }
1043
1044 /* split "backend/server" and make <line> point to server */
1045 for (line = args[2]; *line; line++)
1046 if (*line == '/') {
1047 *line++ = '\0';
1048 break;
1049 }
1050
1051 if (!*line || !*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001052 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001053 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001054 return 1;
1055 }
1056
1057 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001058 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001059 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001060 return 1;
1061 }
1062
Cyril Bonté613f0df2011-03-03 20:49:04 +01001063 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001064 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001065 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001066 return 1;
1067 }
1068
Cyril Bontécd19e512010-01-31 22:34:03 +01001069 if (! (sv->state & SRV_MAINTAIN)) {
1070 /* Not already in maintenance, we can change the server state */
1071 sv->state |= SRV_MAINTAIN;
1072 set_server_down(sv);
1073 }
1074
1075 return 1;
1076 }
1077 else { /* unknown "disable" parameter */
1078 return 0;
1079 }
1080 }
1081 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001082 return 0;
1083 }
1084 return 1;
1085}
1086
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001087/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001088 * used to processes I/O from/to the stats unix socket. The system relies on a
1089 * state machine handling requests and various responses. We read a request,
1090 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001091 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1092 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001093 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001094 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001095static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001096{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001097 struct buffer *req = si->ob;
1098 struct buffer *res = si->ib;
1099 int reql;
1100 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001101
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001102 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1103 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001104
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001105 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001106 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001107 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001108 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001109 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001110 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001111 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001112 /* Let's close for real now. We just close the request
1113 * side, the conditions below will complete if needed.
1114 */
1115 si->shutw(si);
1116 break;
1117 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001118 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001119 /* ensure we have some output room left in the event we
1120 * would want to return some info right after parsing.
1121 */
1122 if (buffer_almost_full(si->ib))
1123 break;
1124
Willy Tarreau74b08c92010-09-08 17:04:31 +02001125 reql = buffer_get_line(si->ob, trash, sizeof(trash));
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001126 if (reql <= 0) { /* closed or EOL not found */
1127 if (reql == 0)
1128 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001129 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001130 continue;
1131 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001132
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001133 /* seek for a possible semi-colon. If we find one, we
1134 * replace it with an LF and skip only this part.
1135 */
1136 for (len = 0; len < reql; len++)
1137 if (trash[len] == ';') {
1138 trash[len] = '\n';
1139 reql = len + 1;
1140 break;
1141 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001142
Willy Tarreau816fc222009-10-04 07:36:58 +02001143 /* now it is time to check that we have a full line,
1144 * remove the trailing \n and possibly \r, then cut the
1145 * line.
1146 */
1147 len = reql - 1;
1148 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001149 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001150 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001151 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001152
Willy Tarreau816fc222009-10-04 07:36:58 +02001153 if (len && trash[len-1] == '\r')
1154 len--;
1155
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001156 trash[len] = '\0';
1157
Willy Tarreaubc4af052011-02-13 13:25:14 +01001158 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001159 if (len) {
1160 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001161 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001162 continue;
1163 }
1164 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001165 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001166 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001167 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001168 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001169 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001170 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001171 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001172 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001173 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001174 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001175 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001176 /* if prompt is disabled, print help on empty lines,
1177 * so that the user at least knows how to enable
1178 * prompt and find help.
1179 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001180 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001181 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001182 }
1183
1184 /* re-adjust req buffer */
1185 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001186 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001187 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001188 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001189 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001190 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001191 continue;
1192 }
1193
Willy Tarreaubc4af052011-02-13 13:25:14 +01001194 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001195 case STAT_CLI_PRINT:
Willy Tarreau295a8372011-03-10 11:25:07 +01001196 if (buffer_feed(si->ib, si->applet.ctx.cli.msg) < 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001197 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001198 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001199 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001200 if (stats_dump_raw_to_buffer(si))
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_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001204 if (stats_dump_sess_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_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001208 if (stats_dump_errors_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 Tarreau69f58c82010-07-12 17:55:33 +02001211 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001212 if (stats_table_request(si, true))
1213 si->applet.st0 = STAT_CLI_PROMPT;
1214 break;
1215 case STAT_CLI_O_CLR:
1216 if (stats_table_request(si, false))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001217 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001218 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001219 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001220 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001221 break;
1222 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001223
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001224 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001225 if (si->applet.st0 == STAT_CLI_PROMPT) {
1226 if (buffer_feed(si->ib, si->applet.st1 ? "\n> " : "\n") < 0)
1227 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001228 }
1229
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001230 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001231 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001232 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001233
1234 /* Now we close the output if one of the writers did so,
1235 * or if we're not in interactive mode and the request
1236 * buffer is empty. This still allows pipelined requests
1237 * to be sent in non-interactive mode.
1238 */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001239 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->applet.st1 && !req->send_max)) {
1240 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001241 continue;
1242 }
1243
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001244 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001245 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001246 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001247 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001248
Willy Tarreaubc4af052011-02-13 13:25:14 +01001249 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001250 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1251 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1252 /* Other size has closed, let's abort if we have no more processing to do
1253 * and nothing more to consume. This is comparable to a broken pipe, so
1254 * we forward the close to the request side so that it flows upstream to
1255 * the client.
1256 */
1257 si->shutw(si);
1258 }
1259
Willy Tarreaubc4af052011-02-13 13:25:14 +01001260 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001261 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1262 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1263 /* We have no more processing to do, and nothing more to send, and
1264 * the client side has closed. So we'll forward this state downstream
1265 * on the response buffer.
1266 */
1267 si->shutr(si);
1268 res->flags |= BF_READ_NULL;
1269 }
1270
1271 /* update all other flags and resync with the other side */
1272 si->update(si);
1273
1274 /* we don't want to expire timeouts while we're processing requests */
1275 si->ib->rex = TICK_ETERNITY;
1276 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001277
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001278 out:
1279 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
1280 __FUNCTION__, __LINE__,
1281 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
1282
1283 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1284 /* check that we have released everything then unregister */
1285 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001286 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001287}
1288
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001289/* This function dumps statistics onto the stream interface's read buffer.
1290 * The data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001291 * It returns 0 as long as it does not complete, non-zero upon completion.
1292 * Some states are not used but it makes the code more similar to other
1293 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001294 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001295static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001296{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001297 struct proxy *px;
1298 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001299 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001300
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001301 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +02001302
Willy Tarreau295a8372011-03-10 11:25:07 +01001303 switch (si->applet.state) {
1304 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001305 /* the function had not been called yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001306 si->applet.state = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001307 /* fall through */
1308
Willy Tarreau295a8372011-03-10 11:25:07 +01001309 case STAT_ST_HEAD:
1310 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001311 print_csv_header(&msg);
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001312 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001313 return 0;
1314 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001315
Willy Tarreau295a8372011-03-10 11:25:07 +01001316 si->applet.state = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001317 /* fall through */
1318
Willy Tarreau295a8372011-03-10 11:25:07 +01001319 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001320 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001321 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001322 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001323 "Name: " PRODUCT_NAME "\n"
1324 "Version: " HAPROXY_VERSION "\n"
1325 "Release_date: " HAPROXY_DATE "\n"
1326 "Nbproc: %d\n"
1327 "Process_num: %d\n"
1328 "Pid: %d\n"
1329 "Uptime: %dd %dh%02dm%02ds\n"
1330 "Uptime_sec: %d\n"
1331 "Memmax_MB: %d\n"
1332 "Ulimit-n: %d\n"
1333 "Maxsock: %d\n"
1334 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001335 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001336 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001337 "PipesUsed: %d\n"
1338 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001339 "Tasks: %d\n"
1340 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001341 "node: %s\n"
1342 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001343 "",
1344 global.nbproc,
1345 relative_pid,
1346 pid,
1347 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1348 up,
1349 global.rlimit_memmax,
1350 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001351 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001352 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001353 nb_tasks_cur, run_queue_cur,
1354 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001355 );
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001356 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001357 return 0;
1358 }
1359
Willy Tarreau295a8372011-03-10 11:25:07 +01001360 si->applet.ctx.stats.px = proxy;
1361 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1362 si->applet.ctx.stats.sv = NULL;
1363 si->applet.state = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001364 /* fall through */
1365
Willy Tarreau295a8372011-03-10 11:25:07 +01001366 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001367 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001368 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1369 while (si->applet.ctx.stats.px) {
1370 px = si->applet.ctx.stats.px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001371 /* skip the disabled proxies and non-networked ones */
1372 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001373 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001374 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001375 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001376 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001377
Willy Tarreau295a8372011-03-10 11:25:07 +01001378 si->applet.ctx.stats.px = px->next;
1379 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001380 }
1381 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001382 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001383
Willy Tarreau295a8372011-03-10 11:25:07 +01001384 si->applet.state = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001385 /* fall through */
1386
Willy Tarreau295a8372011-03-10 11:25:07 +01001387 case STAT_ST_END:
1388 si->applet.state = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001389 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001390
Willy Tarreau295a8372011-03-10 11:25:07 +01001391 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001392 return 1;
1393
1394 default:
1395 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001396 si->applet.state = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001397 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001398 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001399}
1400
1401
Cyril Bonté70be45d2010-10-12 00:14:35 +02001402/* We don't want to land on the posted stats page because a refresh will
1403 * repost the data. We don't want this to happen on accident so we redirect
1404 * the browse to the stats page with a GET.
1405 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001406static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001407{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001408 struct session *s = si->applet.private;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001409 struct chunk msg;
1410
1411 chunk_init(&msg, trash, sizeof(trash));
1412
Willy Tarreau295a8372011-03-10 11:25:07 +01001413 switch (si->applet.state) {
1414 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001415 chunk_printf(&msg,
1416 "HTTP/1.0 303 See Other\r\n"
1417 "Cache-Control: no-cache\r\n"
1418 "Content-Type: text/plain\r\n"
1419 "Connection: close\r\n"
1420 "Location: %s;st=%s",
Willy Tarreau295a8372011-03-10 11:25:07 +01001421 uri->uri_prefix, si->applet.ctx.stats.st_code);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001422 chunk_printf(&msg, "\r\n\r\n");
1423
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001424 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001425 return 0;
1426
1427 s->txn.status = 303;
1428
1429 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1430 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1431 if (!(s->flags & SN_FINST_MASK))
1432 s->flags |= SN_FINST_R;
1433
Willy Tarreau295a8372011-03-10 11:25:07 +01001434 si->applet.state = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001435 return 1;
1436 }
1437 return 1;
1438}
1439
1440
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001441/* This I/O handler runs as an applet embedded in a stream interface. It is
1442 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001443 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001444 * automatically unregisters itself once transfer is complete.
1445 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001446static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001447{
Willy Tarreaubc4af052011-02-13 13:25:14 +01001448 struct session *s = si->applet.private;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001449 struct buffer *req = si->ob;
1450 struct buffer *res = si->ib;
1451
1452 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1453 goto out;
1454
1455 /* check that the output is not closed */
1456 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001457 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001458
Willy Tarreaubc4af052011-02-13 13:25:14 +01001459 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001460 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001461 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001462 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001463 si->shutw(si);
1464 }
1465 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001466 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001467 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001468 si->shutw(si);
1469 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001470 }
1471 }
1472
1473 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
1474 si->shutw(si);
1475
Willy Tarreaubc4af052011-02-13 13:25:14 +01001476 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001477 si->shutr(si);
1478 res->flags |= BF_READ_NULL;
1479 }
1480
1481 /* update all other flags and resync with the other side */
1482 si->update(si);
1483
1484 /* we don't want to expire timeouts while we're processing requests */
1485 si->ib->rex = TICK_ETERNITY;
1486 si->ob->wex = TICK_ETERNITY;
1487
1488 out:
1489 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1490 /* check that we have released everything then unregister */
1491 stream_int_unregister_handler(si);
1492 }
1493}
1494
1495
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001496/* This function dumps statistics in HTTP format onto the stream interface's
1497 * read buffer. The data_ctx must have been zeroed first, and the flags
1498 * properly set. It returns 0 if it had to stop writing data and an I/O is
1499 * needed, 1 if the dump is finished and the session must be closed, or -1
1500 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001501 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001502static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001503{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001504 struct session *s = si->applet.private;
1505 struct buffer *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001506 struct proxy *px;
1507 struct chunk msg;
1508 unsigned int up;
1509
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001510 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001511
Willy Tarreau295a8372011-03-10 11:25:07 +01001512 switch (si->applet.state) {
1513 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001514 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001515 "HTTP/1.0 200 OK\r\n"
1516 "Cache-Control: no-cache\r\n"
1517 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001518 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001519 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001520
Willy Tarreau295a8372011-03-10 11:25:07 +01001521 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001522 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001523 uri->refresh);
1524
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001525 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001526
1527 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001528 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001529 return 0;
1530
Willy Tarreau91861262007-10-17 17:06:05 +02001531 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1532 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1533 if (!(s->flags & SN_FINST_MASK))
1534 s->flags |= SN_FINST_R;
1535
1536 if (s->txn.meth == HTTP_METH_HEAD) {
1537 /* that's all we return in case of HEAD request */
Willy Tarreau295a8372011-03-10 11:25:07 +01001538 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001539 return 1;
1540 }
1541
Willy Tarreau295a8372011-03-10 11:25:07 +01001542 si->applet.state = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001543 /* fall through */
1544
Willy Tarreau295a8372011-03-10 11:25:07 +01001545 case STAT_ST_HEAD:
1546 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001547 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001548 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001549 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1550 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001551 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001552 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1553 "<style type=\"text/css\"><!--\n"
1554 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001555 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001556 " font-size: 12px;"
1557 " font-weight: normal;"
1558 " color: black;"
1559 " background: white;"
1560 "}\n"
1561 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001562 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001563 "}\n"
1564 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001565 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001566 " margin-bottom: 0.5em;"
1567 "}\n"
1568 "h2 {"
1569 " font-family: helvetica, arial;"
1570 " font-size: x-large;"
1571 " font-weight: bold;"
1572 " font-style: italic;"
1573 " color: #6020a0;"
1574 " margin-top: 0em;"
1575 " margin-bottom: 0em;"
1576 "}\n"
1577 "h3 {"
1578 " font-family: helvetica, arial;"
1579 " font-size: 16px;"
1580 " font-weight: bold;"
1581 " color: #b00040;"
1582 " background: #e8e8d0;"
1583 " margin-top: 0em;"
1584 " margin-bottom: 0em;"
1585 "}\n"
1586 "li {"
1587 " margin-top: 0.25em;"
1588 " margin-right: 2em;"
1589 "}\n"
1590 ".hr {margin-top: 0.25em;"
1591 " border-color: black;"
1592 " border-bottom-style: solid;"
1593 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001594 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001595 ".total {background: #20D0D0;color: #ffff80;}\n"
1596 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001597 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001598 ".backend {background: #e8e8d0;}\n"
1599 ".active0 {background: #ff9090;}\n"
1600 ".active1 {background: #ffd020;}\n"
1601 ".active2 {background: #ffffa0;}\n"
1602 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001603 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1604 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1605 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001606 ".backup0 {background: #ff9090;}\n"
1607 ".backup1 {background: #ff80ff;}\n"
1608 ".backup2 {background: #c060ff;}\n"
1609 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001610 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1611 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1612 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001613 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001614 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001615 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001616 "a.px:link {color: #ffff40; text-decoration: none;}"
1617 "a.px:visited {color: #ffff40; text-decoration: none;}"
1618 "a.px:hover {color: #ffffff; text-decoration: none;}"
1619 "a.lfsb:link {color: #000000; text-decoration: none;}"
1620 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1621 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1622 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001623 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001624 "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"
1625 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001626 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001627 "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 +02001628 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001629 "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 +02001630 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001631 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1632 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1633 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001634 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001635 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001636 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001637 (uri->flags&ST_SHNODE) ? " on " : "",
1638 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001639 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001640 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001641 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001642 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001643 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001644 return 0;
1645
Willy Tarreau295a8372011-03-10 11:25:07 +01001646 si->applet.state = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02001647 /* fall through */
1648
Willy Tarreau295a8372011-03-10 11:25:07 +01001649 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02001650 up = (now.tv_sec - start_date.tv_sec);
1651
1652 /* WARNING! this has to fit the first packet too.
1653 * We are around 3.5 kB, add adding entries will
1654 * become tricky if we want to support 4kB buffers !
1655 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001656 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001657 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001658 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1659 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001660 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001661 "<hr width=\"100%%\" class=\"hr\">\n"
1662 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001663 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001664 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001665 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001666 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1667 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
1668 "current conns = %d; current pipes = %d/%d<br>\n"
1669 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001670 "</td><td align=\"center\" nowrap>\n"
1671 "<table class=\"lgd\"><tr>\n"
1672 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1673 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1674 "</tr><tr>\n"
1675 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1676 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1677 "</tr><tr>\n"
1678 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1679 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1680 "</tr><tr>\n"
1681 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001682 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001683 "</tr><tr>\n"
1684 "<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 +02001685 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001686 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001687 "</td>"
1688 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1689 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1690 "",
1691 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001692 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1693 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1694 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001695 up / 86400, (up % 86400) / 3600,
1696 (up % 3600) / 60, (up % 60),
1697 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1698 global.rlimit_memmax ? " MB" : "",
1699 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001700 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001701 actconn, pipes_used, pipes_used+pipes_free,
1702 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +02001703 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001704
Willy Tarreau295a8372011-03-10 11:25:07 +01001705 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001706 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001707 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1708 uri->uri_prefix,
1709 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01001710 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001711 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001712 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001713 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1714 uri->uri_prefix,
1715 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01001716 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001717
Willy Tarreau55bb8452007-10-17 18:44:57 +02001718 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001719 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001720 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001721 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1722 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001723 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001724 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001725 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001726 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001727 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1728 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001729 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001730 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001731 }
Willy Tarreau91861262007-10-17 17:06:05 +02001732
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001733 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001734 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
1735 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001736 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
1737 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001738
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001739 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02001740 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
1741 uri->uri_prefix,
1742 (uri->refresh > 0) ? ";norefresh" : "");
1743
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001744 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001745 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001746 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1747 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
1748 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
1749 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
1750 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
1751 "</ul>"
1752 "</td>"
1753 "</tr></table>\n"
1754 ""
1755 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001756
Willy Tarreau295a8372011-03-10 11:25:07 +01001757 if (si->applet.ctx.stats.st_code) {
1758 if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001759 chunk_printf(&msg,
1760 "<p><div class=active3>"
1761 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1762 "Action processed successfully."
1763 "</div>\n", uri->uri_prefix);
1764 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001765 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_NONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001766 chunk_printf(&msg,
1767 "<p><div class=active2>"
1768 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1769 "Nothing has changed."
1770 "</div>\n", uri->uri_prefix);
1771 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001772 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_EXCD) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001773 chunk_printf(&msg,
1774 "<p><div class=active0>"
1775 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1776 "<b>Action not processed : the buffer couldn't store all the data.<br>"
1777 "You should retry with less servers at a time.</b>"
1778 "</div>\n", uri->uri_prefix);
1779 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001780 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DENY) == 0) {
Cyril Bonté474be412010-10-12 00:14:36 +02001781 chunk_printf(&msg,
1782 "<p><div class=active0>"
1783 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1784 "<b>Action denied.</b>"
1785 "</div>\n", uri->uri_prefix);
1786 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001787 else {
1788 chunk_printf(&msg,
1789 "<p><div class=active6>"
1790 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1791 "Unexpected result."
1792 "</div>\n", uri->uri_prefix);
1793 }
1794 chunk_printf(&msg,"<p>\n");
1795 }
1796
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001797 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001798 return 0;
1799 }
Willy Tarreau91861262007-10-17 17:06:05 +02001800
Willy Tarreau295a8372011-03-10 11:25:07 +01001801 si->applet.ctx.stats.px = proxy;
1802 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1803 si->applet.state = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02001804 /* fall through */
1805
Willy Tarreau295a8372011-03-10 11:25:07 +01001806 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02001807 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001808 while (si->applet.ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001809 if (buffer_almost_full(rep))
1810 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01001811 px = si->applet.ctx.stats.px;
Willy Tarreau91861262007-10-17 17:06:05 +02001812 /* skip the disabled proxies and non-networked ones */
1813 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001814 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001815 return 0;
1816
Willy Tarreau295a8372011-03-10 11:25:07 +01001817 si->applet.ctx.stats.px = px->next;
1818 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02001819 }
1820 /* here, we just have reached the last proxy */
1821
Willy Tarreau295a8372011-03-10 11:25:07 +01001822 si->applet.state = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02001823 /* fall through */
1824
Willy Tarreau295a8372011-03-10 11:25:07 +01001825 case STAT_ST_END:
1826 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001827 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001828 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001829 return 0;
1830 }
Willy Tarreau91861262007-10-17 17:06:05 +02001831
Willy Tarreau295a8372011-03-10 11:25:07 +01001832 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001833 /* fall through */
1834
Willy Tarreau295a8372011-03-10 11:25:07 +01001835 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02001836 return 1;
1837
1838 default:
1839 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001840 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001841 return -1;
1842 }
1843}
1844
1845
1846/*
1847 * Dumps statistics for a proxy.
1848 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1849 * ot non-zero if everything completed.
1850 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001851static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001852{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001853 struct session *s = si->applet.private;
1854 struct buffer *rep = si->ib;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001855 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001856 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02001857 struct chunk msg;
1858
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001859 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001860
Willy Tarreau295a8372011-03-10 11:25:07 +01001861 switch (si->applet.ctx.stats.px_st) {
1862 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02001863 /* we are on a new proxy */
1864
1865 if (uri && uri->scope) {
1866 /* we have a limited scope, we have to check the proxy name */
1867 struct stat_scope *scope;
1868 int len;
1869
1870 len = strlen(px->id);
1871 scope = uri->scope;
1872
1873 while (scope) {
1874 /* match exact proxy name */
1875 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1876 break;
1877
1878 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001879 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001880 break;
1881 scope = scope->next;
1882 }
1883
1884 /* proxy name not found : don't dump anything */
1885 if (scope == NULL)
1886 return 1;
1887 }
1888
Willy Tarreau295a8372011-03-10 11:25:07 +01001889 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
1890 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001891 return 1;
1892
Willy Tarreau295a8372011-03-10 11:25:07 +01001893 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02001894 /* fall through */
1895
Willy Tarreau295a8372011-03-10 11:25:07 +01001896 case STAT_PX_ST_TH:
1897 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
1898 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001899 /* A form to enable/disable this proxy servers */
1900 chunk_printf(&msg,
1901 "<form action=\"%s\" method=\"post\">",
1902 uri->uri_prefix);
1903 }
1904
Willy Tarreau55bb8452007-10-17 18:44:57 +02001905 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001906 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001907 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001908 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001909 "<th class=\"pxname\" width=\"10%%\"");
1910
1911 if (uri->flags&ST_SHLGNDS) {
1912 /* cap, mode, id */
1913 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
1914 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1915 px->uuid);
1916
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001917 chunk_printf(&msg, "\"");
1918 }
1919
1920 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001921 ">%s<a name=\"%s\"></a>"
1922 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001923 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001924 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001925 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001926 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001927 "<tr class=\"titre\">",
1928 (uri->flags & ST_SHLGNDS)?"<u>":"",
1929 px->id, px->id, px->id,
1930 (uri->flags & ST_SHLGNDS)?"</u>":"",
1931 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1932
Willy Tarreau295a8372011-03-10 11:25:07 +01001933 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001934 /* Column heading for Enable or Disable server */
1935 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
1936 }
1937
1938 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001939 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001940 "<th colspan=3>Queue</th>"
1941 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001942 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001943 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001944 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001945 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001946 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001947 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001948 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001949 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001950 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001951 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001952 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001953 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1954 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001955 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001956
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001957 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001958 return 0;
1959 }
Willy Tarreau91861262007-10-17 17:06:05 +02001960
Willy Tarreau295a8372011-03-10 11:25:07 +01001961 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02001962 /* fall through */
1963
Willy Tarreau295a8372011-03-10 11:25:07 +01001964 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02001965 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001966 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01001967 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
1968 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001969 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001970 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02001971 "<tr class=\"frontend\">");
1972
Willy Tarreau295a8372011-03-10 11:25:07 +01001973 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001974 /* Column sub-heading for Enable or Disable server */
1975 chunk_printf(&msg, "<td></td>");
1976 }
1977
1978 chunk_printf(&msg,
1979 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001980 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001981 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
1982 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01001983 "",
1984 px->id, px->id);
1985
1986 if (px->mode == PR_MODE_HTTP) {
1987 chunk_printf(&msg,
1988 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01001989 "<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 +01001990 "",
1991 read_freq_ctr(&px->fe_req_per_sec),
1992 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001993 px->fe_counters.p.http.rps_max,
1994 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01001995 LIM2A2(px->fe_sps_lim, "-"));
1996 } else {
1997 chunk_printf(&msg,
1998 /* sessions rate : current, max, limit */
1999 "<td>%s</td><td>%s</td><td>%s</td>"
2000 "",
2001 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002002 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01002003 }
2004
2005 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002006 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002007 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002008 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002009 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002010 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002011
2012 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2013 if (px->mode == PR_MODE_HTTP) {
2014 int i;
2015
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002016 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002017
2018 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002019 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002020
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002021 chunk_printf(&msg, " other=%lld\"", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002022 }
2023
2024 chunk_printf(&msg,
2025 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002026 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002027 /* bytes : in, out */
2028 "<td>%s</td><td>%s</td>"
2029 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002030 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002031 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002032 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002033 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002034
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002035 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002036 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002037 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002038 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002039 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002040 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002041 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002042 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002043 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002044 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002045 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002046 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002047 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2048 U2H2(px->fe_counters.failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +02002049 px->state == PR_STRUN ? "OPEN" :
2050 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002051 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002052 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002053 /* pxid, name, queue cur, queue max, */
2054 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002055 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002056 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002057 /* bytes : in, out */
2058 "%lld,%lld,"
2059 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002060 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002061 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002062 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002063 /* warnings: retries, redispatches */
2064 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002065 /* server status : reflect frontend status */
2066 "%s,"
2067 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002068 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002069 /* pid, iid, sid, throttle, lbtot, tracked, type */
2070 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002071 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002072 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002073 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002074 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002075 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002076 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2077 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2078 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2079 px->fe_counters.failed_req,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002080 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +01002081 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002082 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002083 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002084 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002085
2086 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2087 if (px->mode == PR_MODE_HTTP) {
2088 int i;
2089
2090 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002091 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002092
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002093 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002094 } else {
2095 chunk_printf(&msg, ",,,,,,");
2096 }
2097
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002098 /* failed health analyses */
2099 chunk_printf(&msg, ",");
2100
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002101 /* requests : req_rate, req_rate_max, req_tot, */
2102 chunk_printf(&msg, "%u,%u,%lld,",
2103 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002104 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002105
Willy Tarreauae526782010-03-04 20:34:23 +01002106 /* errors: cli_aborts, srv_aborts */
2107 chunk_printf(&msg, ",,");
2108
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002109 /* finish with EOL */
2110 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002111 }
Willy Tarreau91861262007-10-17 17:06:05 +02002112
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002113 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002114 return 0;
2115 }
2116
Willy Tarreau295a8372011-03-10 11:25:07 +01002117 si->applet.ctx.stats.l = px->listen; /* may be NULL */
2118 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002119 /* fall through */
2120
Willy Tarreau295a8372011-03-10 11:25:07 +01002121 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002122 /* stats.l has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002123 for (; si->applet.ctx.stats.l != NULL; si->applet.ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002124 if (buffer_almost_full(rep))
2125 return 0;
2126
Willy Tarreau295a8372011-03-10 11:25:07 +01002127 l = si->applet.ctx.stats.l;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002128 if (!l->counters)
2129 continue;
2130
Willy Tarreau295a8372011-03-10 11:25:07 +01002131 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2132 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002133 break;
2134
Willy Tarreau295a8372011-03-10 11:25:07 +01002135 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002136 continue;
2137 }
2138
Willy Tarreau295a8372011-03-10 11:25:07 +01002139 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002140 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002141 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002142 /* Column sub-heading for Enable or Disable server */
2143 chunk_printf(&msg, "<td></td>");
2144 }
2145 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002146
2147 if (uri->flags&ST_SHLGNDS) {
2148 char str[INET6_ADDRSTRLEN], *fmt = NULL;
2149 int port;
2150
2151 chunk_printf(&msg, " title=\"IP: ");
2152
2153 port = (l->addr.ss_family == AF_INET6)
2154 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
2155 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
2156
2157 if (l->addr.ss_family == AF_INET) {
2158 if (inet_ntop(AF_INET,
2159 (const void *)&((struct sockaddr_in *)&l->addr)->sin_addr,
2160 str, sizeof(str)))
2161 fmt = "%s:%d";
2162 } else {
2163 if (inet_ntop(AF_INET6,
2164 (const void *)&((struct sockaddr_in6 *)(&l->addr))->sin6_addr,
2165 str, sizeof(str)))
2166 fmt = "[%s]:%d";
2167 }
2168
2169 if (fmt)
2170 chunk_printf(&msg, fmt, str, port);
2171 else
2172 chunk_printf(&msg, "(%s)", strerror(errno));
2173
2174 /* id */
2175 chunk_printf(&msg, ", id: %d", l->luid);
2176
2177 chunk_printf(&msg, "\"");
2178 }
2179
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002180 chunk_printf(&msg,
2181 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002182 ">%s<a name=\"%s/+%s\"></a>"
2183 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002184 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002185 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002186 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002187 "<td>%s</td><td>%s</td><td>%s</td>"
2188 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002189 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002190 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002191 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002192 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002193 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002194 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002195 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2196 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2197
2198 chunk_printf(&msg,
2199 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002200 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002201 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002202 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002203 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002204 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002205 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002206 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002207 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002208 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002209 "",
2210 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2211 U2H2(l->counters->failed_req),
2212 (l->nbconn < l->maxconn) ? "OPEN" : "FULL");
2213 } else {
2214 chunk_printf(&msg,
2215 /* pxid, name, queue cur, queue max, */
2216 "%s,%s,,,"
2217 /* sessions: current, max, limit, total */
2218 "%d,%d,%d,%lld,"
2219 /* bytes: in, out */
2220 "%lld,%lld,"
2221 /* denied: req, resp */
2222 "%lld,%lld,"
2223 /* errors: request, connect, response */
2224 "%lld,,,"
2225 /* warnings: retries, redispatches */
2226 ",,"
2227 /* server status: reflect listener status */
2228 "%s,"
2229 /* rest of server: nothing */
2230 ",,,,,,,,"
2231 /* pid, iid, sid, throttle, lbtot, tracked, type */
2232 "%d,%d,%d,,,,%d,"
2233 /* rate, rate_lim, rate_max */
2234 ",,,"
2235 /* check_status, check_code, check_duration */
2236 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002237 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2238 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002239 /* failed health analyses */
2240 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002241 /* requests : req_rate, req_rate_max, req_tot, */
2242 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002243 /* errors: cli_aborts, srv_aborts */
2244 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002245 "\n",
2246 px->id, l->name,
2247 l->nbconn, l->counters->conn_max,
2248 l->maxconn, l->counters->cum_conn,
2249 l->counters->bytes_in, l->counters->bytes_out,
2250 l->counters->denied_req, l->counters->denied_resp,
2251 l->counters->failed_req,
2252 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2253 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2254 }
2255
2256 if (buffer_feed_chunk(rep, &msg) >= 0)
2257 return 0;
2258 }
2259
Willy Tarreau295a8372011-03-10 11:25:07 +01002260 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2261 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002262 /* fall through */
2263
Willy Tarreau295a8372011-03-10 11:25:07 +01002264 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002265 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002266 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002267 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 +02002268
Willy Tarreau4e33d862009-10-11 23:35:10 +02002269 if (buffer_almost_full(rep))
2270 return 0;
2271
Willy Tarreau295a8372011-03-10 11:25:07 +01002272 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002273
Willy Tarreau295a8372011-03-10 11:25:07 +01002274 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2275 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002276 break;
2277
Willy Tarreau295a8372011-03-10 11:25:07 +01002278 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002279 continue;
2280 }
2281
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002282 if (sv->tracked)
2283 svs = sv->tracked;
2284 else
2285 svs = sv;
2286
Willy Tarreau91861262007-10-17 17:06:05 +02002287 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002288 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002289 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002290 else if (svs->state & SRV_RUNNING) {
2291 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002292 sv_state = 3; /* UP */
2293 else
2294 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002295
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002296 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002297 sv_state += 2;
2298 }
Willy Tarreau91861262007-10-17 17:06:05 +02002299 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002300 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002301 sv_state = 1; /* going up */
2302 else
2303 sv_state = 0; /* DOWN */
2304
Willy Tarreau295a8372011-03-10 11:25:07 +01002305 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002306 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002307 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002308 continue;
2309 }
2310
Willy Tarreau295a8372011-03-10 11:25:07 +01002311 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002312 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2313 "UP %d/%d &darr;", "UP",
2314 "NOLB %d/%d &darr;", "NOLB",
2315 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002316 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002317 chunk_printf(&msg,
2318 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002319 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002320 );
2321 }
2322 else {
2323 chunk_printf(&msg,
2324 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002325 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002326 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2327 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002328
Willy Tarreau295a8372011-03-10 11:25:07 +01002329 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002330 chunk_printf(&msg,
2331 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2332 sv->id);
2333 }
2334
2335 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002336
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002337 if (uri->flags&ST_SHLGNDS) {
2338 char str[INET6_ADDRSTRLEN];
2339
2340 chunk_printf(&msg, " title=\"IP: ");
2341
2342 /* IP */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002343 switch (sv->addr.ss_family) {
2344 case AF_INET:
2345 if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&sv->addr)->sin_addr, str, sizeof(str)))
2346 chunk_printf(&msg, "%s:%d", str, htons(((struct sockaddr_in *)&sv->addr)->sin_port));
2347 else
2348 chunk_printf(&msg, "(%s)", strerror(errno));
2349 break;
2350 case AF_INET6:
2351 if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&sv->addr)->sin6_addr, str, sizeof(str)))
2352 chunk_printf(&msg, "%s:%d", str, htons(((struct sockaddr_in6 *)&sv->addr)->sin6_port));
2353 else
2354 chunk_printf(&msg, "(%s)", strerror(errno));
2355 break;
2356 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002357
2358 /* id */
2359 chunk_printf(&msg, ", id: %d", sv->puid);
2360
2361 /* cookie */
2362 if (sv->cookie) {
2363 struct chunk src;
2364
2365 chunk_printf(&msg, ", cookie: '");
2366
2367 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2368 chunk_htmlencode(&msg, &src);
2369
2370 chunk_printf(&msg, "'");
2371 }
2372
2373 chunk_printf(&msg, "\"");
2374 }
2375
2376 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002377 ">%s<a name=\"%s/%s\"></a>"
2378 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002379 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002380 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002381 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002382 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002383 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002384 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002385 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002386 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002387 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002388 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002389 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002390 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2391 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002392 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2393
2394 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2395 if (px->mode == PR_MODE_HTTP) {
2396 int i;
2397
2398 chunk_printf(&msg, " title=\"rsp codes:");
2399
2400 for (i = 1; i < 6; i++)
2401 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2402
2403 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2404 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002405
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002406 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002407 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002408 ">%s%s%s</td><td>%s</td>",
2409 (px->mode == PR_MODE_HTTP)?"<u>":"",
2410 U2H0(sv->counters.cum_sess),
2411 (px->mode == PR_MODE_HTTP)?"</u>":"",
2412 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002413
2414 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002415 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002416 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002417 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002418 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002419 /* errors : request, connect */
2420 "<td></td><td>%s</td>"
2421 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002422 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002423 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002424 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002425 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002426 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2427 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002428 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002429 sv->counters.cli_aborts,
2430 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002431 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002432 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002433
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002434 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002435 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002436
Cyril Bontécd19e512010-01-31 22:34:03 +01002437 if (sv->state & SRV_MAINTAIN) {
2438 chunk_printf(&msg, "%s ",
2439 human_time(now.tv_sec - sv->last_change, 1));
2440 chunk_printf(&msg, "MAINT");
2441 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002442 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2443 chunk_printf(&msg, "%s ",
2444 human_time(now.tv_sec - svs->last_change, 1));
2445 chunk_printf(&msg, "MAINT(via)");
2446 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002447 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002448 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002449 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002450
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002451 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002452 srv_hlt_st[sv_state],
2453 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2454 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002455 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002456
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002457 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002458 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002459 get_check_status_description(sv->check_status));
2460
2461 if (*sv->check_desc) {
2462 struct chunk src;
2463
2464 chunk_printf(&msg, ": ");
2465
2466 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2467 chunk_htmlencode(&msg, &src);
2468 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002469
Willy Tarreaue0454092010-02-26 12:29:07 +01002470 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002471 tv_iszero(&sv->check_start)?"":"* ",
2472 get_check_status_info(sv->check_status));
2473
2474 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002475 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002476
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002477 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002478 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002479 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002480 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002481
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002482 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002483 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002484 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002485 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002486 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002487 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002488 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002489 (sv->state & SRV_BACKUP) ? "-" : "Y",
2490 (sv->state & SRV_BACKUP) ? "Y" : "-");
2491
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002492 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002493 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002494 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002495 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2496
2497 if (svs->observe)
2498 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2499
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002500 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002501 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002502 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002503 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002504 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002505 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002506 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002507 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2508 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002509 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002510 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002511 "<td colspan=3></td>");
2512
2513 /* throttle */
2514 if ((sv->state & SRV_WARMINGUP) &&
2515 now.tv_sec < sv->last_change + sv->slowstart &&
2516 now.tv_sec >= sv->last_change) {
2517 unsigned int ratio;
2518 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002519 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002520 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002521 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002522 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002523 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002524 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002525 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002526 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2527 "UP %d/%d,", "UP,",
2528 "NOLB %d/%d,", "NOLB,",
2529 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002530 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002531 /* pxid, name */
2532 "%s,%s,"
2533 /* queue : current, max */
2534 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002535 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002536 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002537 /* bytes : in, out */
2538 "%lld,%lld,"
2539 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002540 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002541 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002542 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002543 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002544 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002545 "",
2546 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002547 sv->nbpend, sv->counters.nbpend_max,
2548 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002549 sv->counters.bytes_in, sv->counters.bytes_out,
2550 sv->counters.failed_secu,
2551 sv->counters.failed_conns, sv->counters.failed_resp,
2552 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002553
Willy Tarreau55bb8452007-10-17 18:44:57 +02002554 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002555 if (sv->state & SRV_MAINTAIN) {
2556 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002557 }
2558 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2559 chunk_printf(&msg, "MAINT(via),");
2560 }
2561 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002562 chunk_printf(&msg,
2563 srv_hlt_st[sv_state],
2564 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2565 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2566 }
Willy Tarreau91861262007-10-17 17:06:05 +02002567
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002568 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002569 /* weight, active, backup */
2570 "%d,%d,%d,"
2571 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002572 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002573 (sv->state & SRV_BACKUP) ? 0 : 1,
2574 (sv->state & SRV_BACKUP) ? 1 : 0);
2575
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002576 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002577 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002578 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002579 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002580 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002581 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002582 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002583 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002584 ",,,,");
2585
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002586 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002587 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002588 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002589 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002590 LIM2A0(sv->maxqueue, ""),
2591 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002592
2593 /* throttle */
2594 if ((sv->state & SRV_WARMINGUP) &&
2595 now.tv_sec < sv->last_change + sv->slowstart &&
2596 now.tv_sec >= sv->last_change) {
2597 unsigned int ratio;
2598 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002599 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002600 }
2601
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002602 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002603 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002604
2605 /* tracked */
2606 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002607 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002608 sv->tracked->proxy->id, sv->tracked->id);
2609 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002610 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002611
Willy Tarreau7f062c42009-03-05 18:43:00 +01002612 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002613 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002614
2615 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002616 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002617 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002618 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002619
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002620 if (sv->state & SRV_CHECKED) {
2621 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002622 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002623
2624 /* check_code */
2625 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002626 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002627 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002628 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002629
2630 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002631 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002632 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002633 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002634 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002635
2636 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002637 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002638 }
2639
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002640 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2641 if (px->mode == PR_MODE_HTTP) {
2642 int i;
2643
2644 for (i=1; i<6; i++)
2645 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2646
2647 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2648 } else {
2649 chunk_printf(&msg, ",,,,,,");
2650 }
2651
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002652 /* failed health analyses */
2653 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2654
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002655 /* requests : req_rate, req_rate_max, req_tot, */
2656 chunk_printf(&msg, ",,,");
2657
Willy Tarreauae526782010-03-04 20:34:23 +01002658 /* errors: cli_aborts, srv_aborts */
2659 chunk_printf(&msg, "%lld,%lld,",
2660 sv->counters.cli_aborts, sv->counters.srv_aborts);
2661
Willy Tarreau7f062c42009-03-05 18:43:00 +01002662 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002663 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002664 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002665 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002666 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002667 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002668
Willy Tarreau295a8372011-03-10 11:25:07 +01002669 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02002670 /* fall through */
2671
Willy Tarreau295a8372011-03-10 11:25:07 +01002672 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02002673 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002674 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002675 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2676 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002677 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01002678 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002679 /* Column sub-heading for Enable or Disable server */
2680 chunk_printf(&msg, "<td></td>");
2681 }
2682 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002683
2684 if (uri->flags&ST_SHLGNDS) {
2685 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002686 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002687 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002688
2689 /* cookie */
2690 if (px->cookie_name) {
2691 struct chunk src;
2692
2693 chunk_printf(&msg, ", cookie: '");
2694
2695 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2696 chunk_htmlencode(&msg, &src);
2697
2698 chunk_printf(&msg, "'");
2699 }
2700
2701 chunk_printf(&msg, "\"");
2702
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002703 }
2704
2705 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002706 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01002707 ">%s<a name=\"%s/Backend\"></a>"
2708 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002709 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002710 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002711 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002712 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002713 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002714 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002715 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002716 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002717 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
2718 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02002719
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002720 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002721 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002722 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002723 "<td"
2724 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002725 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002726
2727 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2728 if (px->mode == PR_MODE_HTTP) {
2729 int i;
2730
2731 chunk_printf(&msg, " title=\"rsp codes:");
2732
2733 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002734 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002735
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002736 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002737 }
2738
2739 chunk_printf(&msg,
2740 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002741 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002742 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002743 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002744 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002745 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002746 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01002747 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002748 U2H7(px->be_counters.cum_lbconn),
2749 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002750
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002751 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002752 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002753 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002754 /* errors : request, connect */
2755 "<td></td><td>%s</td>"
2756 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002757 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002758 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002759 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002760 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02002761 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002762 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02002763 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002764 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2765 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002766 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002767 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
2768 U2H2(px->be_counters.failed_conns),
2769 px->be_counters.cli_aborts,
2770 px->be_counters.srv_aborts,
2771 U2H5(px->be_counters.failed_resp),
2772 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002773 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01002774 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2775 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01002776 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002777 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002778
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002779 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002780 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002781 "<td class=ac>&nbsp;</td><td>%d</td>"
2782 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002783 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002784 "</tr>",
2785 px->down_trans,
2786 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002787 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002788 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002789 /* pxid, name */
2790 "%s,BACKEND,"
2791 /* queue : current, max */
2792 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002793 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002794 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002795 /* bytes : in, out */
2796 "%lld,%lld,"
2797 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002798 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002799 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002800 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002801 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002802 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002803 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02002804 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002805 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02002806 * active and backups. */
2807 "%s,"
2808 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002809 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002810 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002811 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002812 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002813 /* rate, rate_lim, rate_max, */
2814 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002815 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002816 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002817 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002818 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2819 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2820 px->be_counters.bytes_in, px->be_counters.bytes_out,
2821 px->be_counters.denied_req, px->be_counters.denied_resp,
2822 px->be_counters.failed_conns, px->be_counters.failed_resp,
2823 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01002824 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01002825 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002826 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002827 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01002828 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002829 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002830 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002831 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002832 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002833
2834 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2835 if (px->mode == PR_MODE_HTTP) {
2836 int i;
2837
2838 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002839 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002840
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002841 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002842 } else {
2843 chunk_printf(&msg, ",,,,,,");
2844 }
2845
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002846 /* failed health analyses */
2847 chunk_printf(&msg, ",");
2848
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002849 /* requests : req_rate, req_rate_max, req_tot, */
2850 chunk_printf(&msg, ",,,");
2851
Willy Tarreauae526782010-03-04 20:34:23 +01002852 /* errors: cli_aborts, srv_aborts */
2853 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002854 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01002855
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002856 /* finish with EOL */
2857 chunk_printf(&msg, "\n");
2858
Willy Tarreau55bb8452007-10-17 18:44:57 +02002859 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002860 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002861 return 0;
2862 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002863
Willy Tarreau295a8372011-03-10 11:25:07 +01002864 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002865 /* fall through */
2866
Willy Tarreau295a8372011-03-10 11:25:07 +01002867 case STAT_PX_ST_END:
2868 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002869 chunk_printf(&msg, "</table>");
2870
Willy Tarreau295a8372011-03-10 11:25:07 +01002871 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002872 /* close the form used to enable/disable this proxy servers */
2873 chunk_printf(&msg,
2874 "Choose the action to perform on the checked servers : "
2875 "<select name=action>"
2876 "<option value=\"\"></option>"
2877 "<option value=\"disable\">Disable</option>"
2878 "<option value=\"enable\">Enable</option>"
2879 "</select>"
2880 "<input type=\"hidden\" name=\"b\" value=\"%s\">"
2881 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2882 "</form>",
2883 px->id);
2884 }
2885
2886 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02002887
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002888 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002889 return 0;
2890 }
Willy Tarreau91861262007-10-17 17:06:05 +02002891
Willy Tarreau295a8372011-03-10 11:25:07 +01002892 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002893 /* fall through */
2894
Willy Tarreau295a8372011-03-10 11:25:07 +01002895 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002896 return 1;
2897
2898 default:
2899 /* unknown state, we should put an abort() here ! */
2900 return 1;
2901 }
2902}
2903
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002904/* This function dumps a complete session state onto the stream intreface's
2905 * read buffer. The data_ctx must have been zeroed first, and the flags
2906 * properly set. The session has to be set in data_ctx.sess.target. It returns
2907 * 0 if the output buffer is full and it needs to be called again, otherwise
2908 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002909 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002910static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002911{
2912 struct tm tm;
2913 struct chunk msg;
2914 struct session *sess;
2915 extern const char *monthname[12];
2916 char pn[INET6_ADDRSTRLEN];
2917
2918 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau295a8372011-03-10 11:25:07 +01002919 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002920
Willy Tarreau295a8372011-03-10 11:25:07 +01002921 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002922 /* session changed, no need to go any further */
2923 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002924 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002925 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002926 si->applet.ctx.sess.target = NULL;
2927 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002928 return 1;
2929 }
2930
Willy Tarreau295a8372011-03-10 11:25:07 +01002931 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002932 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01002933 si->applet.ctx.sess.uid = sess->uniq_id;
2934 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002935 /* fall through */
2936
2937 case 1:
2938 chunk_printf(&msg,
2939 "%p: id=%u, proto=%s",
2940 sess,
2941 sess->uniq_id,
2942 sess->listener->proto->name);
2943
2944 switch (sess->listener->proto->sock_family) {
2945 case AF_INET:
2946 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002947 (const void *)&((struct sockaddr_in *)&sess->si[0].addr.c.from)->sin_addr,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002948 pn, sizeof(pn));
2949
2950 chunk_printf(&msg,
2951 " source=%s:%d\n",
2952 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002953 ntohs(((struct sockaddr_in *)&sess->si[0].addr.c.from)->sin_port));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002954 break;
2955 case AF_INET6:
2956 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002957 (const void *)&((struct sockaddr_in6 *)(&sess->si[0].addr.c.from))->sin6_addr,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002958 pn, sizeof(pn));
2959
2960 chunk_printf(&msg,
2961 " source=%s:%d\n",
2962 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002963 ntohs(((struct sockaddr_in6 *)&sess->si[0].addr.c.from)->sin6_port));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002964 break;
2965 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02002966 chunk_printf(&msg,
2967 " source=unix:%d\n", sess->listener->luid);
2968 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002969 default:
2970 /* no more information to print right now */
2971 chunk_printf(&msg, "\n");
2972 break;
2973 }
2974
2975 chunk_printf(&msg,
2976 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02002977 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002978
2979 chunk_printf(&msg,
2980 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)\n",
2981 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
2982 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
2983 sess->listener ? sess->listener->luid : 0);
2984
2985 chunk_printf(&msg,
2986 " backend=%s (id=%u mode=%s) server=%s (id=%u)\n",
2987 sess->be->id, sess->be->uuid, sess->be->mode ? "http" : "tcp",
Willy Tarreau827aee92011-03-10 16:55:02 +01002988 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
2989 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002990
2991 chunk_printf(&msg,
2992 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
2993 sess->task,
2994 sess->task->state,
2995 sess->task->nice, sess->task->calls,
2996 sess->task->expire ?
2997 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
2998 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
2999 TICKS_TO_MS(1000)) : "<NEVER>",
3000 task_in_rq(sess->task) ? ", running" : "");
3001
3002 get_localtime(sess->logs.accept_date.tv_sec, &tm);
3003 chunk_printf(&msg,
3004 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
3005 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3006 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3007 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3008
3009 chunk_printf(&msg,
3010 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3011 &sess->si[0],
3012 sess->si[0].state,
3013 sess->si[0].flags,
3014 sess->si[0].fd,
3015 sess->si[0].exp ?
3016 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3017 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3018 TICKS_TO_MS(1000)) : "<NEVER>",
3019 sess->si[0].err_type);
3020
3021 chunk_printf(&msg,
3022 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3023 &sess->si[1],
3024 sess->si[1].state,
3025 sess->si[1].flags,
3026 sess->si[1].fd,
3027 sess->si[1].exp ?
3028 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3029 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3030 TICKS_TO_MS(1000)) : "<NEVER>",
3031 sess->si[1].err_type);
3032
3033 chunk_printf(&msg,
3034 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3035 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3036 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3037
3038
3039 chunk_printf(&msg,
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02003040 " req=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003041 " an_exp=%s",
3042 sess->req,
3043 sess->req->flags, sess->req->analysers,
3044 sess->req->l, sess->req->send_max,
3045 sess->req->pipe ? sess->req->pipe->data : 0,
3046 sess->req->to_forward,
3047 sess->req->analyse_exp ?
3048 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3049 TICKS_TO_MS(1000)) : "<NEVER>");
3050
3051 chunk_printf(&msg,
3052 " rex=%s",
3053 sess->req->rex ?
3054 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3055 TICKS_TO_MS(1000)) : "<NEVER>");
3056
3057 chunk_printf(&msg,
3058 " wex=%s\n"
3059 " data=%p r=%d w=%d lr=%d total=%lld\n",
3060 sess->req->wex ?
3061 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3062 TICKS_TO_MS(1000)) : "<NEVER>",
3063 sess->req->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01003064 (int)(sess->req->r - sess->req->data),
3065 (int)(sess->req->w - sess->req->data),
3066 (int)(sess->req->lr - sess->req->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003067 sess->req->total);
3068
3069 chunk_printf(&msg,
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02003070 " res=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003071 " an_exp=%s",
3072 sess->rep,
3073 sess->rep->flags, sess->rep->analysers,
3074 sess->rep->l, sess->rep->send_max,
3075 sess->rep->pipe ? sess->rep->pipe->data : 0,
3076 sess->rep->to_forward,
3077 sess->rep->analyse_exp ?
3078 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3079 TICKS_TO_MS(1000)) : "<NEVER>");
3080
3081 chunk_printf(&msg,
3082 " rex=%s",
3083 sess->rep->rex ?
3084 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3085 TICKS_TO_MS(1000)) : "<NEVER>");
3086
3087 chunk_printf(&msg,
3088 " wex=%s\n"
3089 " data=%p r=%d w=%d lr=%d total=%lld\n",
3090 sess->rep->wex ?
3091 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3092 TICKS_TO_MS(1000)) : "<NEVER>",
3093 sess->rep->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01003094 (int)(sess->rep->r - sess->rep->data),
3095 (int)(sess->rep->w - sess->rep->data),
3096 (int)(sess->rep->lr - sess->rep->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003097 sess->rep->total);
3098
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003099 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003100 return 0;
3101
3102 /* use other states to dump the contents */
3103 }
3104 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003105 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003106 return 1;
3107}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003108
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003109/* This function dumps all sessions' states onto the stream intreface's
3110 * read buffer. The data_ctx must have been zeroed first, and the flags
3111 * properly set. It returns 0 if the output buffer is full and it needs
3112 * to be called again, otherwise non-zero. It is designed to be called
3113 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003114 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003115static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003116{
3117 struct chunk msg;
3118
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003119 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003120 /* If we're forced to shut down, we might have to remove our
3121 * reference to the last session being dumped.
3122 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003123 if (si->applet.state == STAT_ST_LIST) {
3124 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3125 LIST_DEL(&si->applet.ctx.sess.bref.users);
3126 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003127 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003128 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003129 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003130 }
3131
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003132 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003133
Willy Tarreau295a8372011-03-10 11:25:07 +01003134 switch (si->applet.state) {
3135 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003136 /* the function had not been called yet, let's prepare the
3137 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003138 * pointer to the first in the global list. When a target
3139 * session is being destroyed, it is responsible for updating
3140 * this pointer. We know we have reached the end when this
3141 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003142 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003143 LIST_INIT(&si->applet.ctx.sess.bref.users);
3144 si->applet.ctx.sess.bref.ref = sessions.n;
3145 si->applet.state = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003146 /* fall through */
3147
Willy Tarreau295a8372011-03-10 11:25:07 +01003148 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003149 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003150 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3151 LIST_DEL(&si->applet.ctx.sess.bref.users);
3152 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003153 }
3154
3155 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003156 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003157 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003158 struct session *curr_sess;
3159
Willy Tarreau295a8372011-03-10 11:25:07 +01003160 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003161
Willy Tarreau295a8372011-03-10 11:25:07 +01003162 if (si->applet.ctx.sess.target) {
3163 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003164 goto next_sess;
3165
Willy Tarreau295a8372011-03-10 11:25:07 +01003166 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003167 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003168 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003169 return 0;
3170
3171 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003172 LIST_DEL(&si->applet.ctx.sess.bref.users);
3173 LIST_INIT(&si->applet.ctx.sess.bref.users);
3174 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003175 break;
3176 }
3177
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003178 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003179 "%p: proto=%s",
3180 curr_sess,
3181 curr_sess->listener->proto->name);
3182
3183 switch (curr_sess->listener->proto->sock_family) {
3184 case AF_INET:
3185 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003186 (const void *)&((struct sockaddr_in *)&curr_sess->si[0].addr.c.from)->sin_addr,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003187 pn, sizeof(pn));
3188
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003189 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003190 " src=%s:%d fe=%s be=%s srv=%s",
3191 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003192 ntohs(((struct sockaddr_in *)&curr_sess->si[0].addr.c.from)->sin_port),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003193 curr_sess->fe->id,
3194 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01003195 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003196 );
3197 break;
3198 case AF_INET6:
3199 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003200 (const void *)&((struct sockaddr_in6 *)(&curr_sess->si[0].addr.c.from))->sin6_addr,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003201 pn, sizeof(pn));
3202
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003203 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003204 " src=%s:%d fe=%s be=%s srv=%s",
3205 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003206 ntohs(((struct sockaddr_in6 *)&curr_sess->si[0].addr.c.from)->sin6_port),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003207 curr_sess->fe->id,
3208 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01003209 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003210 );
3211
3212 break;
3213 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02003214 chunk_printf(&msg,
3215 " src=unix:%d fe=%s be=%s srv=%s",
3216 curr_sess->listener->luid,
3217 curr_sess->fe->id,
3218 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01003219 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003220 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003221 break;
3222 }
3223
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003224 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003225 " ts=%02x age=%s calls=%d",
3226 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003227 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3228 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003229
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003230 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003231 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
3232 curr_sess->req->flags,
3233 curr_sess->req->l,
3234 curr_sess->req->analysers,
3235 curr_sess->req->rex ?
3236 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3237 TICKS_TO_MS(1000)) : "");
3238
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003239 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003240 ",wx=%s",
3241 curr_sess->req->wex ?
3242 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3243 TICKS_TO_MS(1000)) : "");
3244
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003245 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003246 ",ax=%s]",
3247 curr_sess->req->analyse_exp ?
3248 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3249 TICKS_TO_MS(1000)) : "");
3250
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003251 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003252 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
3253 curr_sess->rep->flags,
3254 curr_sess->rep->l,
3255 curr_sess->rep->analysers,
3256 curr_sess->rep->rex ?
3257 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3258 TICKS_TO_MS(1000)) : "");
3259
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003260 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003261 ",wx=%s",
3262 curr_sess->rep->wex ?
3263 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3264 TICKS_TO_MS(1000)) : "");
3265
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003266 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003267 ",ax=%s]",
3268 curr_sess->rep->analyse_exp ?
3269 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3270 TICKS_TO_MS(1000)) : "");
3271
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003272 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003273 " s0=[%d,%1xh,fd=%d,ex=%s]",
3274 curr_sess->si[0].state,
3275 curr_sess->si[0].flags,
3276 curr_sess->si[0].fd,
3277 curr_sess->si[0].exp ?
3278 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3279 TICKS_TO_MS(1000)) : "");
3280
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003281 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003282 " s1=[%d,%1xh,fd=%d,ex=%s]",
3283 curr_sess->si[1].state,
3284 curr_sess->si[1].flags,
3285 curr_sess->si[1].fd,
3286 curr_sess->si[1].exp ?
3287 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3288 TICKS_TO_MS(1000)) : "");
3289
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003290 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003291 " exp=%s",
3292 curr_sess->task->expire ?
3293 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3294 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003295 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003296 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003297
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003298 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003299
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003300 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003301 /* let's try again later from this session. We add ourselves into
3302 * this session's users so that it can remove us upon termination.
3303 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003304 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003305 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003306 }
3307
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003308 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003309 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003310 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003311
Willy Tarreau295a8372011-03-10 11:25:07 +01003312 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003313 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003314 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003315 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3316 else
3317 chunk_printf(&msg, "Session not found.\n");
3318
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003319 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003320 return 0;
3321
Willy Tarreau295a8372011-03-10 11:25:07 +01003322 si->applet.ctx.sess.target = NULL;
3323 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003324 return 1;
3325 }
3326
Willy Tarreau295a8372011-03-10 11:25:07 +01003327 si->applet.state = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003328 /* fall through */
3329
3330 default:
Willy Tarreau295a8372011-03-10 11:25:07 +01003331 si->applet.state = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003332 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003333 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003334}
3335
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003336/* This function dumps all tables' states onto the stream intreface's
3337 * read buffer. The data_ctx must have been zeroed first, and the flags
3338 * properly set. It returns 0 if the output buffer is full and it needs
3339 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003340 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003341static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003342{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003343 struct session *s = si->applet.private;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003344 struct chunk msg;
3345 struct ebmb_node *eb;
3346 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003347 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003348
3349 /*
Willy Tarreau295a8372011-03-10 11:25:07 +01003350 * We have 3 possible states in si->applet.state :
3351 * - STAT_ST_INIT : the first call
3352 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003353 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003354 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003355 * and the entry pointer points to the next entry to be dumped,
3356 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003357 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003358 * data though.
3359 */
3360
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003361 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003362 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau295a8372011-03-10 11:25:07 +01003363 if (si->applet.state == STAT_ST_LIST) {
3364 si->applet.ctx.table.entry->ref_cnt--;
3365 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003366 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003367 return 1;
3368 }
3369
3370 chunk_init(&msg, trash, sizeof(trash));
3371
Willy Tarreau295a8372011-03-10 11:25:07 +01003372 while (si->applet.state != STAT_ST_FIN) {
3373 switch (si->applet.state) {
3374 case STAT_ST_INIT:
3375 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3376 if (!si->applet.ctx.table.proxy)
3377 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003378
Willy Tarreau295a8372011-03-10 11:25:07 +01003379 si->applet.ctx.table.entry = NULL;
3380 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003381 break;
3382
Willy Tarreau295a8372011-03-10 11:25:07 +01003383 case STAT_ST_INFO:
3384 if (!si->applet.ctx.table.proxy ||
3385 (si->applet.ctx.table.target &&
3386 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
3387 si->applet.state = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003388 break;
3389 }
3390
Willy Tarreau295a8372011-03-10 11:25:07 +01003391 if (si->applet.ctx.table.proxy->table.size) {
Simon Hormanc88b8872011-06-15 15:18:49 +09003392 if (show && !stats_dump_table_head_to_buffer(&msg, si, si->applet.ctx.table.proxy,
3393 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003394 return 0;
3395
Willy Tarreau295a8372011-03-10 11:25:07 +01003396 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003397 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
3398 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003399 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003400 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003401 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3402 si->applet.ctx.table.entry->ref_cnt++;
3403 si->applet.state = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003404 break;
3405 }
3406 }
3407 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003408 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003409 break;
3410
Willy Tarreau295a8372011-03-10 11:25:07 +01003411 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003412 skip_entry = false;
3413
Willy Tarreau295a8372011-03-10 11:25:07 +01003414 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003415 /* we're filtering on some data contents */
3416 void *ptr;
3417 long long data;
3418
Willy Tarreau295a8372011-03-10 11:25:07 +01003419 dt = si->applet.ctx.table.data_type;
3420 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3421 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003422 dt);
3423
3424 data = 0;
3425 switch (stktable_data_types[dt].std_type) {
3426 case STD_T_SINT:
3427 data = stktable_data_cast(ptr, std_t_sint);
3428 break;
3429 case STD_T_UINT:
3430 data = stktable_data_cast(ptr, std_t_uint);
3431 break;
3432 case STD_T_ULL:
3433 data = stktable_data_cast(ptr, std_t_ull);
3434 break;
3435 case STD_T_FRQP:
3436 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003437 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003438 break;
3439 }
3440
3441 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003442 if ((data < si->applet.ctx.table.value &&
3443 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3444 si->applet.ctx.table.data_op == STD_OP_GT ||
3445 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3446 (data == si->applet.ctx.table.value &&
3447 (si->applet.ctx.table.data_op == STD_OP_NE ||
3448 si->applet.ctx.table.data_op == STD_OP_GT ||
3449 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3450 (data > si->applet.ctx.table.value &&
3451 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3452 si->applet.ctx.table.data_op == STD_OP_LT ||
3453 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003454 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003455 }
3456
Simon Hormanc88b8872011-06-15 15:18:49 +09003457 if (show && !skip_entry &&
3458 !stats_dump_table_entry_to_buffer(&msg, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003459 si->applet.ctx.table.entry))
3460 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003461
Willy Tarreau295a8372011-03-10 11:25:07 +01003462 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003463
Willy Tarreau295a8372011-03-10 11:25:07 +01003464 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003465 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003466 struct stksess *old = si->applet.ctx.table.entry;
3467 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3468 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3469 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003470 break;
3471 }
3472
Simon Hormanc88b8872011-06-15 15:18:49 +09003473
3474 if (show)
3475 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3476 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3477 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3478
Willy Tarreau295a8372011-03-10 11:25:07 +01003479 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
3480 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003481 break;
3482
Willy Tarreau295a8372011-03-10 11:25:07 +01003483 case STAT_ST_END:
3484 si->applet.state = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003485 break;
3486 }
3487 }
3488 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003489}
3490
Willy Tarreaud426a182010-03-05 14:58:26 +01003491/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003492 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3493 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3494 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3495 * lines are respected within the limit of 70 output chars. Lines that are
3496 * continuation of a previous truncated line begin with "+" instead of " "
3497 * after the offset. The new pointer is returned.
3498 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003499static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3500 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003501{
3502 int end;
3503 unsigned char c;
3504
3505 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003506 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003507 return ptr;
3508
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003509 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003510
Willy Tarreaud426a182010-03-05 14:58:26 +01003511 while (ptr < len && ptr < bsize) {
3512 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003513 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003514 if (out->len > end - 2)
3515 break;
3516 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003517 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003518 if (out->len > end - 3)
3519 break;
3520 out->str[out->len++] = '\\';
3521 switch (c) {
3522 case '\t': c = 't'; break;
3523 case '\n': c = 'n'; break;
3524 case '\r': c = 'r'; break;
3525 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003526 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003527 }
3528 out->str[out->len++] = c;
3529 } else {
3530 if (out->len > end - 5)
3531 break;
3532 out->str[out->len++] = '\\';
3533 out->str[out->len++] = 'x';
3534 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3535 out->str[out->len++] = hextab[c & 0xF];
3536 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003537 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003538 /* we had a line break, let's return now */
3539 out->str[out->len++] = '\n';
3540 *line = ptr;
3541 return ptr;
3542 }
3543 }
3544 /* we have an incomplete line, we return it as-is */
3545 out->str[out->len++] = '\n';
3546 return ptr;
3547}
3548
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003549/* This function dumps all captured errors onto the stream intreface's
3550 * read buffer. The data_ctx must have been zeroed first, and the flags
3551 * properly set. It returns 0 if the output buffer is full and it needs
3552 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003553 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003554static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003555{
3556 extern const char *monthname[12];
3557 struct chunk msg;
3558
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003559 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003560 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003561
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003562 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003563
Willy Tarreau295a8372011-03-10 11:25:07 +01003564 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003565 /* the function had not been called yet, let's prepare the
3566 * buffer for a response.
3567 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003568 struct tm tm;
3569
3570 get_localtime(date.tv_sec, &tm);
3571 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3572 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3573 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3574 error_snapshot_id);
3575
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003576 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003577 /* Socket buffer full. Let's try again later from the same point */
3578 return 0;
3579 }
3580
Willy Tarreau295a8372011-03-10 11:25:07 +01003581 si->applet.ctx.errors.px = proxy;
3582 si->applet.ctx.errors.buf = 0;
3583 si->applet.ctx.errors.bol = 0;
3584 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003585 }
3586
3587 /* we have two inner loops here, one for the proxy, the other one for
3588 * the buffer.
3589 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003590 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003591 struct error_snapshot *es;
3592
Willy Tarreau295a8372011-03-10 11:25:07 +01003593 if (si->applet.ctx.errors.buf == 0)
3594 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003595 else
Willy Tarreau295a8372011-03-10 11:25:07 +01003596 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003597
3598 if (!es->when.tv_sec)
3599 goto next;
3600
Willy Tarreau295a8372011-03-10 11:25:07 +01003601 if (si->applet.ctx.errors.iid >= 0 &&
3602 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
3603 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003604 goto next;
3605
Willy Tarreau295a8372011-03-10 11:25:07 +01003606 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003607 /* just print headers now */
3608
3609 char pn[INET6_ADDRSTRLEN];
3610 struct tm tm;
3611
3612 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003613 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003614 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003615 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003616
3617
3618 if (es->src.ss_family == AF_INET)
3619 inet_ntop(AF_INET,
3620 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
3621 pn, sizeof(pn));
3622 else
3623 inet_ntop(AF_INET6,
3624 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
3625 pn, sizeof(pn));
3626
Willy Tarreau295a8372011-03-10 11:25:07 +01003627 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003628 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003629 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003630 " frontend %s (#%d): invalid request\n"
3631 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003632 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003633 " request length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003634 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau3fd6cec2010-03-25 06:45:07 +01003635 pn, es->sid, (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3636 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003637 es->srv ? es->srv->id : "<NONE>",
3638 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003639 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003640 es->len, es->pos);
3641 break;
3642 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003643 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003644 " backend %s (#%d) : invalid response\n"
3645 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003646 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003647 " response length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003648 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003649 pn, es->sid, es->oe->id, es->oe->uuid,
3650 es->srv ? es->srv->id : "<NONE>",
3651 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003652 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003653 es->len, es->pos);
3654 break;
3655 }
3656
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003657 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003658 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003659 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003660 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003661 si->applet.ctx.errors.ptr = 0;
3662 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003663 }
3664
Willy Tarreau295a8372011-03-10 11:25:07 +01003665 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003666 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003667 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003668 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003669 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau61b34732009-10-03 23:49:35 +02003670 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003671 goto next;
3672 }
3673
3674 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01003675 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003676 int newptr;
3677 int newline;
3678
Willy Tarreau295a8372011-03-10 11:25:07 +01003679 newline = si->applet.ctx.errors.bol;
3680 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
3681 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02003682 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003683
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003684 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003685 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003686 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003687 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003688 si->applet.ctx.errors.ptr = newptr;
3689 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003690 };
3691 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01003692 si->applet.ctx.errors.bol = 0;
3693 si->applet.ctx.errors.ptr = -1;
3694 si->applet.ctx.errors.buf++;
3695 if (si->applet.ctx.errors.buf > 1) {
3696 si->applet.ctx.errors.buf = 0;
3697 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003698 }
3699 }
3700
3701 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02003702 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003703}
3704
Willy Tarreaub24281b2011-02-13 13:16:36 +01003705struct si_applet http_stats_applet = {
3706 .name = "<STATS>", /* used for logging */
3707 .fct = http_stats_io_handler,
3708};
3709
Simon Horman9bd2c732011-06-15 15:18:44 +09003710static struct si_applet cli_applet = {
Willy Tarreaub24281b2011-02-13 13:16:36 +01003711 .name = "<CLI>", /* used for logging */
3712 .fct = cli_io_handler,
3713};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003714
Willy Tarreau10522fd2008-07-09 20:12:41 +02003715static struct cfg_kw_list cfg_kws = {{ },{
3716 { CFG_GLOBAL, "stats", stats_parse_global },
3717 { 0, NULL, NULL },
3718}};
3719
3720__attribute__((constructor))
3721static void __dumpstats_module_init(void)
3722{
3723 cfg_register_keywords(&cfg_kws);
3724}
3725
Willy Tarreau91861262007-10-17 17:06:05 +02003726/*
3727 * Local variables:
3728 * c-indent-level: 8
3729 * c-basic-offset: 8
3730 * End:
3731 */