blob: 4fde2919a95c324e196533139c498121b6fed8df [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaueb472682010-05-28 18:46:57 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
Simon Horman17bce342011-06-15 15:18:47 +090017#include <stdbool.h>
Simon Hormancec9a222011-06-15 15:18:51 +090018#include <stdint.h>
Willy Tarreau91861262007-10-17 17:06:05 +020019#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020022#include <pwd.h>
23#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020024
25#include <sys/socket.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28
Willy Tarreau10522fd2008-07-09 20:12:41 +020029#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020030#include <common/compat.h>
31#include <common/config.h>
32#include <common/debug.h>
33#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
40
Willy Tarreau91861262007-10-17 17:06:05 +020041#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020042
43#include <proto/backend.h>
44#include <proto/buffers.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020045#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020046#include <proto/dumpstats.h>
47#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010048#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020049#include <proto/log.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010050#include <proto/pipe.h>
Willy Tarreau2a0f4d22011-08-02 11:49:05 +020051#include <proto/protocols.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020052#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020053#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020054#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020055#include <proto/server.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010056#include <proto/stream_interface.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020057#include <proto/stream_sock.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010058#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020059
Simon Horman9bd2c732011-06-15 15:18:44 +090060static int stats_dump_raw_to_buffer(struct stream_interface *si);
61static int stats_dump_full_sess_to_buffer(struct stream_interface *si);
62static int stats_dump_sess_to_buffer(struct stream_interface *si);
63static int stats_dump_errors_to_buffer(struct stream_interface *si);
Simon Hormanc88b8872011-06-15 15:18:49 +090064static int stats_table_request(struct stream_interface *si, bool show);
Simon Horman9bd2c732011-06-15 15:18:44 +090065static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
66static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri);
67
68static struct si_applet cli_applet;
69
70static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020071 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +020072 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +020073 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020074 " help : this message\n"
75 " prompt : toggle interactive mode with prompt\n"
76 " quit : disconnect\n"
77 " show info : report information about the running process\n"
78 " show stat : report counters for each proxy and server\n"
79 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +010080 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +020081 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +020082 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +020083 " set weight : change a server's weight\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +010084 " set timeout : change a timeout setting\n"
Cyril Bontécd19e512010-01-31 22:34:03 +010085 " disable server : set a server in maintenance mode\n"
86 " enable server : re-enable a server that was previously in maintenance mode\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +020087 " set maxconn : change a maxconn setting\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020088 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020089
Simon Horman9bd2c732011-06-15 15:18:44 +090090static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +020091 "Permission denied\n"
92 "";
93
Willy Tarreau295a8372011-03-10 11:25:07 +010094/* data transmission states for the stats responses */
95enum {
96 STAT_ST_INIT = 0,
97 STAT_ST_HEAD,
98 STAT_ST_INFO,
99 STAT_ST_LIST,
100 STAT_ST_END,
101 STAT_ST_FIN,
102};
103
104/* data transmission states for the stats responses inside a proxy */
105enum {
106 STAT_PX_ST_INIT = 0,
107 STAT_PX_ST_TH,
108 STAT_PX_ST_FE,
109 STAT_PX_ST_LI,
110 STAT_PX_ST_SV,
111 STAT_PX_ST_BE,
112 STAT_PX_ST_END,
113 STAT_PX_ST_FIN,
114};
115
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200116/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +0200117 * a new stats socket. It returns a positive value upon success, 0 if the connection
118 * needs to be closed and ignored, or a negative value upon critical failure.
119 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900120static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200121{
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200122 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100123 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200124 copy_target(&s->target, &s->si[1].target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +0100125 s->si[1].applet.private = s;
126 s->si[1].applet.st1 = 0;
127 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200128
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200129 tv_zero(&s->logs.tv_request);
130 s->logs.t_queue = 0;
131 s->logs.t_connect = 0;
132 s->logs.t_data = 0;
133 s->logs.t_close = 0;
134 s->logs.bytes_in = s->logs.bytes_out = 0;
135 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
136 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200137
Willy Tarreaueb472682010-05-28 18:46:57 +0200138 s->req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
139
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200140 if (s->listener->timeout) {
141 s->req->rto = *s->listener->timeout;
142 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200143 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200144 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200145}
146
Willy Tarreau07e9e642010-08-17 21:48:17 +0200147/* allocate a new stats frontend named <name>, and return it
148 * (or NULL in case of lack of memory).
149 */
150static struct proxy *alloc_stats_fe(const char *name)
151{
152 struct proxy *fe;
153
154 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
155 if (!fe)
156 return NULL;
157
Willy Tarreau237250c2011-07-29 01:49:03 +0200158 init_new_proxy(fe);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200159
160 fe->last_change = now.tv_sec;
161 fe->id = strdup("GLOBAL");
162 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200163 fe->maxconn = 10; /* default to 10 concurrent connections */
164 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
165
Willy Tarreau07e9e642010-08-17 21:48:17 +0200166 return fe;
167}
168
Willy Tarreaufbee7132007-10-18 13:53:22 +0200169/* This function parses a "stats" statement in the "global" section. It returns
170 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
171 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
172 * zero included. The trailing '\n' must not be written. The function must be
173 * called with <args> pointing to the first word after "stats".
174 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200175static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
176 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200177{
Willy Tarreau10522fd2008-07-09 20:12:41 +0200178 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200179 if (!strcmp(args[0], "socket")) {
Andreas Kohn16171e22011-01-19 20:29:32 +0100180 struct sockaddr_un *su;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200181 int cur_arg;
182
183 if (*args[1] == 0) {
184 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
185 return -1;
186 }
187
188 if (global.stats_sock.state != LI_NEW) {
189 snprintf(err, errlen, "'stats socket' already specified in global section");
190 return -1;
191 }
192
Andreas Kohn16171e22011-01-19 20:29:32 +0100193 su = str2sun(args[1]);
194 if (!su) {
195 snprintf(err, errlen, "'stats socket' path would require truncation");
196 return -1;
197 }
198 memcpy(&global.stats_sock.addr, su, sizeof(struct sockaddr_un)); // guaranteed to fit
Willy Tarreaufbee7132007-10-18 13:53:22 +0200199
Willy Tarreau89a63132009-08-16 17:41:45 +0200200 if (!global.stats_fe) {
Willy Tarreau07e9e642010-08-17 21:48:17 +0200201 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau89a63132009-08-16 17:41:45 +0200202 snprintf(err, errlen, "out of memory");
203 return -1;
204 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200205 }
206
Willy Tarreaufbee7132007-10-18 13:53:22 +0200207 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100208 global.stats_sock.options = LI_O_NONE;
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200209 global.stats_sock.accept = session_accept;
210 global.stats_fe->accept = stats_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200211 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200212 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200213 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreaueb472682010-05-28 18:46:57 +0200214 global.stats_sock.frontend = global.stats_fe;
Willy Tarreau6162db22009-10-10 17:13:00 +0200215 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200216 global.stats_sock.maxconn = global.stats_fe->maxconn;
Willy Tarreau89a63132009-08-16 17:41:45 +0200217 global.stats_sock.timeout = &global.stats_fe->timeout.client;
218
219 global.stats_sock.next = global.stats_fe->listen;
220 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200221
222 cur_arg = 2;
223 while (*args[cur_arg]) {
224 if (!strcmp(args[cur_arg], "uid")) {
225 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
226 cur_arg += 2;
227 }
228 else if (!strcmp(args[cur_arg], "gid")) {
229 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
230 cur_arg += 2;
231 }
232 else if (!strcmp(args[cur_arg], "mode")) {
233 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
234 cur_arg += 2;
235 }
236 else if (!strcmp(args[cur_arg], "user")) {
237 struct passwd *user;
238 user = getpwnam(args[cur_arg + 1]);
239 if (!user) {
240 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
241 args[cur_arg + 1]);
242 return -1;
243 }
244 global.stats_sock.perm.ux.uid = user->pw_uid;
245 cur_arg += 2;
246 }
247 else if (!strcmp(args[cur_arg], "group")) {
248 struct group *group;
249 group = getgrnam(args[cur_arg + 1]);
250 if (!group) {
251 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
252 args[cur_arg + 1]);
253 return -1;
254 }
255 global.stats_sock.perm.ux.gid = group->gr_gid;
256 cur_arg += 2;
257 }
Willy Tarreau6162db22009-10-10 17:13:00 +0200258 else if (!strcmp(args[cur_arg], "level")) {
259 if (!strcmp(args[cur_arg+1], "user"))
260 global.stats_sock.perm.ux.level = ACCESS_LVL_USER;
261 else if (!strcmp(args[cur_arg+1], "operator"))
262 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER;
263 else if (!strcmp(args[cur_arg+1], "admin"))
264 global.stats_sock.perm.ux.level = ACCESS_LVL_ADMIN;
265 else {
266 snprintf(err, errlen, "'stats socket level' only supports 'user', 'operator', and 'admin'");
267 return -1;
268 }
269 cur_arg += 2;
270 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200271 else {
Willy Tarreau6162db22009-10-10 17:13:00 +0200272 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', 'level', and 'mode'");
Willy Tarreaufbee7132007-10-18 13:53:22 +0200273 return -1;
274 }
275 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100276
Willy Tarreaufbee7132007-10-18 13:53:22 +0200277 uxst_add_listener(&global.stats_sock);
278 global.maxsock++;
279 }
280 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100281 unsigned timeout;
282 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
283
284 if (res) {
285 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
286 return -1;
287 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200288
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100289 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200290 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
291 return -1;
292 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200293 if (!global.stats_fe) {
294 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
295 snprintf(err, errlen, "out of memory");
296 return -1;
297 }
298 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200299 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200300 }
301 else if (!strcmp(args[0], "maxconn")) {
302 int maxconn = atol(args[1]);
303
304 if (maxconn <= 0) {
305 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
306 return -1;
307 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200308
309 if (!global.stats_fe) {
310 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
311 snprintf(err, errlen, "out of memory");
312 return -1;
313 }
314 }
315 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200316 }
317 else {
318 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
319 return -1;
320 }
321 return 0;
322}
323
Simon Horman9bd2c732011-06-15 15:18:44 +0900324static int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100325{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200326 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100327 "# pxname,svname,"
328 "qcur,qmax,"
329 "scur,smax,slim,stot,"
330 "bin,bout,"
331 "dreq,dresp,"
332 "ereq,econ,eresp,"
333 "wretr,wredis,"
334 "status,weight,act,bck,"
335 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200336 "pid,iid,sid,throttle,lbtot,tracked,type,"
337 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200338 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100339 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100340 "req_rate,req_rate_max,req_tot,"
341 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100342 "\n");
343}
344
Simon Hormand9366582011-06-15 15:18:45 +0900345/* print a string of text buffer to <out>. The format is :
346 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
347 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
348 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
349 */
350static int dump_text(struct chunk *out, const char *buf, int bsize)
351{
352 unsigned char c;
353 int ptr = 0;
354
355 while (buf[ptr] && ptr < bsize) {
356 c = buf[ptr];
357 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
358 if (out->len > out->size - 1)
359 break;
360 out->str[out->len++] = c;
361 }
362 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
363 if (out->len > out->size - 2)
364 break;
365 out->str[out->len++] = '\\';
366 switch (c) {
367 case ' ': c = ' '; break;
368 case '\t': c = 't'; break;
369 case '\n': c = 'n'; break;
370 case '\r': c = 'r'; break;
371 case '\e': c = 'e'; break;
372 case '\\': c = '\\'; break;
373 }
374 out->str[out->len++] = c;
375 }
376 else {
377 if (out->len > out->size - 4)
378 break;
379 out->str[out->len++] = '\\';
380 out->str[out->len++] = 'x';
381 out->str[out->len++] = hextab[(c >> 4) & 0xF];
382 out->str[out->len++] = hextab[c & 0xF];
383 }
384 ptr++;
385 }
386
387 return ptr;
388}
389
390/* print a buffer in hexa.
391 * Print stopped if <bsize> is reached, or if no more place in the chunk.
392 */
393static int dump_binary(struct chunk *out, const char *buf, int bsize)
394{
395 unsigned char c;
396 int ptr = 0;
397
398 while (ptr < bsize) {
399 c = buf[ptr];
400
401 if (out->len > out->size - 2)
402 break;
403 out->str[out->len++] = hextab[(c >> 4) & 0xF];
404 out->str[out->len++] = hextab[c & 0xF];
405
406 ptr++;
407 }
408 return ptr;
409}
410
411/* Dump the status of a table to a stream interface's
412 * read buffer. It returns 0 if the output buffer is full
413 * and needs to be called again, otherwise non-zero.
414 */
415static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
416 struct proxy *proxy, struct proxy *target)
417{
418 struct session *s = si->applet.private;
419
420 chunk_printf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
421 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
422
423 /* any other information should be dumped here */
424
425 if (target && s->listener->perm.ux.level < ACCESS_LVL_OPER)
426 chunk_printf(msg, "# contents not dumped due to insufficient privileges\n");
427
428 if (buffer_feed_chunk(si->ib, msg) >= 0)
429 return 0;
430
431 return 1;
432}
433
434/* Dump the a table entry to a stream interface's
435 * read buffer. It returns 0 if the output buffer is full
436 * and needs to be called again, otherwise non-zero.
437 */
438static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
439 struct proxy *proxy, struct stksess *entry)
440{
441 int dt;
442
443 chunk_printf(msg, "%p:", entry);
444
445 if (proxy->table.type == STKTABLE_TYPE_IP) {
446 char addr[INET_ADDRSTRLEN];
447 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
448 chunk_printf(msg, " key=%s", addr);
449 }
450 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
451 char addr[INET6_ADDRSTRLEN];
452 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
453 chunk_printf(msg, " key=%s", addr);
454 }
455 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
456 chunk_printf(msg, " key=%u", *(unsigned int *)entry->key.key);
457 }
458 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
459 chunk_printf(msg, " key=");
460 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
461 }
462 else {
463 chunk_printf(msg, " key=");
464 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
465 }
466
467 chunk_printf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
468
469 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
470 void *ptr;
471
472 if (proxy->table.data_ofs[dt] == 0)
473 continue;
474 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
475 chunk_printf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
476 else
477 chunk_printf(msg, " %s=", stktable_data_types[dt].name);
478
479 ptr = stktable_data_ptr(&proxy->table, entry, dt);
480 switch (stktable_data_types[dt].std_type) {
481 case STD_T_SINT:
482 chunk_printf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
483 break;
484 case STD_T_UINT:
485 chunk_printf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
486 break;
487 case STD_T_ULL:
488 chunk_printf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
489 break;
490 case STD_T_FRQP:
491 chunk_printf(msg, "%d",
492 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
493 proxy->table.data_arg[dt].u));
494 break;
495 }
496 }
497 chunk_printf(msg, "\n");
498
499 if (buffer_feed_chunk(si->ib, msg) >= 0)
500 return 0;
501
502 return 1;
503}
504
Simon Horman17bce342011-06-15 15:18:47 +0900505static void stats_sock_table_key_request(struct stream_interface *si, char **args, bool show)
Simon Horman121f3052011-06-15 15:18:46 +0900506{
507 struct session *s = si->applet.private;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900508 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900509 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900510 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900511 unsigned char ip6_key[sizeof(struct in6_addr)];
Simon Horman121f3052011-06-15 15:18:46 +0900512
Simon Hormand5b9fd92011-06-15 15:18:48 +0900513 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900514
515 if (!*args[4]) {
516 si->applet.ctx.cli.msg = "Key value expected\n";
517 si->applet.st0 = STAT_CLI_PRINT;
518 return;
519 }
520
Simon Hormanc5b89f62011-06-15 15:18:50 +0900521 switch (px->table.type) {
522 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900523 uint32_key = htonl(inetaddr_host(args[4]));
524 static_table_key.key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900525 break;
526 case STKTABLE_TYPE_IPV6:
527 inet_pton(AF_INET6, args[4], ip6_key);
528 static_table_key.key = &ip6_key;
529 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900530 case STKTABLE_TYPE_INTEGER:
531 {
532 char *endptr;
533 unsigned long val;
534 errno = 0;
535 val = strtoul(args[4], &endptr, 10);
536 if ((errno == ERANGE && val == ULONG_MAX) ||
537 (errno != 0 && val == 0) || endptr == args[4] ||
538 val > 0xffffffff) {
539 si->applet.ctx.cli.msg = "Invalid key\n";
540 si->applet.st0 = STAT_CLI_PRINT;
541 return;
542 }
543 uint32_key = (uint32_t) val;
544 static_table_key.key = &uint32_key;
545 break;
546 }
547 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900548 case STKTABLE_TYPE_STRING:
549 static_table_key.key = args[4];
550 static_table_key.key_len = strlen(args[4]);
551 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900552 default:
Simon Horman17bce342011-06-15 15:18:47 +0900553 if (show)
Simon Hormancec9a222011-06-15 15:18:51 +0900554 si->applet.ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6 and integer is not supported\n";
Simon Horman17bce342011-06-15 15:18:47 +0900555 else
Simon Hormancec9a222011-06-15 15:18:51 +0900556 si->applet.ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6 and integer is not supported\n";
Simon Horman121f3052011-06-15 15:18:46 +0900557 si->applet.st0 = STAT_CLI_PRINT;
558 return;
559 }
560
561 /* check permissions */
562 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
563 si->applet.ctx.cli.msg = stats_permission_denied_msg;
564 si->applet.st0 = STAT_CLI_PRINT;
565 return;
566 }
567
568 ts = stktable_lookup_key(&px->table, &static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900569 if (!ts)
570 return;
571
572 if (show) {
573 struct chunk msg;
574 chunk_init(&msg, trash, sizeof(trash));
575 if (!stats_dump_table_head_to_buffer(&msg, si, px, px))
576 return;
577 stats_dump_table_entry_to_buffer(&msg, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900578 return;
579 }
Simon Horman17bce342011-06-15 15:18:47 +0900580
581 if (ts->ref_cnt) {
Simon Horman121f3052011-06-15 15:18:46 +0900582 /* don't delete an entry which is currently referenced */
583 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
584 si->applet.st0 = STAT_CLI_PRINT;
585 return;
586 }
587
588 stksess_kill(&px->table, ts);
589}
590
Simon Hormand5b9fd92011-06-15 15:18:48 +0900591static void stats_sock_table_data_request(struct stream_interface *si, char **args)
592{
593 /* condition on stored data value */
594 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
595 if (si->applet.ctx.table.data_type < 0) {
596 si->applet.ctx.cli.msg = "Unknown data type\n";
597 si->applet.st0 = STAT_CLI_PRINT;
598 return;
599 }
600
601 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
602 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
603 si->applet.st0 = STAT_CLI_PRINT;
604 return;
605 }
606
607 si->applet.ctx.table.data_op = get_std_op(args[4]);
608 if (si->applet.ctx.table.data_op < 0) {
609 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
610 si->applet.st0 = STAT_CLI_PRINT;
611 return;
612 }
613
614 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
615 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
616 si->applet.st0 = STAT_CLI_PRINT;
617 return;
618 }
619}
620
621static void stats_sock_table_request(struct stream_interface *si, char **args, bool show)
622{
623 si->applet.ctx.table.data_type = -1;
624 si->applet.state = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200625 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900626 si->applet.ctx.table.proxy = NULL;
627 si->applet.ctx.table.entry = NULL;
Simon Hormanc88b8872011-06-15 15:18:49 +0900628 if (show)
629 si->applet.st0 = STAT_CLI_O_TAB;
630 else
631 si->applet.st0 = STAT_CLI_O_CLR;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900632
633 if (*args[2]) {
634 si->applet.ctx.table.target = find_stktable(args[2]);
635 if (!si->applet.ctx.table.target) {
636 si->applet.ctx.cli.msg = "No such table\n";
637 si->applet.st0 = STAT_CLI_PRINT;
638 return;
639 }
640 }
641 else {
642 if (!show)
643 goto err_args;
644 return;
645 }
646
647 if (strcmp(args[3], "key") == 0)
648 stats_sock_table_key_request(si, args, show);
Simon Hormanc88b8872011-06-15 15:18:49 +0900649 else if (strncmp(args[3], "data.", 5) == 0)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900650 stats_sock_table_data_request(si, args);
Simon Hormanc88b8872011-06-15 15:18:49 +0900651 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900652 goto err_args;
653
654 return;
655
656err_args:
657 if (show)
658 si->applet.ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
659 else
Simon Hormanc88b8872011-06-15 15:18:49 +0900660 si->applet.ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
Simon Hormand5b9fd92011-06-15 15:18:48 +0900661 si->applet.st0 = STAT_CLI_PRINT;
662}
663
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200664/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200665 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100666 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200667 * designating the function which will have to process the request, which can
668 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200669 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900670static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200671{
Willy Tarreaubc4af052011-02-13 13:25:14 +0100672 struct session *s = si->applet.private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200673 char *args[MAX_STATS_ARGS + 1];
674 int arg;
675
676 while (isspace((unsigned char)*line))
677 line++;
678
679 arg = 0;
680 args[arg] = line;
681
682 while (*line && arg < MAX_STATS_ARGS) {
683 if (isspace((unsigned char)*line)) {
684 *line++ = '\0';
685
686 while (isspace((unsigned char)*line))
687 line++;
688
689 args[++arg] = line;
690 continue;
691 }
692
693 line++;
694 }
695
696 while (++arg <= MAX_STATS_ARGS)
697 args[arg] = line;
698
Willy Tarreau295a8372011-03-10 11:25:07 +0100699 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200700 if (strcmp(args[0], "show") == 0) {
701 if (strcmp(args[1], "stat") == 0) {
702 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100703 si->applet.ctx.stats.flags |= STAT_BOUND;
704 si->applet.ctx.stats.iid = atoi(args[2]);
705 si->applet.ctx.stats.type = atoi(args[3]);
706 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200707 }
708
Willy Tarreau295a8372011-03-10 11:25:07 +0100709 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
710 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
711 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100712 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200713 }
714 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100715 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
716 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
717 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100718 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200719 }
720 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100721 si->applet.state = STAT_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200722 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100723 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100724 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200725 return 1;
726 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100727 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100728 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100729 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100730 si->applet.ctx.sess.target = NULL;
731 si->applet.ctx.sess.section = 0; /* start with session status */
732 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100733 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200734 }
735 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200736 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100737 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100738 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200739 return 1;
740 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200741 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100742 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200743 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100744 si->applet.ctx.errors.iid = -1;
745 si->applet.ctx.errors.px = NULL;
746 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100747 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200748 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200749 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900750 stats_sock_table_request(si, args, true);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200751 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200752 else { /* neither "stat" nor "info" nor "sess" nor "errors" no "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200753 return 0;
754 }
755 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200756 else if (strcmp(args[0], "clear") == 0) {
757 if (strcmp(args[1], "counters") == 0) {
758 struct proxy *px;
759 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200760 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200761 int clrall = 0;
762
763 if (strcmp(args[2], "all") == 0)
764 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200765
Willy Tarreau6162db22009-10-10 17:13:00 +0200766 /* check permissions */
767 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
768 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100769 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100770 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200771 return 1;
772 }
773
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200774 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100775 if (clrall) {
776 memset(&px->be_counters, 0, sizeof(px->be_counters));
777 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
778 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200779 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100780 px->be_counters.conn_max = 0;
781 px->be_counters.p.http.rps_max = 0;
782 px->be_counters.sps_max = 0;
783 px->be_counters.cps_max = 0;
784 px->be_counters.nbpend_max = 0;
785
786 px->fe_counters.conn_max = 0;
787 px->fe_counters.p.http.rps_max = 0;
788 px->fe_counters.sps_max = 0;
789 px->fe_counters.cps_max = 0;
790 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200791 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200792
793 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200794 if (clrall)
795 memset(&sv->counters, 0, sizeof(sv->counters));
796 else {
797 sv->counters.cur_sess_max = 0;
798 sv->counters.nbpend_max = 0;
799 sv->counters.sps_max = 0;
800 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200801
802 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200803 if (li->counters) {
804 if (clrall)
805 memset(li->counters, 0, sizeof(*li->counters));
806 else
807 li->counters->conn_max = 0;
808 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200809 }
810
Willy Tarreau81c25d02011-09-07 15:17:21 +0200811 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200812 return 1;
813 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200814 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900815 stats_sock_table_request(si, args, false);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200816 /* end of processing */
817 return 1;
818 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200819 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200820 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200821 return 0;
822 }
823 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200824 else if (strcmp(args[0], "get") == 0) {
825 if (strcmp(args[1], "weight") == 0) {
826 struct proxy *px;
827 struct server *sv;
828
829 /* split "backend/server" and make <line> point to server */
830 for (line = args[2]; *line; line++)
831 if (*line == '/') {
832 *line++ = '\0';
833 break;
834 }
835
836 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100837 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100838 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200839 return 1;
840 }
841
842 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100843 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100844 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200845 return 1;
846 }
847
848 /* return server's effective weight at the moment */
849 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
850 buffer_feed(si->ib, trash);
851 return 1;
852 }
853 else { /* not "get weight" */
854 return 0;
855 }
856 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200857 else if (strcmp(args[0], "set") == 0) {
858 if (strcmp(args[1], "weight") == 0) {
859 struct proxy *px;
860 struct server *sv;
861 int w;
862
863 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100864 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100865 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200866 return 1;
867 }
868
869 /* split "backend/server" and make <line> point to server */
870 for (line = args[2]; *line; line++)
871 if (*line == '/') {
872 *line++ = '\0';
873 break;
874 }
875
876 if (!*line || !*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100877 si->applet.ctx.cli.msg = "Require 'backend/server' and 'weight' or 'weight%'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100878 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200879 return 1;
880 }
881
882 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100883 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100884 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200885 return 1;
886 }
887
Cyril Bonté613f0df2011-03-03 20:49:04 +0100888 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100889 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100890 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100891 return 1;
892 }
893
Willy Tarreau4483d432009-10-10 19:30:08 +0200894 /* if the weight is terminated with '%', it is set relative to
895 * the initial weight, otherwise it is absolute.
896 */
897 w = atoi(args[3]);
898 if (strchr(args[3], '%') != NULL) {
899 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100900 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100901 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200902 return 1;
903 }
904 w = sv->iweight * w / 100;
905 }
906 else {
907 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100908 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100909 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200910 return 1;
911 }
912 }
913
914 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100915 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 +0100916 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200917 return 1;
918 }
919
920 sv->uweight = w;
921
922 if (px->lbprm.algo & BE_LB_PROP_DYN) {
923 /* we must take care of not pushing the server to full throttle during slow starts */
924 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
925 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
926 else
927 sv->eweight = BE_WEIGHT_SCALE;
928 sv->eweight *= sv->uweight;
929 } else {
930 sv->eweight = sv->uweight;
931 }
932
933 /* static LB algorithms are a bit harder to update */
934 if (px->lbprm.update_server_eweight)
935 px->lbprm.update_server_eweight(sv);
936 else if (sv->eweight)
937 px->lbprm.set_server_status_up(sv);
938 else
939 px->lbprm.set_server_status_down(sv);
940
941 return 1;
942 }
Willy Tarreau7aabd112010-01-26 10:59:06 +0100943 else if (strcmp(args[1], "timeout") == 0) {
944 if (strcmp(args[2], "cli") == 0) {
945 unsigned timeout;
946 const char *res;
947
948 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100949 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100950 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100951 return 1;
952 }
953
954 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
955 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100956 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100957 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100958 return 1;
959 }
960
961 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
962 return 1;
963 }
964 else {
Willy Tarreau295a8372011-03-10 11:25:07 +0100965 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100966 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100967 return 1;
968 }
969 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200970 else if (strcmp(args[1], "maxconn") == 0) {
971 if (strcmp(args[2], "frontend") == 0) {
972 struct proxy *px;
973 struct listener *l;
974 int v;
975
976 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
977 si->applet.ctx.cli.msg = stats_permission_denied_msg;
978 si->applet.st0 = STAT_CLI_PRINT;
979 return 1;
980 }
981
982 if (!*args[3]) {
983 si->applet.ctx.cli.msg = "Frontend name expected.\n";
984 si->applet.st0 = STAT_CLI_PRINT;
985 return 1;
986 }
987
988 px = findproxy(args[3], PR_CAP_FE);
989 if (!px) {
990 si->applet.ctx.cli.msg = "No such frontend.\n";
991 si->applet.st0 = STAT_CLI_PRINT;
992 return 1;
993 }
994
995 if (!*args[4]) {
996 si->applet.ctx.cli.msg = "Integer value expected.\n";
997 si->applet.st0 = STAT_CLI_PRINT;
998 return 1;
999 }
1000
1001 v = atoi(args[4]);
1002 /* check for unlimited values, we restore default setting (cfg_maxpconn) */
1003 if (v < 1) {
1004 si->applet.ctx.cli.msg = "Value out of range.\n";
1005 si->applet.st0 = STAT_CLI_PRINT;
1006 return 1;
1007 }
1008
1009 /* OK, the value is fine, so we assign it to the proxy and to all of
1010 * its listeners. The blocked ones will be dequeued.
1011 */
1012 px->maxconn = v;
1013 for (l = px->listen; l != NULL; l = l->next) {
1014 l->maxconn = v;
1015 if (l->state == LI_FULL)
1016 resume_listener(l);
1017 }
1018
1019 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1020 dequeue_all_listeners(&s->fe->listener_queue);
1021
1022 return 1;
1023 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001024 else if (strcmp(args[2], "global") == 0) {
1025 int v;
1026
1027 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1028 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1029 si->applet.st0 = STAT_CLI_PRINT;
1030 return 1;
1031 }
1032
1033 if (!*args[3]) {
1034 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1035 si->applet.st0 = STAT_CLI_PRINT;
1036 return 1;
1037 }
1038
1039 v = atoi(args[3]);
1040 if (v > global.hardmaxconn) {
1041 si->applet.ctx.cli.msg = "Value out of range.\n";
1042 si->applet.st0 = STAT_CLI_PRINT;
1043 return 1;
1044 }
1045
1046 /* check for unlimited values */
1047 if (v <= 0)
1048 v = global.hardmaxconn;
1049
1050 global.maxconn = v;
1051
1052 /* Dequeues all of the listeners waiting for a resource */
1053 if (!LIST_ISEMPTY(&global_listener_queue))
1054 dequeue_all_listeners(&global_listener_queue);
1055
1056 return 1;
1057 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001058 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001059 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001060 si->applet.st0 = STAT_CLI_PRINT;
1061 return 1;
1062 }
1063 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001064 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001065 return 0;
1066 }
1067 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001068 else if (strcmp(args[0], "enable") == 0) {
1069 if (strcmp(args[1], "server") == 0) {
1070 struct proxy *px;
1071 struct server *sv;
1072
1073 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001074 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001075 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001076 return 1;
1077 }
1078
1079 /* split "backend/server" and make <line> point to server */
1080 for (line = args[2]; *line; line++)
1081 if (*line == '/') {
1082 *line++ = '\0';
1083 break;
1084 }
1085
1086 if (!*line || !*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001087 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001088 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001089 return 1;
1090 }
1091
1092 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001093 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001094 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001095 return 1;
1096 }
1097
Cyril Bonté613f0df2011-03-03 20:49:04 +01001098 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001099 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001100 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001101 return 1;
1102 }
1103
Cyril Bontécd19e512010-01-31 22:34:03 +01001104 if (sv->state & SRV_MAINTAIN) {
1105 /* The server is really in maintenance, we can change the server state */
1106 if (sv->tracked) {
1107 /* If this server tracks the status of another one,
1108 * we must restore the good status.
1109 */
1110 if (sv->tracked->state & SRV_RUNNING) {
1111 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001112 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001113 } else {
1114 sv->state &= ~SRV_MAINTAIN;
1115 set_server_down(sv);
1116 }
1117 } else {
1118 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001119 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001120 }
1121 }
1122
1123 return 1;
1124 }
1125 else { /* unknown "enable" parameter */
1126 return 0;
1127 }
1128 }
1129 else if (strcmp(args[0], "disable") == 0) {
1130 if (strcmp(args[1], "server") == 0) {
1131 struct proxy *px;
1132 struct server *sv;
1133
1134 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001135 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001136 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001137 return 1;
1138 }
1139
1140 /* split "backend/server" and make <line> point to server */
1141 for (line = args[2]; *line; line++)
1142 if (*line == '/') {
1143 *line++ = '\0';
1144 break;
1145 }
1146
1147 if (!*line || !*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001148 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001149 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001150 return 1;
1151 }
1152
1153 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001154 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001155 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001156 return 1;
1157 }
1158
Cyril Bonté613f0df2011-03-03 20:49:04 +01001159 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001160 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001161 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001162 return 1;
1163 }
1164
Cyril Bontécd19e512010-01-31 22:34:03 +01001165 if (! (sv->state & SRV_MAINTAIN)) {
1166 /* Not already in maintenance, we can change the server state */
1167 sv->state |= SRV_MAINTAIN;
1168 set_server_down(sv);
1169 }
1170
1171 return 1;
1172 }
1173 else { /* unknown "disable" parameter */
1174 return 0;
1175 }
1176 }
1177 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001178 return 0;
1179 }
1180 return 1;
1181}
1182
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001183/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001184 * used to processes I/O from/to the stats unix socket. The system relies on a
1185 * state machine handling requests and various responses. We read a request,
1186 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001187 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1188 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001189 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001190 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001191static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001192{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001193 struct buffer *req = si->ob;
1194 struct buffer *res = si->ib;
1195 int reql;
1196 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001197
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001198 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1199 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001200
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001201 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001202 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001203 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001204 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001205 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001206 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001207 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001208 /* Let's close for real now. We just close the request
1209 * side, the conditions below will complete if needed.
1210 */
1211 si->shutw(si);
1212 break;
1213 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001214 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001215 /* ensure we have some output room left in the event we
1216 * would want to return some info right after parsing.
1217 */
1218 if (buffer_almost_full(si->ib))
1219 break;
1220
Willy Tarreau74b08c92010-09-08 17:04:31 +02001221 reql = buffer_get_line(si->ob, trash, sizeof(trash));
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001222 if (reql <= 0) { /* closed or EOL not found */
1223 if (reql == 0)
1224 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001225 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001226 continue;
1227 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001228
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001229 /* seek for a possible semi-colon. If we find one, we
1230 * replace it with an LF and skip only this part.
1231 */
1232 for (len = 0; len < reql; len++)
1233 if (trash[len] == ';') {
1234 trash[len] = '\n';
1235 reql = len + 1;
1236 break;
1237 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001238
Willy Tarreau816fc222009-10-04 07:36:58 +02001239 /* now it is time to check that we have a full line,
1240 * remove the trailing \n and possibly \r, then cut the
1241 * line.
1242 */
1243 len = reql - 1;
1244 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001245 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001246 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001247 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001248
Willy Tarreau816fc222009-10-04 07:36:58 +02001249 if (len && trash[len-1] == '\r')
1250 len--;
1251
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001252 trash[len] = '\0';
1253
Willy Tarreaubc4af052011-02-13 13:25:14 +01001254 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001255 if (len) {
1256 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001257 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001258 continue;
1259 }
1260 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001261 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001262 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001263 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001264 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001265 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001266 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001267 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001268 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001269 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001270 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001271 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001272 /* if prompt is disabled, print help on empty lines,
1273 * so that the user at least knows how to enable
1274 * prompt and find help.
1275 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001276 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001277 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001278 }
1279
1280 /* re-adjust req buffer */
1281 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001282 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001283 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001284 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001285 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001286 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001287 continue;
1288 }
1289
Willy Tarreaubc4af052011-02-13 13:25:14 +01001290 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001291 case STAT_CLI_PRINT:
Willy Tarreau295a8372011-03-10 11:25:07 +01001292 if (buffer_feed(si->ib, si->applet.ctx.cli.msg) < 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001293 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001294 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001295 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001296 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001297 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001298 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001299 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001300 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001301 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001302 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001303 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001304 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001305 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001306 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001307 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001308 if (stats_table_request(si, true))
1309 si->applet.st0 = STAT_CLI_PROMPT;
1310 break;
1311 case STAT_CLI_O_CLR:
1312 if (stats_table_request(si, false))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001313 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001314 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001315 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001316 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001317 break;
1318 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001319
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001320 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001321 if (si->applet.st0 == STAT_CLI_PROMPT) {
1322 if (buffer_feed(si->ib, si->applet.st1 ? "\n> " : "\n") < 0)
1323 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001324 }
1325
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001326 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001327 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001328 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001329
1330 /* Now we close the output if one of the writers did so,
1331 * or if we're not in interactive mode and the request
1332 * buffer is empty. This still allows pipelined requests
1333 * to be sent in non-interactive mode.
1334 */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001335 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->applet.st1 && !req->send_max)) {
1336 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001337 continue;
1338 }
1339
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001340 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001341 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001342 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001343 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001344
Willy Tarreaubc4af052011-02-13 13:25:14 +01001345 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001346 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1347 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1348 /* Other size has closed, let's abort if we have no more processing to do
1349 * and nothing more to consume. This is comparable to a broken pipe, so
1350 * we forward the close to the request side so that it flows upstream to
1351 * the client.
1352 */
1353 si->shutw(si);
1354 }
1355
Willy Tarreaubc4af052011-02-13 13:25:14 +01001356 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001357 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1358 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1359 /* We have no more processing to do, and nothing more to send, and
1360 * the client side has closed. So we'll forward this state downstream
1361 * on the response buffer.
1362 */
1363 si->shutr(si);
1364 res->flags |= BF_READ_NULL;
1365 }
1366
1367 /* update all other flags and resync with the other side */
1368 si->update(si);
1369
1370 /* we don't want to expire timeouts while we're processing requests */
1371 si->ib->rex = TICK_ETERNITY;
1372 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001373
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001374 out:
1375 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
1376 __FUNCTION__, __LINE__,
1377 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
1378
1379 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1380 /* check that we have released everything then unregister */
1381 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001382 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001383}
1384
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001385/* This function dumps statistics onto the stream interface's read buffer.
1386 * The data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001387 * It returns 0 as long as it does not complete, non-zero upon completion.
1388 * Some states are not used but it makes the code more similar to other
1389 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001390 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001391static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001392{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001393 struct proxy *px;
1394 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001395 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001396
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001397 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +02001398
Willy Tarreau295a8372011-03-10 11:25:07 +01001399 switch (si->applet.state) {
1400 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001401 /* the function had not been called yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001402 si->applet.state = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001403 /* fall through */
1404
Willy Tarreau295a8372011-03-10 11:25:07 +01001405 case STAT_ST_HEAD:
1406 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001407 print_csv_header(&msg);
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001408 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001409 return 0;
1410 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001411
Willy Tarreau295a8372011-03-10 11:25:07 +01001412 si->applet.state = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001413 /* fall through */
1414
Willy Tarreau295a8372011-03-10 11:25:07 +01001415 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001416 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001417 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001418 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001419 "Name: " PRODUCT_NAME "\n"
1420 "Version: " HAPROXY_VERSION "\n"
1421 "Release_date: " HAPROXY_DATE "\n"
1422 "Nbproc: %d\n"
1423 "Process_num: %d\n"
1424 "Pid: %d\n"
1425 "Uptime: %dd %dh%02dm%02ds\n"
1426 "Uptime_sec: %d\n"
1427 "Memmax_MB: %d\n"
1428 "Ulimit-n: %d\n"
1429 "Maxsock: %d\n"
1430 "Maxconn: %d\n"
Willy Tarreau91886b62011-09-07 14:38:31 +02001431 "Hard_maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001432 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001433 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001434 "PipesUsed: %d\n"
1435 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001436 "Tasks: %d\n"
1437 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001438 "node: %s\n"
1439 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001440 "",
1441 global.nbproc,
1442 relative_pid,
1443 pid,
1444 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1445 up,
1446 global.rlimit_memmax,
1447 global.rlimit_nofile,
Willy Tarreau91886b62011-09-07 14:38:31 +02001448 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001449 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001450 nb_tasks_cur, run_queue_cur,
1451 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001452 );
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001453 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001454 return 0;
1455 }
1456
Willy Tarreau295a8372011-03-10 11:25:07 +01001457 si->applet.ctx.stats.px = proxy;
1458 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1459 si->applet.ctx.stats.sv = NULL;
1460 si->applet.state = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001461 /* fall through */
1462
Willy Tarreau295a8372011-03-10 11:25:07 +01001463 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001464 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001465 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1466 while (si->applet.ctx.stats.px) {
1467 px = si->applet.ctx.stats.px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001468 /* skip the disabled proxies and non-networked ones */
1469 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001470 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001471 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001472 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001473 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001474
Willy Tarreau295a8372011-03-10 11:25:07 +01001475 si->applet.ctx.stats.px = px->next;
1476 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001477 }
1478 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001479 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001480
Willy Tarreau295a8372011-03-10 11:25:07 +01001481 si->applet.state = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001482 /* fall through */
1483
Willy Tarreau295a8372011-03-10 11:25:07 +01001484 case STAT_ST_END:
1485 si->applet.state = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001486 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001487
Willy Tarreau295a8372011-03-10 11:25:07 +01001488 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001489 return 1;
1490
1491 default:
1492 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001493 si->applet.state = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001494 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001495 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001496}
1497
1498
Cyril Bonté70be45d2010-10-12 00:14:35 +02001499/* We don't want to land on the posted stats page because a refresh will
1500 * repost the data. We don't want this to happen on accident so we redirect
1501 * the browse to the stats page with a GET.
1502 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001503static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001504{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001505 struct session *s = si->applet.private;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001506 struct chunk msg;
1507
1508 chunk_init(&msg, trash, sizeof(trash));
1509
Willy Tarreau295a8372011-03-10 11:25:07 +01001510 switch (si->applet.state) {
1511 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001512 chunk_printf(&msg,
1513 "HTTP/1.0 303 See Other\r\n"
1514 "Cache-Control: no-cache\r\n"
1515 "Content-Type: text/plain\r\n"
1516 "Connection: close\r\n"
1517 "Location: %s;st=%s",
Willy Tarreau295a8372011-03-10 11:25:07 +01001518 uri->uri_prefix, si->applet.ctx.stats.st_code);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001519 chunk_printf(&msg, "\r\n\r\n");
1520
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001521 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001522 return 0;
1523
1524 s->txn.status = 303;
1525
1526 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1527 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1528 if (!(s->flags & SN_FINST_MASK))
1529 s->flags |= SN_FINST_R;
1530
Willy Tarreau295a8372011-03-10 11:25:07 +01001531 si->applet.state = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001532 return 1;
1533 }
1534 return 1;
1535}
1536
1537
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001538/* This I/O handler runs as an applet embedded in a stream interface. It is
1539 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001540 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001541 * automatically unregisters itself once transfer is complete.
1542 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001543static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001544{
Willy Tarreaubc4af052011-02-13 13:25:14 +01001545 struct session *s = si->applet.private;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001546 struct buffer *req = si->ob;
1547 struct buffer *res = si->ib;
1548
1549 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1550 goto out;
1551
1552 /* check that the output is not closed */
1553 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001554 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001555
Willy Tarreaubc4af052011-02-13 13:25:14 +01001556 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001557 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001558 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001559 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001560 si->shutw(si);
1561 }
1562 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001563 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001564 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001565 si->shutw(si);
1566 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001567 }
1568 }
1569
1570 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
1571 si->shutw(si);
1572
Willy Tarreaubc4af052011-02-13 13:25:14 +01001573 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001574 si->shutr(si);
1575 res->flags |= BF_READ_NULL;
1576 }
1577
1578 /* update all other flags and resync with the other side */
1579 si->update(si);
1580
1581 /* we don't want to expire timeouts while we're processing requests */
1582 si->ib->rex = TICK_ETERNITY;
1583 si->ob->wex = TICK_ETERNITY;
1584
1585 out:
1586 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1587 /* check that we have released everything then unregister */
1588 stream_int_unregister_handler(si);
1589 }
1590}
1591
1592
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001593/* This function dumps statistics in HTTP format onto the stream interface's
1594 * read buffer. The data_ctx must have been zeroed first, and the flags
1595 * properly set. It returns 0 if it had to stop writing data and an I/O is
1596 * needed, 1 if the dump is finished and the session must be closed, or -1
1597 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001598 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001599static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001600{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001601 struct session *s = si->applet.private;
1602 struct buffer *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001603 struct proxy *px;
1604 struct chunk msg;
1605 unsigned int up;
1606
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001607 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001608
Willy Tarreau295a8372011-03-10 11:25:07 +01001609 switch (si->applet.state) {
1610 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001611 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001612 "HTTP/1.0 200 OK\r\n"
1613 "Cache-Control: no-cache\r\n"
1614 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001615 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001616 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001617
Willy Tarreau295a8372011-03-10 11:25:07 +01001618 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001619 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001620 uri->refresh);
1621
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001622 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001623
1624 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001625 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001626 return 0;
1627
Willy Tarreau91861262007-10-17 17:06:05 +02001628 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1629 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1630 if (!(s->flags & SN_FINST_MASK))
1631 s->flags |= SN_FINST_R;
1632
1633 if (s->txn.meth == HTTP_METH_HEAD) {
1634 /* that's all we return in case of HEAD request */
Willy Tarreau295a8372011-03-10 11:25:07 +01001635 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001636 return 1;
1637 }
1638
Willy Tarreau295a8372011-03-10 11:25:07 +01001639 si->applet.state = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001640 /* fall through */
1641
Willy Tarreau295a8372011-03-10 11:25:07 +01001642 case STAT_ST_HEAD:
1643 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001644 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001645 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001646 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1647 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001648 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001649 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1650 "<style type=\"text/css\"><!--\n"
1651 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001652 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001653 " font-size: 12px;"
1654 " font-weight: normal;"
1655 " color: black;"
1656 " background: white;"
1657 "}\n"
1658 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001659 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001660 "}\n"
1661 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001662 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001663 " margin-bottom: 0.5em;"
1664 "}\n"
1665 "h2 {"
1666 " font-family: helvetica, arial;"
1667 " font-size: x-large;"
1668 " font-weight: bold;"
1669 " font-style: italic;"
1670 " color: #6020a0;"
1671 " margin-top: 0em;"
1672 " margin-bottom: 0em;"
1673 "}\n"
1674 "h3 {"
1675 " font-family: helvetica, arial;"
1676 " font-size: 16px;"
1677 " font-weight: bold;"
1678 " color: #b00040;"
1679 " background: #e8e8d0;"
1680 " margin-top: 0em;"
1681 " margin-bottom: 0em;"
1682 "}\n"
1683 "li {"
1684 " margin-top: 0.25em;"
1685 " margin-right: 2em;"
1686 "}\n"
1687 ".hr {margin-top: 0.25em;"
1688 " border-color: black;"
1689 " border-bottom-style: solid;"
1690 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001691 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001692 ".total {background: #20D0D0;color: #ffff80;}\n"
1693 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001694 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001695 ".backend {background: #e8e8d0;}\n"
1696 ".active0 {background: #ff9090;}\n"
1697 ".active1 {background: #ffd020;}\n"
1698 ".active2 {background: #ffffa0;}\n"
1699 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001700 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1701 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1702 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001703 ".backup0 {background: #ff9090;}\n"
1704 ".backup1 {background: #ff80ff;}\n"
1705 ".backup2 {background: #c060ff;}\n"
1706 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001707 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1708 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1709 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001710 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001711 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001712 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001713 "a.px:link {color: #ffff40; text-decoration: none;}"
1714 "a.px:visited {color: #ffff40; text-decoration: none;}"
1715 "a.px:hover {color: #ffffff; text-decoration: none;}"
1716 "a.lfsb:link {color: #000000; text-decoration: none;}"
1717 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1718 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1719 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001720 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001721 "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"
1722 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001723 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001724 "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 +02001725 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001726 "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 +02001727 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001728 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1729 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1730 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001731 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001732 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001733 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001734 (uri->flags&ST_SHNODE) ? " on " : "",
1735 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001736 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001737 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001738 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001739 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001740 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001741 return 0;
1742
Willy Tarreau295a8372011-03-10 11:25:07 +01001743 si->applet.state = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02001744 /* fall through */
1745
Willy Tarreau295a8372011-03-10 11:25:07 +01001746 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02001747 up = (now.tv_sec - start_date.tv_sec);
1748
1749 /* WARNING! this has to fit the first packet too.
1750 * We are around 3.5 kB, add adding entries will
1751 * become tricky if we want to support 4kB buffers !
1752 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001753 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001754 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001755 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1756 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001757 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001758 "<hr width=\"100%%\" class=\"hr\">\n"
1759 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001760 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001761 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001762 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001763 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1764 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
1765 "current conns = %d; current pipes = %d/%d<br>\n"
1766 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001767 "</td><td align=\"center\" nowrap>\n"
1768 "<table class=\"lgd\"><tr>\n"
1769 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1770 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1771 "</tr><tr>\n"
1772 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1773 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1774 "</tr><tr>\n"
1775 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1776 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1777 "</tr><tr>\n"
1778 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001779 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001780 "</tr><tr>\n"
1781 "<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 +02001782 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001783 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001784 "</td>"
1785 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1786 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1787 "",
1788 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001789 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1790 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1791 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001792 up / 86400, (up % 86400) / 3600,
1793 (up % 3600) / 60, (up % 60),
1794 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1795 global.rlimit_memmax ? " MB" : "",
1796 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001797 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001798 actconn, pipes_used, pipes_used+pipes_free,
1799 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +02001800 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001801
Willy Tarreau295a8372011-03-10 11:25:07 +01001802 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001803 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001804 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1805 uri->uri_prefix,
1806 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01001807 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001808 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001809 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001810 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1811 uri->uri_prefix,
1812 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01001813 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001814
Willy Tarreau55bb8452007-10-17 18:44:57 +02001815 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001816 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001817 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001818 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1819 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001820 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001821 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001822 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001823 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001824 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1825 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001826 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001827 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001828 }
Willy Tarreau91861262007-10-17 17:06:05 +02001829
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001830 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001831 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
1832 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001833 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
1834 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001835
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001836 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02001837 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
1838 uri->uri_prefix,
1839 (uri->refresh > 0) ? ";norefresh" : "");
1840
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001841 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001842 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001843 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1844 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
1845 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
1846 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
1847 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
1848 "</ul>"
1849 "</td>"
1850 "</tr></table>\n"
1851 ""
1852 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001853
Willy Tarreau295a8372011-03-10 11:25:07 +01001854 if (si->applet.ctx.stats.st_code) {
1855 if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001856 chunk_printf(&msg,
1857 "<p><div class=active3>"
1858 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1859 "Action processed successfully."
1860 "</div>\n", uri->uri_prefix);
1861 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001862 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_NONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001863 chunk_printf(&msg,
1864 "<p><div class=active2>"
1865 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1866 "Nothing has changed."
1867 "</div>\n", uri->uri_prefix);
1868 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001869 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_EXCD) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001870 chunk_printf(&msg,
1871 "<p><div class=active0>"
1872 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1873 "<b>Action not processed : the buffer couldn't store all the data.<br>"
1874 "You should retry with less servers at a time.</b>"
1875 "</div>\n", uri->uri_prefix);
1876 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001877 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DENY) == 0) {
Cyril Bonté474be412010-10-12 00:14:36 +02001878 chunk_printf(&msg,
1879 "<p><div class=active0>"
1880 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1881 "<b>Action denied.</b>"
1882 "</div>\n", uri->uri_prefix);
1883 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001884 else {
1885 chunk_printf(&msg,
1886 "<p><div class=active6>"
1887 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1888 "Unexpected result."
1889 "</div>\n", uri->uri_prefix);
1890 }
1891 chunk_printf(&msg,"<p>\n");
1892 }
1893
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001894 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001895 return 0;
1896 }
Willy Tarreau91861262007-10-17 17:06:05 +02001897
Willy Tarreau295a8372011-03-10 11:25:07 +01001898 si->applet.ctx.stats.px = proxy;
1899 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1900 si->applet.state = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02001901 /* fall through */
1902
Willy Tarreau295a8372011-03-10 11:25:07 +01001903 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02001904 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001905 while (si->applet.ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001906 if (buffer_almost_full(rep))
1907 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01001908 px = si->applet.ctx.stats.px;
Willy Tarreau91861262007-10-17 17:06:05 +02001909 /* skip the disabled proxies and non-networked ones */
1910 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001911 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001912 return 0;
1913
Willy Tarreau295a8372011-03-10 11:25:07 +01001914 si->applet.ctx.stats.px = px->next;
1915 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02001916 }
1917 /* here, we just have reached the last proxy */
1918
Willy Tarreau295a8372011-03-10 11:25:07 +01001919 si->applet.state = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02001920 /* fall through */
1921
Willy Tarreau295a8372011-03-10 11:25:07 +01001922 case STAT_ST_END:
1923 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001924 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001925 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001926 return 0;
1927 }
Willy Tarreau91861262007-10-17 17:06:05 +02001928
Willy Tarreau295a8372011-03-10 11:25:07 +01001929 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001930 /* fall through */
1931
Willy Tarreau295a8372011-03-10 11:25:07 +01001932 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02001933 return 1;
1934
1935 default:
1936 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001937 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001938 return -1;
1939 }
1940}
1941
1942
1943/*
1944 * Dumps statistics for a proxy.
1945 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1946 * ot non-zero if everything completed.
1947 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001948static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001949{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001950 struct session *s = si->applet.private;
1951 struct buffer *rep = si->ib;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001952 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001953 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02001954 struct chunk msg;
1955
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001956 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001957
Willy Tarreau295a8372011-03-10 11:25:07 +01001958 switch (si->applet.ctx.stats.px_st) {
1959 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02001960 /* we are on a new proxy */
1961
1962 if (uri && uri->scope) {
1963 /* we have a limited scope, we have to check the proxy name */
1964 struct stat_scope *scope;
1965 int len;
1966
1967 len = strlen(px->id);
1968 scope = uri->scope;
1969
1970 while (scope) {
1971 /* match exact proxy name */
1972 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1973 break;
1974
1975 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001976 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001977 break;
1978 scope = scope->next;
1979 }
1980
1981 /* proxy name not found : don't dump anything */
1982 if (scope == NULL)
1983 return 1;
1984 }
1985
Willy Tarreau295a8372011-03-10 11:25:07 +01001986 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
1987 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001988 return 1;
1989
Willy Tarreau295a8372011-03-10 11:25:07 +01001990 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02001991 /* fall through */
1992
Willy Tarreau295a8372011-03-10 11:25:07 +01001993 case STAT_PX_ST_TH:
1994 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
1995 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001996 /* A form to enable/disable this proxy servers */
1997 chunk_printf(&msg,
1998 "<form action=\"%s\" method=\"post\">",
1999 uri->uri_prefix);
2000 }
2001
Willy Tarreau55bb8452007-10-17 18:44:57 +02002002 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002003 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002004 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002005 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002006 "<th class=\"pxname\" width=\"10%%\"");
2007
2008 if (uri->flags&ST_SHLGNDS) {
2009 /* cap, mode, id */
2010 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
2011 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2012 px->uuid);
2013
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002014 chunk_printf(&msg, "\"");
2015 }
2016
2017 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002018 ">%s<a name=\"%s\"></a>"
2019 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002020 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002021 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002022 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002023 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002024 "<tr class=\"titre\">",
2025 (uri->flags & ST_SHLGNDS)?"<u>":"",
2026 px->id, px->id, px->id,
2027 (uri->flags & ST_SHLGNDS)?"</u>":"",
2028 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2029
Willy Tarreau295a8372011-03-10 11:25:07 +01002030 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002031 /* Column heading for Enable or Disable server */
2032 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
2033 }
2034
2035 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002036 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002037 "<th colspan=3>Queue</th>"
2038 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002039 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002040 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002041 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002042 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002043 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002044 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002045 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002046 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002047 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002048 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002049 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002050 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2051 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002052 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002053
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002054 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002055 return 0;
2056 }
Willy Tarreau91861262007-10-17 17:06:05 +02002057
Willy Tarreau295a8372011-03-10 11:25:07 +01002058 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002059 /* fall through */
2060
Willy Tarreau295a8372011-03-10 11:25:07 +01002061 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002062 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002063 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002064 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
2065 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002066 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002067 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002068 "<tr class=\"frontend\">");
2069
Willy Tarreau295a8372011-03-10 11:25:07 +01002070 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002071 /* Column sub-heading for Enable or Disable server */
2072 chunk_printf(&msg, "<td></td>");
2073 }
2074
2075 chunk_printf(&msg,
2076 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002077 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002078 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2079 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002080 "",
2081 px->id, px->id);
2082
2083 if (px->mode == PR_MODE_HTTP) {
2084 chunk_printf(&msg,
2085 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01002086 "<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 +01002087 "",
2088 read_freq_ctr(&px->fe_req_per_sec),
2089 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002090 px->fe_counters.p.http.rps_max,
2091 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01002092 LIM2A2(px->fe_sps_lim, "-"));
2093 } else {
2094 chunk_printf(&msg,
2095 /* sessions rate : current, max, limit */
2096 "<td>%s</td><td>%s</td><td>%s</td>"
2097 "",
2098 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002099 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01002100 }
2101
2102 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002103 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002104 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002105 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002106 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002107 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002108
2109 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2110 if (px->mode == PR_MODE_HTTP) {
2111 int i;
2112
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002113 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002114
2115 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002116 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002117
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002118 chunk_printf(&msg, " other=%lld\"", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002119 }
2120
2121 chunk_printf(&msg,
2122 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002123 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002124 /* bytes : in, out */
2125 "<td>%s</td><td>%s</td>"
2126 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002127 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002128 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002129 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002130 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002131
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002132 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002133 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002134 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002135 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002136 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002137 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002138 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002139 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002140 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002141 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002142 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002143 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002144 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2145 U2H2(px->fe_counters.failed_req),
Willy Tarreau562515c2011-07-25 08:11:52 +02002146 px->state == PR_STREADY ? "OPEN" :
2147 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002148 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002149 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002150 /* pxid, name, queue cur, queue max, */
2151 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002152 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002153 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002154 /* bytes : in, out */
2155 "%lld,%lld,"
2156 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002157 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002158 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002159 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002160 /* warnings: retries, redispatches */
2161 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002162 /* server status : reflect frontend status */
2163 "%s,"
2164 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002165 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002166 /* pid, iid, sid, throttle, lbtot, tracked, type */
2167 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002168 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002169 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002170 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002171 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002172 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002173 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2174 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2175 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2176 px->fe_counters.failed_req,
Willy Tarreau562515c2011-07-25 08:11:52 +02002177 px->state == PR_STREADY ? "OPEN" :
2178 px->state == PR_STFULL ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002179 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002180 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002181 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002182
2183 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2184 if (px->mode == PR_MODE_HTTP) {
2185 int i;
2186
2187 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002188 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002189
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002190 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002191 } else {
2192 chunk_printf(&msg, ",,,,,,");
2193 }
2194
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002195 /* failed health analyses */
2196 chunk_printf(&msg, ",");
2197
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002198 /* requests : req_rate, req_rate_max, req_tot, */
2199 chunk_printf(&msg, "%u,%u,%lld,",
2200 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002201 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002202
Willy Tarreauae526782010-03-04 20:34:23 +01002203 /* errors: cli_aborts, srv_aborts */
2204 chunk_printf(&msg, ",,");
2205
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002206 /* finish with EOL */
2207 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002208 }
Willy Tarreau91861262007-10-17 17:06:05 +02002209
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002210 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002211 return 0;
2212 }
2213
Willy Tarreau295a8372011-03-10 11:25:07 +01002214 si->applet.ctx.stats.l = px->listen; /* may be NULL */
2215 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002216 /* fall through */
2217
Willy Tarreau295a8372011-03-10 11:25:07 +01002218 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002219 /* stats.l has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002220 for (; si->applet.ctx.stats.l != NULL; si->applet.ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002221 if (buffer_almost_full(rep))
2222 return 0;
2223
Willy Tarreau295a8372011-03-10 11:25:07 +01002224 l = si->applet.ctx.stats.l;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002225 if (!l->counters)
2226 continue;
2227
Willy Tarreau295a8372011-03-10 11:25:07 +01002228 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2229 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002230 break;
2231
Willy Tarreau295a8372011-03-10 11:25:07 +01002232 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002233 continue;
2234 }
2235
Willy Tarreau295a8372011-03-10 11:25:07 +01002236 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002237 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002238 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002239 /* Column sub-heading for Enable or Disable server */
2240 chunk_printf(&msg, "<td></td>");
2241 }
2242 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002243
2244 if (uri->flags&ST_SHLGNDS) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02002245 char str[INET6_ADDRSTRLEN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002246 int port;
2247
Willy Tarreau631f01c2011-09-05 00:36:48 +02002248 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002249
Willy Tarreau631f01c2011-09-05 00:36:48 +02002250 port = get_host_port(&l->addr);
2251 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2252 case AF_INET:
2253 chunk_printf(&msg, "IPv4: %s:%d, ", str, port);
2254 break;
2255 case AF_INET6:
2256 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, port);
2257 break;
2258 case AF_UNIX:
2259 chunk_printf(&msg, "unix, ");
2260 break;
2261 case -1:
2262 chunk_printf(&msg, "(%s), ", strerror(errno));
2263 break;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002264 }
2265
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002266 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002267 chunk_printf(&msg, "id: %d\"", l->luid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002268 }
2269
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002270 chunk_printf(&msg,
2271 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002272 ">%s<a name=\"%s/+%s\"></a>"
2273 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002274 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002275 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002276 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002277 "<td>%s</td><td>%s</td><td>%s</td>"
2278 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002279 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002280 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002281 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002282 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002283 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002284 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002285 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2286 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2287
2288 chunk_printf(&msg,
2289 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002290 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002291 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002292 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002293 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002294 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002295 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002296 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002297 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002298 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002299 "",
2300 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2301 U2H2(l->counters->failed_req),
Willy Tarreaua17c2d92011-07-25 08:16:20 +02002302 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002303 } else {
2304 chunk_printf(&msg,
2305 /* pxid, name, queue cur, queue max, */
2306 "%s,%s,,,"
2307 /* sessions: current, max, limit, total */
2308 "%d,%d,%d,%lld,"
2309 /* bytes: in, out */
2310 "%lld,%lld,"
2311 /* denied: req, resp */
2312 "%lld,%lld,"
2313 /* errors: request, connect, response */
2314 "%lld,,,"
2315 /* warnings: retries, redispatches */
2316 ",,"
2317 /* server status: reflect listener status */
2318 "%s,"
2319 /* rest of server: nothing */
2320 ",,,,,,,,"
2321 /* pid, iid, sid, throttle, lbtot, tracked, type */
2322 "%d,%d,%d,,,,%d,"
2323 /* rate, rate_lim, rate_max */
2324 ",,,"
2325 /* check_status, check_code, check_duration */
2326 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002327 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2328 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002329 /* failed health analyses */
2330 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002331 /* requests : req_rate, req_rate_max, req_tot, */
2332 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002333 /* errors: cli_aborts, srv_aborts */
2334 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002335 "\n",
2336 px->id, l->name,
2337 l->nbconn, l->counters->conn_max,
2338 l->maxconn, l->counters->cum_conn,
2339 l->counters->bytes_in, l->counters->bytes_out,
2340 l->counters->denied_req, l->counters->denied_resp,
2341 l->counters->failed_req,
2342 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2343 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2344 }
2345
2346 if (buffer_feed_chunk(rep, &msg) >= 0)
2347 return 0;
2348 }
2349
Willy Tarreau295a8372011-03-10 11:25:07 +01002350 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2351 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002352 /* fall through */
2353
Willy Tarreau295a8372011-03-10 11:25:07 +01002354 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002355 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002356 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002357 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 +02002358
Willy Tarreau4e33d862009-10-11 23:35:10 +02002359 if (buffer_almost_full(rep))
2360 return 0;
2361
Willy Tarreau295a8372011-03-10 11:25:07 +01002362 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002363
Willy Tarreau295a8372011-03-10 11:25:07 +01002364 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2365 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002366 break;
2367
Willy Tarreau295a8372011-03-10 11:25:07 +01002368 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002369 continue;
2370 }
2371
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002372 if (sv->tracked)
2373 svs = sv->tracked;
2374 else
2375 svs = sv;
2376
Willy Tarreau91861262007-10-17 17:06:05 +02002377 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002378 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002379 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002380 else if (svs->state & SRV_RUNNING) {
2381 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002382 sv_state = 3; /* UP */
2383 else
2384 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002385
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002386 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002387 sv_state += 2;
2388 }
Willy Tarreau91861262007-10-17 17:06:05 +02002389 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002390 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002391 sv_state = 1; /* going up */
2392 else
2393 sv_state = 0; /* DOWN */
2394
Willy Tarreau295a8372011-03-10 11:25:07 +01002395 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002396 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002397 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002398 continue;
2399 }
2400
Willy Tarreau295a8372011-03-10 11:25:07 +01002401 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002402 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2403 "UP %d/%d &darr;", "UP",
2404 "NOLB %d/%d &darr;", "NOLB",
2405 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002406 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002407 chunk_printf(&msg,
2408 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002409 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002410 );
2411 }
2412 else {
2413 chunk_printf(&msg,
2414 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002415 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002416 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2417 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002418
Willy Tarreau295a8372011-03-10 11:25:07 +01002419 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002420 chunk_printf(&msg,
2421 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2422 sv->id);
2423 }
2424
2425 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002426
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002427 if (uri->flags&ST_SHLGNDS) {
2428 char str[INET6_ADDRSTRLEN];
2429
Willy Tarreau631f01c2011-09-05 00:36:48 +02002430 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002431
Willy Tarreau631f01c2011-09-05 00:36:48 +02002432 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01002433 case AF_INET:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002434 chunk_printf(&msg, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
David du Colombier6f5ccb12011-03-10 22:26:24 +01002435 break;
2436 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002437 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2438 break;
2439 case AF_UNIX:
2440 chunk_printf(&msg, "unix, ");
2441 break;
2442 case -1:
2443 chunk_printf(&msg, "(%s), ", strerror(errno));
2444 break;
2445 default: /* address family not supported */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002446 break;
2447 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002448
2449 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002450 chunk_printf(&msg, "id: %d", sv->puid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002451
2452 /* cookie */
2453 if (sv->cookie) {
2454 struct chunk src;
2455
2456 chunk_printf(&msg, ", cookie: '");
2457
2458 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2459 chunk_htmlencode(&msg, &src);
2460
2461 chunk_printf(&msg, "'");
2462 }
2463
2464 chunk_printf(&msg, "\"");
2465 }
2466
2467 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002468 ">%s<a name=\"%s/%s\"></a>"
2469 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002470 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002471 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002472 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002473 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002474 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002475 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002476 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002477 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002478 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002479 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002480 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002481 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2482 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002483 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2484
2485 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2486 if (px->mode == PR_MODE_HTTP) {
2487 int i;
2488
2489 chunk_printf(&msg, " title=\"rsp codes:");
2490
2491 for (i = 1; i < 6; i++)
2492 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2493
2494 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2495 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002496
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002497 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002498 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002499 ">%s%s%s</td><td>%s</td>",
2500 (px->mode == PR_MODE_HTTP)?"<u>":"",
2501 U2H0(sv->counters.cum_sess),
2502 (px->mode == PR_MODE_HTTP)?"</u>":"",
2503 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002504
2505 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002506 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002507 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002508 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002509 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002510 /* errors : request, connect */
2511 "<td></td><td>%s</td>"
2512 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002513 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002514 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002515 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002516 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002517 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2518 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002519 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002520 sv->counters.cli_aborts,
2521 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002522 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002523 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002524
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002525 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002526 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002527
Cyril Bontécd19e512010-01-31 22:34:03 +01002528 if (sv->state & SRV_MAINTAIN) {
2529 chunk_printf(&msg, "%s ",
2530 human_time(now.tv_sec - sv->last_change, 1));
2531 chunk_printf(&msg, "MAINT");
2532 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002533 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2534 chunk_printf(&msg, "%s ",
2535 human_time(now.tv_sec - svs->last_change, 1));
2536 chunk_printf(&msg, "MAINT(via)");
2537 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002538 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002539 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002540 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002541
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002542 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002543 srv_hlt_st[sv_state],
2544 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2545 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002546 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002547
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002548 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002549 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002550 get_check_status_description(sv->check_status));
2551
2552 if (*sv->check_desc) {
2553 struct chunk src;
2554
2555 chunk_printf(&msg, ": ");
2556
2557 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2558 chunk_htmlencode(&msg, &src);
2559 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002560
Willy Tarreaue0454092010-02-26 12:29:07 +01002561 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002562 tv_iszero(&sv->check_start)?"":"* ",
2563 get_check_status_info(sv->check_status));
2564
2565 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002566 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002567
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002568 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002569 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002570 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002571 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002572
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002573 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002574 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002575 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002576 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002577 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002578 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002579 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002580 (sv->state & SRV_BACKUP) ? "-" : "Y",
2581 (sv->state & SRV_BACKUP) ? "Y" : "-");
2582
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002583 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002584 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002585 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002586 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2587
2588 if (svs->observe)
2589 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2590
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002591 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002592 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002593 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002594 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002595 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002596 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002597 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002598 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2599 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002600 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002601 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002602 "<td colspan=3></td>");
2603
2604 /* throttle */
2605 if ((sv->state & SRV_WARMINGUP) &&
2606 now.tv_sec < sv->last_change + sv->slowstart &&
2607 now.tv_sec >= sv->last_change) {
2608 unsigned int ratio;
2609 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002610 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002611 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002612 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002613 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002614 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002615 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002616 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002617 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2618 "UP %d/%d,", "UP,",
2619 "NOLB %d/%d,", "NOLB,",
2620 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002621 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002622 /* pxid, name */
2623 "%s,%s,"
2624 /* queue : current, max */
2625 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002626 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002627 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002628 /* bytes : in, out */
2629 "%lld,%lld,"
2630 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002631 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002632 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002633 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002634 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002635 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002636 "",
2637 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002638 sv->nbpend, sv->counters.nbpend_max,
2639 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002640 sv->counters.bytes_in, sv->counters.bytes_out,
2641 sv->counters.failed_secu,
2642 sv->counters.failed_conns, sv->counters.failed_resp,
2643 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002644
Willy Tarreau55bb8452007-10-17 18:44:57 +02002645 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002646 if (sv->state & SRV_MAINTAIN) {
2647 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002648 }
2649 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2650 chunk_printf(&msg, "MAINT(via),");
2651 }
2652 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002653 chunk_printf(&msg,
2654 srv_hlt_st[sv_state],
2655 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2656 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2657 }
Willy Tarreau91861262007-10-17 17:06:05 +02002658
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002659 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002660 /* weight, active, backup */
2661 "%d,%d,%d,"
2662 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002663 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002664 (sv->state & SRV_BACKUP) ? 0 : 1,
2665 (sv->state & SRV_BACKUP) ? 1 : 0);
2666
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002667 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002668 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002669 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002670 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002671 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002672 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002673 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002674 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002675 ",,,,");
2676
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002677 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002678 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002679 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002680 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002681 LIM2A0(sv->maxqueue, ""),
2682 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002683
2684 /* throttle */
2685 if ((sv->state & SRV_WARMINGUP) &&
2686 now.tv_sec < sv->last_change + sv->slowstart &&
2687 now.tv_sec >= sv->last_change) {
2688 unsigned int ratio;
2689 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002690 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002691 }
2692
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002693 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002694 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002695
2696 /* tracked */
2697 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002698 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002699 sv->tracked->proxy->id, sv->tracked->id);
2700 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002701 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002702
Willy Tarreau7f062c42009-03-05 18:43:00 +01002703 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002704 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002705
2706 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002707 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002708 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002709 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002710
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002711 if (sv->state & SRV_CHECKED) {
2712 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002713 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002714
2715 /* check_code */
2716 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002717 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002718 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002719 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002720
2721 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002722 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002723 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002724 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002725 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002726
2727 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002728 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002729 }
2730
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002731 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2732 if (px->mode == PR_MODE_HTTP) {
2733 int i;
2734
2735 for (i=1; i<6; i++)
2736 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2737
2738 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2739 } else {
2740 chunk_printf(&msg, ",,,,,,");
2741 }
2742
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002743 /* failed health analyses */
2744 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2745
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002746 /* requests : req_rate, req_rate_max, req_tot, */
2747 chunk_printf(&msg, ",,,");
2748
Willy Tarreauae526782010-03-04 20:34:23 +01002749 /* errors: cli_aborts, srv_aborts */
2750 chunk_printf(&msg, "%lld,%lld,",
2751 sv->counters.cli_aborts, sv->counters.srv_aborts);
2752
Willy Tarreau7f062c42009-03-05 18:43:00 +01002753 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002754 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002755 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002756 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002757 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002758 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002759
Willy Tarreau295a8372011-03-10 11:25:07 +01002760 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02002761 /* fall through */
2762
Willy Tarreau295a8372011-03-10 11:25:07 +01002763 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02002764 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002765 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002766 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2767 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002768 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01002769 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002770 /* Column sub-heading for Enable or Disable server */
2771 chunk_printf(&msg, "<td></td>");
2772 }
2773 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002774
2775 if (uri->flags&ST_SHLGNDS) {
2776 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002777 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002778 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002779
2780 /* cookie */
2781 if (px->cookie_name) {
2782 struct chunk src;
2783
2784 chunk_printf(&msg, ", cookie: '");
2785
2786 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2787 chunk_htmlencode(&msg, &src);
2788
2789 chunk_printf(&msg, "'");
2790 }
2791
2792 chunk_printf(&msg, "\"");
2793
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002794 }
2795
2796 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002797 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01002798 ">%s<a name=\"%s/Backend\"></a>"
2799 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002800 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002801 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002802 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002803 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002804 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002805 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002806 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002807 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002808 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
2809 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02002810
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002811 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002812 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002813 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002814 "<td"
2815 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002816 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002817
2818 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2819 if (px->mode == PR_MODE_HTTP) {
2820 int i;
2821
2822 chunk_printf(&msg, " title=\"rsp codes:");
2823
2824 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002825 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002826
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002827 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002828 }
2829
2830 chunk_printf(&msg,
2831 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002832 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002833 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002834 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002835 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002836 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002837 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01002838 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002839 U2H7(px->be_counters.cum_lbconn),
2840 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002841
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002842 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002843 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002844 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002845 /* errors : request, connect */
2846 "<td></td><td>%s</td>"
2847 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002848 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002849 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002850 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002851 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02002852 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002853 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02002854 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002855 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2856 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002857 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002858 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
2859 U2H2(px->be_counters.failed_conns),
2860 px->be_counters.cli_aborts,
2861 px->be_counters.srv_aborts,
2862 U2H5(px->be_counters.failed_resp),
2863 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002864 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01002865 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2866 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01002867 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002868 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002869
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002870 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002871 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002872 "<td class=ac>&nbsp;</td><td>%d</td>"
2873 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002874 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002875 "</tr>",
2876 px->down_trans,
2877 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002878 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002879 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002880 /* pxid, name */
2881 "%s,BACKEND,"
2882 /* queue : current, max */
2883 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002884 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002885 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002886 /* bytes : in, out */
2887 "%lld,%lld,"
2888 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002889 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002890 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002891 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002892 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002893 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002894 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02002895 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002896 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02002897 * active and backups. */
2898 "%s,"
2899 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002900 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002901 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002902 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002903 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002904 /* rate, rate_lim, rate_max, */
2905 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002906 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002907 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002908 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002909 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2910 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2911 px->be_counters.bytes_in, px->be_counters.bytes_out,
2912 px->be_counters.denied_req, px->be_counters.denied_resp,
2913 px->be_counters.failed_conns, px->be_counters.failed_resp,
2914 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01002915 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01002916 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002917 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002918 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01002919 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002920 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002921 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002922 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002923 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002924
2925 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2926 if (px->mode == PR_MODE_HTTP) {
2927 int i;
2928
2929 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002930 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002931
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002932 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002933 } else {
2934 chunk_printf(&msg, ",,,,,,");
2935 }
2936
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002937 /* failed health analyses */
2938 chunk_printf(&msg, ",");
2939
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002940 /* requests : req_rate, req_rate_max, req_tot, */
2941 chunk_printf(&msg, ",,,");
2942
Willy Tarreauae526782010-03-04 20:34:23 +01002943 /* errors: cli_aborts, srv_aborts */
2944 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002945 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01002946
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002947 /* finish with EOL */
2948 chunk_printf(&msg, "\n");
2949
Willy Tarreau55bb8452007-10-17 18:44:57 +02002950 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002951 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002952 return 0;
2953 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002954
Willy Tarreau295a8372011-03-10 11:25:07 +01002955 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002956 /* fall through */
2957
Willy Tarreau295a8372011-03-10 11:25:07 +01002958 case STAT_PX_ST_END:
2959 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002960 chunk_printf(&msg, "</table>");
2961
Willy Tarreau295a8372011-03-10 11:25:07 +01002962 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002963 /* close the form used to enable/disable this proxy servers */
2964 chunk_printf(&msg,
2965 "Choose the action to perform on the checked servers : "
2966 "<select name=action>"
2967 "<option value=\"\"></option>"
2968 "<option value=\"disable\">Disable</option>"
2969 "<option value=\"enable\">Enable</option>"
2970 "</select>"
2971 "<input type=\"hidden\" name=\"b\" value=\"%s\">"
2972 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2973 "</form>",
2974 px->id);
2975 }
2976
2977 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02002978
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002979 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002980 return 0;
2981 }
Willy Tarreau91861262007-10-17 17:06:05 +02002982
Willy Tarreau295a8372011-03-10 11:25:07 +01002983 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002984 /* fall through */
2985
Willy Tarreau295a8372011-03-10 11:25:07 +01002986 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002987 return 1;
2988
2989 default:
2990 /* unknown state, we should put an abort() here ! */
2991 return 1;
2992 }
2993}
2994
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002995/* This function dumps a complete session state onto the stream intreface's
2996 * read buffer. The data_ctx must have been zeroed first, and the flags
2997 * properly set. The session has to be set in data_ctx.sess.target. It returns
2998 * 0 if the output buffer is full and it needs to be called again, otherwise
2999 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003000 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003001static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003002{
3003 struct tm tm;
3004 struct chunk msg;
3005 struct session *sess;
3006 extern const char *monthname[12];
3007 char pn[INET6_ADDRSTRLEN];
3008
3009 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau295a8372011-03-10 11:25:07 +01003010 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003011
Willy Tarreau295a8372011-03-10 11:25:07 +01003012 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003013 /* session changed, no need to go any further */
3014 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003015 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003016 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003017 si->applet.ctx.sess.target = NULL;
3018 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003019 return 1;
3020 }
3021
Willy Tarreau295a8372011-03-10 11:25:07 +01003022 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003023 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003024 si->applet.ctx.sess.uid = sess->uniq_id;
3025 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003026 /* fall through */
3027
3028 case 1:
3029 chunk_printf(&msg,
3030 "%p: id=%u, proto=%s",
3031 sess,
3032 sess->uniq_id,
3033 sess->listener->proto->name);
3034
Willy Tarreau631f01c2011-09-05 00:36:48 +02003035 switch (addr_to_str(&sess->si[0].addr.c.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003036 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003037 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003038 chunk_printf(&msg, " source=%s:%d\n",
3039 pn, get_host_port(&sess->si[0].addr.c.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003040 break;
3041 case AF_UNIX:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003042 chunk_printf(&msg, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003043 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003044 default:
3045 /* no more information to print right now */
3046 chunk_printf(&msg, "\n");
3047 break;
3048 }
3049
3050 chunk_printf(&msg,
3051 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003052 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003053
3054 chunk_printf(&msg,
3055 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)\n",
3056 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3057 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3058 sess->listener ? sess->listener->luid : 0);
3059
Willy Tarreau50943332011-09-02 17:33:05 +02003060 if (sess->be->cap & PR_CAP_BE)
3061 chunk_printf(&msg,
3062 " backend=%s (id=%u mode=%s) server=%s (id=%u)\n",
3063 sess->be->id,
3064 sess->be->uuid, sess->be->mode ? "http" : "tcp",
3065 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
3066 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
3067 else
3068 chunk_printf(&msg, " backend=<NONE> (id=-1 mode=-) server=<NONE> (id=-1)\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003069
3070 chunk_printf(&msg,
3071 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
3072 sess->task,
3073 sess->task->state,
3074 sess->task->nice, sess->task->calls,
3075 sess->task->expire ?
3076 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3077 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3078 TICKS_TO_MS(1000)) : "<NEVER>",
3079 task_in_rq(sess->task) ? ", running" : "");
3080
3081 get_localtime(sess->logs.accept_date.tv_sec, &tm);
3082 chunk_printf(&msg,
3083 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
3084 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3085 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3086 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3087
3088 chunk_printf(&msg,
3089 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3090 &sess->si[0],
3091 sess->si[0].state,
3092 sess->si[0].flags,
3093 sess->si[0].fd,
3094 sess->si[0].exp ?
3095 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3096 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3097 TICKS_TO_MS(1000)) : "<NEVER>",
3098 sess->si[0].err_type);
3099
3100 chunk_printf(&msg,
3101 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3102 &sess->si[1],
3103 sess->si[1].state,
3104 sess->si[1].flags,
3105 sess->si[1].fd,
3106 sess->si[1].exp ?
3107 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3108 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3109 TICKS_TO_MS(1000)) : "<NEVER>",
3110 sess->si[1].err_type);
3111
3112 chunk_printf(&msg,
3113 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3114 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3115 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3116
3117
3118 chunk_printf(&msg,
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02003119 " req=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003120 " an_exp=%s",
3121 sess->req,
3122 sess->req->flags, sess->req->analysers,
3123 sess->req->l, sess->req->send_max,
3124 sess->req->pipe ? sess->req->pipe->data : 0,
3125 sess->req->to_forward,
3126 sess->req->analyse_exp ?
3127 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3128 TICKS_TO_MS(1000)) : "<NEVER>");
3129
3130 chunk_printf(&msg,
3131 " rex=%s",
3132 sess->req->rex ?
3133 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3134 TICKS_TO_MS(1000)) : "<NEVER>");
3135
3136 chunk_printf(&msg,
3137 " wex=%s\n"
3138 " data=%p r=%d w=%d lr=%d total=%lld\n",
3139 sess->req->wex ?
3140 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3141 TICKS_TO_MS(1000)) : "<NEVER>",
3142 sess->req->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01003143 (int)(sess->req->r - sess->req->data),
3144 (int)(sess->req->w - sess->req->data),
3145 (int)(sess->req->lr - sess->req->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003146 sess->req->total);
3147
3148 chunk_printf(&msg,
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02003149 " res=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003150 " an_exp=%s",
3151 sess->rep,
3152 sess->rep->flags, sess->rep->analysers,
3153 sess->rep->l, sess->rep->send_max,
3154 sess->rep->pipe ? sess->rep->pipe->data : 0,
3155 sess->rep->to_forward,
3156 sess->rep->analyse_exp ?
3157 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3158 TICKS_TO_MS(1000)) : "<NEVER>");
3159
3160 chunk_printf(&msg,
3161 " rex=%s",
3162 sess->rep->rex ?
3163 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3164 TICKS_TO_MS(1000)) : "<NEVER>");
3165
3166 chunk_printf(&msg,
3167 " wex=%s\n"
3168 " data=%p r=%d w=%d lr=%d total=%lld\n",
3169 sess->rep->wex ?
3170 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3171 TICKS_TO_MS(1000)) : "<NEVER>",
3172 sess->rep->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01003173 (int)(sess->rep->r - sess->rep->data),
3174 (int)(sess->rep->w - sess->rep->data),
3175 (int)(sess->rep->lr - sess->rep->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003176 sess->rep->total);
3177
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003178 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003179 return 0;
3180
3181 /* use other states to dump the contents */
3182 }
3183 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003184 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003185 return 1;
3186}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003187
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003188/* This function dumps all sessions' states onto the stream intreface's
3189 * read buffer. The data_ctx must have been zeroed first, and the flags
3190 * properly set. It returns 0 if the output buffer is full and it needs
3191 * to be called again, otherwise non-zero. It is designed to be called
3192 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003193 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003194static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003195{
3196 struct chunk msg;
3197
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003198 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003199 /* If we're forced to shut down, we might have to remove our
3200 * reference to the last session being dumped.
3201 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003202 if (si->applet.state == STAT_ST_LIST) {
3203 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3204 LIST_DEL(&si->applet.ctx.sess.bref.users);
3205 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003206 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003207 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003208 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003209 }
3210
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003211 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003212
Willy Tarreau295a8372011-03-10 11:25:07 +01003213 switch (si->applet.state) {
3214 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003215 /* the function had not been called yet, let's prepare the
3216 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003217 * pointer to the first in the global list. When a target
3218 * session is being destroyed, it is responsible for updating
3219 * this pointer. We know we have reached the end when this
3220 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003221 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003222 LIST_INIT(&si->applet.ctx.sess.bref.users);
3223 si->applet.ctx.sess.bref.ref = sessions.n;
3224 si->applet.state = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003225 /* fall through */
3226
Willy Tarreau295a8372011-03-10 11:25:07 +01003227 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003228 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003229 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3230 LIST_DEL(&si->applet.ctx.sess.bref.users);
3231 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003232 }
3233
3234 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003235 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003236 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003237 struct session *curr_sess;
3238
Willy Tarreau295a8372011-03-10 11:25:07 +01003239 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003240
Willy Tarreau295a8372011-03-10 11:25:07 +01003241 if (si->applet.ctx.sess.target) {
3242 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003243 goto next_sess;
3244
Willy Tarreau295a8372011-03-10 11:25:07 +01003245 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003246 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003247 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003248 return 0;
3249
3250 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003251 LIST_DEL(&si->applet.ctx.sess.bref.users);
3252 LIST_INIT(&si->applet.ctx.sess.bref.users);
3253 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003254 break;
3255 }
3256
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003257 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003258 "%p: proto=%s",
3259 curr_sess,
3260 curr_sess->listener->proto->name);
3261
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003262
Willy Tarreau631f01c2011-09-05 00:36:48 +02003263 switch (addr_to_str(&curr_sess->si[0].addr.c.from, pn, sizeof(pn))) {
3264 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003265 case AF_INET6:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003266 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003267 " src=%s:%d fe=%s be=%s srv=%s",
3268 pn,
Willy Tarreau631f01c2011-09-05 00:36:48 +02003269 get_host_port(&curr_sess->si[0].addr.c.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003270 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003271 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003272 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003273 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003274 break;
3275 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02003276 chunk_printf(&msg,
3277 " src=unix:%d fe=%s be=%s srv=%s",
3278 curr_sess->listener->luid,
3279 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003280 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003281 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003282 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003283 break;
3284 }
3285
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003286 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003287 " ts=%02x age=%s calls=%d",
3288 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003289 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3290 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003291
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003292 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003293 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
3294 curr_sess->req->flags,
3295 curr_sess->req->l,
3296 curr_sess->req->analysers,
3297 curr_sess->req->rex ?
3298 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3299 TICKS_TO_MS(1000)) : "");
3300
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003301 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003302 ",wx=%s",
3303 curr_sess->req->wex ?
3304 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3305 TICKS_TO_MS(1000)) : "");
3306
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003307 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003308 ",ax=%s]",
3309 curr_sess->req->analyse_exp ?
3310 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3311 TICKS_TO_MS(1000)) : "");
3312
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003313 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003314 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
3315 curr_sess->rep->flags,
3316 curr_sess->rep->l,
3317 curr_sess->rep->analysers,
3318 curr_sess->rep->rex ?
3319 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3320 TICKS_TO_MS(1000)) : "");
3321
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003322 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003323 ",wx=%s",
3324 curr_sess->rep->wex ?
3325 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3326 TICKS_TO_MS(1000)) : "");
3327
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003328 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003329 ",ax=%s]",
3330 curr_sess->rep->analyse_exp ?
3331 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3332 TICKS_TO_MS(1000)) : "");
3333
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003334 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003335 " s0=[%d,%1xh,fd=%d,ex=%s]",
3336 curr_sess->si[0].state,
3337 curr_sess->si[0].flags,
3338 curr_sess->si[0].fd,
3339 curr_sess->si[0].exp ?
3340 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3341 TICKS_TO_MS(1000)) : "");
3342
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003343 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003344 " s1=[%d,%1xh,fd=%d,ex=%s]",
3345 curr_sess->si[1].state,
3346 curr_sess->si[1].flags,
3347 curr_sess->si[1].fd,
3348 curr_sess->si[1].exp ?
3349 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3350 TICKS_TO_MS(1000)) : "");
3351
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003352 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003353 " exp=%s",
3354 curr_sess->task->expire ?
3355 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3356 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003357 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003358 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003359
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003360 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003361
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003362 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003363 /* let's try again later from this session. We add ourselves into
3364 * this session's users so that it can remove us upon termination.
3365 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003366 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003367 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003368 }
3369
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003370 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003371 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003372 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003373
Willy Tarreau295a8372011-03-10 11:25:07 +01003374 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003375 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003376 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003377 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3378 else
3379 chunk_printf(&msg, "Session not found.\n");
3380
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003381 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003382 return 0;
3383
Willy Tarreau295a8372011-03-10 11:25:07 +01003384 si->applet.ctx.sess.target = NULL;
3385 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003386 return 1;
3387 }
3388
Willy Tarreau295a8372011-03-10 11:25:07 +01003389 si->applet.state = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003390 /* fall through */
3391
3392 default:
Willy Tarreau295a8372011-03-10 11:25:07 +01003393 si->applet.state = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003394 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003395 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003396}
3397
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003398/* This function dumps all tables' states onto the stream intreface's
3399 * read buffer. The data_ctx must have been zeroed first, and the flags
3400 * properly set. It returns 0 if the output buffer is full and it needs
3401 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003402 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003403static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003404{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003405 struct session *s = si->applet.private;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003406 struct chunk msg;
3407 struct ebmb_node *eb;
3408 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003409 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003410
3411 /*
Willy Tarreau295a8372011-03-10 11:25:07 +01003412 * We have 3 possible states in si->applet.state :
3413 * - STAT_ST_INIT : the first call
3414 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003415 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003416 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003417 * and the entry pointer points to the next entry to be dumped,
3418 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003419 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003420 * data though.
3421 */
3422
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003423 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003424 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau295a8372011-03-10 11:25:07 +01003425 if (si->applet.state == STAT_ST_LIST) {
3426 si->applet.ctx.table.entry->ref_cnt--;
3427 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003428 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003429 return 1;
3430 }
3431
3432 chunk_init(&msg, trash, sizeof(trash));
3433
Willy Tarreau295a8372011-03-10 11:25:07 +01003434 while (si->applet.state != STAT_ST_FIN) {
3435 switch (si->applet.state) {
3436 case STAT_ST_INIT:
3437 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3438 if (!si->applet.ctx.table.proxy)
3439 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003440
Willy Tarreau295a8372011-03-10 11:25:07 +01003441 si->applet.ctx.table.entry = NULL;
3442 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003443 break;
3444
Willy Tarreau295a8372011-03-10 11:25:07 +01003445 case STAT_ST_INFO:
3446 if (!si->applet.ctx.table.proxy ||
3447 (si->applet.ctx.table.target &&
3448 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
3449 si->applet.state = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003450 break;
3451 }
3452
Willy Tarreau295a8372011-03-10 11:25:07 +01003453 if (si->applet.ctx.table.proxy->table.size) {
Simon Hormanc88b8872011-06-15 15:18:49 +09003454 if (show && !stats_dump_table_head_to_buffer(&msg, si, si->applet.ctx.table.proxy,
3455 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003456 return 0;
3457
Willy Tarreau295a8372011-03-10 11:25:07 +01003458 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003459 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
3460 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003461 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003462 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003463 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3464 si->applet.ctx.table.entry->ref_cnt++;
3465 si->applet.state = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003466 break;
3467 }
3468 }
3469 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003470 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003471 break;
3472
Willy Tarreau295a8372011-03-10 11:25:07 +01003473 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003474 skip_entry = false;
3475
Willy Tarreau295a8372011-03-10 11:25:07 +01003476 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003477 /* we're filtering on some data contents */
3478 void *ptr;
3479 long long data;
3480
Willy Tarreau295a8372011-03-10 11:25:07 +01003481 dt = si->applet.ctx.table.data_type;
3482 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3483 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003484 dt);
3485
3486 data = 0;
3487 switch (stktable_data_types[dt].std_type) {
3488 case STD_T_SINT:
3489 data = stktable_data_cast(ptr, std_t_sint);
3490 break;
3491 case STD_T_UINT:
3492 data = stktable_data_cast(ptr, std_t_uint);
3493 break;
3494 case STD_T_ULL:
3495 data = stktable_data_cast(ptr, std_t_ull);
3496 break;
3497 case STD_T_FRQP:
3498 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003499 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003500 break;
3501 }
3502
3503 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003504 if ((data < si->applet.ctx.table.value &&
3505 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3506 si->applet.ctx.table.data_op == STD_OP_GT ||
3507 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3508 (data == si->applet.ctx.table.value &&
3509 (si->applet.ctx.table.data_op == STD_OP_NE ||
3510 si->applet.ctx.table.data_op == STD_OP_GT ||
3511 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3512 (data > si->applet.ctx.table.value &&
3513 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3514 si->applet.ctx.table.data_op == STD_OP_LT ||
3515 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003516 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003517 }
3518
Simon Hormanc88b8872011-06-15 15:18:49 +09003519 if (show && !skip_entry &&
3520 !stats_dump_table_entry_to_buffer(&msg, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003521 si->applet.ctx.table.entry))
3522 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003523
Willy Tarreau295a8372011-03-10 11:25:07 +01003524 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003525
Willy Tarreau295a8372011-03-10 11:25:07 +01003526 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003527 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003528 struct stksess *old = si->applet.ctx.table.entry;
3529 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3530 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3531 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003532 break;
3533 }
3534
Simon Hormanc88b8872011-06-15 15:18:49 +09003535
3536 if (show)
3537 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3538 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3539 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3540
Willy Tarreau295a8372011-03-10 11:25:07 +01003541 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
3542 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003543 break;
3544
Willy Tarreau295a8372011-03-10 11:25:07 +01003545 case STAT_ST_END:
3546 si->applet.state = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003547 break;
3548 }
3549 }
3550 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003551}
3552
Willy Tarreaud426a182010-03-05 14:58:26 +01003553/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003554 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3555 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3556 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3557 * lines are respected within the limit of 70 output chars. Lines that are
3558 * continuation of a previous truncated line begin with "+" instead of " "
3559 * after the offset. The new pointer is returned.
3560 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003561static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3562 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003563{
3564 int end;
3565 unsigned char c;
3566
3567 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003568 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003569 return ptr;
3570
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003571 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003572
Willy Tarreaud426a182010-03-05 14:58:26 +01003573 while (ptr < len && ptr < bsize) {
3574 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003575 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003576 if (out->len > end - 2)
3577 break;
3578 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003579 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003580 if (out->len > end - 3)
3581 break;
3582 out->str[out->len++] = '\\';
3583 switch (c) {
3584 case '\t': c = 't'; break;
3585 case '\n': c = 'n'; break;
3586 case '\r': c = 'r'; break;
3587 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003588 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003589 }
3590 out->str[out->len++] = c;
3591 } else {
3592 if (out->len > end - 5)
3593 break;
3594 out->str[out->len++] = '\\';
3595 out->str[out->len++] = 'x';
3596 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3597 out->str[out->len++] = hextab[c & 0xF];
3598 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003599 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003600 /* we had a line break, let's return now */
3601 out->str[out->len++] = '\n';
3602 *line = ptr;
3603 return ptr;
3604 }
3605 }
3606 /* we have an incomplete line, we return it as-is */
3607 out->str[out->len++] = '\n';
3608 return ptr;
3609}
3610
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003611/* This function dumps all captured errors onto the stream intreface's
3612 * read buffer. The data_ctx must have been zeroed first, and the flags
3613 * properly set. It returns 0 if the output buffer is full and it needs
3614 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003615 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003616static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003617{
3618 extern const char *monthname[12];
3619 struct chunk msg;
3620
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003621 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003622 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003623
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003624 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003625
Willy Tarreau295a8372011-03-10 11:25:07 +01003626 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003627 /* the function had not been called yet, let's prepare the
3628 * buffer for a response.
3629 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003630 struct tm tm;
3631
3632 get_localtime(date.tv_sec, &tm);
3633 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3634 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3635 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3636 error_snapshot_id);
3637
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003638 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003639 /* Socket buffer full. Let's try again later from the same point */
3640 return 0;
3641 }
3642
Willy Tarreau295a8372011-03-10 11:25:07 +01003643 si->applet.ctx.errors.px = proxy;
3644 si->applet.ctx.errors.buf = 0;
3645 si->applet.ctx.errors.bol = 0;
3646 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003647 }
3648
3649 /* we have two inner loops here, one for the proxy, the other one for
3650 * the buffer.
3651 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003652 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003653 struct error_snapshot *es;
3654
Willy Tarreau295a8372011-03-10 11:25:07 +01003655 if (si->applet.ctx.errors.buf == 0)
3656 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003657 else
Willy Tarreau295a8372011-03-10 11:25:07 +01003658 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003659
3660 if (!es->when.tv_sec)
3661 goto next;
3662
Willy Tarreau295a8372011-03-10 11:25:07 +01003663 if (si->applet.ctx.errors.iid >= 0 &&
3664 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
3665 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003666 goto next;
3667
Willy Tarreau295a8372011-03-10 11:25:07 +01003668 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003669 /* just print headers now */
3670
3671 char pn[INET6_ADDRSTRLEN];
3672 struct tm tm;
3673
3674 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003675 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003676 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003677 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003678
Willy Tarreau631f01c2011-09-05 00:36:48 +02003679 addr_to_str(&es->src, pn, sizeof(pn));
Willy Tarreau295a8372011-03-10 11:25:07 +01003680 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003681 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003682 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003683 " frontend %s (#%d): invalid request\n"
3684 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003685 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003686 " request length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003687 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau3fd6cec2010-03-25 06:45:07 +01003688 pn, es->sid, (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3689 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003690 es->srv ? es->srv->id : "<NONE>",
3691 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003692 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003693 es->len, es->pos);
3694 break;
3695 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003696 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003697 " backend %s (#%d) : invalid response\n"
3698 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003699 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003700 " response length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003701 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003702 pn, es->sid, es->oe->id, es->oe->uuid,
3703 es->srv ? es->srv->id : "<NONE>",
3704 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003705 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003706 es->len, es->pos);
3707 break;
3708 }
3709
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003710 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003711 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003712 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003713 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003714 si->applet.ctx.errors.ptr = 0;
3715 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003716 }
3717
Willy Tarreau295a8372011-03-10 11:25:07 +01003718 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003719 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003720 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003721 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003722 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau61b34732009-10-03 23:49:35 +02003723 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003724 goto next;
3725 }
3726
3727 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01003728 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003729 int newptr;
3730 int newline;
3731
Willy Tarreau295a8372011-03-10 11:25:07 +01003732 newline = si->applet.ctx.errors.bol;
3733 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
3734 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02003735 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003736
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003737 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003738 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003739 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003740 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003741 si->applet.ctx.errors.ptr = newptr;
3742 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003743 };
3744 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01003745 si->applet.ctx.errors.bol = 0;
3746 si->applet.ctx.errors.ptr = -1;
3747 si->applet.ctx.errors.buf++;
3748 if (si->applet.ctx.errors.buf > 1) {
3749 si->applet.ctx.errors.buf = 0;
3750 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003751 }
3752 }
3753
3754 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02003755 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003756}
3757
Willy Tarreaub24281b2011-02-13 13:16:36 +01003758struct si_applet http_stats_applet = {
3759 .name = "<STATS>", /* used for logging */
3760 .fct = http_stats_io_handler,
3761};
3762
Simon Horman9bd2c732011-06-15 15:18:44 +09003763static struct si_applet cli_applet = {
Willy Tarreaub24281b2011-02-13 13:16:36 +01003764 .name = "<CLI>", /* used for logging */
3765 .fct = cli_io_handler,
3766};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003767
Willy Tarreau10522fd2008-07-09 20:12:41 +02003768static struct cfg_kw_list cfg_kws = {{ },{
3769 { CFG_GLOBAL, "stats", stats_parse_global },
3770 { 0, NULL, NULL },
3771}};
3772
3773__attribute__((constructor))
3774static void __dumpstats_module_init(void)
3775{
3776 cfg_register_keywords(&cfg_kws);
3777}
3778
Willy Tarreau91861262007-10-17 17:06:05 +02003779/*
3780 * Local variables:
3781 * c-indent-level: 8
3782 * c-basic-offset: 8
3783 * End:
3784 */