blob: 309651d5e770cc82bac71c3a287fa3423bd8d32a [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>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020020#include <pwd.h>
21#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020022
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreau10522fd2008-07-09 20:12:41 +020027#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020028#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
38
Willy Tarreau91861262007-10-17 17:06:05 +020039#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020040
41#include <proto/backend.h>
42#include <proto/buffers.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020043#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020044#include <proto/dumpstats.h>
45#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010046#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020047#include <proto/log.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010048#include <proto/pipe.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020049#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020050#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020051#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020052#include <proto/server.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010053#include <proto/stream_interface.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020054#include <proto/stream_sock.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010055#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020056
Simon Horman9bd2c732011-06-15 15:18:44 +090057static int stats_dump_raw_to_buffer(struct stream_interface *si);
58static int stats_dump_full_sess_to_buffer(struct stream_interface *si);
59static int stats_dump_sess_to_buffer(struct stream_interface *si);
60static int stats_dump_errors_to_buffer(struct stream_interface *si);
61static int stats_dump_table_to_buffer(struct stream_interface *si);
62static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
63static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri);
64
65static struct si_applet cli_applet;
66
67static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020068 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +020069 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +020070 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020071 " help : this message\n"
72 " prompt : toggle interactive mode with prompt\n"
73 " quit : disconnect\n"
74 " show info : report information about the running process\n"
75 " show stat : report counters for each proxy and server\n"
76 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +010077 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +020078 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +020079 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +020080 " set weight : change a server's weight\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +010081 " set timeout : change a timeout setting\n"
Cyril Bontécd19e512010-01-31 22:34:03 +010082 " disable server : set a server in maintenance mode\n"
83 " enable server : re-enable a server that was previously in maintenance mode\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020084 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020085
Simon Horman9bd2c732011-06-15 15:18:44 +090086static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +020087 "Permission denied\n"
88 "";
89
Willy Tarreau295a8372011-03-10 11:25:07 +010090/* data transmission states for the stats responses */
91enum {
92 STAT_ST_INIT = 0,
93 STAT_ST_HEAD,
94 STAT_ST_INFO,
95 STAT_ST_LIST,
96 STAT_ST_END,
97 STAT_ST_FIN,
98};
99
100/* data transmission states for the stats responses inside a proxy */
101enum {
102 STAT_PX_ST_INIT = 0,
103 STAT_PX_ST_TH,
104 STAT_PX_ST_FE,
105 STAT_PX_ST_LI,
106 STAT_PX_ST_SV,
107 STAT_PX_ST_BE,
108 STAT_PX_ST_END,
109 STAT_PX_ST_FIN,
110};
111
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200112/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +0200113 * a new stats socket. It returns a positive value upon success, 0 if the connection
114 * needs to be closed and ignored, or a negative value upon critical failure.
115 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900116static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200117{
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200118 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100119 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200120 copy_target(&s->target, &s->si[1].target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +0100121 s->si[1].applet.private = s;
122 s->si[1].applet.st1 = 0;
123 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200124
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200125 tv_zero(&s->logs.tv_request);
126 s->logs.t_queue = 0;
127 s->logs.t_connect = 0;
128 s->logs.t_data = 0;
129 s->logs.t_close = 0;
130 s->logs.bytes_in = s->logs.bytes_out = 0;
131 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
132 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200133
Willy Tarreaueb472682010-05-28 18:46:57 +0200134 s->req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
135
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200136 if (s->listener->timeout) {
137 s->req->rto = *s->listener->timeout;
138 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200139 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200140 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200141}
142
Willy Tarreau07e9e642010-08-17 21:48:17 +0200143/* allocate a new stats frontend named <name>, and return it
144 * (or NULL in case of lack of memory).
145 */
146static struct proxy *alloc_stats_fe(const char *name)
147{
148 struct proxy *fe;
149
150 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
151 if (!fe)
152 return NULL;
153
154 LIST_INIT(&fe->pendconns);
155 LIST_INIT(&fe->acl);
156 LIST_INIT(&fe->block_cond);
157 LIST_INIT(&fe->redirect_rules);
158 LIST_INIT(&fe->mon_fail_cond);
159 LIST_INIT(&fe->switching_rules);
160 LIST_INIT(&fe->tcp_req.inspect_rules);
161
162 /* Timeouts are defined as -1, so we cannot use the zeroed area
163 * as a default value.
164 */
165 proxy_reset_timeouts(fe);
166
167 fe->last_change = now.tv_sec;
168 fe->id = strdup("GLOBAL");
169 fe->cap = PR_CAP_FE;
170 return fe;
171}
172
Willy Tarreaufbee7132007-10-18 13:53:22 +0200173/* This function parses a "stats" statement in the "global" section. It returns
174 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
175 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
176 * zero included. The trailing '\n' must not be written. The function must be
177 * called with <args> pointing to the first word after "stats".
178 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200179static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
180 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200181{
Willy Tarreau10522fd2008-07-09 20:12:41 +0200182 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200183 if (!strcmp(args[0], "socket")) {
Andreas Kohn16171e22011-01-19 20:29:32 +0100184 struct sockaddr_un *su;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200185 int cur_arg;
186
187 if (*args[1] == 0) {
188 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
189 return -1;
190 }
191
192 if (global.stats_sock.state != LI_NEW) {
193 snprintf(err, errlen, "'stats socket' already specified in global section");
194 return -1;
195 }
196
Andreas Kohn16171e22011-01-19 20:29:32 +0100197 su = str2sun(args[1]);
198 if (!su) {
199 snprintf(err, errlen, "'stats socket' path would require truncation");
200 return -1;
201 }
202 memcpy(&global.stats_sock.addr, su, sizeof(struct sockaddr_un)); // guaranteed to fit
Willy Tarreaufbee7132007-10-18 13:53:22 +0200203
Willy Tarreau89a63132009-08-16 17:41:45 +0200204 if (!global.stats_fe) {
Willy Tarreau07e9e642010-08-17 21:48:17 +0200205 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau89a63132009-08-16 17:41:45 +0200206 snprintf(err, errlen, "out of memory");
207 return -1;
208 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200209 global.stats_fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreau89a63132009-08-16 17:41:45 +0200210 }
211
Willy Tarreaufbee7132007-10-18 13:53:22 +0200212 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100213 global.stats_sock.options = LI_O_NONE;
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200214 global.stats_sock.accept = session_accept;
215 global.stats_fe->accept = stats_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200216 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200217 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200218 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreaueb472682010-05-28 18:46:57 +0200219 global.stats_sock.frontend = global.stats_fe;
Willy Tarreau6162db22009-10-10 17:13:00 +0200220 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau07e9e642010-08-17 21:48:17 +0200221 global.stats_fe->maxconn = global.stats_sock.maxconn;
Willy Tarreau89a63132009-08-16 17:41:45 +0200222 global.stats_sock.timeout = &global.stats_fe->timeout.client;
223
224 global.stats_sock.next = global.stats_fe->listen;
225 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200226
227 cur_arg = 2;
228 while (*args[cur_arg]) {
229 if (!strcmp(args[cur_arg], "uid")) {
230 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
231 cur_arg += 2;
232 }
233 else if (!strcmp(args[cur_arg], "gid")) {
234 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
235 cur_arg += 2;
236 }
237 else if (!strcmp(args[cur_arg], "mode")) {
238 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
239 cur_arg += 2;
240 }
241 else if (!strcmp(args[cur_arg], "user")) {
242 struct passwd *user;
243 user = getpwnam(args[cur_arg + 1]);
244 if (!user) {
245 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
246 args[cur_arg + 1]);
247 return -1;
248 }
249 global.stats_sock.perm.ux.uid = user->pw_uid;
250 cur_arg += 2;
251 }
252 else if (!strcmp(args[cur_arg], "group")) {
253 struct group *group;
254 group = getgrnam(args[cur_arg + 1]);
255 if (!group) {
256 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
257 args[cur_arg + 1]);
258 return -1;
259 }
260 global.stats_sock.perm.ux.gid = group->gr_gid;
261 cur_arg += 2;
262 }
Willy Tarreau6162db22009-10-10 17:13:00 +0200263 else if (!strcmp(args[cur_arg], "level")) {
264 if (!strcmp(args[cur_arg+1], "user"))
265 global.stats_sock.perm.ux.level = ACCESS_LVL_USER;
266 else if (!strcmp(args[cur_arg+1], "operator"))
267 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER;
268 else if (!strcmp(args[cur_arg+1], "admin"))
269 global.stats_sock.perm.ux.level = ACCESS_LVL_ADMIN;
270 else {
271 snprintf(err, errlen, "'stats socket level' only supports 'user', 'operator', and 'admin'");
272 return -1;
273 }
274 cur_arg += 2;
275 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200276 else {
Willy Tarreau6162db22009-10-10 17:13:00 +0200277 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', 'level', and 'mode'");
Willy Tarreaufbee7132007-10-18 13:53:22 +0200278 return -1;
279 }
280 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100281
Willy Tarreaufbee7132007-10-18 13:53:22 +0200282 uxst_add_listener(&global.stats_sock);
283 global.maxsock++;
284 }
285 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100286 unsigned timeout;
287 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
288
289 if (res) {
290 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
291 return -1;
292 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200293
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100294 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200295 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
296 return -1;
297 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200298 if (!global.stats_fe) {
299 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
300 snprintf(err, errlen, "out of memory");
301 return -1;
302 }
303 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200304 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200305 }
306 else if (!strcmp(args[0], "maxconn")) {
307 int maxconn = atol(args[1]);
308
309 if (maxconn <= 0) {
310 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
311 return -1;
312 }
313 global.maxsock -= global.stats_sock.maxconn;
314 global.stats_sock.maxconn = maxconn;
315 global.maxsock += global.stats_sock.maxconn;
Willy Tarreaueb472682010-05-28 18:46:57 +0200316 if (global.stats_fe)
317 global.stats_fe->maxconn = global.stats_sock.maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200318 }
319 else {
320 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
321 return -1;
322 }
323 return 0;
324}
325
Simon Horman9bd2c732011-06-15 15:18:44 +0900326static int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100327{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200328 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100329 "# pxname,svname,"
330 "qcur,qmax,"
331 "scur,smax,slim,stot,"
332 "bin,bout,"
333 "dreq,dresp,"
334 "ereq,econ,eresp,"
335 "wretr,wredis,"
336 "status,weight,act,bck,"
337 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200338 "pid,iid,sid,throttle,lbtot,tracked,type,"
339 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200340 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100341 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100342 "req_rate,req_rate_max,req_tot,"
343 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100344 "\n");
345}
346
Simon Hormand9366582011-06-15 15:18:45 +0900347/* print a string of text buffer to <out>. The format is :
348 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
349 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
350 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
351 */
352static int dump_text(struct chunk *out, const char *buf, int bsize)
353{
354 unsigned char c;
355 int ptr = 0;
356
357 while (buf[ptr] && ptr < bsize) {
358 c = buf[ptr];
359 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
360 if (out->len > out->size - 1)
361 break;
362 out->str[out->len++] = c;
363 }
364 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
365 if (out->len > out->size - 2)
366 break;
367 out->str[out->len++] = '\\';
368 switch (c) {
369 case ' ': c = ' '; break;
370 case '\t': c = 't'; break;
371 case '\n': c = 'n'; break;
372 case '\r': c = 'r'; break;
373 case '\e': c = 'e'; break;
374 case '\\': c = '\\'; break;
375 }
376 out->str[out->len++] = c;
377 }
378 else {
379 if (out->len > out->size - 4)
380 break;
381 out->str[out->len++] = '\\';
382 out->str[out->len++] = 'x';
383 out->str[out->len++] = hextab[(c >> 4) & 0xF];
384 out->str[out->len++] = hextab[c & 0xF];
385 }
386 ptr++;
387 }
388
389 return ptr;
390}
391
392/* print a buffer in hexa.
393 * Print stopped if <bsize> is reached, or if no more place in the chunk.
394 */
395static int dump_binary(struct chunk *out, const char *buf, int bsize)
396{
397 unsigned char c;
398 int ptr = 0;
399
400 while (ptr < bsize) {
401 c = buf[ptr];
402
403 if (out->len > out->size - 2)
404 break;
405 out->str[out->len++] = hextab[(c >> 4) & 0xF];
406 out->str[out->len++] = hextab[c & 0xF];
407
408 ptr++;
409 }
410 return ptr;
411}
412
413/* Dump the status of a table to a stream interface's
414 * read buffer. It returns 0 if the output buffer is full
415 * and needs to be called again, otherwise non-zero.
416 */
417static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
418 struct proxy *proxy, struct proxy *target)
419{
420 struct session *s = si->applet.private;
421
422 chunk_printf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
423 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
424
425 /* any other information should be dumped here */
426
427 if (target && s->listener->perm.ux.level < ACCESS_LVL_OPER)
428 chunk_printf(msg, "# contents not dumped due to insufficient privileges\n");
429
430 if (buffer_feed_chunk(si->ib, msg) >= 0)
431 return 0;
432
433 return 1;
434}
435
436/* Dump the a table entry to a stream interface's
437 * read buffer. It returns 0 if the output buffer is full
438 * and needs to be called again, otherwise non-zero.
439 */
440static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
441 struct proxy *proxy, struct stksess *entry)
442{
443 int dt;
444
445 chunk_printf(msg, "%p:", entry);
446
447 if (proxy->table.type == STKTABLE_TYPE_IP) {
448 char addr[INET_ADDRSTRLEN];
449 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
450 chunk_printf(msg, " key=%s", addr);
451 }
452 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
453 char addr[INET6_ADDRSTRLEN];
454 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
455 chunk_printf(msg, " key=%s", addr);
456 }
457 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
458 chunk_printf(msg, " key=%u", *(unsigned int *)entry->key.key);
459 }
460 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
461 chunk_printf(msg, " key=");
462 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
463 }
464 else {
465 chunk_printf(msg, " key=");
466 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
467 }
468
469 chunk_printf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
470
471 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
472 void *ptr;
473
474 if (proxy->table.data_ofs[dt] == 0)
475 continue;
476 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
477 chunk_printf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
478 else
479 chunk_printf(msg, " %s=", stktable_data_types[dt].name);
480
481 ptr = stktable_data_ptr(&proxy->table, entry, dt);
482 switch (stktable_data_types[dt].std_type) {
483 case STD_T_SINT:
484 chunk_printf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
485 break;
486 case STD_T_UINT:
487 chunk_printf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
488 break;
489 case STD_T_ULL:
490 chunk_printf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
491 break;
492 case STD_T_FRQP:
493 chunk_printf(msg, "%d",
494 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
495 proxy->table.data_arg[dt].u));
496 break;
497 }
498 }
499 chunk_printf(msg, "\n");
500
501 if (buffer_feed_chunk(si->ib, msg) >= 0)
502 return 0;
503
504 return 1;
505}
506
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200507/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200508 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100509 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200510 * designating the function which will have to process the request, which can
511 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200512 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900513static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200514{
Willy Tarreaubc4af052011-02-13 13:25:14 +0100515 struct session *s = si->applet.private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200516 char *args[MAX_STATS_ARGS + 1];
517 int arg;
518
519 while (isspace((unsigned char)*line))
520 line++;
521
522 arg = 0;
523 args[arg] = line;
524
525 while (*line && arg < MAX_STATS_ARGS) {
526 if (isspace((unsigned char)*line)) {
527 *line++ = '\0';
528
529 while (isspace((unsigned char)*line))
530 line++;
531
532 args[++arg] = line;
533 continue;
534 }
535
536 line++;
537 }
538
539 while (++arg <= MAX_STATS_ARGS)
540 args[arg] = line;
541
Willy Tarreau295a8372011-03-10 11:25:07 +0100542 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200543 if (strcmp(args[0], "show") == 0) {
544 if (strcmp(args[1], "stat") == 0) {
545 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100546 si->applet.ctx.stats.flags |= STAT_BOUND;
547 si->applet.ctx.stats.iid = atoi(args[2]);
548 si->applet.ctx.stats.type = atoi(args[3]);
549 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200550 }
551
Willy Tarreau295a8372011-03-10 11:25:07 +0100552 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
553 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
554 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100555 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200556 }
557 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100558 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
559 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
560 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100561 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200562 }
563 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100564 si->applet.state = STAT_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200565 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100566 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100567 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200568 return 1;
569 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100570 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100571 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100572 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100573 si->applet.ctx.sess.target = NULL;
574 si->applet.ctx.sess.section = 0; /* start with session status */
575 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100576 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200577 }
578 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200579 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100580 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100581 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200582 return 1;
583 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200584 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100585 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200586 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100587 si->applet.ctx.errors.iid = -1;
588 si->applet.ctx.errors.px = NULL;
589 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100590 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200591 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200592 else if (strcmp(args[1], "table") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100593 si->applet.state = STAT_ST_INIT;
Willy Tarreau603861e2010-07-18 08:31:59 +0200594 if (*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100595 si->applet.ctx.table.target = find_stktable(args[2]);
596 if (!si->applet.ctx.table.target) {
597 si->applet.ctx.cli.msg = "No such table\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100598 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau603861e2010-07-18 08:31:59 +0200599 return 1;
600 }
601 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200602 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100603 si->applet.ctx.table.target = NULL;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200604
Willy Tarreau295a8372011-03-10 11:25:07 +0100605 si->applet.ctx.table.data_type = -1;
606 if (si->applet.ctx.table.target && strncmp(args[3], "data.", 5) == 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200607 /* condition on stored data value */
Willy Tarreau295a8372011-03-10 11:25:07 +0100608 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
609 if (si->applet.ctx.table.data_type < 0) {
610 si->applet.ctx.cli.msg = "Unknown data type\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100611 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200612 return 1;
613 }
614
Willy Tarreau295a8372011-03-10 11:25:07 +0100615 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
616 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100617 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200618 return 1;
619 }
620
Willy Tarreau295a8372011-03-10 11:25:07 +0100621 si->applet.ctx.table.data_op = get_std_op(args[4]);
622 if (si->applet.ctx.table.data_op < 0) {
623 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100624 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200625 return 1;
626 }
627
Willy Tarreau295a8372011-03-10 11:25:07 +0100628 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
629 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100630 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200631 return 1;
632 }
633 }
634 else if (*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100635 si->applet.ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value>\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100636 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200637 return 1;
638 }
639
Willy Tarreau295a8372011-03-10 11:25:07 +0100640 si->applet.ctx.table.proxy = NULL;
641 si->applet.ctx.table.entry = NULL;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100642 si->applet.st0 = STAT_CLI_O_TAB; // stats_dump_table_to_buffer
Willy Tarreau69f58c82010-07-12 17:55:33 +0200643 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200644 else { /* neither "stat" nor "info" nor "sess" nor "errors" no "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200645 return 0;
646 }
647 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200648 else if (strcmp(args[0], "clear") == 0) {
649 if (strcmp(args[1], "counters") == 0) {
650 struct proxy *px;
651 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200652 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200653 int clrall = 0;
654
655 if (strcmp(args[2], "all") == 0)
656 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200657
Willy Tarreau6162db22009-10-10 17:13:00 +0200658 /* check permissions */
659 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
660 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100661 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100662 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200663 return 1;
664 }
665
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200666 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100667 if (clrall) {
668 memset(&px->be_counters, 0, sizeof(px->be_counters));
669 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
670 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200671 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100672 px->be_counters.conn_max = 0;
673 px->be_counters.p.http.rps_max = 0;
674 px->be_counters.sps_max = 0;
675 px->be_counters.cps_max = 0;
676 px->be_counters.nbpend_max = 0;
677
678 px->fe_counters.conn_max = 0;
679 px->fe_counters.p.http.rps_max = 0;
680 px->fe_counters.sps_max = 0;
681 px->fe_counters.cps_max = 0;
682 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200683 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200684
685 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200686 if (clrall)
687 memset(&sv->counters, 0, sizeof(sv->counters));
688 else {
689 sv->counters.cur_sess_max = 0;
690 sv->counters.nbpend_max = 0;
691 sv->counters.sps_max = 0;
692 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200693
694 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200695 if (li->counters) {
696 if (clrall)
697 memset(li->counters, 0, sizeof(*li->counters));
698 else
699 li->counters->conn_max = 0;
700 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200701 }
702
703 return 1;
704 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200705 else if (strcmp(args[1], "table") == 0) {
706 struct proxy *px;
707 struct stksess *ts;
708 unsigned int ip_key;
709
710 if (!*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100711 si->applet.ctx.cli.msg = "\"table\" argument expected\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100712 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200713 return 1;
714 }
715
716 px = find_stktable(args[2]);
717
718 if (!px) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100719 si->applet.ctx.cli.msg = "No such table\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100720 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200721 return 1;
722 }
723
724 if (strcmp(args[3], "key") != 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100725 si->applet.ctx.cli.msg = "\"key\" argument expected\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100726 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200727 return 1;
728 }
729
730 if (!*args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100731 si->applet.ctx.cli.msg = "Key value expected\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100732 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200733 return 1;
734 }
735
736 if (px->table.type == STKTABLE_TYPE_IP) {
737 ip_key = htonl(inetaddr_host(args[4]));
738 static_table_key.key = (void *)&ip_key;
739 }
740 else {
Willy Tarreau295a8372011-03-10 11:25:07 +0100741 si->applet.ctx.cli.msg = "Removing keys from non-ip tables is not supported\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100742 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200743 return 1;
744 }
745
746 /* check permissions */
747 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100748 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100749 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200750 return 1;
751 }
752
753 ts = stktable_lookup_key(&px->table, &static_table_key);
754 if (!ts) {
755 /* silent return, entry was already removed */
756 return 1;
757 }
758 else if (ts->ref_cnt) {
759 /* don't delete an entry which is currently referenced */
Willy Tarreau295a8372011-03-10 11:25:07 +0100760 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100761 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200762 return 1;
763 }
764
Willy Tarreaudf39e952010-08-10 11:11:40 +0200765 stksess_kill(&px->table, ts);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200766 /* end of processing */
767 return 1;
768 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200769 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200770 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200771 return 0;
772 }
773 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200774 else if (strcmp(args[0], "get") == 0) {
775 if (strcmp(args[1], "weight") == 0) {
776 struct proxy *px;
777 struct server *sv;
778
779 /* split "backend/server" and make <line> point to server */
780 for (line = args[2]; *line; line++)
781 if (*line == '/') {
782 *line++ = '\0';
783 break;
784 }
785
786 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100787 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100788 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200789 return 1;
790 }
791
792 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100793 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100794 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200795 return 1;
796 }
797
798 /* return server's effective weight at the moment */
799 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
800 buffer_feed(si->ib, trash);
801 return 1;
802 }
803 else { /* not "get weight" */
804 return 0;
805 }
806 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200807 else if (strcmp(args[0], "set") == 0) {
808 if (strcmp(args[1], "weight") == 0) {
809 struct proxy *px;
810 struct server *sv;
811 int w;
812
813 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100814 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100815 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200816 return 1;
817 }
818
819 /* split "backend/server" and make <line> point to server */
820 for (line = args[2]; *line; line++)
821 if (*line == '/') {
822 *line++ = '\0';
823 break;
824 }
825
826 if (!*line || !*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100827 si->applet.ctx.cli.msg = "Require 'backend/server' and 'weight' or 'weight%'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100828 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200829 return 1;
830 }
831
832 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100833 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100834 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200835 return 1;
836 }
837
Cyril Bonté613f0df2011-03-03 20:49:04 +0100838 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100839 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100840 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100841 return 1;
842 }
843
Willy Tarreau4483d432009-10-10 19:30:08 +0200844 /* if the weight is terminated with '%', it is set relative to
845 * the initial weight, otherwise it is absolute.
846 */
847 w = atoi(args[3]);
848 if (strchr(args[3], '%') != NULL) {
849 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100850 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100851 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200852 return 1;
853 }
854 w = sv->iweight * w / 100;
855 }
856 else {
857 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100858 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100859 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200860 return 1;
861 }
862 }
863
864 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100865 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 +0100866 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200867 return 1;
868 }
869
870 sv->uweight = w;
871
872 if (px->lbprm.algo & BE_LB_PROP_DYN) {
873 /* we must take care of not pushing the server to full throttle during slow starts */
874 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
875 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
876 else
877 sv->eweight = BE_WEIGHT_SCALE;
878 sv->eweight *= sv->uweight;
879 } else {
880 sv->eweight = sv->uweight;
881 }
882
883 /* static LB algorithms are a bit harder to update */
884 if (px->lbprm.update_server_eweight)
885 px->lbprm.update_server_eweight(sv);
886 else if (sv->eweight)
887 px->lbprm.set_server_status_up(sv);
888 else
889 px->lbprm.set_server_status_down(sv);
890
891 return 1;
892 }
Willy Tarreau7aabd112010-01-26 10:59:06 +0100893 else if (strcmp(args[1], "timeout") == 0) {
894 if (strcmp(args[2], "cli") == 0) {
895 unsigned timeout;
896 const char *res;
897
898 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100899 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100900 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100901 return 1;
902 }
903
904 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
905 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100906 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100907 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100908 return 1;
909 }
910
911 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
912 return 1;
913 }
914 else {
Willy Tarreau295a8372011-03-10 11:25:07 +0100915 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100916 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100917 return 1;
918 }
919 }
920 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +0200921 return 0;
922 }
923 }
Cyril Bontécd19e512010-01-31 22:34:03 +0100924 else if (strcmp(args[0], "enable") == 0) {
925 if (strcmp(args[1], "server") == 0) {
926 struct proxy *px;
927 struct server *sv;
928
929 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100930 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100931 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100932 return 1;
933 }
934
935 /* split "backend/server" and make <line> point to server */
936 for (line = args[2]; *line; line++)
937 if (*line == '/') {
938 *line++ = '\0';
939 break;
940 }
941
942 if (!*line || !*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100943 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100944 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100945 return 1;
946 }
947
948 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100949 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100950 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100951 return 1;
952 }
953
Cyril Bonté613f0df2011-03-03 20:49:04 +0100954 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100955 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100956 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100957 return 1;
958 }
959
Cyril Bontécd19e512010-01-31 22:34:03 +0100960 if (sv->state & SRV_MAINTAIN) {
961 /* The server is really in maintenance, we can change the server state */
962 if (sv->tracked) {
963 /* If this server tracks the status of another one,
964 * we must restore the good status.
965 */
966 if (sv->tracked->state & SRV_RUNNING) {
967 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +0200968 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +0100969 } else {
970 sv->state &= ~SRV_MAINTAIN;
971 set_server_down(sv);
972 }
973 } else {
974 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +0200975 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +0100976 }
977 }
978
979 return 1;
980 }
981 else { /* unknown "enable" parameter */
982 return 0;
983 }
984 }
985 else if (strcmp(args[0], "disable") == 0) {
986 if (strcmp(args[1], "server") == 0) {
987 struct proxy *px;
988 struct server *sv;
989
990 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100991 si->applet.ctx.cli.msg = stats_permission_denied_msg;
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 /* split "backend/server" and make <line> point to server */
997 for (line = args[2]; *line; line++)
998 if (*line == '/') {
999 *line++ = '\0';
1000 break;
1001 }
1002
1003 if (!*line || !*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001004 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001005 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001006 return 1;
1007 }
1008
1009 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001010 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001011 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001012 return 1;
1013 }
1014
Cyril Bonté613f0df2011-03-03 20:49:04 +01001015 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001016 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001017 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001018 return 1;
1019 }
1020
Cyril Bontécd19e512010-01-31 22:34:03 +01001021 if (! (sv->state & SRV_MAINTAIN)) {
1022 /* Not already in maintenance, we can change the server state */
1023 sv->state |= SRV_MAINTAIN;
1024 set_server_down(sv);
1025 }
1026
1027 return 1;
1028 }
1029 else { /* unknown "disable" parameter */
1030 return 0;
1031 }
1032 }
1033 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001034 return 0;
1035 }
1036 return 1;
1037}
1038
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001039/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001040 * used to processes I/O from/to the stats unix socket. The system relies on a
1041 * state machine handling requests and various responses. We read a request,
1042 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001043 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1044 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001045 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001046 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001047static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001048{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001049 struct buffer *req = si->ob;
1050 struct buffer *res = si->ib;
1051 int reql;
1052 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001053
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001054 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1055 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001056
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001057 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001058 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001059 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001060 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001061 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001062 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001063 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001064 /* Let's close for real now. We just close the request
1065 * side, the conditions below will complete if needed.
1066 */
1067 si->shutw(si);
1068 break;
1069 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001070 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001071 /* ensure we have some output room left in the event we
1072 * would want to return some info right after parsing.
1073 */
1074 if (buffer_almost_full(si->ib))
1075 break;
1076
Willy Tarreau74b08c92010-09-08 17:04:31 +02001077 reql = buffer_get_line(si->ob, trash, sizeof(trash));
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001078 if (reql <= 0) { /* closed or EOL not found */
1079 if (reql == 0)
1080 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001081 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001082 continue;
1083 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001084
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001085 /* seek for a possible semi-colon. If we find one, we
1086 * replace it with an LF and skip only this part.
1087 */
1088 for (len = 0; len < reql; len++)
1089 if (trash[len] == ';') {
1090 trash[len] = '\n';
1091 reql = len + 1;
1092 break;
1093 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001094
Willy Tarreau816fc222009-10-04 07:36:58 +02001095 /* now it is time to check that we have a full line,
1096 * remove the trailing \n and possibly \r, then cut the
1097 * line.
1098 */
1099 len = reql - 1;
1100 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001101 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001102 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001103 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001104
Willy Tarreau816fc222009-10-04 07:36:58 +02001105 if (len && trash[len-1] == '\r')
1106 len--;
1107
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001108 trash[len] = '\0';
1109
Willy Tarreaubc4af052011-02-13 13:25:14 +01001110 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001111 if (len) {
1112 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001113 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001114 continue;
1115 }
1116 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001117 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001118 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001119 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001120 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001121 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001122 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001123 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001124 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001125 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001126 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001127 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001128 /* if prompt is disabled, print help on empty lines,
1129 * so that the user at least knows how to enable
1130 * prompt and find help.
1131 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001132 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001133 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001134 }
1135
1136 /* re-adjust req buffer */
1137 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001138 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001139 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001140 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001141 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001142 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001143 continue;
1144 }
1145
Willy Tarreaubc4af052011-02-13 13:25:14 +01001146 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001147 case STAT_CLI_PRINT:
Willy Tarreau295a8372011-03-10 11:25:07 +01001148 if (buffer_feed(si->ib, si->applet.ctx.cli.msg) < 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001149 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001150 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001151 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001152 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001153 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001154 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001155 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001156 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001157 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001158 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001159 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001160 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001161 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001162 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001163 case STAT_CLI_O_TAB:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001164 if (stats_dump_table_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001165 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001166 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001167 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001168 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001169 break;
1170 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001171
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001172 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001173 if (si->applet.st0 == STAT_CLI_PROMPT) {
1174 if (buffer_feed(si->ib, si->applet.st1 ? "\n> " : "\n") < 0)
1175 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001176 }
1177
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001178 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001179 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001180 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001181
1182 /* Now we close the output if one of the writers did so,
1183 * or if we're not in interactive mode and the request
1184 * buffer is empty. This still allows pipelined requests
1185 * to be sent in non-interactive mode.
1186 */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001187 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->applet.st1 && !req->send_max)) {
1188 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001189 continue;
1190 }
1191
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001192 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001193 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001194 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001195 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001196
Willy Tarreaubc4af052011-02-13 13:25:14 +01001197 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001198 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1199 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1200 /* Other size has closed, let's abort if we have no more processing to do
1201 * and nothing more to consume. This is comparable to a broken pipe, so
1202 * we forward the close to the request side so that it flows upstream to
1203 * the client.
1204 */
1205 si->shutw(si);
1206 }
1207
Willy Tarreaubc4af052011-02-13 13:25:14 +01001208 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001209 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1210 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1211 /* We have no more processing to do, and nothing more to send, and
1212 * the client side has closed. So we'll forward this state downstream
1213 * on the response buffer.
1214 */
1215 si->shutr(si);
1216 res->flags |= BF_READ_NULL;
1217 }
1218
1219 /* update all other flags and resync with the other side */
1220 si->update(si);
1221
1222 /* we don't want to expire timeouts while we're processing requests */
1223 si->ib->rex = TICK_ETERNITY;
1224 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001225
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001226 out:
1227 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
1228 __FUNCTION__, __LINE__,
1229 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
1230
1231 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1232 /* check that we have released everything then unregister */
1233 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001234 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001235}
1236
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001237/* This function dumps statistics onto the stream interface's read buffer.
1238 * The data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001239 * It returns 0 as long as it does not complete, non-zero upon completion.
1240 * Some states are not used but it makes the code more similar to other
1241 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001242 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001243static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001244{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001245 struct proxy *px;
1246 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001247 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001248
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001249 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +02001250
Willy Tarreau295a8372011-03-10 11:25:07 +01001251 switch (si->applet.state) {
1252 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001253 /* the function had not been called yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001254 si->applet.state = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001255 /* fall through */
1256
Willy Tarreau295a8372011-03-10 11:25:07 +01001257 case STAT_ST_HEAD:
1258 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001259 print_csv_header(&msg);
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001260 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001261 return 0;
1262 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001263
Willy Tarreau295a8372011-03-10 11:25:07 +01001264 si->applet.state = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001265 /* fall through */
1266
Willy Tarreau295a8372011-03-10 11:25:07 +01001267 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001268 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001269 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001270 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001271 "Name: " PRODUCT_NAME "\n"
1272 "Version: " HAPROXY_VERSION "\n"
1273 "Release_date: " HAPROXY_DATE "\n"
1274 "Nbproc: %d\n"
1275 "Process_num: %d\n"
1276 "Pid: %d\n"
1277 "Uptime: %dd %dh%02dm%02ds\n"
1278 "Uptime_sec: %d\n"
1279 "Memmax_MB: %d\n"
1280 "Ulimit-n: %d\n"
1281 "Maxsock: %d\n"
1282 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001283 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001284 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001285 "PipesUsed: %d\n"
1286 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001287 "Tasks: %d\n"
1288 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001289 "node: %s\n"
1290 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001291 "",
1292 global.nbproc,
1293 relative_pid,
1294 pid,
1295 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1296 up,
1297 global.rlimit_memmax,
1298 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001299 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001300 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001301 nb_tasks_cur, run_queue_cur,
1302 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001303 );
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001304 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001305 return 0;
1306 }
1307
Willy Tarreau295a8372011-03-10 11:25:07 +01001308 si->applet.ctx.stats.px = proxy;
1309 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1310 si->applet.ctx.stats.sv = NULL;
1311 si->applet.state = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001312 /* fall through */
1313
Willy Tarreau295a8372011-03-10 11:25:07 +01001314 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001315 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001316 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1317 while (si->applet.ctx.stats.px) {
1318 px = si->applet.ctx.stats.px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001319 /* skip the disabled proxies and non-networked ones */
1320 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001321 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001322 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001323 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001324 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001325
Willy Tarreau295a8372011-03-10 11:25:07 +01001326 si->applet.ctx.stats.px = px->next;
1327 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001328 }
1329 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001330 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001331
Willy Tarreau295a8372011-03-10 11:25:07 +01001332 si->applet.state = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001333 /* fall through */
1334
Willy Tarreau295a8372011-03-10 11:25:07 +01001335 case STAT_ST_END:
1336 si->applet.state = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001337 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001338
Willy Tarreau295a8372011-03-10 11:25:07 +01001339 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001340 return 1;
1341
1342 default:
1343 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001344 si->applet.state = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001345 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001346 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001347}
1348
1349
Cyril Bonté70be45d2010-10-12 00:14:35 +02001350/* We don't want to land on the posted stats page because a refresh will
1351 * repost the data. We don't want this to happen on accident so we redirect
1352 * the browse to the stats page with a GET.
1353 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001354static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001355{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001356 struct session *s = si->applet.private;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001357 struct chunk msg;
1358
1359 chunk_init(&msg, trash, sizeof(trash));
1360
Willy Tarreau295a8372011-03-10 11:25:07 +01001361 switch (si->applet.state) {
1362 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001363 chunk_printf(&msg,
1364 "HTTP/1.0 303 See Other\r\n"
1365 "Cache-Control: no-cache\r\n"
1366 "Content-Type: text/plain\r\n"
1367 "Connection: close\r\n"
1368 "Location: %s;st=%s",
Willy Tarreau295a8372011-03-10 11:25:07 +01001369 uri->uri_prefix, si->applet.ctx.stats.st_code);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001370 chunk_printf(&msg, "\r\n\r\n");
1371
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001372 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001373 return 0;
1374
1375 s->txn.status = 303;
1376
1377 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1378 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1379 if (!(s->flags & SN_FINST_MASK))
1380 s->flags |= SN_FINST_R;
1381
Willy Tarreau295a8372011-03-10 11:25:07 +01001382 si->applet.state = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001383 return 1;
1384 }
1385 return 1;
1386}
1387
1388
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001389/* This I/O handler runs as an applet embedded in a stream interface. It is
1390 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001391 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001392 * automatically unregisters itself once transfer is complete.
1393 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001394static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001395{
Willy Tarreaubc4af052011-02-13 13:25:14 +01001396 struct session *s = si->applet.private;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001397 struct buffer *req = si->ob;
1398 struct buffer *res = si->ib;
1399
1400 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1401 goto out;
1402
1403 /* check that the output is not closed */
1404 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001405 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001406
Willy Tarreaubc4af052011-02-13 13:25:14 +01001407 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001408 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001409 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001410 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001411 si->shutw(si);
1412 }
1413 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001414 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001415 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001416 si->shutw(si);
1417 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001418 }
1419 }
1420
1421 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
1422 si->shutw(si);
1423
Willy Tarreaubc4af052011-02-13 13:25:14 +01001424 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001425 si->shutr(si);
1426 res->flags |= BF_READ_NULL;
1427 }
1428
1429 /* update all other flags and resync with the other side */
1430 si->update(si);
1431
1432 /* we don't want to expire timeouts while we're processing requests */
1433 si->ib->rex = TICK_ETERNITY;
1434 si->ob->wex = TICK_ETERNITY;
1435
1436 out:
1437 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1438 /* check that we have released everything then unregister */
1439 stream_int_unregister_handler(si);
1440 }
1441}
1442
1443
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001444/* This function dumps statistics in HTTP format onto the stream interface's
1445 * read buffer. The data_ctx must have been zeroed first, and the flags
1446 * properly set. It returns 0 if it had to stop writing data and an I/O is
1447 * needed, 1 if the dump is finished and the session must be closed, or -1
1448 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001449 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001450static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001451{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001452 struct session *s = si->applet.private;
1453 struct buffer *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001454 struct proxy *px;
1455 struct chunk msg;
1456 unsigned int up;
1457
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001458 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001459
Willy Tarreau295a8372011-03-10 11:25:07 +01001460 switch (si->applet.state) {
1461 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001462 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001463 "HTTP/1.0 200 OK\r\n"
1464 "Cache-Control: no-cache\r\n"
1465 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001466 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001467 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001468
Willy Tarreau295a8372011-03-10 11:25:07 +01001469 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001470 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001471 uri->refresh);
1472
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001473 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001474
1475 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001476 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001477 return 0;
1478
Willy Tarreau91861262007-10-17 17:06:05 +02001479 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1480 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1481 if (!(s->flags & SN_FINST_MASK))
1482 s->flags |= SN_FINST_R;
1483
1484 if (s->txn.meth == HTTP_METH_HEAD) {
1485 /* that's all we return in case of HEAD request */
Willy Tarreau295a8372011-03-10 11:25:07 +01001486 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001487 return 1;
1488 }
1489
Willy Tarreau295a8372011-03-10 11:25:07 +01001490 si->applet.state = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001491 /* fall through */
1492
Willy Tarreau295a8372011-03-10 11:25:07 +01001493 case STAT_ST_HEAD:
1494 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001495 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001496 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001497 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1498 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001499 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001500 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1501 "<style type=\"text/css\"><!--\n"
1502 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001503 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001504 " font-size: 12px;"
1505 " font-weight: normal;"
1506 " color: black;"
1507 " background: white;"
1508 "}\n"
1509 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001510 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001511 "}\n"
1512 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001513 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001514 " margin-bottom: 0.5em;"
1515 "}\n"
1516 "h2 {"
1517 " font-family: helvetica, arial;"
1518 " font-size: x-large;"
1519 " font-weight: bold;"
1520 " font-style: italic;"
1521 " color: #6020a0;"
1522 " margin-top: 0em;"
1523 " margin-bottom: 0em;"
1524 "}\n"
1525 "h3 {"
1526 " font-family: helvetica, arial;"
1527 " font-size: 16px;"
1528 " font-weight: bold;"
1529 " color: #b00040;"
1530 " background: #e8e8d0;"
1531 " margin-top: 0em;"
1532 " margin-bottom: 0em;"
1533 "}\n"
1534 "li {"
1535 " margin-top: 0.25em;"
1536 " margin-right: 2em;"
1537 "}\n"
1538 ".hr {margin-top: 0.25em;"
1539 " border-color: black;"
1540 " border-bottom-style: solid;"
1541 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001542 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001543 ".total {background: #20D0D0;color: #ffff80;}\n"
1544 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001545 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001546 ".backend {background: #e8e8d0;}\n"
1547 ".active0 {background: #ff9090;}\n"
1548 ".active1 {background: #ffd020;}\n"
1549 ".active2 {background: #ffffa0;}\n"
1550 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001551 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1552 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1553 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001554 ".backup0 {background: #ff9090;}\n"
1555 ".backup1 {background: #ff80ff;}\n"
1556 ".backup2 {background: #c060ff;}\n"
1557 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001558 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1559 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1560 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001561 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001562 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001563 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001564 "a.px:link {color: #ffff40; text-decoration: none;}"
1565 "a.px:visited {color: #ffff40; text-decoration: none;}"
1566 "a.px:hover {color: #ffffff; text-decoration: none;}"
1567 "a.lfsb:link {color: #000000; text-decoration: none;}"
1568 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1569 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1570 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001571 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001572 "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"
1573 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001574 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001575 "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 +02001576 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001577 "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 +02001578 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001579 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1580 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1581 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001582 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001583 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001584 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001585 (uri->flags&ST_SHNODE) ? " on " : "",
1586 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001587 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001588 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001589 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001590 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001591 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001592 return 0;
1593
Willy Tarreau295a8372011-03-10 11:25:07 +01001594 si->applet.state = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02001595 /* fall through */
1596
Willy Tarreau295a8372011-03-10 11:25:07 +01001597 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02001598 up = (now.tv_sec - start_date.tv_sec);
1599
1600 /* WARNING! this has to fit the first packet too.
1601 * We are around 3.5 kB, add adding entries will
1602 * become tricky if we want to support 4kB buffers !
1603 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001604 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001605 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001606 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1607 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001608 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001609 "<hr width=\"100%%\" class=\"hr\">\n"
1610 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001611 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001612 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001613 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001614 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1615 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
1616 "current conns = %d; current pipes = %d/%d<br>\n"
1617 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001618 "</td><td align=\"center\" nowrap>\n"
1619 "<table class=\"lgd\"><tr>\n"
1620 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1621 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1622 "</tr><tr>\n"
1623 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1624 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1625 "</tr><tr>\n"
1626 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1627 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1628 "</tr><tr>\n"
1629 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001630 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001631 "</tr><tr>\n"
1632 "<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 +02001633 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001634 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001635 "</td>"
1636 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1637 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1638 "",
1639 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001640 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1641 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1642 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001643 up / 86400, (up % 86400) / 3600,
1644 (up % 3600) / 60, (up % 60),
1645 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1646 global.rlimit_memmax ? " MB" : "",
1647 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001648 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001649 actconn, pipes_used, pipes_used+pipes_free,
1650 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +02001651 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001652
Willy Tarreau295a8372011-03-10 11:25:07 +01001653 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001654 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001655 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1656 uri->uri_prefix,
1657 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01001658 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001659 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001660 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001661 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1662 uri->uri_prefix,
1663 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01001664 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001665
Willy Tarreau55bb8452007-10-17 18:44:57 +02001666 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001667 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001668 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001669 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1670 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001671 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001672 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001673 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001674 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001675 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1676 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001677 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001678 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001679 }
Willy Tarreau91861262007-10-17 17:06:05 +02001680
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001681 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001682 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
1683 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001684 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
1685 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001686
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001687 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02001688 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
1689 uri->uri_prefix,
1690 (uri->refresh > 0) ? ";norefresh" : "");
1691
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001692 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001693 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001694 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1695 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
1696 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
1697 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
1698 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
1699 "</ul>"
1700 "</td>"
1701 "</tr></table>\n"
1702 ""
1703 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001704
Willy Tarreau295a8372011-03-10 11:25:07 +01001705 if (si->applet.ctx.stats.st_code) {
1706 if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001707 chunk_printf(&msg,
1708 "<p><div class=active3>"
1709 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1710 "Action processed successfully."
1711 "</div>\n", uri->uri_prefix);
1712 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001713 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_NONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001714 chunk_printf(&msg,
1715 "<p><div class=active2>"
1716 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1717 "Nothing has changed."
1718 "</div>\n", uri->uri_prefix);
1719 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001720 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_EXCD) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001721 chunk_printf(&msg,
1722 "<p><div class=active0>"
1723 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1724 "<b>Action not processed : the buffer couldn't store all the data.<br>"
1725 "You should retry with less servers at a time.</b>"
1726 "</div>\n", uri->uri_prefix);
1727 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001728 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DENY) == 0) {
Cyril Bonté474be412010-10-12 00:14:36 +02001729 chunk_printf(&msg,
1730 "<p><div class=active0>"
1731 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1732 "<b>Action denied.</b>"
1733 "</div>\n", uri->uri_prefix);
1734 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001735 else {
1736 chunk_printf(&msg,
1737 "<p><div class=active6>"
1738 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1739 "Unexpected result."
1740 "</div>\n", uri->uri_prefix);
1741 }
1742 chunk_printf(&msg,"<p>\n");
1743 }
1744
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001745 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001746 return 0;
1747 }
Willy Tarreau91861262007-10-17 17:06:05 +02001748
Willy Tarreau295a8372011-03-10 11:25:07 +01001749 si->applet.ctx.stats.px = proxy;
1750 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1751 si->applet.state = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02001752 /* fall through */
1753
Willy Tarreau295a8372011-03-10 11:25:07 +01001754 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02001755 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001756 while (si->applet.ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001757 if (buffer_almost_full(rep))
1758 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01001759 px = si->applet.ctx.stats.px;
Willy Tarreau91861262007-10-17 17:06:05 +02001760 /* skip the disabled proxies and non-networked ones */
1761 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001762 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001763 return 0;
1764
Willy Tarreau295a8372011-03-10 11:25:07 +01001765 si->applet.ctx.stats.px = px->next;
1766 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02001767 }
1768 /* here, we just have reached the last proxy */
1769
Willy Tarreau295a8372011-03-10 11:25:07 +01001770 si->applet.state = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02001771 /* fall through */
1772
Willy Tarreau295a8372011-03-10 11:25:07 +01001773 case STAT_ST_END:
1774 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001775 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001776 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001777 return 0;
1778 }
Willy Tarreau91861262007-10-17 17:06:05 +02001779
Willy Tarreau295a8372011-03-10 11:25:07 +01001780 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001781 /* fall through */
1782
Willy Tarreau295a8372011-03-10 11:25:07 +01001783 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02001784 return 1;
1785
1786 default:
1787 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001788 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001789 return -1;
1790 }
1791}
1792
1793
1794/*
1795 * Dumps statistics for a proxy.
1796 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1797 * ot non-zero if everything completed.
1798 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001799static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001800{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001801 struct session *s = si->applet.private;
1802 struct buffer *rep = si->ib;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001803 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001804 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02001805 struct chunk msg;
1806
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001807 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001808
Willy Tarreau295a8372011-03-10 11:25:07 +01001809 switch (si->applet.ctx.stats.px_st) {
1810 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02001811 /* we are on a new proxy */
1812
1813 if (uri && uri->scope) {
1814 /* we have a limited scope, we have to check the proxy name */
1815 struct stat_scope *scope;
1816 int len;
1817
1818 len = strlen(px->id);
1819 scope = uri->scope;
1820
1821 while (scope) {
1822 /* match exact proxy name */
1823 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1824 break;
1825
1826 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001827 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001828 break;
1829 scope = scope->next;
1830 }
1831
1832 /* proxy name not found : don't dump anything */
1833 if (scope == NULL)
1834 return 1;
1835 }
1836
Willy Tarreau295a8372011-03-10 11:25:07 +01001837 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
1838 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001839 return 1;
1840
Willy Tarreau295a8372011-03-10 11:25:07 +01001841 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02001842 /* fall through */
1843
Willy Tarreau295a8372011-03-10 11:25:07 +01001844 case STAT_PX_ST_TH:
1845 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
1846 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001847 /* A form to enable/disable this proxy servers */
1848 chunk_printf(&msg,
1849 "<form action=\"%s\" method=\"post\">",
1850 uri->uri_prefix);
1851 }
1852
Willy Tarreau55bb8452007-10-17 18:44:57 +02001853 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001854 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001855 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001856 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001857 "<th class=\"pxname\" width=\"10%%\"");
1858
1859 if (uri->flags&ST_SHLGNDS) {
1860 /* cap, mode, id */
1861 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
1862 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1863 px->uuid);
1864
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001865 chunk_printf(&msg, "\"");
1866 }
1867
1868 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001869 ">%s<a name=\"%s\"></a>"
1870 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001871 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001872 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001873 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001874 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001875 "<tr class=\"titre\">",
1876 (uri->flags & ST_SHLGNDS)?"<u>":"",
1877 px->id, px->id, px->id,
1878 (uri->flags & ST_SHLGNDS)?"</u>":"",
1879 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1880
Willy Tarreau295a8372011-03-10 11:25:07 +01001881 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001882 /* Column heading for Enable or Disable server */
1883 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
1884 }
1885
1886 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001887 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001888 "<th colspan=3>Queue</th>"
1889 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001890 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001891 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001892 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001893 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001894 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001895 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001896 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001897 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001898 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001899 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001900 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001901 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1902 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001903 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001904
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001905 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001906 return 0;
1907 }
Willy Tarreau91861262007-10-17 17:06:05 +02001908
Willy Tarreau295a8372011-03-10 11:25:07 +01001909 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02001910 /* fall through */
1911
Willy Tarreau295a8372011-03-10 11:25:07 +01001912 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02001913 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001914 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01001915 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
1916 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001917 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001918 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02001919 "<tr class=\"frontend\">");
1920
Willy Tarreau295a8372011-03-10 11:25:07 +01001921 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001922 /* Column sub-heading for Enable or Disable server */
1923 chunk_printf(&msg, "<td></td>");
1924 }
1925
1926 chunk_printf(&msg,
1927 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001928 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001929 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
1930 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01001931 "",
1932 px->id, px->id);
1933
1934 if (px->mode == PR_MODE_HTTP) {
1935 chunk_printf(&msg,
1936 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01001937 "<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 +01001938 "",
1939 read_freq_ctr(&px->fe_req_per_sec),
1940 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001941 px->fe_counters.p.http.rps_max,
1942 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01001943 LIM2A2(px->fe_sps_lim, "-"));
1944 } else {
1945 chunk_printf(&msg,
1946 /* sessions rate : current, max, limit */
1947 "<td>%s</td><td>%s</td><td>%s</td>"
1948 "",
1949 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001950 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01001951 }
1952
1953 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001954 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001955 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001956 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001957 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001958 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001959
1960 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1961 if (px->mode == PR_MODE_HTTP) {
1962 int i;
1963
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001964 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001965
1966 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001967 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001968
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001969 chunk_printf(&msg, " other=%lld\"", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001970 }
1971
1972 chunk_printf(&msg,
1973 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01001974 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001975 /* bytes : in, out */
1976 "<td>%s</td><td>%s</td>"
1977 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001978 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001979 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01001980 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001981 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001982
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001983 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001984 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001985 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001986 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001987 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001988 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001989 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001990 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001991 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001992 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001993 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02001994 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001995 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
1996 U2H2(px->fe_counters.failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +02001997 px->state == PR_STRUN ? "OPEN" :
1998 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001999 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002000 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002001 /* pxid, name, queue cur, queue max, */
2002 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002003 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002004 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002005 /* bytes : in, out */
2006 "%lld,%lld,"
2007 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002008 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002009 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002010 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002011 /* warnings: retries, redispatches */
2012 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002013 /* server status : reflect frontend status */
2014 "%s,"
2015 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002016 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002017 /* pid, iid, sid, throttle, lbtot, tracked, type */
2018 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002019 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002020 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002021 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002022 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002023 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002024 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2025 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2026 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2027 px->fe_counters.failed_req,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002028 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +01002029 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002030 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002031 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002032 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002033
2034 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2035 if (px->mode == PR_MODE_HTTP) {
2036 int i;
2037
2038 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002039 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002040
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002041 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002042 } else {
2043 chunk_printf(&msg, ",,,,,,");
2044 }
2045
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002046 /* failed health analyses */
2047 chunk_printf(&msg, ",");
2048
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002049 /* requests : req_rate, req_rate_max, req_tot, */
2050 chunk_printf(&msg, "%u,%u,%lld,",
2051 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002052 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002053
Willy Tarreauae526782010-03-04 20:34:23 +01002054 /* errors: cli_aborts, srv_aborts */
2055 chunk_printf(&msg, ",,");
2056
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002057 /* finish with EOL */
2058 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002059 }
Willy Tarreau91861262007-10-17 17:06:05 +02002060
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002061 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002062 return 0;
2063 }
2064
Willy Tarreau295a8372011-03-10 11:25:07 +01002065 si->applet.ctx.stats.l = px->listen; /* may be NULL */
2066 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002067 /* fall through */
2068
Willy Tarreau295a8372011-03-10 11:25:07 +01002069 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002070 /* stats.l has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002071 for (; si->applet.ctx.stats.l != NULL; si->applet.ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002072 if (buffer_almost_full(rep))
2073 return 0;
2074
Willy Tarreau295a8372011-03-10 11:25:07 +01002075 l = si->applet.ctx.stats.l;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002076 if (!l->counters)
2077 continue;
2078
Willy Tarreau295a8372011-03-10 11:25:07 +01002079 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2080 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002081 break;
2082
Willy Tarreau295a8372011-03-10 11:25:07 +01002083 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002084 continue;
2085 }
2086
Willy Tarreau295a8372011-03-10 11:25:07 +01002087 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002088 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002089 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002090 /* Column sub-heading for Enable or Disable server */
2091 chunk_printf(&msg, "<td></td>");
2092 }
2093 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002094
2095 if (uri->flags&ST_SHLGNDS) {
2096 char str[INET6_ADDRSTRLEN], *fmt = NULL;
2097 int port;
2098
2099 chunk_printf(&msg, " title=\"IP: ");
2100
2101 port = (l->addr.ss_family == AF_INET6)
2102 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
2103 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
2104
2105 if (l->addr.ss_family == AF_INET) {
2106 if (inet_ntop(AF_INET,
2107 (const void *)&((struct sockaddr_in *)&l->addr)->sin_addr,
2108 str, sizeof(str)))
2109 fmt = "%s:%d";
2110 } else {
2111 if (inet_ntop(AF_INET6,
2112 (const void *)&((struct sockaddr_in6 *)(&l->addr))->sin6_addr,
2113 str, sizeof(str)))
2114 fmt = "[%s]:%d";
2115 }
2116
2117 if (fmt)
2118 chunk_printf(&msg, fmt, str, port);
2119 else
2120 chunk_printf(&msg, "(%s)", strerror(errno));
2121
2122 /* id */
2123 chunk_printf(&msg, ", id: %d", l->luid);
2124
2125 chunk_printf(&msg, "\"");
2126 }
2127
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002128 chunk_printf(&msg,
2129 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002130 ">%s<a name=\"%s/+%s\"></a>"
2131 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002132 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002133 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002134 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002135 "<td>%s</td><td>%s</td><td>%s</td>"
2136 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002137 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002138 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002139 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002140 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002141 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002142 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002143 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2144 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2145
2146 chunk_printf(&msg,
2147 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002148 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002149 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002150 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002151 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002152 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002153 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002154 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002155 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002156 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002157 "",
2158 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2159 U2H2(l->counters->failed_req),
2160 (l->nbconn < l->maxconn) ? "OPEN" : "FULL");
2161 } else {
2162 chunk_printf(&msg,
2163 /* pxid, name, queue cur, queue max, */
2164 "%s,%s,,,"
2165 /* sessions: current, max, limit, total */
2166 "%d,%d,%d,%lld,"
2167 /* bytes: in, out */
2168 "%lld,%lld,"
2169 /* denied: req, resp */
2170 "%lld,%lld,"
2171 /* errors: request, connect, response */
2172 "%lld,,,"
2173 /* warnings: retries, redispatches */
2174 ",,"
2175 /* server status: reflect listener status */
2176 "%s,"
2177 /* rest of server: nothing */
2178 ",,,,,,,,"
2179 /* pid, iid, sid, throttle, lbtot, tracked, type */
2180 "%d,%d,%d,,,,%d,"
2181 /* rate, rate_lim, rate_max */
2182 ",,,"
2183 /* check_status, check_code, check_duration */
2184 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002185 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2186 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002187 /* failed health analyses */
2188 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002189 /* requests : req_rate, req_rate_max, req_tot, */
2190 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002191 /* errors: cli_aborts, srv_aborts */
2192 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002193 "\n",
2194 px->id, l->name,
2195 l->nbconn, l->counters->conn_max,
2196 l->maxconn, l->counters->cum_conn,
2197 l->counters->bytes_in, l->counters->bytes_out,
2198 l->counters->denied_req, l->counters->denied_resp,
2199 l->counters->failed_req,
2200 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2201 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2202 }
2203
2204 if (buffer_feed_chunk(rep, &msg) >= 0)
2205 return 0;
2206 }
2207
Willy Tarreau295a8372011-03-10 11:25:07 +01002208 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2209 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002210 /* fall through */
2211
Willy Tarreau295a8372011-03-10 11:25:07 +01002212 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002213 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002214 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002215 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 +02002216
Willy Tarreau4e33d862009-10-11 23:35:10 +02002217 if (buffer_almost_full(rep))
2218 return 0;
2219
Willy Tarreau295a8372011-03-10 11:25:07 +01002220 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002221
Willy Tarreau295a8372011-03-10 11:25:07 +01002222 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2223 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002224 break;
2225
Willy Tarreau295a8372011-03-10 11:25:07 +01002226 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002227 continue;
2228 }
2229
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002230 if (sv->tracked)
2231 svs = sv->tracked;
2232 else
2233 svs = sv;
2234
Willy Tarreau91861262007-10-17 17:06:05 +02002235 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002236 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002237 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002238 else if (svs->state & SRV_RUNNING) {
2239 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002240 sv_state = 3; /* UP */
2241 else
2242 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002243
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002244 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002245 sv_state += 2;
2246 }
Willy Tarreau91861262007-10-17 17:06:05 +02002247 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002248 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002249 sv_state = 1; /* going up */
2250 else
2251 sv_state = 0; /* DOWN */
2252
Willy Tarreau295a8372011-03-10 11:25:07 +01002253 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002254 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002255 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002256 continue;
2257 }
2258
Willy Tarreau295a8372011-03-10 11:25:07 +01002259 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002260 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2261 "UP %d/%d &darr;", "UP",
2262 "NOLB %d/%d &darr;", "NOLB",
2263 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002264 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002265 chunk_printf(&msg,
2266 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002267 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002268 );
2269 }
2270 else {
2271 chunk_printf(&msg,
2272 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002273 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002274 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2275 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002276
Willy Tarreau295a8372011-03-10 11:25:07 +01002277 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002278 chunk_printf(&msg,
2279 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2280 sv->id);
2281 }
2282
2283 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002284
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002285 if (uri->flags&ST_SHLGNDS) {
2286 char str[INET6_ADDRSTRLEN];
2287
2288 chunk_printf(&msg, " title=\"IP: ");
2289
2290 /* IP */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002291 switch (sv->addr.ss_family) {
2292 case AF_INET:
2293 if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&sv->addr)->sin_addr, str, sizeof(str)))
2294 chunk_printf(&msg, "%s:%d", str, htons(((struct sockaddr_in *)&sv->addr)->sin_port));
2295 else
2296 chunk_printf(&msg, "(%s)", strerror(errno));
2297 break;
2298 case AF_INET6:
2299 if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&sv->addr)->sin6_addr, str, sizeof(str)))
2300 chunk_printf(&msg, "%s:%d", str, htons(((struct sockaddr_in6 *)&sv->addr)->sin6_port));
2301 else
2302 chunk_printf(&msg, "(%s)", strerror(errno));
2303 break;
2304 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002305
2306 /* id */
2307 chunk_printf(&msg, ", id: %d", sv->puid);
2308
2309 /* cookie */
2310 if (sv->cookie) {
2311 struct chunk src;
2312
2313 chunk_printf(&msg, ", cookie: '");
2314
2315 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2316 chunk_htmlencode(&msg, &src);
2317
2318 chunk_printf(&msg, "'");
2319 }
2320
2321 chunk_printf(&msg, "\"");
2322 }
2323
2324 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002325 ">%s<a name=\"%s/%s\"></a>"
2326 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002327 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002328 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002329 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002330 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002331 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002332 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002333 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002334 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002335 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002336 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002337 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002338 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2339 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002340 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2341
2342 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2343 if (px->mode == PR_MODE_HTTP) {
2344 int i;
2345
2346 chunk_printf(&msg, " title=\"rsp codes:");
2347
2348 for (i = 1; i < 6; i++)
2349 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2350
2351 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2352 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002353
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002354 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002355 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002356 ">%s%s%s</td><td>%s</td>",
2357 (px->mode == PR_MODE_HTTP)?"<u>":"",
2358 U2H0(sv->counters.cum_sess),
2359 (px->mode == PR_MODE_HTTP)?"</u>":"",
2360 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002361
2362 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002363 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002364 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002365 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002366 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002367 /* errors : request, connect */
2368 "<td></td><td>%s</td>"
2369 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002370 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002371 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002372 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002373 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002374 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2375 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002376 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002377 sv->counters.cli_aborts,
2378 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002379 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002380 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002381
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002382 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002383 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002384
Cyril Bontécd19e512010-01-31 22:34:03 +01002385 if (sv->state & SRV_MAINTAIN) {
2386 chunk_printf(&msg, "%s ",
2387 human_time(now.tv_sec - sv->last_change, 1));
2388 chunk_printf(&msg, "MAINT");
2389 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002390 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2391 chunk_printf(&msg, "%s ",
2392 human_time(now.tv_sec - svs->last_change, 1));
2393 chunk_printf(&msg, "MAINT(via)");
2394 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002395 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002396 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002397 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002398
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002399 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002400 srv_hlt_st[sv_state],
2401 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2402 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002403 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002404
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002405 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002406 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002407 get_check_status_description(sv->check_status));
2408
2409 if (*sv->check_desc) {
2410 struct chunk src;
2411
2412 chunk_printf(&msg, ": ");
2413
2414 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2415 chunk_htmlencode(&msg, &src);
2416 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002417
Willy Tarreaue0454092010-02-26 12:29:07 +01002418 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002419 tv_iszero(&sv->check_start)?"":"* ",
2420 get_check_status_info(sv->check_status));
2421
2422 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002423 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002424
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002425 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002426 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002427 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002428 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002429
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002430 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002431 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002432 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002433 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002434 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002435 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002436 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002437 (sv->state & SRV_BACKUP) ? "-" : "Y",
2438 (sv->state & SRV_BACKUP) ? "Y" : "-");
2439
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002440 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002441 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002442 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002443 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2444
2445 if (svs->observe)
2446 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2447
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002448 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002449 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002450 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002451 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002452 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002453 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002454 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002455 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2456 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002457 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002458 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002459 "<td colspan=3></td>");
2460
2461 /* throttle */
2462 if ((sv->state & SRV_WARMINGUP) &&
2463 now.tv_sec < sv->last_change + sv->slowstart &&
2464 now.tv_sec >= sv->last_change) {
2465 unsigned int ratio;
2466 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002467 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002468 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002469 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002470 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002471 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002472 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002473 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002474 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2475 "UP %d/%d,", "UP,",
2476 "NOLB %d/%d,", "NOLB,",
2477 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002478 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002479 /* pxid, name */
2480 "%s,%s,"
2481 /* queue : current, max */
2482 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002483 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002484 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002485 /* bytes : in, out */
2486 "%lld,%lld,"
2487 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002488 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002489 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002490 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002491 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002492 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002493 "",
2494 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002495 sv->nbpend, sv->counters.nbpend_max,
2496 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002497 sv->counters.bytes_in, sv->counters.bytes_out,
2498 sv->counters.failed_secu,
2499 sv->counters.failed_conns, sv->counters.failed_resp,
2500 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002501
Willy Tarreau55bb8452007-10-17 18:44:57 +02002502 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002503 if (sv->state & SRV_MAINTAIN) {
2504 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002505 }
2506 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2507 chunk_printf(&msg, "MAINT(via),");
2508 }
2509 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002510 chunk_printf(&msg,
2511 srv_hlt_st[sv_state],
2512 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2513 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2514 }
Willy Tarreau91861262007-10-17 17:06:05 +02002515
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002516 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002517 /* weight, active, backup */
2518 "%d,%d,%d,"
2519 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002520 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002521 (sv->state & SRV_BACKUP) ? 0 : 1,
2522 (sv->state & SRV_BACKUP) ? 1 : 0);
2523
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002524 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002525 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002526 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002527 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002528 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002529 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002530 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002531 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002532 ",,,,");
2533
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002534 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002535 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002536 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002537 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002538 LIM2A0(sv->maxqueue, ""),
2539 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002540
2541 /* throttle */
2542 if ((sv->state & SRV_WARMINGUP) &&
2543 now.tv_sec < sv->last_change + sv->slowstart &&
2544 now.tv_sec >= sv->last_change) {
2545 unsigned int ratio;
2546 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002547 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002548 }
2549
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002550 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002551 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002552
2553 /* tracked */
2554 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002555 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002556 sv->tracked->proxy->id, sv->tracked->id);
2557 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002558 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002559
Willy Tarreau7f062c42009-03-05 18:43:00 +01002560 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002561 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002562
2563 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002564 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002565 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002566 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002567
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002568 if (sv->state & SRV_CHECKED) {
2569 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002570 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002571
2572 /* check_code */
2573 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002574 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002575 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002576 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002577
2578 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002579 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002580 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002581 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002582 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002583
2584 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002585 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002586 }
2587
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002588 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2589 if (px->mode == PR_MODE_HTTP) {
2590 int i;
2591
2592 for (i=1; i<6; i++)
2593 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2594
2595 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2596 } else {
2597 chunk_printf(&msg, ",,,,,,");
2598 }
2599
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002600 /* failed health analyses */
2601 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2602
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002603 /* requests : req_rate, req_rate_max, req_tot, */
2604 chunk_printf(&msg, ",,,");
2605
Willy Tarreauae526782010-03-04 20:34:23 +01002606 /* errors: cli_aborts, srv_aborts */
2607 chunk_printf(&msg, "%lld,%lld,",
2608 sv->counters.cli_aborts, sv->counters.srv_aborts);
2609
Willy Tarreau7f062c42009-03-05 18:43:00 +01002610 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002611 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002612 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002613 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002614 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002615 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002616
Willy Tarreau295a8372011-03-10 11:25:07 +01002617 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02002618 /* fall through */
2619
Willy Tarreau295a8372011-03-10 11:25:07 +01002620 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02002621 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002622 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002623 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2624 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002625 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01002626 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002627 /* Column sub-heading for Enable or Disable server */
2628 chunk_printf(&msg, "<td></td>");
2629 }
2630 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002631
2632 if (uri->flags&ST_SHLGNDS) {
2633 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002634 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002635 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002636
2637 /* cookie */
2638 if (px->cookie_name) {
2639 struct chunk src;
2640
2641 chunk_printf(&msg, ", cookie: '");
2642
2643 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2644 chunk_htmlencode(&msg, &src);
2645
2646 chunk_printf(&msg, "'");
2647 }
2648
2649 chunk_printf(&msg, "\"");
2650
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002651 }
2652
2653 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002654 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01002655 ">%s<a name=\"%s/Backend\"></a>"
2656 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002657 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002658 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002659 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002660 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002661 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002662 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002663 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002664 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002665 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
2666 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02002667
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002668 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002669 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002670 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002671 "<td"
2672 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002673 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002674
2675 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2676 if (px->mode == PR_MODE_HTTP) {
2677 int i;
2678
2679 chunk_printf(&msg, " title=\"rsp codes:");
2680
2681 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002682 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002683
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002684 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002685 }
2686
2687 chunk_printf(&msg,
2688 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002689 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002690 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002691 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002692 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002693 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002694 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01002695 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002696 U2H7(px->be_counters.cum_lbconn),
2697 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002698
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002699 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002700 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002701 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002702 /* errors : request, connect */
2703 "<td></td><td>%s</td>"
2704 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002705 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002706 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002707 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002708 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02002709 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002710 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02002711 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002712 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2713 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002714 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002715 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
2716 U2H2(px->be_counters.failed_conns),
2717 px->be_counters.cli_aborts,
2718 px->be_counters.srv_aborts,
2719 U2H5(px->be_counters.failed_resp),
2720 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002721 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01002722 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2723 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01002724 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002725 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002726
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002727 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002728 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002729 "<td class=ac>&nbsp;</td><td>%d</td>"
2730 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002731 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002732 "</tr>",
2733 px->down_trans,
2734 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002735 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002736 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002737 /* pxid, name */
2738 "%s,BACKEND,"
2739 /* queue : current, max */
2740 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002741 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002742 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002743 /* bytes : in, out */
2744 "%lld,%lld,"
2745 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002746 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002747 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002748 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002749 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002750 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002751 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02002752 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002753 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02002754 * active and backups. */
2755 "%s,"
2756 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002757 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002758 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002759 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002760 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002761 /* rate, rate_lim, rate_max, */
2762 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002763 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002764 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002765 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002766 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2767 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2768 px->be_counters.bytes_in, px->be_counters.bytes_out,
2769 px->be_counters.denied_req, px->be_counters.denied_resp,
2770 px->be_counters.failed_conns, px->be_counters.failed_resp,
2771 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01002772 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01002773 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002774 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002775 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01002776 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002777 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002778 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002779 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002780 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002781
2782 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2783 if (px->mode == PR_MODE_HTTP) {
2784 int i;
2785
2786 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002787 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002788
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002789 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002790 } else {
2791 chunk_printf(&msg, ",,,,,,");
2792 }
2793
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002794 /* failed health analyses */
2795 chunk_printf(&msg, ",");
2796
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002797 /* requests : req_rate, req_rate_max, req_tot, */
2798 chunk_printf(&msg, ",,,");
2799
Willy Tarreauae526782010-03-04 20:34:23 +01002800 /* errors: cli_aborts, srv_aborts */
2801 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002802 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01002803
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002804 /* finish with EOL */
2805 chunk_printf(&msg, "\n");
2806
Willy Tarreau55bb8452007-10-17 18:44:57 +02002807 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002808 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002809 return 0;
2810 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002811
Willy Tarreau295a8372011-03-10 11:25:07 +01002812 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002813 /* fall through */
2814
Willy Tarreau295a8372011-03-10 11:25:07 +01002815 case STAT_PX_ST_END:
2816 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002817 chunk_printf(&msg, "</table>");
2818
Willy Tarreau295a8372011-03-10 11:25:07 +01002819 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002820 /* close the form used to enable/disable this proxy servers */
2821 chunk_printf(&msg,
2822 "Choose the action to perform on the checked servers : "
2823 "<select name=action>"
2824 "<option value=\"\"></option>"
2825 "<option value=\"disable\">Disable</option>"
2826 "<option value=\"enable\">Enable</option>"
2827 "</select>"
2828 "<input type=\"hidden\" name=\"b\" value=\"%s\">"
2829 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2830 "</form>",
2831 px->id);
2832 }
2833
2834 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02002835
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002836 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002837 return 0;
2838 }
Willy Tarreau91861262007-10-17 17:06:05 +02002839
Willy Tarreau295a8372011-03-10 11:25:07 +01002840 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002841 /* fall through */
2842
Willy Tarreau295a8372011-03-10 11:25:07 +01002843 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002844 return 1;
2845
2846 default:
2847 /* unknown state, we should put an abort() here ! */
2848 return 1;
2849 }
2850}
2851
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002852/* This function dumps a complete session state onto the stream intreface's
2853 * read buffer. The data_ctx must have been zeroed first, and the flags
2854 * properly set. The session has to be set in data_ctx.sess.target. It returns
2855 * 0 if the output buffer is full and it needs to be called again, otherwise
2856 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002857 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002858static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002859{
2860 struct tm tm;
2861 struct chunk msg;
2862 struct session *sess;
2863 extern const char *monthname[12];
2864 char pn[INET6_ADDRSTRLEN];
2865
2866 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau295a8372011-03-10 11:25:07 +01002867 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002868
Willy Tarreau295a8372011-03-10 11:25:07 +01002869 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002870 /* session changed, no need to go any further */
2871 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002872 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002873 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002874 si->applet.ctx.sess.target = NULL;
2875 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002876 return 1;
2877 }
2878
Willy Tarreau295a8372011-03-10 11:25:07 +01002879 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002880 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01002881 si->applet.ctx.sess.uid = sess->uniq_id;
2882 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002883 /* fall through */
2884
2885 case 1:
2886 chunk_printf(&msg,
2887 "%p: id=%u, proto=%s",
2888 sess,
2889 sess->uniq_id,
2890 sess->listener->proto->name);
2891
2892 switch (sess->listener->proto->sock_family) {
2893 case AF_INET:
2894 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002895 (const void *)&((struct sockaddr_in *)&sess->si[0].addr.c.from)->sin_addr,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002896 pn, sizeof(pn));
2897
2898 chunk_printf(&msg,
2899 " source=%s:%d\n",
2900 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002901 ntohs(((struct sockaddr_in *)&sess->si[0].addr.c.from)->sin_port));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002902 break;
2903 case AF_INET6:
2904 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002905 (const void *)&((struct sockaddr_in6 *)(&sess->si[0].addr.c.from))->sin6_addr,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002906 pn, sizeof(pn));
2907
2908 chunk_printf(&msg,
2909 " source=%s:%d\n",
2910 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002911 ntohs(((struct sockaddr_in6 *)&sess->si[0].addr.c.from)->sin6_port));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002912 break;
2913 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02002914 chunk_printf(&msg,
2915 " source=unix:%d\n", sess->listener->luid);
2916 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002917 default:
2918 /* no more information to print right now */
2919 chunk_printf(&msg, "\n");
2920 break;
2921 }
2922
2923 chunk_printf(&msg,
2924 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02002925 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002926
2927 chunk_printf(&msg,
2928 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)\n",
2929 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
2930 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
2931 sess->listener ? sess->listener->luid : 0);
2932
2933 chunk_printf(&msg,
2934 " backend=%s (id=%u mode=%s) server=%s (id=%u)\n",
2935 sess->be->id, sess->be->uuid, sess->be->mode ? "http" : "tcp",
Willy Tarreau827aee92011-03-10 16:55:02 +01002936 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
2937 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002938
2939 chunk_printf(&msg,
2940 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
2941 sess->task,
2942 sess->task->state,
2943 sess->task->nice, sess->task->calls,
2944 sess->task->expire ?
2945 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
2946 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
2947 TICKS_TO_MS(1000)) : "<NEVER>",
2948 task_in_rq(sess->task) ? ", running" : "");
2949
2950 get_localtime(sess->logs.accept_date.tv_sec, &tm);
2951 chunk_printf(&msg,
2952 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
2953 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2954 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
2955 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
2956
2957 chunk_printf(&msg,
2958 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
2959 &sess->si[0],
2960 sess->si[0].state,
2961 sess->si[0].flags,
2962 sess->si[0].fd,
2963 sess->si[0].exp ?
2964 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
2965 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
2966 TICKS_TO_MS(1000)) : "<NEVER>",
2967 sess->si[0].err_type);
2968
2969 chunk_printf(&msg,
2970 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
2971 &sess->si[1],
2972 sess->si[1].state,
2973 sess->si[1].flags,
2974 sess->si[1].fd,
2975 sess->si[1].exp ?
2976 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
2977 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
2978 TICKS_TO_MS(1000)) : "<NEVER>",
2979 sess->si[1].err_type);
2980
2981 chunk_printf(&msg,
2982 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
2983 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
2984 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
2985
2986
2987 chunk_printf(&msg,
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02002988 " req=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002989 " an_exp=%s",
2990 sess->req,
2991 sess->req->flags, sess->req->analysers,
2992 sess->req->l, sess->req->send_max,
2993 sess->req->pipe ? sess->req->pipe->data : 0,
2994 sess->req->to_forward,
2995 sess->req->analyse_exp ?
2996 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
2997 TICKS_TO_MS(1000)) : "<NEVER>");
2998
2999 chunk_printf(&msg,
3000 " rex=%s",
3001 sess->req->rex ?
3002 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3003 TICKS_TO_MS(1000)) : "<NEVER>");
3004
3005 chunk_printf(&msg,
3006 " wex=%s\n"
3007 " data=%p r=%d w=%d lr=%d total=%lld\n",
3008 sess->req->wex ?
3009 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3010 TICKS_TO_MS(1000)) : "<NEVER>",
3011 sess->req->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01003012 (int)(sess->req->r - sess->req->data),
3013 (int)(sess->req->w - sess->req->data),
3014 (int)(sess->req->lr - sess->req->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003015 sess->req->total);
3016
3017 chunk_printf(&msg,
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02003018 " res=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003019 " an_exp=%s",
3020 sess->rep,
3021 sess->rep->flags, sess->rep->analysers,
3022 sess->rep->l, sess->rep->send_max,
3023 sess->rep->pipe ? sess->rep->pipe->data : 0,
3024 sess->rep->to_forward,
3025 sess->rep->analyse_exp ?
3026 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3027 TICKS_TO_MS(1000)) : "<NEVER>");
3028
3029 chunk_printf(&msg,
3030 " rex=%s",
3031 sess->rep->rex ?
3032 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3033 TICKS_TO_MS(1000)) : "<NEVER>");
3034
3035 chunk_printf(&msg,
3036 " wex=%s\n"
3037 " data=%p r=%d w=%d lr=%d total=%lld\n",
3038 sess->rep->wex ?
3039 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3040 TICKS_TO_MS(1000)) : "<NEVER>",
3041 sess->rep->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01003042 (int)(sess->rep->r - sess->rep->data),
3043 (int)(sess->rep->w - sess->rep->data),
3044 (int)(sess->rep->lr - sess->rep->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003045 sess->rep->total);
3046
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003047 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003048 return 0;
3049
3050 /* use other states to dump the contents */
3051 }
3052 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003053 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003054 return 1;
3055}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003056
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003057/* This function dumps all sessions' states onto the stream intreface's
3058 * read buffer. The data_ctx must have been zeroed first, and the flags
3059 * properly set. It returns 0 if the output buffer is full and it needs
3060 * to be called again, otherwise non-zero. It is designed to be called
3061 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003062 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003063static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003064{
3065 struct chunk msg;
3066
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003067 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003068 /* If we're forced to shut down, we might have to remove our
3069 * reference to the last session being dumped.
3070 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003071 if (si->applet.state == STAT_ST_LIST) {
3072 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3073 LIST_DEL(&si->applet.ctx.sess.bref.users);
3074 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003075 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003076 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003077 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003078 }
3079
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003080 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003081
Willy Tarreau295a8372011-03-10 11:25:07 +01003082 switch (si->applet.state) {
3083 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003084 /* the function had not been called yet, let's prepare the
3085 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003086 * pointer to the first in the global list. When a target
3087 * session is being destroyed, it is responsible for updating
3088 * this pointer. We know we have reached the end when this
3089 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003090 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003091 LIST_INIT(&si->applet.ctx.sess.bref.users);
3092 si->applet.ctx.sess.bref.ref = sessions.n;
3093 si->applet.state = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003094 /* fall through */
3095
Willy Tarreau295a8372011-03-10 11:25:07 +01003096 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003097 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003098 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3099 LIST_DEL(&si->applet.ctx.sess.bref.users);
3100 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003101 }
3102
3103 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003104 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003105 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003106 struct session *curr_sess;
3107
Willy Tarreau295a8372011-03-10 11:25:07 +01003108 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003109
Willy Tarreau295a8372011-03-10 11:25:07 +01003110 if (si->applet.ctx.sess.target) {
3111 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003112 goto next_sess;
3113
Willy Tarreau295a8372011-03-10 11:25:07 +01003114 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003115 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003116 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003117 return 0;
3118
3119 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003120 LIST_DEL(&si->applet.ctx.sess.bref.users);
3121 LIST_INIT(&si->applet.ctx.sess.bref.users);
3122 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003123 break;
3124 }
3125
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003126 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003127 "%p: proto=%s",
3128 curr_sess,
3129 curr_sess->listener->proto->name);
3130
3131 switch (curr_sess->listener->proto->sock_family) {
3132 case AF_INET:
3133 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003134 (const void *)&((struct sockaddr_in *)&curr_sess->si[0].addr.c.from)->sin_addr,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003135 pn, sizeof(pn));
3136
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003137 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003138 " src=%s:%d fe=%s be=%s srv=%s",
3139 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003140 ntohs(((struct sockaddr_in *)&curr_sess->si[0].addr.c.from)->sin_port),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003141 curr_sess->fe->id,
3142 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01003143 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003144 );
3145 break;
3146 case AF_INET6:
3147 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003148 (const void *)&((struct sockaddr_in6 *)(&curr_sess->si[0].addr.c.from))->sin6_addr,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003149 pn, sizeof(pn));
3150
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003151 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003152 " src=%s:%d fe=%s be=%s srv=%s",
3153 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003154 ntohs(((struct sockaddr_in6 *)&curr_sess->si[0].addr.c.from)->sin6_port),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003155 curr_sess->fe->id,
3156 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01003157 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003158 );
3159
3160 break;
3161 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02003162 chunk_printf(&msg,
3163 " src=unix:%d fe=%s be=%s srv=%s",
3164 curr_sess->listener->luid,
3165 curr_sess->fe->id,
3166 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01003167 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003168 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003169 break;
3170 }
3171
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003172 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003173 " ts=%02x age=%s calls=%d",
3174 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003175 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3176 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003177
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003178 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003179 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
3180 curr_sess->req->flags,
3181 curr_sess->req->l,
3182 curr_sess->req->analysers,
3183 curr_sess->req->rex ?
3184 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3185 TICKS_TO_MS(1000)) : "");
3186
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003187 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003188 ",wx=%s",
3189 curr_sess->req->wex ?
3190 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3191 TICKS_TO_MS(1000)) : "");
3192
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003193 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003194 ",ax=%s]",
3195 curr_sess->req->analyse_exp ?
3196 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3197 TICKS_TO_MS(1000)) : "");
3198
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003199 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003200 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
3201 curr_sess->rep->flags,
3202 curr_sess->rep->l,
3203 curr_sess->rep->analysers,
3204 curr_sess->rep->rex ?
3205 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3206 TICKS_TO_MS(1000)) : "");
3207
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003208 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003209 ",wx=%s",
3210 curr_sess->rep->wex ?
3211 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3212 TICKS_TO_MS(1000)) : "");
3213
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003214 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003215 ",ax=%s]",
3216 curr_sess->rep->analyse_exp ?
3217 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3218 TICKS_TO_MS(1000)) : "");
3219
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003220 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003221 " s0=[%d,%1xh,fd=%d,ex=%s]",
3222 curr_sess->si[0].state,
3223 curr_sess->si[0].flags,
3224 curr_sess->si[0].fd,
3225 curr_sess->si[0].exp ?
3226 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3227 TICKS_TO_MS(1000)) : "");
3228
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003229 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003230 " s1=[%d,%1xh,fd=%d,ex=%s]",
3231 curr_sess->si[1].state,
3232 curr_sess->si[1].flags,
3233 curr_sess->si[1].fd,
3234 curr_sess->si[1].exp ?
3235 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3236 TICKS_TO_MS(1000)) : "");
3237
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003238 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003239 " exp=%s",
3240 curr_sess->task->expire ?
3241 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3242 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003243 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003244 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003245
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003246 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003247
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003248 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003249 /* let's try again later from this session. We add ourselves into
3250 * this session's users so that it can remove us upon termination.
3251 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003252 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003253 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003254 }
3255
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003256 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003257 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003258 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003259
Willy Tarreau295a8372011-03-10 11:25:07 +01003260 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003261 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003262 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003263 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3264 else
3265 chunk_printf(&msg, "Session not found.\n");
3266
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003267 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003268 return 0;
3269
Willy Tarreau295a8372011-03-10 11:25:07 +01003270 si->applet.ctx.sess.target = NULL;
3271 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003272 return 1;
3273 }
3274
Willy Tarreau295a8372011-03-10 11:25:07 +01003275 si->applet.state = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003276 /* fall through */
3277
3278 default:
Willy Tarreau295a8372011-03-10 11:25:07 +01003279 si->applet.state = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003280 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003281 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003282}
3283
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003284/* This function dumps all tables' states onto the stream intreface's
3285 * read buffer. The data_ctx must have been zeroed first, and the flags
3286 * properly set. It returns 0 if the output buffer is full and it needs
3287 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003288 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003289static int stats_dump_table_to_buffer(struct stream_interface *si)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003290{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003291 struct session *s = si->applet.private;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003292 struct chunk msg;
3293 struct ebmb_node *eb;
3294 int dt;
3295
3296 /*
Willy Tarreau295a8372011-03-10 11:25:07 +01003297 * We have 3 possible states in si->applet.state :
3298 * - STAT_ST_INIT : the first call
3299 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003300 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003301 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003302 * and the entry pointer points to the next entry to be dumped,
3303 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003304 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003305 * data though.
3306 */
3307
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003308 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003309 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau295a8372011-03-10 11:25:07 +01003310 if (si->applet.state == STAT_ST_LIST) {
3311 si->applet.ctx.table.entry->ref_cnt--;
3312 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003313 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003314 return 1;
3315 }
3316
3317 chunk_init(&msg, trash, sizeof(trash));
3318
Willy Tarreau295a8372011-03-10 11:25:07 +01003319 while (si->applet.state != STAT_ST_FIN) {
3320 switch (si->applet.state) {
3321 case STAT_ST_INIT:
3322 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3323 if (!si->applet.ctx.table.proxy)
3324 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003325
Willy Tarreau295a8372011-03-10 11:25:07 +01003326 si->applet.ctx.table.entry = NULL;
3327 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003328 break;
3329
Willy Tarreau295a8372011-03-10 11:25:07 +01003330 case STAT_ST_INFO:
3331 if (!si->applet.ctx.table.proxy ||
3332 (si->applet.ctx.table.target &&
3333 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
3334 si->applet.state = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003335 break;
3336 }
3337
Willy Tarreau295a8372011-03-10 11:25:07 +01003338 if (si->applet.ctx.table.proxy->table.size) {
Simon Hormand9366582011-06-15 15:18:45 +09003339 if (!stats_dump_table_head_to_buffer(&msg, si, si->applet.ctx.table.proxy,
3340 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003341 return 0;
3342
Willy Tarreau295a8372011-03-10 11:25:07 +01003343 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003344 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
3345 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003346 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003347 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003348 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3349 si->applet.ctx.table.entry->ref_cnt++;
3350 si->applet.state = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003351 break;
3352 }
3353 }
3354 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003355 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003356 break;
3357
Willy Tarreau295a8372011-03-10 11:25:07 +01003358 case STAT_ST_LIST:
3359 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003360 /* we're filtering on some data contents */
3361 void *ptr;
3362 long long data;
3363
Willy Tarreau295a8372011-03-10 11:25:07 +01003364 dt = si->applet.ctx.table.data_type;
3365 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3366 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003367 dt);
3368
3369 data = 0;
3370 switch (stktable_data_types[dt].std_type) {
3371 case STD_T_SINT:
3372 data = stktable_data_cast(ptr, std_t_sint);
3373 break;
3374 case STD_T_UINT:
3375 data = stktable_data_cast(ptr, std_t_uint);
3376 break;
3377 case STD_T_ULL:
3378 data = stktable_data_cast(ptr, std_t_ull);
3379 break;
3380 case STD_T_FRQP:
3381 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003382 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003383 break;
3384 }
3385
3386 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003387 if ((data < si->applet.ctx.table.value &&
3388 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3389 si->applet.ctx.table.data_op == STD_OP_GT ||
3390 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3391 (data == si->applet.ctx.table.value &&
3392 (si->applet.ctx.table.data_op == STD_OP_NE ||
3393 si->applet.ctx.table.data_op == STD_OP_GT ||
3394 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3395 (data > si->applet.ctx.table.value &&
3396 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3397 si->applet.ctx.table.data_op == STD_OP_LT ||
3398 si->applet.ctx.table.data_op == STD_OP_LE)))
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003399 goto skip_entry;
3400 }
3401
Simon Hormand9366582011-06-15 15:18:45 +09003402 if (!stats_dump_table_entry_to_buffer(&msg, si, si->applet.ctx.table.proxy,
3403 si->applet.ctx.table.entry))
3404 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003405
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003406 skip_entry:
Willy Tarreau295a8372011-03-10 11:25:07 +01003407 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003408
Willy Tarreau295a8372011-03-10 11:25:07 +01003409 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003410 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003411 struct stksess *old = si->applet.ctx.table.entry;
3412 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3413 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3414 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003415 break;
3416 }
3417
Willy Tarreau295a8372011-03-10 11:25:07 +01003418 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3419 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
3420 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003421 break;
3422
Willy Tarreau295a8372011-03-10 11:25:07 +01003423 case STAT_ST_END:
3424 si->applet.state = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003425 break;
3426 }
3427 }
3428 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003429}
3430
Willy Tarreaud426a182010-03-05 14:58:26 +01003431/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003432 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3433 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3434 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3435 * lines are respected within the limit of 70 output chars. Lines that are
3436 * continuation of a previous truncated line begin with "+" instead of " "
3437 * after the offset. The new pointer is returned.
3438 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003439static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3440 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003441{
3442 int end;
3443 unsigned char c;
3444
3445 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003446 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003447 return ptr;
3448
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003449 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003450
Willy Tarreaud426a182010-03-05 14:58:26 +01003451 while (ptr < len && ptr < bsize) {
3452 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003453 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003454 if (out->len > end - 2)
3455 break;
3456 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003457 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003458 if (out->len > end - 3)
3459 break;
3460 out->str[out->len++] = '\\';
3461 switch (c) {
3462 case '\t': c = 't'; break;
3463 case '\n': c = 'n'; break;
3464 case '\r': c = 'r'; break;
3465 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003466 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003467 }
3468 out->str[out->len++] = c;
3469 } else {
3470 if (out->len > end - 5)
3471 break;
3472 out->str[out->len++] = '\\';
3473 out->str[out->len++] = 'x';
3474 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3475 out->str[out->len++] = hextab[c & 0xF];
3476 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003477 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003478 /* we had a line break, let's return now */
3479 out->str[out->len++] = '\n';
3480 *line = ptr;
3481 return ptr;
3482 }
3483 }
3484 /* we have an incomplete line, we return it as-is */
3485 out->str[out->len++] = '\n';
3486 return ptr;
3487}
3488
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003489/* This function dumps all captured errors onto the stream intreface's
3490 * read buffer. The data_ctx must have been zeroed first, and the flags
3491 * properly set. It returns 0 if the output buffer is full and it needs
3492 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003493 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003494static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003495{
3496 extern const char *monthname[12];
3497 struct chunk msg;
3498
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003499 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003500 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003501
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003502 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003503
Willy Tarreau295a8372011-03-10 11:25:07 +01003504 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003505 /* the function had not been called yet, let's prepare the
3506 * buffer for a response.
3507 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003508 struct tm tm;
3509
3510 get_localtime(date.tv_sec, &tm);
3511 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3512 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3513 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3514 error_snapshot_id);
3515
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003516 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003517 /* Socket buffer full. Let's try again later from the same point */
3518 return 0;
3519 }
3520
Willy Tarreau295a8372011-03-10 11:25:07 +01003521 si->applet.ctx.errors.px = proxy;
3522 si->applet.ctx.errors.buf = 0;
3523 si->applet.ctx.errors.bol = 0;
3524 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003525 }
3526
3527 /* we have two inner loops here, one for the proxy, the other one for
3528 * the buffer.
3529 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003530 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003531 struct error_snapshot *es;
3532
Willy Tarreau295a8372011-03-10 11:25:07 +01003533 if (si->applet.ctx.errors.buf == 0)
3534 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003535 else
Willy Tarreau295a8372011-03-10 11:25:07 +01003536 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003537
3538 if (!es->when.tv_sec)
3539 goto next;
3540
Willy Tarreau295a8372011-03-10 11:25:07 +01003541 if (si->applet.ctx.errors.iid >= 0 &&
3542 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
3543 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003544 goto next;
3545
Willy Tarreau295a8372011-03-10 11:25:07 +01003546 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003547 /* just print headers now */
3548
3549 char pn[INET6_ADDRSTRLEN];
3550 struct tm tm;
3551
3552 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003553 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003554 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003555 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003556
3557
3558 if (es->src.ss_family == AF_INET)
3559 inet_ntop(AF_INET,
3560 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
3561 pn, sizeof(pn));
3562 else
3563 inet_ntop(AF_INET6,
3564 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
3565 pn, sizeof(pn));
3566
Willy Tarreau295a8372011-03-10 11:25:07 +01003567 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003568 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003569 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003570 " frontend %s (#%d): invalid request\n"
3571 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003572 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003573 " request length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003574 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau3fd6cec2010-03-25 06:45:07 +01003575 pn, es->sid, (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3576 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003577 es->srv ? es->srv->id : "<NONE>",
3578 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003579 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003580 es->len, es->pos);
3581 break;
3582 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003583 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003584 " backend %s (#%d) : invalid response\n"
3585 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003586 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003587 " response length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003588 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003589 pn, es->sid, es->oe->id, es->oe->uuid,
3590 es->srv ? es->srv->id : "<NONE>",
3591 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003592 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003593 es->len, es->pos);
3594 break;
3595 }
3596
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003597 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003598 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003599 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003600 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003601 si->applet.ctx.errors.ptr = 0;
3602 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003603 }
3604
Willy Tarreau295a8372011-03-10 11:25:07 +01003605 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003606 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003607 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003608 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003609 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau61b34732009-10-03 23:49:35 +02003610 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003611 goto next;
3612 }
3613
3614 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01003615 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003616 int newptr;
3617 int newline;
3618
Willy Tarreau295a8372011-03-10 11:25:07 +01003619 newline = si->applet.ctx.errors.bol;
3620 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
3621 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02003622 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003623
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003624 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003625 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003626 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003627 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003628 si->applet.ctx.errors.ptr = newptr;
3629 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003630 };
3631 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01003632 si->applet.ctx.errors.bol = 0;
3633 si->applet.ctx.errors.ptr = -1;
3634 si->applet.ctx.errors.buf++;
3635 if (si->applet.ctx.errors.buf > 1) {
3636 si->applet.ctx.errors.buf = 0;
3637 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003638 }
3639 }
3640
3641 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02003642 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003643}
3644
Willy Tarreaub24281b2011-02-13 13:16:36 +01003645struct si_applet http_stats_applet = {
3646 .name = "<STATS>", /* used for logging */
3647 .fct = http_stats_io_handler,
3648};
3649
Simon Horman9bd2c732011-06-15 15:18:44 +09003650static struct si_applet cli_applet = {
Willy Tarreaub24281b2011-02-13 13:16:36 +01003651 .name = "<CLI>", /* used for logging */
3652 .fct = cli_io_handler,
3653};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003654
Willy Tarreau10522fd2008-07-09 20:12:41 +02003655static struct cfg_kw_list cfg_kws = {{ },{
3656 { CFG_GLOBAL, "stats", stats_parse_global },
3657 { 0, NULL, NULL },
3658}};
3659
3660__attribute__((constructor))
3661static void __dumpstats_module_init(void)
3662{
3663 cfg_register_keywords(&cfg_kws);
3664}
3665
Willy Tarreau91861262007-10-17 17:06:05 +02003666/*
3667 * Local variables:
3668 * c-indent-level: 8
3669 * c-basic-offset: 8
3670 * End:
3671 */