blob: a3dc52e317fd4b4dec70696ff15ab1bfaece5448 [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;
163 return fe;
164}
165
Willy Tarreaufbee7132007-10-18 13:53:22 +0200166/* This function parses a "stats" statement in the "global" section. It returns
167 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
168 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
169 * zero included. The trailing '\n' must not be written. The function must be
170 * called with <args> pointing to the first word after "stats".
171 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200172static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
173 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200174{
Willy Tarreau10522fd2008-07-09 20:12:41 +0200175 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200176 if (!strcmp(args[0], "socket")) {
Andreas Kohn16171e22011-01-19 20:29:32 +0100177 struct sockaddr_un *su;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200178 int cur_arg;
179
180 if (*args[1] == 0) {
181 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
182 return -1;
183 }
184
185 if (global.stats_sock.state != LI_NEW) {
186 snprintf(err, errlen, "'stats socket' already specified in global section");
187 return -1;
188 }
189
Andreas Kohn16171e22011-01-19 20:29:32 +0100190 su = str2sun(args[1]);
191 if (!su) {
192 snprintf(err, errlen, "'stats socket' path would require truncation");
193 return -1;
194 }
195 memcpy(&global.stats_sock.addr, su, sizeof(struct sockaddr_un)); // guaranteed to fit
Willy Tarreaufbee7132007-10-18 13:53:22 +0200196
Willy Tarreau89a63132009-08-16 17:41:45 +0200197 if (!global.stats_fe) {
Willy Tarreau07e9e642010-08-17 21:48:17 +0200198 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau89a63132009-08-16 17:41:45 +0200199 snprintf(err, errlen, "out of memory");
200 return -1;
201 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200202 global.stats_fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreau89a63132009-08-16 17:41:45 +0200203 }
204
Willy Tarreaufbee7132007-10-18 13:53:22 +0200205 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100206 global.stats_sock.options = LI_O_NONE;
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200207 global.stats_sock.accept = session_accept;
208 global.stats_fe->accept = stats_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200209 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200210 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200211 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreaueb472682010-05-28 18:46:57 +0200212 global.stats_sock.frontend = global.stats_fe;
Willy Tarreau6162db22009-10-10 17:13:00 +0200213 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau07e9e642010-08-17 21:48:17 +0200214 global.stats_fe->maxconn = global.stats_sock.maxconn;
Willy Tarreau89a63132009-08-16 17:41:45 +0200215 global.stats_sock.timeout = &global.stats_fe->timeout.client;
216
217 global.stats_sock.next = global.stats_fe->listen;
218 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200219
220 cur_arg = 2;
221 while (*args[cur_arg]) {
222 if (!strcmp(args[cur_arg], "uid")) {
223 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
224 cur_arg += 2;
225 }
226 else if (!strcmp(args[cur_arg], "gid")) {
227 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
228 cur_arg += 2;
229 }
230 else if (!strcmp(args[cur_arg], "mode")) {
231 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
232 cur_arg += 2;
233 }
234 else if (!strcmp(args[cur_arg], "user")) {
235 struct passwd *user;
236 user = getpwnam(args[cur_arg + 1]);
237 if (!user) {
238 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
239 args[cur_arg + 1]);
240 return -1;
241 }
242 global.stats_sock.perm.ux.uid = user->pw_uid;
243 cur_arg += 2;
244 }
245 else if (!strcmp(args[cur_arg], "group")) {
246 struct group *group;
247 group = getgrnam(args[cur_arg + 1]);
248 if (!group) {
249 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
250 args[cur_arg + 1]);
251 return -1;
252 }
253 global.stats_sock.perm.ux.gid = group->gr_gid;
254 cur_arg += 2;
255 }
Willy Tarreau6162db22009-10-10 17:13:00 +0200256 else if (!strcmp(args[cur_arg], "level")) {
257 if (!strcmp(args[cur_arg+1], "user"))
258 global.stats_sock.perm.ux.level = ACCESS_LVL_USER;
259 else if (!strcmp(args[cur_arg+1], "operator"))
260 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER;
261 else if (!strcmp(args[cur_arg+1], "admin"))
262 global.stats_sock.perm.ux.level = ACCESS_LVL_ADMIN;
263 else {
264 snprintf(err, errlen, "'stats socket level' only supports 'user', 'operator', and 'admin'");
265 return -1;
266 }
267 cur_arg += 2;
268 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200269 else {
Willy Tarreau6162db22009-10-10 17:13:00 +0200270 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', 'level', and 'mode'");
Willy Tarreaufbee7132007-10-18 13:53:22 +0200271 return -1;
272 }
273 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100274
Willy Tarreaufbee7132007-10-18 13:53:22 +0200275 uxst_add_listener(&global.stats_sock);
276 global.maxsock++;
277 }
278 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100279 unsigned timeout;
280 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
281
282 if (res) {
283 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
284 return -1;
285 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200286
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100287 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200288 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
289 return -1;
290 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200291 if (!global.stats_fe) {
292 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
293 snprintf(err, errlen, "out of memory");
294 return -1;
295 }
296 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200297 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200298 }
299 else if (!strcmp(args[0], "maxconn")) {
300 int maxconn = atol(args[1]);
301
302 if (maxconn <= 0) {
303 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
304 return -1;
305 }
306 global.maxsock -= global.stats_sock.maxconn;
307 global.stats_sock.maxconn = maxconn;
308 global.maxsock += global.stats_sock.maxconn;
Willy Tarreaueb472682010-05-28 18:46:57 +0200309 if (global.stats_fe)
310 global.stats_fe->maxconn = global.stats_sock.maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200311 }
312 else {
313 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
314 return -1;
315 }
316 return 0;
317}
318
Simon Horman9bd2c732011-06-15 15:18:44 +0900319static int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100320{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200321 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100322 "# pxname,svname,"
323 "qcur,qmax,"
324 "scur,smax,slim,stot,"
325 "bin,bout,"
326 "dreq,dresp,"
327 "ereq,econ,eresp,"
328 "wretr,wredis,"
329 "status,weight,act,bck,"
330 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200331 "pid,iid,sid,throttle,lbtot,tracked,type,"
332 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200333 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100334 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100335 "req_rate,req_rate_max,req_tot,"
336 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100337 "\n");
338}
339
Simon Hormand9366582011-06-15 15:18:45 +0900340/* print a string of text buffer to <out>. The format is :
341 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
342 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
343 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
344 */
345static int dump_text(struct chunk *out, const char *buf, int bsize)
346{
347 unsigned char c;
348 int ptr = 0;
349
350 while (buf[ptr] && ptr < bsize) {
351 c = buf[ptr];
352 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
353 if (out->len > out->size - 1)
354 break;
355 out->str[out->len++] = c;
356 }
357 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
358 if (out->len > out->size - 2)
359 break;
360 out->str[out->len++] = '\\';
361 switch (c) {
362 case ' ': c = ' '; break;
363 case '\t': c = 't'; break;
364 case '\n': c = 'n'; break;
365 case '\r': c = 'r'; break;
366 case '\e': c = 'e'; break;
367 case '\\': c = '\\'; break;
368 }
369 out->str[out->len++] = c;
370 }
371 else {
372 if (out->len > out->size - 4)
373 break;
374 out->str[out->len++] = '\\';
375 out->str[out->len++] = 'x';
376 out->str[out->len++] = hextab[(c >> 4) & 0xF];
377 out->str[out->len++] = hextab[c & 0xF];
378 }
379 ptr++;
380 }
381
382 return ptr;
383}
384
385/* print a buffer in hexa.
386 * Print stopped if <bsize> is reached, or if no more place in the chunk.
387 */
388static int dump_binary(struct chunk *out, const char *buf, int bsize)
389{
390 unsigned char c;
391 int ptr = 0;
392
393 while (ptr < bsize) {
394 c = buf[ptr];
395
396 if (out->len > out->size - 2)
397 break;
398 out->str[out->len++] = hextab[(c >> 4) & 0xF];
399 out->str[out->len++] = hextab[c & 0xF];
400
401 ptr++;
402 }
403 return ptr;
404}
405
406/* Dump the status of a table to a stream interface's
407 * read buffer. It returns 0 if the output buffer is full
408 * and needs to be called again, otherwise non-zero.
409 */
410static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
411 struct proxy *proxy, struct proxy *target)
412{
413 struct session *s = si->applet.private;
414
415 chunk_printf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
416 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
417
418 /* any other information should be dumped here */
419
420 if (target && s->listener->perm.ux.level < ACCESS_LVL_OPER)
421 chunk_printf(msg, "# contents not dumped due to insufficient privileges\n");
422
423 if (buffer_feed_chunk(si->ib, msg) >= 0)
424 return 0;
425
426 return 1;
427}
428
429/* Dump the a table entry to a stream interface's
430 * read buffer. It returns 0 if the output buffer is full
431 * and needs to be called again, otherwise non-zero.
432 */
433static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
434 struct proxy *proxy, struct stksess *entry)
435{
436 int dt;
437
438 chunk_printf(msg, "%p:", entry);
439
440 if (proxy->table.type == STKTABLE_TYPE_IP) {
441 char addr[INET_ADDRSTRLEN];
442 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
443 chunk_printf(msg, " key=%s", addr);
444 }
445 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
446 char addr[INET6_ADDRSTRLEN];
447 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
448 chunk_printf(msg, " key=%s", addr);
449 }
450 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
451 chunk_printf(msg, " key=%u", *(unsigned int *)entry->key.key);
452 }
453 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
454 chunk_printf(msg, " key=");
455 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
456 }
457 else {
458 chunk_printf(msg, " key=");
459 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
460 }
461
462 chunk_printf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
463
464 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
465 void *ptr;
466
467 if (proxy->table.data_ofs[dt] == 0)
468 continue;
469 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
470 chunk_printf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
471 else
472 chunk_printf(msg, " %s=", stktable_data_types[dt].name);
473
474 ptr = stktable_data_ptr(&proxy->table, entry, dt);
475 switch (stktable_data_types[dt].std_type) {
476 case STD_T_SINT:
477 chunk_printf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
478 break;
479 case STD_T_UINT:
480 chunk_printf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
481 break;
482 case STD_T_ULL:
483 chunk_printf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
484 break;
485 case STD_T_FRQP:
486 chunk_printf(msg, "%d",
487 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
488 proxy->table.data_arg[dt].u));
489 break;
490 }
491 }
492 chunk_printf(msg, "\n");
493
494 if (buffer_feed_chunk(si->ib, msg) >= 0)
495 return 0;
496
497 return 1;
498}
499
Simon Horman17bce342011-06-15 15:18:47 +0900500static void stats_sock_table_key_request(struct stream_interface *si, char **args, bool show)
Simon Horman121f3052011-06-15 15:18:46 +0900501{
502 struct session *s = si->applet.private;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900503 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900504 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900505 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900506 unsigned char ip6_key[sizeof(struct in6_addr)];
Simon Horman121f3052011-06-15 15:18:46 +0900507
Simon Hormand5b9fd92011-06-15 15:18:48 +0900508 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900509
510 if (!*args[4]) {
511 si->applet.ctx.cli.msg = "Key value expected\n";
512 si->applet.st0 = STAT_CLI_PRINT;
513 return;
514 }
515
Simon Hormanc5b89f62011-06-15 15:18:50 +0900516 switch (px->table.type) {
517 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900518 uint32_key = htonl(inetaddr_host(args[4]));
519 static_table_key.key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900520 break;
521 case STKTABLE_TYPE_IPV6:
522 inet_pton(AF_INET6, args[4], ip6_key);
523 static_table_key.key = &ip6_key;
524 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900525 case STKTABLE_TYPE_INTEGER:
526 {
527 char *endptr;
528 unsigned long val;
529 errno = 0;
530 val = strtoul(args[4], &endptr, 10);
531 if ((errno == ERANGE && val == ULONG_MAX) ||
532 (errno != 0 && val == 0) || endptr == args[4] ||
533 val > 0xffffffff) {
534 si->applet.ctx.cli.msg = "Invalid key\n";
535 si->applet.st0 = STAT_CLI_PRINT;
536 return;
537 }
538 uint32_key = (uint32_t) val;
539 static_table_key.key = &uint32_key;
540 break;
541 }
542 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900543 case STKTABLE_TYPE_STRING:
544 static_table_key.key = args[4];
545 static_table_key.key_len = strlen(args[4]);
546 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900547 default:
Simon Horman17bce342011-06-15 15:18:47 +0900548 if (show)
Simon Hormancec9a222011-06-15 15:18:51 +0900549 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 +0900550 else
Simon Hormancec9a222011-06-15 15:18:51 +0900551 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 +0900552 si->applet.st0 = STAT_CLI_PRINT;
553 return;
554 }
555
556 /* check permissions */
557 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
558 si->applet.ctx.cli.msg = stats_permission_denied_msg;
559 si->applet.st0 = STAT_CLI_PRINT;
560 return;
561 }
562
563 ts = stktable_lookup_key(&px->table, &static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900564 if (!ts)
565 return;
566
567 if (show) {
568 struct chunk msg;
569 chunk_init(&msg, trash, sizeof(trash));
570 if (!stats_dump_table_head_to_buffer(&msg, si, px, px))
571 return;
572 stats_dump_table_entry_to_buffer(&msg, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900573 return;
574 }
Simon Horman17bce342011-06-15 15:18:47 +0900575
576 if (ts->ref_cnt) {
Simon Horman121f3052011-06-15 15:18:46 +0900577 /* don't delete an entry which is currently referenced */
578 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
579 si->applet.st0 = STAT_CLI_PRINT;
580 return;
581 }
582
583 stksess_kill(&px->table, ts);
584}
585
Simon Hormand5b9fd92011-06-15 15:18:48 +0900586static void stats_sock_table_data_request(struct stream_interface *si, char **args)
587{
588 /* condition on stored data value */
589 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
590 if (si->applet.ctx.table.data_type < 0) {
591 si->applet.ctx.cli.msg = "Unknown data type\n";
592 si->applet.st0 = STAT_CLI_PRINT;
593 return;
594 }
595
596 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
597 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
598 si->applet.st0 = STAT_CLI_PRINT;
599 return;
600 }
601
602 si->applet.ctx.table.data_op = get_std_op(args[4]);
603 if (si->applet.ctx.table.data_op < 0) {
604 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
605 si->applet.st0 = STAT_CLI_PRINT;
606 return;
607 }
608
609 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
610 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
611 si->applet.st0 = STAT_CLI_PRINT;
612 return;
613 }
614}
615
616static void stats_sock_table_request(struct stream_interface *si, char **args, bool show)
617{
618 si->applet.ctx.table.data_type = -1;
619 si->applet.state = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200620 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900621 si->applet.ctx.table.proxy = NULL;
622 si->applet.ctx.table.entry = NULL;
Simon Hormanc88b8872011-06-15 15:18:49 +0900623 if (show)
624 si->applet.st0 = STAT_CLI_O_TAB;
625 else
626 si->applet.st0 = STAT_CLI_O_CLR;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900627
628 if (*args[2]) {
629 si->applet.ctx.table.target = find_stktable(args[2]);
630 if (!si->applet.ctx.table.target) {
631 si->applet.ctx.cli.msg = "No such table\n";
632 si->applet.st0 = STAT_CLI_PRINT;
633 return;
634 }
635 }
636 else {
637 if (!show)
638 goto err_args;
639 return;
640 }
641
642 if (strcmp(args[3], "key") == 0)
643 stats_sock_table_key_request(si, args, show);
Simon Hormanc88b8872011-06-15 15:18:49 +0900644 else if (strncmp(args[3], "data.", 5) == 0)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900645 stats_sock_table_data_request(si, args);
Simon Hormanc88b8872011-06-15 15:18:49 +0900646 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900647 goto err_args;
648
649 return;
650
651err_args:
652 if (show)
653 si->applet.ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
654 else
Simon Hormanc88b8872011-06-15 15:18:49 +0900655 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 +0900656 si->applet.st0 = STAT_CLI_PRINT;
657}
658
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200659/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200660 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100661 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200662 * designating the function which will have to process the request, which can
663 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200664 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900665static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200666{
Willy Tarreaubc4af052011-02-13 13:25:14 +0100667 struct session *s = si->applet.private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200668 char *args[MAX_STATS_ARGS + 1];
669 int arg;
670
671 while (isspace((unsigned char)*line))
672 line++;
673
674 arg = 0;
675 args[arg] = line;
676
677 while (*line && arg < MAX_STATS_ARGS) {
678 if (isspace((unsigned char)*line)) {
679 *line++ = '\0';
680
681 while (isspace((unsigned char)*line))
682 line++;
683
684 args[++arg] = line;
685 continue;
686 }
687
688 line++;
689 }
690
691 while (++arg <= MAX_STATS_ARGS)
692 args[arg] = line;
693
Willy Tarreau295a8372011-03-10 11:25:07 +0100694 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200695 if (strcmp(args[0], "show") == 0) {
696 if (strcmp(args[1], "stat") == 0) {
697 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100698 si->applet.ctx.stats.flags |= STAT_BOUND;
699 si->applet.ctx.stats.iid = atoi(args[2]);
700 si->applet.ctx.stats.type = atoi(args[3]);
701 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200702 }
703
Willy Tarreau295a8372011-03-10 11:25:07 +0100704 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
705 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
706 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100707 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200708 }
709 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100710 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
711 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
712 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100713 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200714 }
715 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100716 si->applet.state = STAT_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200717 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100718 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100719 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200720 return 1;
721 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100722 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100723 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100724 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100725 si->applet.ctx.sess.target = NULL;
726 si->applet.ctx.sess.section = 0; /* start with session status */
727 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100728 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200729 }
730 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200731 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100732 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100733 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200734 return 1;
735 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200736 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100737 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200738 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100739 si->applet.ctx.errors.iid = -1;
740 si->applet.ctx.errors.px = NULL;
741 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100742 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200743 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200744 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900745 stats_sock_table_request(si, args, true);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200746 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200747 else { /* neither "stat" nor "info" nor "sess" nor "errors" no "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200748 return 0;
749 }
750 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200751 else if (strcmp(args[0], "clear") == 0) {
752 if (strcmp(args[1], "counters") == 0) {
753 struct proxy *px;
754 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200755 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200756 int clrall = 0;
757
758 if (strcmp(args[2], "all") == 0)
759 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200760
Willy Tarreau6162db22009-10-10 17:13:00 +0200761 /* check permissions */
762 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
763 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100764 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100765 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200766 return 1;
767 }
768
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200769 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100770 if (clrall) {
771 memset(&px->be_counters, 0, sizeof(px->be_counters));
772 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
773 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200774 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100775 px->be_counters.conn_max = 0;
776 px->be_counters.p.http.rps_max = 0;
777 px->be_counters.sps_max = 0;
778 px->be_counters.cps_max = 0;
779 px->be_counters.nbpend_max = 0;
780
781 px->fe_counters.conn_max = 0;
782 px->fe_counters.p.http.rps_max = 0;
783 px->fe_counters.sps_max = 0;
784 px->fe_counters.cps_max = 0;
785 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200786 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200787
788 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200789 if (clrall)
790 memset(&sv->counters, 0, sizeof(sv->counters));
791 else {
792 sv->counters.cur_sess_max = 0;
793 sv->counters.nbpend_max = 0;
794 sv->counters.sps_max = 0;
795 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200796
797 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200798 if (li->counters) {
799 if (clrall)
800 memset(li->counters, 0, sizeof(*li->counters));
801 else
802 li->counters->conn_max = 0;
803 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200804 }
805
806 return 1;
807 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200808 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900809 stats_sock_table_request(si, args, false);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200810 /* end of processing */
811 return 1;
812 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200813 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200814 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200815 return 0;
816 }
817 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200818 else if (strcmp(args[0], "get") == 0) {
819 if (strcmp(args[1], "weight") == 0) {
820 struct proxy *px;
821 struct server *sv;
822
823 /* split "backend/server" and make <line> point to server */
824 for (line = args[2]; *line; line++)
825 if (*line == '/') {
826 *line++ = '\0';
827 break;
828 }
829
830 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100831 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100832 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200833 return 1;
834 }
835
836 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100837 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\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 /* return server's effective weight at the moment */
843 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
844 buffer_feed(si->ib, trash);
845 return 1;
846 }
847 else { /* not "get weight" */
848 return 0;
849 }
850 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200851 else if (strcmp(args[0], "set") == 0) {
852 if (strcmp(args[1], "weight") == 0) {
853 struct proxy *px;
854 struct server *sv;
855 int w;
856
857 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100858 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100859 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200860 return 1;
861 }
862
863 /* split "backend/server" and make <line> point to server */
864 for (line = args[2]; *line; line++)
865 if (*line == '/') {
866 *line++ = '\0';
867 break;
868 }
869
870 if (!*line || !*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100871 si->applet.ctx.cli.msg = "Require 'backend/server' and 'weight' or 'weight%'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100872 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200873 return 1;
874 }
875
876 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100877 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\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
Cyril Bonté613f0df2011-03-03 20:49:04 +0100882 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100883 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100884 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100885 return 1;
886 }
887
Willy Tarreau4483d432009-10-10 19:30:08 +0200888 /* if the weight is terminated with '%', it is set relative to
889 * the initial weight, otherwise it is absolute.
890 */
891 w = atoi(args[3]);
892 if (strchr(args[3], '%') != NULL) {
893 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100894 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100895 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200896 return 1;
897 }
898 w = sv->iweight * w / 100;
899 }
900 else {
901 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100902 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100903 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200904 return 1;
905 }
906 }
907
908 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100909 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 +0100910 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200911 return 1;
912 }
913
914 sv->uweight = w;
915
916 if (px->lbprm.algo & BE_LB_PROP_DYN) {
917 /* we must take care of not pushing the server to full throttle during slow starts */
918 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
919 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
920 else
921 sv->eweight = BE_WEIGHT_SCALE;
922 sv->eweight *= sv->uweight;
923 } else {
924 sv->eweight = sv->uweight;
925 }
926
927 /* static LB algorithms are a bit harder to update */
928 if (px->lbprm.update_server_eweight)
929 px->lbprm.update_server_eweight(sv);
930 else if (sv->eweight)
931 px->lbprm.set_server_status_up(sv);
932 else
933 px->lbprm.set_server_status_down(sv);
934
935 return 1;
936 }
Willy Tarreau7aabd112010-01-26 10:59:06 +0100937 else if (strcmp(args[1], "timeout") == 0) {
938 if (strcmp(args[2], "cli") == 0) {
939 unsigned timeout;
940 const char *res;
941
942 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100943 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100944 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100945 return 1;
946 }
947
948 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
949 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100950 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100951 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100952 return 1;
953 }
954
955 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
956 return 1;
957 }
958 else {
Willy Tarreau295a8372011-03-10 11:25:07 +0100959 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100960 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100961 return 1;
962 }
963 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200964 else if (strcmp(args[1], "maxconn") == 0) {
965 if (strcmp(args[2], "frontend") == 0) {
966 struct proxy *px;
967 struct listener *l;
968 int v;
969
970 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
971 si->applet.ctx.cli.msg = stats_permission_denied_msg;
972 si->applet.st0 = STAT_CLI_PRINT;
973 return 1;
974 }
975
976 if (!*args[3]) {
977 si->applet.ctx.cli.msg = "Frontend name expected.\n";
978 si->applet.st0 = STAT_CLI_PRINT;
979 return 1;
980 }
981
982 px = findproxy(args[3], PR_CAP_FE);
983 if (!px) {
984 si->applet.ctx.cli.msg = "No such frontend.\n";
985 si->applet.st0 = STAT_CLI_PRINT;
986 return 1;
987 }
988
989 if (!*args[4]) {
990 si->applet.ctx.cli.msg = "Integer value expected.\n";
991 si->applet.st0 = STAT_CLI_PRINT;
992 return 1;
993 }
994
995 v = atoi(args[4]);
996 /* check for unlimited values, we restore default setting (cfg_maxpconn) */
997 if (v < 1) {
998 si->applet.ctx.cli.msg = "Value out of range.\n";
999 si->applet.st0 = STAT_CLI_PRINT;
1000 return 1;
1001 }
1002
1003 /* OK, the value is fine, so we assign it to the proxy and to all of
1004 * its listeners. The blocked ones will be dequeued.
1005 */
1006 px->maxconn = v;
1007 for (l = px->listen; l != NULL; l = l->next) {
1008 l->maxconn = v;
1009 if (l->state == LI_FULL)
1010 resume_listener(l);
1011 }
1012
1013 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1014 dequeue_all_listeners(&s->fe->listener_queue);
1015
1016 return 1;
1017 }
1018 else {
1019 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend'.\n";
1020 si->applet.st0 = STAT_CLI_PRINT;
1021 return 1;
1022 }
1023 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001024 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001025 return 0;
1026 }
1027 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001028 else if (strcmp(args[0], "enable") == 0) {
1029 if (strcmp(args[1], "server") == 0) {
1030 struct proxy *px;
1031 struct server *sv;
1032
1033 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001034 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001035 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001036 return 1;
1037 }
1038
1039 /* split "backend/server" and make <line> point to server */
1040 for (line = args[2]; *line; line++)
1041 if (*line == '/') {
1042 *line++ = '\0';
1043 break;
1044 }
1045
1046 if (!*line || !*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001047 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001048 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001049 return 1;
1050 }
1051
1052 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001053 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001054 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001055 return 1;
1056 }
1057
Cyril Bonté613f0df2011-03-03 20:49:04 +01001058 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001059 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001060 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001061 return 1;
1062 }
1063
Cyril Bontécd19e512010-01-31 22:34:03 +01001064 if (sv->state & SRV_MAINTAIN) {
1065 /* The server is really in maintenance, we can change the server state */
1066 if (sv->tracked) {
1067 /* If this server tracks the status of another one,
1068 * we must restore the good status.
1069 */
1070 if (sv->tracked->state & SRV_RUNNING) {
1071 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001072 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001073 } else {
1074 sv->state &= ~SRV_MAINTAIN;
1075 set_server_down(sv);
1076 }
1077 } else {
1078 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001079 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001080 }
1081 }
1082
1083 return 1;
1084 }
1085 else { /* unknown "enable" parameter */
1086 return 0;
1087 }
1088 }
1089 else if (strcmp(args[0], "disable") == 0) {
1090 if (strcmp(args[1], "server") == 0) {
1091 struct proxy *px;
1092 struct server *sv;
1093
1094 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001095 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001096 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001097 return 1;
1098 }
1099
1100 /* split "backend/server" and make <line> point to server */
1101 for (line = args[2]; *line; line++)
1102 if (*line == '/') {
1103 *line++ = '\0';
1104 break;
1105 }
1106
1107 if (!*line || !*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001108 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001109 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001110 return 1;
1111 }
1112
1113 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001114 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001115 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001116 return 1;
1117 }
1118
Cyril Bonté613f0df2011-03-03 20:49:04 +01001119 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001120 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001121 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001122 return 1;
1123 }
1124
Cyril Bontécd19e512010-01-31 22:34:03 +01001125 if (! (sv->state & SRV_MAINTAIN)) {
1126 /* Not already in maintenance, we can change the server state */
1127 sv->state |= SRV_MAINTAIN;
1128 set_server_down(sv);
1129 }
1130
1131 return 1;
1132 }
1133 else { /* unknown "disable" parameter */
1134 return 0;
1135 }
1136 }
1137 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001138 return 0;
1139 }
1140 return 1;
1141}
1142
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001143/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001144 * used to processes I/O from/to the stats unix socket. The system relies on a
1145 * state machine handling requests and various responses. We read a request,
1146 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001147 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1148 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001149 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001150 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001151static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001152{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001153 struct buffer *req = si->ob;
1154 struct buffer *res = si->ib;
1155 int reql;
1156 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001157
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001158 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1159 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001160
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001161 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001162 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001163 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001164 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001165 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001166 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001167 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001168 /* Let's close for real now. We just close the request
1169 * side, the conditions below will complete if needed.
1170 */
1171 si->shutw(si);
1172 break;
1173 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001174 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001175 /* ensure we have some output room left in the event we
1176 * would want to return some info right after parsing.
1177 */
1178 if (buffer_almost_full(si->ib))
1179 break;
1180
Willy Tarreau74b08c92010-09-08 17:04:31 +02001181 reql = buffer_get_line(si->ob, trash, sizeof(trash));
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001182 if (reql <= 0) { /* closed or EOL not found */
1183 if (reql == 0)
1184 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001185 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001186 continue;
1187 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001188
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001189 /* seek for a possible semi-colon. If we find one, we
1190 * replace it with an LF and skip only this part.
1191 */
1192 for (len = 0; len < reql; len++)
1193 if (trash[len] == ';') {
1194 trash[len] = '\n';
1195 reql = len + 1;
1196 break;
1197 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001198
Willy Tarreau816fc222009-10-04 07:36:58 +02001199 /* now it is time to check that we have a full line,
1200 * remove the trailing \n and possibly \r, then cut the
1201 * line.
1202 */
1203 len = reql - 1;
1204 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001205 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001206 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001207 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001208
Willy Tarreau816fc222009-10-04 07:36:58 +02001209 if (len && trash[len-1] == '\r')
1210 len--;
1211
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001212 trash[len] = '\0';
1213
Willy Tarreaubc4af052011-02-13 13:25:14 +01001214 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001215 if (len) {
1216 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001217 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001218 continue;
1219 }
1220 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001221 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001222 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001223 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001224 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001225 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001226 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001227 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001228 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001229 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001230 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001231 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001232 /* if prompt is disabled, print help on empty lines,
1233 * so that the user at least knows how to enable
1234 * prompt and find help.
1235 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001236 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001237 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001238 }
1239
1240 /* re-adjust req buffer */
1241 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001242 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001243 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001244 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001245 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001246 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001247 continue;
1248 }
1249
Willy Tarreaubc4af052011-02-13 13:25:14 +01001250 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001251 case STAT_CLI_PRINT:
Willy Tarreau295a8372011-03-10 11:25:07 +01001252 if (buffer_feed(si->ib, si->applet.ctx.cli.msg) < 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001253 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001254 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001255 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001256 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001257 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001258 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001259 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001260 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001261 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001262 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001263 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001264 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001265 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001266 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001267 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001268 if (stats_table_request(si, true))
1269 si->applet.st0 = STAT_CLI_PROMPT;
1270 break;
1271 case STAT_CLI_O_CLR:
1272 if (stats_table_request(si, false))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001273 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001274 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001275 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001276 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001277 break;
1278 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001279
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001280 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001281 if (si->applet.st0 == STAT_CLI_PROMPT) {
1282 if (buffer_feed(si->ib, si->applet.st1 ? "\n> " : "\n") < 0)
1283 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001284 }
1285
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001286 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001287 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001288 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001289
1290 /* Now we close the output if one of the writers did so,
1291 * or if we're not in interactive mode and the request
1292 * buffer is empty. This still allows pipelined requests
1293 * to be sent in non-interactive mode.
1294 */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001295 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->applet.st1 && !req->send_max)) {
1296 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001297 continue;
1298 }
1299
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001300 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001301 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001302 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001303 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001304
Willy Tarreaubc4af052011-02-13 13:25:14 +01001305 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001306 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1307 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1308 /* Other size has closed, let's abort if we have no more processing to do
1309 * and nothing more to consume. This is comparable to a broken pipe, so
1310 * we forward the close to the request side so that it flows upstream to
1311 * the client.
1312 */
1313 si->shutw(si);
1314 }
1315
Willy Tarreaubc4af052011-02-13 13:25:14 +01001316 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001317 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1318 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1319 /* We have no more processing to do, and nothing more to send, and
1320 * the client side has closed. So we'll forward this state downstream
1321 * on the response buffer.
1322 */
1323 si->shutr(si);
1324 res->flags |= BF_READ_NULL;
1325 }
1326
1327 /* update all other flags and resync with the other side */
1328 si->update(si);
1329
1330 /* we don't want to expire timeouts while we're processing requests */
1331 si->ib->rex = TICK_ETERNITY;
1332 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001333
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001334 out:
1335 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
1336 __FUNCTION__, __LINE__,
1337 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
1338
1339 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1340 /* check that we have released everything then unregister */
1341 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001342 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001343}
1344
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001345/* This function dumps statistics onto the stream interface's read buffer.
1346 * The data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001347 * It returns 0 as long as it does not complete, non-zero upon completion.
1348 * Some states are not used but it makes the code more similar to other
1349 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001350 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001351static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001352{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001353 struct proxy *px;
1354 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001355 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001356
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001357 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +02001358
Willy Tarreau295a8372011-03-10 11:25:07 +01001359 switch (si->applet.state) {
1360 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001361 /* the function had not been called yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001362 si->applet.state = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001363 /* fall through */
1364
Willy Tarreau295a8372011-03-10 11:25:07 +01001365 case STAT_ST_HEAD:
1366 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001367 print_csv_header(&msg);
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001368 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001369 return 0;
1370 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001371
Willy Tarreau295a8372011-03-10 11:25:07 +01001372 si->applet.state = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001373 /* fall through */
1374
Willy Tarreau295a8372011-03-10 11:25:07 +01001375 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001376 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001377 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001378 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001379 "Name: " PRODUCT_NAME "\n"
1380 "Version: " HAPROXY_VERSION "\n"
1381 "Release_date: " HAPROXY_DATE "\n"
1382 "Nbproc: %d\n"
1383 "Process_num: %d\n"
1384 "Pid: %d\n"
1385 "Uptime: %dd %dh%02dm%02ds\n"
1386 "Uptime_sec: %d\n"
1387 "Memmax_MB: %d\n"
1388 "Ulimit-n: %d\n"
1389 "Maxsock: %d\n"
1390 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001391 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001392 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001393 "PipesUsed: %d\n"
1394 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001395 "Tasks: %d\n"
1396 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001397 "node: %s\n"
1398 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001399 "",
1400 global.nbproc,
1401 relative_pid,
1402 pid,
1403 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1404 up,
1405 global.rlimit_memmax,
1406 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001407 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001408 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001409 nb_tasks_cur, run_queue_cur,
1410 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001411 );
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001412 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001413 return 0;
1414 }
1415
Willy Tarreau295a8372011-03-10 11:25:07 +01001416 si->applet.ctx.stats.px = proxy;
1417 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1418 si->applet.ctx.stats.sv = NULL;
1419 si->applet.state = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001420 /* fall through */
1421
Willy Tarreau295a8372011-03-10 11:25:07 +01001422 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001423 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001424 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1425 while (si->applet.ctx.stats.px) {
1426 px = si->applet.ctx.stats.px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001427 /* skip the disabled proxies and non-networked ones */
1428 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001429 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001430 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001431 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001432 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001433
Willy Tarreau295a8372011-03-10 11:25:07 +01001434 si->applet.ctx.stats.px = px->next;
1435 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001436 }
1437 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001438 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001439
Willy Tarreau295a8372011-03-10 11:25:07 +01001440 si->applet.state = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001441 /* fall through */
1442
Willy Tarreau295a8372011-03-10 11:25:07 +01001443 case STAT_ST_END:
1444 si->applet.state = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001445 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001446
Willy Tarreau295a8372011-03-10 11:25:07 +01001447 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001448 return 1;
1449
1450 default:
1451 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001452 si->applet.state = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001453 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001454 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001455}
1456
1457
Cyril Bonté70be45d2010-10-12 00:14:35 +02001458/* We don't want to land on the posted stats page because a refresh will
1459 * repost the data. We don't want this to happen on accident so we redirect
1460 * the browse to the stats page with a GET.
1461 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001462static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001463{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001464 struct session *s = si->applet.private;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001465 struct chunk msg;
1466
1467 chunk_init(&msg, trash, sizeof(trash));
1468
Willy Tarreau295a8372011-03-10 11:25:07 +01001469 switch (si->applet.state) {
1470 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001471 chunk_printf(&msg,
1472 "HTTP/1.0 303 See Other\r\n"
1473 "Cache-Control: no-cache\r\n"
1474 "Content-Type: text/plain\r\n"
1475 "Connection: close\r\n"
1476 "Location: %s;st=%s",
Willy Tarreau295a8372011-03-10 11:25:07 +01001477 uri->uri_prefix, si->applet.ctx.stats.st_code);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001478 chunk_printf(&msg, "\r\n\r\n");
1479
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001480 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001481 return 0;
1482
1483 s->txn.status = 303;
1484
1485 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1486 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1487 if (!(s->flags & SN_FINST_MASK))
1488 s->flags |= SN_FINST_R;
1489
Willy Tarreau295a8372011-03-10 11:25:07 +01001490 si->applet.state = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001491 return 1;
1492 }
1493 return 1;
1494}
1495
1496
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001497/* This I/O handler runs as an applet embedded in a stream interface. It is
1498 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001499 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001500 * automatically unregisters itself once transfer is complete.
1501 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001502static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001503{
Willy Tarreaubc4af052011-02-13 13:25:14 +01001504 struct session *s = si->applet.private;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001505 struct buffer *req = si->ob;
1506 struct buffer *res = si->ib;
1507
1508 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1509 goto out;
1510
1511 /* check that the output is not closed */
1512 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001513 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001514
Willy Tarreaubc4af052011-02-13 13:25:14 +01001515 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001516 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001517 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001518 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001519 si->shutw(si);
1520 }
1521 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001522 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001523 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001524 si->shutw(si);
1525 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001526 }
1527 }
1528
1529 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
1530 si->shutw(si);
1531
Willy Tarreaubc4af052011-02-13 13:25:14 +01001532 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001533 si->shutr(si);
1534 res->flags |= BF_READ_NULL;
1535 }
1536
1537 /* update all other flags and resync with the other side */
1538 si->update(si);
1539
1540 /* we don't want to expire timeouts while we're processing requests */
1541 si->ib->rex = TICK_ETERNITY;
1542 si->ob->wex = TICK_ETERNITY;
1543
1544 out:
1545 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1546 /* check that we have released everything then unregister */
1547 stream_int_unregister_handler(si);
1548 }
1549}
1550
1551
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001552/* This function dumps statistics in HTTP format onto the stream interface's
1553 * read buffer. The data_ctx must have been zeroed first, and the flags
1554 * properly set. It returns 0 if it had to stop writing data and an I/O is
1555 * needed, 1 if the dump is finished and the session must be closed, or -1
1556 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001557 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001558static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001559{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001560 struct session *s = si->applet.private;
1561 struct buffer *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001562 struct proxy *px;
1563 struct chunk msg;
1564 unsigned int up;
1565
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001566 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001567
Willy Tarreau295a8372011-03-10 11:25:07 +01001568 switch (si->applet.state) {
1569 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001570 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001571 "HTTP/1.0 200 OK\r\n"
1572 "Cache-Control: no-cache\r\n"
1573 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001574 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001575 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001576
Willy Tarreau295a8372011-03-10 11:25:07 +01001577 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001578 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001579 uri->refresh);
1580
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001581 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001582
1583 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001584 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001585 return 0;
1586
Willy Tarreau91861262007-10-17 17:06:05 +02001587 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1588 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1589 if (!(s->flags & SN_FINST_MASK))
1590 s->flags |= SN_FINST_R;
1591
1592 if (s->txn.meth == HTTP_METH_HEAD) {
1593 /* that's all we return in case of HEAD request */
Willy Tarreau295a8372011-03-10 11:25:07 +01001594 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001595 return 1;
1596 }
1597
Willy Tarreau295a8372011-03-10 11:25:07 +01001598 si->applet.state = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001599 /* fall through */
1600
Willy Tarreau295a8372011-03-10 11:25:07 +01001601 case STAT_ST_HEAD:
1602 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001603 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001604 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001605 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1606 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001607 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001608 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1609 "<style type=\"text/css\"><!--\n"
1610 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001611 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001612 " font-size: 12px;"
1613 " font-weight: normal;"
1614 " color: black;"
1615 " background: white;"
1616 "}\n"
1617 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001618 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001619 "}\n"
1620 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001621 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001622 " margin-bottom: 0.5em;"
1623 "}\n"
1624 "h2 {"
1625 " font-family: helvetica, arial;"
1626 " font-size: x-large;"
1627 " font-weight: bold;"
1628 " font-style: italic;"
1629 " color: #6020a0;"
1630 " margin-top: 0em;"
1631 " margin-bottom: 0em;"
1632 "}\n"
1633 "h3 {"
1634 " font-family: helvetica, arial;"
1635 " font-size: 16px;"
1636 " font-weight: bold;"
1637 " color: #b00040;"
1638 " background: #e8e8d0;"
1639 " margin-top: 0em;"
1640 " margin-bottom: 0em;"
1641 "}\n"
1642 "li {"
1643 " margin-top: 0.25em;"
1644 " margin-right: 2em;"
1645 "}\n"
1646 ".hr {margin-top: 0.25em;"
1647 " border-color: black;"
1648 " border-bottom-style: solid;"
1649 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001650 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001651 ".total {background: #20D0D0;color: #ffff80;}\n"
1652 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001653 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001654 ".backend {background: #e8e8d0;}\n"
1655 ".active0 {background: #ff9090;}\n"
1656 ".active1 {background: #ffd020;}\n"
1657 ".active2 {background: #ffffa0;}\n"
1658 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001659 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1660 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1661 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001662 ".backup0 {background: #ff9090;}\n"
1663 ".backup1 {background: #ff80ff;}\n"
1664 ".backup2 {background: #c060ff;}\n"
1665 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001666 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1667 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1668 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001669 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001670 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001671 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001672 "a.px:link {color: #ffff40; text-decoration: none;}"
1673 "a.px:visited {color: #ffff40; text-decoration: none;}"
1674 "a.px:hover {color: #ffffff; text-decoration: none;}"
1675 "a.lfsb:link {color: #000000; text-decoration: none;}"
1676 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1677 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1678 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001679 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001680 "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"
1681 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001682 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001683 "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 +02001684 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001685 "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 +02001686 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001687 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1688 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1689 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001690 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001691 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001692 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001693 (uri->flags&ST_SHNODE) ? " on " : "",
1694 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001695 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001696 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001697 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001698 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001699 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001700 return 0;
1701
Willy Tarreau295a8372011-03-10 11:25:07 +01001702 si->applet.state = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02001703 /* fall through */
1704
Willy Tarreau295a8372011-03-10 11:25:07 +01001705 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02001706 up = (now.tv_sec - start_date.tv_sec);
1707
1708 /* WARNING! this has to fit the first packet too.
1709 * We are around 3.5 kB, add adding entries will
1710 * become tricky if we want to support 4kB buffers !
1711 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001712 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001713 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001714 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1715 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001716 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001717 "<hr width=\"100%%\" class=\"hr\">\n"
1718 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001719 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001720 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001721 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001722 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1723 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
1724 "current conns = %d; current pipes = %d/%d<br>\n"
1725 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001726 "</td><td align=\"center\" nowrap>\n"
1727 "<table class=\"lgd\"><tr>\n"
1728 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1729 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1730 "</tr><tr>\n"
1731 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1732 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1733 "</tr><tr>\n"
1734 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1735 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1736 "</tr><tr>\n"
1737 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001738 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001739 "</tr><tr>\n"
1740 "<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 +02001741 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001742 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001743 "</td>"
1744 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1745 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1746 "",
1747 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001748 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1749 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1750 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001751 up / 86400, (up % 86400) / 3600,
1752 (up % 3600) / 60, (up % 60),
1753 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1754 global.rlimit_memmax ? " MB" : "",
1755 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001756 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001757 actconn, pipes_used, pipes_used+pipes_free,
1758 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +02001759 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001760
Willy Tarreau295a8372011-03-10 11:25:07 +01001761 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001762 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001763 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1764 uri->uri_prefix,
1765 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01001766 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001767 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001768 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001769 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1770 uri->uri_prefix,
1771 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01001772 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001773
Willy Tarreau55bb8452007-10-17 18:44:57 +02001774 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001775 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001776 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001777 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1778 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001779 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001780 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001781 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001782 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001783 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1784 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001785 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001786 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001787 }
Willy Tarreau91861262007-10-17 17:06:05 +02001788
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001789 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001790 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
1791 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001792 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
1793 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001794
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001795 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02001796 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
1797 uri->uri_prefix,
1798 (uri->refresh > 0) ? ";norefresh" : "");
1799
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001800 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001801 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001802 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1803 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
1804 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
1805 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
1806 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
1807 "</ul>"
1808 "</td>"
1809 "</tr></table>\n"
1810 ""
1811 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001812
Willy Tarreau295a8372011-03-10 11:25:07 +01001813 if (si->applet.ctx.stats.st_code) {
1814 if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001815 chunk_printf(&msg,
1816 "<p><div class=active3>"
1817 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1818 "Action processed successfully."
1819 "</div>\n", uri->uri_prefix);
1820 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001821 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_NONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001822 chunk_printf(&msg,
1823 "<p><div class=active2>"
1824 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1825 "Nothing has changed."
1826 "</div>\n", uri->uri_prefix);
1827 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001828 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_EXCD) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001829 chunk_printf(&msg,
1830 "<p><div class=active0>"
1831 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1832 "<b>Action not processed : the buffer couldn't store all the data.<br>"
1833 "You should retry with less servers at a time.</b>"
1834 "</div>\n", uri->uri_prefix);
1835 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001836 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DENY) == 0) {
Cyril Bonté474be412010-10-12 00:14:36 +02001837 chunk_printf(&msg,
1838 "<p><div class=active0>"
1839 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1840 "<b>Action denied.</b>"
1841 "</div>\n", uri->uri_prefix);
1842 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001843 else {
1844 chunk_printf(&msg,
1845 "<p><div class=active6>"
1846 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1847 "Unexpected result."
1848 "</div>\n", uri->uri_prefix);
1849 }
1850 chunk_printf(&msg,"<p>\n");
1851 }
1852
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001853 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001854 return 0;
1855 }
Willy Tarreau91861262007-10-17 17:06:05 +02001856
Willy Tarreau295a8372011-03-10 11:25:07 +01001857 si->applet.ctx.stats.px = proxy;
1858 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1859 si->applet.state = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02001860 /* fall through */
1861
Willy Tarreau295a8372011-03-10 11:25:07 +01001862 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02001863 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001864 while (si->applet.ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001865 if (buffer_almost_full(rep))
1866 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01001867 px = si->applet.ctx.stats.px;
Willy Tarreau91861262007-10-17 17:06:05 +02001868 /* skip the disabled proxies and non-networked ones */
1869 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001870 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001871 return 0;
1872
Willy Tarreau295a8372011-03-10 11:25:07 +01001873 si->applet.ctx.stats.px = px->next;
1874 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02001875 }
1876 /* here, we just have reached the last proxy */
1877
Willy Tarreau295a8372011-03-10 11:25:07 +01001878 si->applet.state = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02001879 /* fall through */
1880
Willy Tarreau295a8372011-03-10 11:25:07 +01001881 case STAT_ST_END:
1882 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001883 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001884 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001885 return 0;
1886 }
Willy Tarreau91861262007-10-17 17:06:05 +02001887
Willy Tarreau295a8372011-03-10 11:25:07 +01001888 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001889 /* fall through */
1890
Willy Tarreau295a8372011-03-10 11:25:07 +01001891 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02001892 return 1;
1893
1894 default:
1895 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001896 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001897 return -1;
1898 }
1899}
1900
1901
1902/*
1903 * Dumps statistics for a proxy.
1904 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1905 * ot non-zero if everything completed.
1906 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001907static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001908{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001909 struct session *s = si->applet.private;
1910 struct buffer *rep = si->ib;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001911 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001912 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02001913 struct chunk msg;
1914
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001915 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001916
Willy Tarreau295a8372011-03-10 11:25:07 +01001917 switch (si->applet.ctx.stats.px_st) {
1918 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02001919 /* we are on a new proxy */
1920
1921 if (uri && uri->scope) {
1922 /* we have a limited scope, we have to check the proxy name */
1923 struct stat_scope *scope;
1924 int len;
1925
1926 len = strlen(px->id);
1927 scope = uri->scope;
1928
1929 while (scope) {
1930 /* match exact proxy name */
1931 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1932 break;
1933
1934 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001935 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001936 break;
1937 scope = scope->next;
1938 }
1939
1940 /* proxy name not found : don't dump anything */
1941 if (scope == NULL)
1942 return 1;
1943 }
1944
Willy Tarreau295a8372011-03-10 11:25:07 +01001945 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
1946 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001947 return 1;
1948
Willy Tarreau295a8372011-03-10 11:25:07 +01001949 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02001950 /* fall through */
1951
Willy Tarreau295a8372011-03-10 11:25:07 +01001952 case STAT_PX_ST_TH:
1953 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
1954 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001955 /* A form to enable/disable this proxy servers */
1956 chunk_printf(&msg,
1957 "<form action=\"%s\" method=\"post\">",
1958 uri->uri_prefix);
1959 }
1960
Willy Tarreau55bb8452007-10-17 18:44:57 +02001961 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001962 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001963 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001964 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001965 "<th class=\"pxname\" width=\"10%%\"");
1966
1967 if (uri->flags&ST_SHLGNDS) {
1968 /* cap, mode, id */
1969 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
1970 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1971 px->uuid);
1972
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001973 chunk_printf(&msg, "\"");
1974 }
1975
1976 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001977 ">%s<a name=\"%s\"></a>"
1978 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001979 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001980 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001981 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001982 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001983 "<tr class=\"titre\">",
1984 (uri->flags & ST_SHLGNDS)?"<u>":"",
1985 px->id, px->id, px->id,
1986 (uri->flags & ST_SHLGNDS)?"</u>":"",
1987 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1988
Willy Tarreau295a8372011-03-10 11:25:07 +01001989 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001990 /* Column heading for Enable or Disable server */
1991 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
1992 }
1993
1994 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001995 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001996 "<th colspan=3>Queue</th>"
1997 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001998 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001999 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002000 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002001 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002002 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002003 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002004 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002005 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002006 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002007 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002008 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002009 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2010 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002011 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002012
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002013 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002014 return 0;
2015 }
Willy Tarreau91861262007-10-17 17:06:05 +02002016
Willy Tarreau295a8372011-03-10 11:25:07 +01002017 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002018 /* fall through */
2019
Willy Tarreau295a8372011-03-10 11:25:07 +01002020 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002021 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002022 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002023 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
2024 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002025 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002026 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002027 "<tr class=\"frontend\">");
2028
Willy Tarreau295a8372011-03-10 11:25:07 +01002029 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002030 /* Column sub-heading for Enable or Disable server */
2031 chunk_printf(&msg, "<td></td>");
2032 }
2033
2034 chunk_printf(&msg,
2035 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002036 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002037 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2038 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002039 "",
2040 px->id, px->id);
2041
2042 if (px->mode == PR_MODE_HTTP) {
2043 chunk_printf(&msg,
2044 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01002045 "<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 +01002046 "",
2047 read_freq_ctr(&px->fe_req_per_sec),
2048 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002049 px->fe_counters.p.http.rps_max,
2050 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01002051 LIM2A2(px->fe_sps_lim, "-"));
2052 } else {
2053 chunk_printf(&msg,
2054 /* sessions rate : current, max, limit */
2055 "<td>%s</td><td>%s</td><td>%s</td>"
2056 "",
2057 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002058 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01002059 }
2060
2061 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002062 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002063 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002064 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002065 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002066 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002067
2068 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2069 if (px->mode == PR_MODE_HTTP) {
2070 int i;
2071
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002072 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002073
2074 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002075 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002076
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002077 chunk_printf(&msg, " other=%lld\"", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002078 }
2079
2080 chunk_printf(&msg,
2081 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002082 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002083 /* bytes : in, out */
2084 "<td>%s</td><td>%s</td>"
2085 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002086 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002087 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002088 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002089 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002090
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002091 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002092 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002093 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002094 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002095 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002096 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002097 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002098 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002099 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002100 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002101 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002102 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002103 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2104 U2H2(px->fe_counters.failed_req),
Willy Tarreau562515c2011-07-25 08:11:52 +02002105 px->state == PR_STREADY ? "OPEN" :
2106 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002107 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002108 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002109 /* pxid, name, queue cur, queue max, */
2110 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002111 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002112 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002113 /* bytes : in, out */
2114 "%lld,%lld,"
2115 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002116 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002117 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002118 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002119 /* warnings: retries, redispatches */
2120 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002121 /* server status : reflect frontend status */
2122 "%s,"
2123 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002124 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002125 /* pid, iid, sid, throttle, lbtot, tracked, type */
2126 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002127 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002128 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002129 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002130 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002131 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002132 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2133 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2134 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2135 px->fe_counters.failed_req,
Willy Tarreau562515c2011-07-25 08:11:52 +02002136 px->state == PR_STREADY ? "OPEN" :
2137 px->state == PR_STFULL ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002138 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002139 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002140 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002141
2142 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2143 if (px->mode == PR_MODE_HTTP) {
2144 int i;
2145
2146 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002147 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002148
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002149 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002150 } else {
2151 chunk_printf(&msg, ",,,,,,");
2152 }
2153
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002154 /* failed health analyses */
2155 chunk_printf(&msg, ",");
2156
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002157 /* requests : req_rate, req_rate_max, req_tot, */
2158 chunk_printf(&msg, "%u,%u,%lld,",
2159 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002160 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002161
Willy Tarreauae526782010-03-04 20:34:23 +01002162 /* errors: cli_aborts, srv_aborts */
2163 chunk_printf(&msg, ",,");
2164
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002165 /* finish with EOL */
2166 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002167 }
Willy Tarreau91861262007-10-17 17:06:05 +02002168
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002169 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002170 return 0;
2171 }
2172
Willy Tarreau295a8372011-03-10 11:25:07 +01002173 si->applet.ctx.stats.l = px->listen; /* may be NULL */
2174 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002175 /* fall through */
2176
Willy Tarreau295a8372011-03-10 11:25:07 +01002177 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002178 /* stats.l has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002179 for (; si->applet.ctx.stats.l != NULL; si->applet.ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002180 if (buffer_almost_full(rep))
2181 return 0;
2182
Willy Tarreau295a8372011-03-10 11:25:07 +01002183 l = si->applet.ctx.stats.l;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002184 if (!l->counters)
2185 continue;
2186
Willy Tarreau295a8372011-03-10 11:25:07 +01002187 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2188 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002189 break;
2190
Willy Tarreau295a8372011-03-10 11:25:07 +01002191 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002192 continue;
2193 }
2194
Willy Tarreau295a8372011-03-10 11:25:07 +01002195 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002196 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002197 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002198 /* Column sub-heading for Enable or Disable server */
2199 chunk_printf(&msg, "<td></td>");
2200 }
2201 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002202
2203 if (uri->flags&ST_SHLGNDS) {
2204 char str[INET6_ADDRSTRLEN], *fmt = NULL;
2205 int port;
2206
2207 chunk_printf(&msg, " title=\"IP: ");
2208
2209 port = (l->addr.ss_family == AF_INET6)
2210 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
2211 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
2212
2213 if (l->addr.ss_family == AF_INET) {
2214 if (inet_ntop(AF_INET,
2215 (const void *)&((struct sockaddr_in *)&l->addr)->sin_addr,
2216 str, sizeof(str)))
2217 fmt = "%s:%d";
2218 } else {
2219 if (inet_ntop(AF_INET6,
2220 (const void *)&((struct sockaddr_in6 *)(&l->addr))->sin6_addr,
2221 str, sizeof(str)))
2222 fmt = "[%s]:%d";
2223 }
2224
2225 if (fmt)
2226 chunk_printf(&msg, fmt, str, port);
2227 else
2228 chunk_printf(&msg, "(%s)", strerror(errno));
2229
2230 /* id */
2231 chunk_printf(&msg, ", id: %d", l->luid);
2232
2233 chunk_printf(&msg, "\"");
2234 }
2235
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002236 chunk_printf(&msg,
2237 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002238 ">%s<a name=\"%s/+%s\"></a>"
2239 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002240 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002241 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002242 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002243 "<td>%s</td><td>%s</td><td>%s</td>"
2244 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002245 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002246 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002247 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002248 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002249 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002250 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002251 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2252 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2253
2254 chunk_printf(&msg,
2255 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002256 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002257 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002258 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002259 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002260 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002261 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002262 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002263 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002264 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002265 "",
2266 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2267 U2H2(l->counters->failed_req),
Willy Tarreaua17c2d92011-07-25 08:16:20 +02002268 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002269 } else {
2270 chunk_printf(&msg,
2271 /* pxid, name, queue cur, queue max, */
2272 "%s,%s,,,"
2273 /* sessions: current, max, limit, total */
2274 "%d,%d,%d,%lld,"
2275 /* bytes: in, out */
2276 "%lld,%lld,"
2277 /* denied: req, resp */
2278 "%lld,%lld,"
2279 /* errors: request, connect, response */
2280 "%lld,,,"
2281 /* warnings: retries, redispatches */
2282 ",,"
2283 /* server status: reflect listener status */
2284 "%s,"
2285 /* rest of server: nothing */
2286 ",,,,,,,,"
2287 /* pid, iid, sid, throttle, lbtot, tracked, type */
2288 "%d,%d,%d,,,,%d,"
2289 /* rate, rate_lim, rate_max */
2290 ",,,"
2291 /* check_status, check_code, check_duration */
2292 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002293 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2294 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002295 /* failed health analyses */
2296 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002297 /* requests : req_rate, req_rate_max, req_tot, */
2298 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002299 /* errors: cli_aborts, srv_aborts */
2300 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002301 "\n",
2302 px->id, l->name,
2303 l->nbconn, l->counters->conn_max,
2304 l->maxconn, l->counters->cum_conn,
2305 l->counters->bytes_in, l->counters->bytes_out,
2306 l->counters->denied_req, l->counters->denied_resp,
2307 l->counters->failed_req,
2308 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2309 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2310 }
2311
2312 if (buffer_feed_chunk(rep, &msg) >= 0)
2313 return 0;
2314 }
2315
Willy Tarreau295a8372011-03-10 11:25:07 +01002316 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2317 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002318 /* fall through */
2319
Willy Tarreau295a8372011-03-10 11:25:07 +01002320 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002321 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002322 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002323 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 +02002324
Willy Tarreau4e33d862009-10-11 23:35:10 +02002325 if (buffer_almost_full(rep))
2326 return 0;
2327
Willy Tarreau295a8372011-03-10 11:25:07 +01002328 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002329
Willy Tarreau295a8372011-03-10 11:25:07 +01002330 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2331 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002332 break;
2333
Willy Tarreau295a8372011-03-10 11:25:07 +01002334 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002335 continue;
2336 }
2337
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002338 if (sv->tracked)
2339 svs = sv->tracked;
2340 else
2341 svs = sv;
2342
Willy Tarreau91861262007-10-17 17:06:05 +02002343 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002344 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002345 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002346 else if (svs->state & SRV_RUNNING) {
2347 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002348 sv_state = 3; /* UP */
2349 else
2350 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002351
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002352 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002353 sv_state += 2;
2354 }
Willy Tarreau91861262007-10-17 17:06:05 +02002355 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002356 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002357 sv_state = 1; /* going up */
2358 else
2359 sv_state = 0; /* DOWN */
2360
Willy Tarreau295a8372011-03-10 11:25:07 +01002361 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002362 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002363 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002364 continue;
2365 }
2366
Willy Tarreau295a8372011-03-10 11:25:07 +01002367 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002368 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2369 "UP %d/%d &darr;", "UP",
2370 "NOLB %d/%d &darr;", "NOLB",
2371 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002372 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002373 chunk_printf(&msg,
2374 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002375 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002376 );
2377 }
2378 else {
2379 chunk_printf(&msg,
2380 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002381 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002382 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2383 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002384
Willy Tarreau295a8372011-03-10 11:25:07 +01002385 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002386 chunk_printf(&msg,
2387 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2388 sv->id);
2389 }
2390
2391 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002392
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002393 if (uri->flags&ST_SHLGNDS) {
2394 char str[INET6_ADDRSTRLEN];
2395
2396 chunk_printf(&msg, " title=\"IP: ");
2397
2398 /* IP */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002399 switch (sv->addr.ss_family) {
2400 case AF_INET:
2401 if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&sv->addr)->sin_addr, str, sizeof(str)))
2402 chunk_printf(&msg, "%s:%d", str, htons(((struct sockaddr_in *)&sv->addr)->sin_port));
2403 else
2404 chunk_printf(&msg, "(%s)", strerror(errno));
2405 break;
2406 case AF_INET6:
2407 if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&sv->addr)->sin6_addr, str, sizeof(str)))
2408 chunk_printf(&msg, "%s:%d", str, htons(((struct sockaddr_in6 *)&sv->addr)->sin6_port));
2409 else
2410 chunk_printf(&msg, "(%s)", strerror(errno));
2411 break;
2412 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002413
2414 /* id */
2415 chunk_printf(&msg, ", id: %d", sv->puid);
2416
2417 /* cookie */
2418 if (sv->cookie) {
2419 struct chunk src;
2420
2421 chunk_printf(&msg, ", cookie: '");
2422
2423 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2424 chunk_htmlencode(&msg, &src);
2425
2426 chunk_printf(&msg, "'");
2427 }
2428
2429 chunk_printf(&msg, "\"");
2430 }
2431
2432 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002433 ">%s<a name=\"%s/%s\"></a>"
2434 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002435 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002436 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002437 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002438 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002439 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002440 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002441 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002442 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002443 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002444 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002445 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002446 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2447 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002448 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2449
2450 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2451 if (px->mode == PR_MODE_HTTP) {
2452 int i;
2453
2454 chunk_printf(&msg, " title=\"rsp codes:");
2455
2456 for (i = 1; i < 6; i++)
2457 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2458
2459 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2460 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002461
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002462 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002463 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002464 ">%s%s%s</td><td>%s</td>",
2465 (px->mode == PR_MODE_HTTP)?"<u>":"",
2466 U2H0(sv->counters.cum_sess),
2467 (px->mode == PR_MODE_HTTP)?"</u>":"",
2468 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002469
2470 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002471 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002472 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002473 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002474 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002475 /* errors : request, connect */
2476 "<td></td><td>%s</td>"
2477 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002478 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002479 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002480 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002481 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002482 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2483 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002484 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002485 sv->counters.cli_aborts,
2486 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002487 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002488 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002489
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002490 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002491 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002492
Cyril Bontécd19e512010-01-31 22:34:03 +01002493 if (sv->state & SRV_MAINTAIN) {
2494 chunk_printf(&msg, "%s ",
2495 human_time(now.tv_sec - sv->last_change, 1));
2496 chunk_printf(&msg, "MAINT");
2497 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002498 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2499 chunk_printf(&msg, "%s ",
2500 human_time(now.tv_sec - svs->last_change, 1));
2501 chunk_printf(&msg, "MAINT(via)");
2502 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002503 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002504 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002505 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002506
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002507 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002508 srv_hlt_st[sv_state],
2509 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2510 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002511 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002512
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002513 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002514 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002515 get_check_status_description(sv->check_status));
2516
2517 if (*sv->check_desc) {
2518 struct chunk src;
2519
2520 chunk_printf(&msg, ": ");
2521
2522 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2523 chunk_htmlencode(&msg, &src);
2524 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002525
Willy Tarreaue0454092010-02-26 12:29:07 +01002526 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002527 tv_iszero(&sv->check_start)?"":"* ",
2528 get_check_status_info(sv->check_status));
2529
2530 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002531 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002532
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002533 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002534 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002535 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002536 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002537
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002538 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002539 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002540 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002541 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002542 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002543 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002544 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002545 (sv->state & SRV_BACKUP) ? "-" : "Y",
2546 (sv->state & SRV_BACKUP) ? "Y" : "-");
2547
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002548 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002549 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002550 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002551 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2552
2553 if (svs->observe)
2554 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2555
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002556 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002557 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002558 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002559 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002560 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002561 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002562 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002563 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2564 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002565 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002566 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002567 "<td colspan=3></td>");
2568
2569 /* throttle */
2570 if ((sv->state & SRV_WARMINGUP) &&
2571 now.tv_sec < sv->last_change + sv->slowstart &&
2572 now.tv_sec >= sv->last_change) {
2573 unsigned int ratio;
2574 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002575 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002576 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002577 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002578 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002579 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002580 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002581 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002582 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2583 "UP %d/%d,", "UP,",
2584 "NOLB %d/%d,", "NOLB,",
2585 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002586 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002587 /* pxid, name */
2588 "%s,%s,"
2589 /* queue : current, max */
2590 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002591 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002592 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002593 /* bytes : in, out */
2594 "%lld,%lld,"
2595 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002596 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002597 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002598 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002599 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002600 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002601 "",
2602 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002603 sv->nbpend, sv->counters.nbpend_max,
2604 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002605 sv->counters.bytes_in, sv->counters.bytes_out,
2606 sv->counters.failed_secu,
2607 sv->counters.failed_conns, sv->counters.failed_resp,
2608 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002609
Willy Tarreau55bb8452007-10-17 18:44:57 +02002610 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002611 if (sv->state & SRV_MAINTAIN) {
2612 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002613 }
2614 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2615 chunk_printf(&msg, "MAINT(via),");
2616 }
2617 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002618 chunk_printf(&msg,
2619 srv_hlt_st[sv_state],
2620 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2621 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2622 }
Willy Tarreau91861262007-10-17 17:06:05 +02002623
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002624 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002625 /* weight, active, backup */
2626 "%d,%d,%d,"
2627 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002628 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002629 (sv->state & SRV_BACKUP) ? 0 : 1,
2630 (sv->state & SRV_BACKUP) ? 1 : 0);
2631
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002632 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002633 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002634 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002635 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002636 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002637 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002638 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002639 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002640 ",,,,");
2641
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002642 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002643 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002644 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002645 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002646 LIM2A0(sv->maxqueue, ""),
2647 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002648
2649 /* throttle */
2650 if ((sv->state & SRV_WARMINGUP) &&
2651 now.tv_sec < sv->last_change + sv->slowstart &&
2652 now.tv_sec >= sv->last_change) {
2653 unsigned int ratio;
2654 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002655 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002656 }
2657
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002658 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002659 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002660
2661 /* tracked */
2662 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002663 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002664 sv->tracked->proxy->id, sv->tracked->id);
2665 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002666 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002667
Willy Tarreau7f062c42009-03-05 18:43:00 +01002668 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002669 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002670
2671 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002672 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002673 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002674 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002675
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002676 if (sv->state & SRV_CHECKED) {
2677 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002678 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002679
2680 /* check_code */
2681 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002682 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002683 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002684 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002685
2686 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002687 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002688 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002689 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002690 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002691
2692 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002693 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002694 }
2695
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002696 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2697 if (px->mode == PR_MODE_HTTP) {
2698 int i;
2699
2700 for (i=1; i<6; i++)
2701 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2702
2703 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2704 } else {
2705 chunk_printf(&msg, ",,,,,,");
2706 }
2707
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002708 /* failed health analyses */
2709 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2710
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002711 /* requests : req_rate, req_rate_max, req_tot, */
2712 chunk_printf(&msg, ",,,");
2713
Willy Tarreauae526782010-03-04 20:34:23 +01002714 /* errors: cli_aborts, srv_aborts */
2715 chunk_printf(&msg, "%lld,%lld,",
2716 sv->counters.cli_aborts, sv->counters.srv_aborts);
2717
Willy Tarreau7f062c42009-03-05 18:43:00 +01002718 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002719 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002720 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002721 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002722 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002723 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002724
Willy Tarreau295a8372011-03-10 11:25:07 +01002725 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02002726 /* fall through */
2727
Willy Tarreau295a8372011-03-10 11:25:07 +01002728 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02002729 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002730 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002731 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2732 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002733 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01002734 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002735 /* Column sub-heading for Enable or Disable server */
2736 chunk_printf(&msg, "<td></td>");
2737 }
2738 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002739
2740 if (uri->flags&ST_SHLGNDS) {
2741 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002742 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002743 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002744
2745 /* cookie */
2746 if (px->cookie_name) {
2747 struct chunk src;
2748
2749 chunk_printf(&msg, ", cookie: '");
2750
2751 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2752 chunk_htmlencode(&msg, &src);
2753
2754 chunk_printf(&msg, "'");
2755 }
2756
2757 chunk_printf(&msg, "\"");
2758
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002759 }
2760
2761 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002762 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01002763 ">%s<a name=\"%s/Backend\"></a>"
2764 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002765 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002766 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002767 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002768 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002769 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002770 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002771 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002772 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002773 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
2774 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02002775
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002776 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002777 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002778 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002779 "<td"
2780 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002781 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002782
2783 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2784 if (px->mode == PR_MODE_HTTP) {
2785 int i;
2786
2787 chunk_printf(&msg, " title=\"rsp codes:");
2788
2789 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002790 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002791
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002792 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002793 }
2794
2795 chunk_printf(&msg,
2796 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002797 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002798 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002799 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002800 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002801 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002802 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01002803 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002804 U2H7(px->be_counters.cum_lbconn),
2805 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002806
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002807 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002808 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002809 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002810 /* errors : request, connect */
2811 "<td></td><td>%s</td>"
2812 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002813 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002814 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002815 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002816 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02002817 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002818 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02002819 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002820 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2821 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002822 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002823 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
2824 U2H2(px->be_counters.failed_conns),
2825 px->be_counters.cli_aborts,
2826 px->be_counters.srv_aborts,
2827 U2H5(px->be_counters.failed_resp),
2828 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002829 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01002830 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2831 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01002832 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002833 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002834
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002835 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002836 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002837 "<td class=ac>&nbsp;</td><td>%d</td>"
2838 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002839 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002840 "</tr>",
2841 px->down_trans,
2842 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002843 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002844 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002845 /* pxid, name */
2846 "%s,BACKEND,"
2847 /* queue : current, max */
2848 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002849 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002850 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002851 /* bytes : in, out */
2852 "%lld,%lld,"
2853 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002854 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002855 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002856 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002857 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002858 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002859 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02002860 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002861 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02002862 * active and backups. */
2863 "%s,"
2864 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002865 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002866 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002867 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002868 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002869 /* rate, rate_lim, rate_max, */
2870 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002871 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002872 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002873 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002874 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2875 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2876 px->be_counters.bytes_in, px->be_counters.bytes_out,
2877 px->be_counters.denied_req, px->be_counters.denied_resp,
2878 px->be_counters.failed_conns, px->be_counters.failed_resp,
2879 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01002880 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01002881 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002882 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002883 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01002884 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002885 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002886 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002887 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002888 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002889
2890 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2891 if (px->mode == PR_MODE_HTTP) {
2892 int i;
2893
2894 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002895 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002896
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002897 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002898 } else {
2899 chunk_printf(&msg, ",,,,,,");
2900 }
2901
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002902 /* failed health analyses */
2903 chunk_printf(&msg, ",");
2904
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002905 /* requests : req_rate, req_rate_max, req_tot, */
2906 chunk_printf(&msg, ",,,");
2907
Willy Tarreauae526782010-03-04 20:34:23 +01002908 /* errors: cli_aborts, srv_aborts */
2909 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002910 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01002911
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002912 /* finish with EOL */
2913 chunk_printf(&msg, "\n");
2914
Willy Tarreau55bb8452007-10-17 18:44:57 +02002915 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002916 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002917 return 0;
2918 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002919
Willy Tarreau295a8372011-03-10 11:25:07 +01002920 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002921 /* fall through */
2922
Willy Tarreau295a8372011-03-10 11:25:07 +01002923 case STAT_PX_ST_END:
2924 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002925 chunk_printf(&msg, "</table>");
2926
Willy Tarreau295a8372011-03-10 11:25:07 +01002927 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002928 /* close the form used to enable/disable this proxy servers */
2929 chunk_printf(&msg,
2930 "Choose the action to perform on the checked servers : "
2931 "<select name=action>"
2932 "<option value=\"\"></option>"
2933 "<option value=\"disable\">Disable</option>"
2934 "<option value=\"enable\">Enable</option>"
2935 "</select>"
2936 "<input type=\"hidden\" name=\"b\" value=\"%s\">"
2937 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2938 "</form>",
2939 px->id);
2940 }
2941
2942 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02002943
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002944 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002945 return 0;
2946 }
Willy Tarreau91861262007-10-17 17:06:05 +02002947
Willy Tarreau295a8372011-03-10 11:25:07 +01002948 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002949 /* fall through */
2950
Willy Tarreau295a8372011-03-10 11:25:07 +01002951 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002952 return 1;
2953
2954 default:
2955 /* unknown state, we should put an abort() here ! */
2956 return 1;
2957 }
2958}
2959
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002960/* This function dumps a complete session state onto the stream intreface's
2961 * read buffer. The data_ctx must have been zeroed first, and the flags
2962 * properly set. The session has to be set in data_ctx.sess.target. It returns
2963 * 0 if the output buffer is full and it needs to be called again, otherwise
2964 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002965 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002966static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002967{
2968 struct tm tm;
2969 struct chunk msg;
2970 struct session *sess;
2971 extern const char *monthname[12];
2972 char pn[INET6_ADDRSTRLEN];
2973
2974 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau295a8372011-03-10 11:25:07 +01002975 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002976
Willy Tarreau295a8372011-03-10 11:25:07 +01002977 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002978 /* session changed, no need to go any further */
2979 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002980 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002981 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002982 si->applet.ctx.sess.target = NULL;
2983 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002984 return 1;
2985 }
2986
Willy Tarreau295a8372011-03-10 11:25:07 +01002987 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002988 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01002989 si->applet.ctx.sess.uid = sess->uniq_id;
2990 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002991 /* fall through */
2992
2993 case 1:
2994 chunk_printf(&msg,
2995 "%p: id=%u, proto=%s",
2996 sess,
2997 sess->uniq_id,
2998 sess->listener->proto->name);
2999
3000 switch (sess->listener->proto->sock_family) {
3001 case AF_INET:
3002 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003003 (const void *)&((struct sockaddr_in *)&sess->si[0].addr.c.from)->sin_addr,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003004 pn, sizeof(pn));
3005
3006 chunk_printf(&msg,
3007 " source=%s:%d\n",
3008 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003009 ntohs(((struct sockaddr_in *)&sess->si[0].addr.c.from)->sin_port));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003010 break;
3011 case AF_INET6:
3012 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003013 (const void *)&((struct sockaddr_in6 *)(&sess->si[0].addr.c.from))->sin6_addr,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003014 pn, sizeof(pn));
3015
3016 chunk_printf(&msg,
3017 " source=%s:%d\n",
3018 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003019 ntohs(((struct sockaddr_in6 *)&sess->si[0].addr.c.from)->sin6_port));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003020 break;
3021 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02003022 chunk_printf(&msg,
3023 " source=unix:%d\n", sess->listener->luid);
3024 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003025 default:
3026 /* no more information to print right now */
3027 chunk_printf(&msg, "\n");
3028 break;
3029 }
3030
3031 chunk_printf(&msg,
3032 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003033 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003034
3035 chunk_printf(&msg,
3036 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)\n",
3037 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3038 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3039 sess->listener ? sess->listener->luid : 0);
3040
3041 chunk_printf(&msg,
3042 " backend=%s (id=%u mode=%s) server=%s (id=%u)\n",
3043 sess->be->id, sess->be->uuid, sess->be->mode ? "http" : "tcp",
Willy Tarreau827aee92011-03-10 16:55:02 +01003044 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
3045 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003046
3047 chunk_printf(&msg,
3048 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
3049 sess->task,
3050 sess->task->state,
3051 sess->task->nice, sess->task->calls,
3052 sess->task->expire ?
3053 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3054 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3055 TICKS_TO_MS(1000)) : "<NEVER>",
3056 task_in_rq(sess->task) ? ", running" : "");
3057
3058 get_localtime(sess->logs.accept_date.tv_sec, &tm);
3059 chunk_printf(&msg,
3060 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
3061 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3062 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3063 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3064
3065 chunk_printf(&msg,
3066 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3067 &sess->si[0],
3068 sess->si[0].state,
3069 sess->si[0].flags,
3070 sess->si[0].fd,
3071 sess->si[0].exp ?
3072 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3073 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3074 TICKS_TO_MS(1000)) : "<NEVER>",
3075 sess->si[0].err_type);
3076
3077 chunk_printf(&msg,
3078 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3079 &sess->si[1],
3080 sess->si[1].state,
3081 sess->si[1].flags,
3082 sess->si[1].fd,
3083 sess->si[1].exp ?
3084 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3085 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3086 TICKS_TO_MS(1000)) : "<NEVER>",
3087 sess->si[1].err_type);
3088
3089 chunk_printf(&msg,
3090 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3091 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3092 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3093
3094
3095 chunk_printf(&msg,
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02003096 " req=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003097 " an_exp=%s",
3098 sess->req,
3099 sess->req->flags, sess->req->analysers,
3100 sess->req->l, sess->req->send_max,
3101 sess->req->pipe ? sess->req->pipe->data : 0,
3102 sess->req->to_forward,
3103 sess->req->analyse_exp ?
3104 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3105 TICKS_TO_MS(1000)) : "<NEVER>");
3106
3107 chunk_printf(&msg,
3108 " rex=%s",
3109 sess->req->rex ?
3110 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3111 TICKS_TO_MS(1000)) : "<NEVER>");
3112
3113 chunk_printf(&msg,
3114 " wex=%s\n"
3115 " data=%p r=%d w=%d lr=%d total=%lld\n",
3116 sess->req->wex ?
3117 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3118 TICKS_TO_MS(1000)) : "<NEVER>",
3119 sess->req->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01003120 (int)(sess->req->r - sess->req->data),
3121 (int)(sess->req->w - sess->req->data),
3122 (int)(sess->req->lr - sess->req->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003123 sess->req->total);
3124
3125 chunk_printf(&msg,
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02003126 " res=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003127 " an_exp=%s",
3128 sess->rep,
3129 sess->rep->flags, sess->rep->analysers,
3130 sess->rep->l, sess->rep->send_max,
3131 sess->rep->pipe ? sess->rep->pipe->data : 0,
3132 sess->rep->to_forward,
3133 sess->rep->analyse_exp ?
3134 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3135 TICKS_TO_MS(1000)) : "<NEVER>");
3136
3137 chunk_printf(&msg,
3138 " rex=%s",
3139 sess->rep->rex ?
3140 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3141 TICKS_TO_MS(1000)) : "<NEVER>");
3142
3143 chunk_printf(&msg,
3144 " wex=%s\n"
3145 " data=%p r=%d w=%d lr=%d total=%lld\n",
3146 sess->rep->wex ?
3147 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3148 TICKS_TO_MS(1000)) : "<NEVER>",
3149 sess->rep->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01003150 (int)(sess->rep->r - sess->rep->data),
3151 (int)(sess->rep->w - sess->rep->data),
3152 (int)(sess->rep->lr - sess->rep->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003153 sess->rep->total);
3154
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003155 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003156 return 0;
3157
3158 /* use other states to dump the contents */
3159 }
3160 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003161 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003162 return 1;
3163}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003164
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003165/* This function dumps all sessions' states onto the stream intreface's
3166 * read buffer. The data_ctx must have been zeroed first, and the flags
3167 * properly set. It returns 0 if the output buffer is full and it needs
3168 * to be called again, otherwise non-zero. It is designed to be called
3169 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003170 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003171static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003172{
3173 struct chunk msg;
3174
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003175 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003176 /* If we're forced to shut down, we might have to remove our
3177 * reference to the last session being dumped.
3178 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003179 if (si->applet.state == STAT_ST_LIST) {
3180 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3181 LIST_DEL(&si->applet.ctx.sess.bref.users);
3182 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003183 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003184 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003185 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003186 }
3187
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003188 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003189
Willy Tarreau295a8372011-03-10 11:25:07 +01003190 switch (si->applet.state) {
3191 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003192 /* the function had not been called yet, let's prepare the
3193 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003194 * pointer to the first in the global list. When a target
3195 * session is being destroyed, it is responsible for updating
3196 * this pointer. We know we have reached the end when this
3197 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003198 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003199 LIST_INIT(&si->applet.ctx.sess.bref.users);
3200 si->applet.ctx.sess.bref.ref = sessions.n;
3201 si->applet.state = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003202 /* fall through */
3203
Willy Tarreau295a8372011-03-10 11:25:07 +01003204 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003205 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003206 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3207 LIST_DEL(&si->applet.ctx.sess.bref.users);
3208 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003209 }
3210
3211 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003212 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003213 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003214 struct session *curr_sess;
3215
Willy Tarreau295a8372011-03-10 11:25:07 +01003216 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003217
Willy Tarreau295a8372011-03-10 11:25:07 +01003218 if (si->applet.ctx.sess.target) {
3219 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003220 goto next_sess;
3221
Willy Tarreau295a8372011-03-10 11:25:07 +01003222 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003223 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003224 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003225 return 0;
3226
3227 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003228 LIST_DEL(&si->applet.ctx.sess.bref.users);
3229 LIST_INIT(&si->applet.ctx.sess.bref.users);
3230 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003231 break;
3232 }
3233
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003234 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003235 "%p: proto=%s",
3236 curr_sess,
3237 curr_sess->listener->proto->name);
3238
3239 switch (curr_sess->listener->proto->sock_family) {
3240 case AF_INET:
3241 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003242 (const void *)&((struct sockaddr_in *)&curr_sess->si[0].addr.c.from)->sin_addr,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003243 pn, sizeof(pn));
3244
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003245 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003246 " src=%s:%d fe=%s be=%s srv=%s",
3247 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003248 ntohs(((struct sockaddr_in *)&curr_sess->si[0].addr.c.from)->sin_port),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003249 curr_sess->fe->id,
3250 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01003251 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003252 );
3253 break;
3254 case AF_INET6:
3255 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003256 (const void *)&((struct sockaddr_in6 *)(&curr_sess->si[0].addr.c.from))->sin6_addr,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003257 pn, sizeof(pn));
3258
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003259 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003260 " src=%s:%d fe=%s be=%s srv=%s",
3261 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003262 ntohs(((struct sockaddr_in6 *)&curr_sess->si[0].addr.c.from)->sin6_port),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003263 curr_sess->fe->id,
3264 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01003265 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003266 );
3267
3268 break;
3269 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02003270 chunk_printf(&msg,
3271 " src=unix:%d fe=%s be=%s srv=%s",
3272 curr_sess->listener->luid,
3273 curr_sess->fe->id,
3274 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01003275 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003276 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003277 break;
3278 }
3279
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003280 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003281 " ts=%02x age=%s calls=%d",
3282 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003283 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3284 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003285
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003286 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003287 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
3288 curr_sess->req->flags,
3289 curr_sess->req->l,
3290 curr_sess->req->analysers,
3291 curr_sess->req->rex ?
3292 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3293 TICKS_TO_MS(1000)) : "");
3294
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003295 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003296 ",wx=%s",
3297 curr_sess->req->wex ?
3298 human_time(TICKS_TO_MS(curr_sess->req->wex - 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 ",ax=%s]",
3303 curr_sess->req->analyse_exp ?
3304 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - 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 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
3309 curr_sess->rep->flags,
3310 curr_sess->rep->l,
3311 curr_sess->rep->analysers,
3312 curr_sess->rep->rex ?
3313 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3314 TICKS_TO_MS(1000)) : "");
3315
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003316 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003317 ",wx=%s",
3318 curr_sess->rep->wex ?
3319 human_time(TICKS_TO_MS(curr_sess->rep->wex - 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 ",ax=%s]",
3324 curr_sess->rep->analyse_exp ?
3325 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - 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 " s0=[%d,%1xh,fd=%d,ex=%s]",
3330 curr_sess->si[0].state,
3331 curr_sess->si[0].flags,
3332 curr_sess->si[0].fd,
3333 curr_sess->si[0].exp ?
3334 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3335 TICKS_TO_MS(1000)) : "");
3336
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003337 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003338 " s1=[%d,%1xh,fd=%d,ex=%s]",
3339 curr_sess->si[1].state,
3340 curr_sess->si[1].flags,
3341 curr_sess->si[1].fd,
3342 curr_sess->si[1].exp ?
3343 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3344 TICKS_TO_MS(1000)) : "");
3345
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003346 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003347 " exp=%s",
3348 curr_sess->task->expire ?
3349 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3350 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003351 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003352 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003353
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003354 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003355
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003356 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003357 /* let's try again later from this session. We add ourselves into
3358 * this session's users so that it can remove us upon termination.
3359 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003360 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003361 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003362 }
3363
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003364 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003365 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003366 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003367
Willy Tarreau295a8372011-03-10 11:25:07 +01003368 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003369 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003370 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003371 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3372 else
3373 chunk_printf(&msg, "Session not found.\n");
3374
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003375 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003376 return 0;
3377
Willy Tarreau295a8372011-03-10 11:25:07 +01003378 si->applet.ctx.sess.target = NULL;
3379 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003380 return 1;
3381 }
3382
Willy Tarreau295a8372011-03-10 11:25:07 +01003383 si->applet.state = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003384 /* fall through */
3385
3386 default:
Willy Tarreau295a8372011-03-10 11:25:07 +01003387 si->applet.state = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003388 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003389 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003390}
3391
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003392/* This function dumps all tables' states onto the stream intreface's
3393 * read buffer. The data_ctx must have been zeroed first, and the flags
3394 * properly set. It returns 0 if the output buffer is full and it needs
3395 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003396 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003397static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003398{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003399 struct session *s = si->applet.private;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003400 struct chunk msg;
3401 struct ebmb_node *eb;
3402 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003403 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003404
3405 /*
Willy Tarreau295a8372011-03-10 11:25:07 +01003406 * We have 3 possible states in si->applet.state :
3407 * - STAT_ST_INIT : the first call
3408 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003409 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003410 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003411 * and the entry pointer points to the next entry to be dumped,
3412 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003413 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003414 * data though.
3415 */
3416
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003417 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003418 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau295a8372011-03-10 11:25:07 +01003419 if (si->applet.state == STAT_ST_LIST) {
3420 si->applet.ctx.table.entry->ref_cnt--;
3421 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003422 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003423 return 1;
3424 }
3425
3426 chunk_init(&msg, trash, sizeof(trash));
3427
Willy Tarreau295a8372011-03-10 11:25:07 +01003428 while (si->applet.state != STAT_ST_FIN) {
3429 switch (si->applet.state) {
3430 case STAT_ST_INIT:
3431 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3432 if (!si->applet.ctx.table.proxy)
3433 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003434
Willy Tarreau295a8372011-03-10 11:25:07 +01003435 si->applet.ctx.table.entry = NULL;
3436 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003437 break;
3438
Willy Tarreau295a8372011-03-10 11:25:07 +01003439 case STAT_ST_INFO:
3440 if (!si->applet.ctx.table.proxy ||
3441 (si->applet.ctx.table.target &&
3442 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
3443 si->applet.state = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003444 break;
3445 }
3446
Willy Tarreau295a8372011-03-10 11:25:07 +01003447 if (si->applet.ctx.table.proxy->table.size) {
Simon Hormanc88b8872011-06-15 15:18:49 +09003448 if (show && !stats_dump_table_head_to_buffer(&msg, si, si->applet.ctx.table.proxy,
3449 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003450 return 0;
3451
Willy Tarreau295a8372011-03-10 11:25:07 +01003452 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003453 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
3454 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003455 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003456 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003457 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3458 si->applet.ctx.table.entry->ref_cnt++;
3459 si->applet.state = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003460 break;
3461 }
3462 }
3463 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003464 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003465 break;
3466
Willy Tarreau295a8372011-03-10 11:25:07 +01003467 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003468 skip_entry = false;
3469
Willy Tarreau295a8372011-03-10 11:25:07 +01003470 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003471 /* we're filtering on some data contents */
3472 void *ptr;
3473 long long data;
3474
Willy Tarreau295a8372011-03-10 11:25:07 +01003475 dt = si->applet.ctx.table.data_type;
3476 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3477 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003478 dt);
3479
3480 data = 0;
3481 switch (stktable_data_types[dt].std_type) {
3482 case STD_T_SINT:
3483 data = stktable_data_cast(ptr, std_t_sint);
3484 break;
3485 case STD_T_UINT:
3486 data = stktable_data_cast(ptr, std_t_uint);
3487 break;
3488 case STD_T_ULL:
3489 data = stktable_data_cast(ptr, std_t_ull);
3490 break;
3491 case STD_T_FRQP:
3492 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003493 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003494 break;
3495 }
3496
3497 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003498 if ((data < si->applet.ctx.table.value &&
3499 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3500 si->applet.ctx.table.data_op == STD_OP_GT ||
3501 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3502 (data == si->applet.ctx.table.value &&
3503 (si->applet.ctx.table.data_op == STD_OP_NE ||
3504 si->applet.ctx.table.data_op == STD_OP_GT ||
3505 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3506 (data > si->applet.ctx.table.value &&
3507 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3508 si->applet.ctx.table.data_op == STD_OP_LT ||
3509 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003510 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003511 }
3512
Simon Hormanc88b8872011-06-15 15:18:49 +09003513 if (show && !skip_entry &&
3514 !stats_dump_table_entry_to_buffer(&msg, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003515 si->applet.ctx.table.entry))
3516 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003517
Willy Tarreau295a8372011-03-10 11:25:07 +01003518 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003519
Willy Tarreau295a8372011-03-10 11:25:07 +01003520 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003521 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003522 struct stksess *old = si->applet.ctx.table.entry;
3523 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3524 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3525 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003526 break;
3527 }
3528
Simon Hormanc88b8872011-06-15 15:18:49 +09003529
3530 if (show)
3531 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3532 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3533 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3534
Willy Tarreau295a8372011-03-10 11:25:07 +01003535 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
3536 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003537 break;
3538
Willy Tarreau295a8372011-03-10 11:25:07 +01003539 case STAT_ST_END:
3540 si->applet.state = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003541 break;
3542 }
3543 }
3544 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003545}
3546
Willy Tarreaud426a182010-03-05 14:58:26 +01003547/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003548 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3549 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3550 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3551 * lines are respected within the limit of 70 output chars. Lines that are
3552 * continuation of a previous truncated line begin with "+" instead of " "
3553 * after the offset. The new pointer is returned.
3554 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003555static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3556 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003557{
3558 int end;
3559 unsigned char c;
3560
3561 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003562 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003563 return ptr;
3564
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003565 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003566
Willy Tarreaud426a182010-03-05 14:58:26 +01003567 while (ptr < len && ptr < bsize) {
3568 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003569 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003570 if (out->len > end - 2)
3571 break;
3572 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003573 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003574 if (out->len > end - 3)
3575 break;
3576 out->str[out->len++] = '\\';
3577 switch (c) {
3578 case '\t': c = 't'; break;
3579 case '\n': c = 'n'; break;
3580 case '\r': c = 'r'; break;
3581 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003582 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003583 }
3584 out->str[out->len++] = c;
3585 } else {
3586 if (out->len > end - 5)
3587 break;
3588 out->str[out->len++] = '\\';
3589 out->str[out->len++] = 'x';
3590 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3591 out->str[out->len++] = hextab[c & 0xF];
3592 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003593 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003594 /* we had a line break, let's return now */
3595 out->str[out->len++] = '\n';
3596 *line = ptr;
3597 return ptr;
3598 }
3599 }
3600 /* we have an incomplete line, we return it as-is */
3601 out->str[out->len++] = '\n';
3602 return ptr;
3603}
3604
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003605/* This function dumps all captured errors onto the stream intreface's
3606 * read buffer. The data_ctx must have been zeroed first, and the flags
3607 * properly set. It returns 0 if the output buffer is full and it needs
3608 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003609 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003610static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003611{
3612 extern const char *monthname[12];
3613 struct chunk msg;
3614
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003615 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003616 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003617
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003618 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003619
Willy Tarreau295a8372011-03-10 11:25:07 +01003620 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003621 /* the function had not been called yet, let's prepare the
3622 * buffer for a response.
3623 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003624 struct tm tm;
3625
3626 get_localtime(date.tv_sec, &tm);
3627 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3628 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3629 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3630 error_snapshot_id);
3631
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003632 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003633 /* Socket buffer full. Let's try again later from the same point */
3634 return 0;
3635 }
3636
Willy Tarreau295a8372011-03-10 11:25:07 +01003637 si->applet.ctx.errors.px = proxy;
3638 si->applet.ctx.errors.buf = 0;
3639 si->applet.ctx.errors.bol = 0;
3640 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003641 }
3642
3643 /* we have two inner loops here, one for the proxy, the other one for
3644 * the buffer.
3645 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003646 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003647 struct error_snapshot *es;
3648
Willy Tarreau295a8372011-03-10 11:25:07 +01003649 if (si->applet.ctx.errors.buf == 0)
3650 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003651 else
Willy Tarreau295a8372011-03-10 11:25:07 +01003652 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003653
3654 if (!es->when.tv_sec)
3655 goto next;
3656
Willy Tarreau295a8372011-03-10 11:25:07 +01003657 if (si->applet.ctx.errors.iid >= 0 &&
3658 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
3659 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003660 goto next;
3661
Willy Tarreau295a8372011-03-10 11:25:07 +01003662 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003663 /* just print headers now */
3664
3665 char pn[INET6_ADDRSTRLEN];
3666 struct tm tm;
3667
3668 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003669 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003670 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003671 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003672
3673
3674 if (es->src.ss_family == AF_INET)
3675 inet_ntop(AF_INET,
3676 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
3677 pn, sizeof(pn));
3678 else
3679 inet_ntop(AF_INET6,
3680 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
3681 pn, sizeof(pn));
3682
Willy Tarreau295a8372011-03-10 11:25:07 +01003683 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003684 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003685 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003686 " frontend %s (#%d): invalid request\n"
3687 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003688 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003689 " request length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003690 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau3fd6cec2010-03-25 06:45:07 +01003691 pn, es->sid, (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3692 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003693 es->srv ? es->srv->id : "<NONE>",
3694 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003695 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003696 es->len, es->pos);
3697 break;
3698 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003699 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003700 " backend %s (#%d) : invalid response\n"
3701 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003702 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003703 " response length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003704 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003705 pn, es->sid, es->oe->id, es->oe->uuid,
3706 es->srv ? es->srv->id : "<NONE>",
3707 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003708 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003709 es->len, es->pos);
3710 break;
3711 }
3712
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003713 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003714 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003715 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003716 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003717 si->applet.ctx.errors.ptr = 0;
3718 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003719 }
3720
Willy Tarreau295a8372011-03-10 11:25:07 +01003721 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003722 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003723 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003724 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003725 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau61b34732009-10-03 23:49:35 +02003726 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003727 goto next;
3728 }
3729
3730 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01003731 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003732 int newptr;
3733 int newline;
3734
Willy Tarreau295a8372011-03-10 11:25:07 +01003735 newline = si->applet.ctx.errors.bol;
3736 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
3737 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02003738 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003739
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003740 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003741 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003742 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003743 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003744 si->applet.ctx.errors.ptr = newptr;
3745 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003746 };
3747 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01003748 si->applet.ctx.errors.bol = 0;
3749 si->applet.ctx.errors.ptr = -1;
3750 si->applet.ctx.errors.buf++;
3751 if (si->applet.ctx.errors.buf > 1) {
3752 si->applet.ctx.errors.buf = 0;
3753 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003754 }
3755 }
3756
3757 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02003758 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003759}
3760
Willy Tarreaub24281b2011-02-13 13:16:36 +01003761struct si_applet http_stats_applet = {
3762 .name = "<STATS>", /* used for logging */
3763 .fct = http_stats_io_handler,
3764};
3765
Simon Horman9bd2c732011-06-15 15:18:44 +09003766static struct si_applet cli_applet = {
Willy Tarreaub24281b2011-02-13 13:16:36 +01003767 .name = "<CLI>", /* used for logging */
3768 .fct = cli_io_handler,
3769};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003770
Willy Tarreau10522fd2008-07-09 20:12:41 +02003771static struct cfg_kw_list cfg_kws = {{ },{
3772 { CFG_GLOBAL, "stats", stats_parse_global },
3773 { 0, NULL, NULL },
3774}};
3775
3776__attribute__((constructor))
3777static void __dumpstats_module_init(void)
3778{
3779 cfg_register_keywords(&cfg_kws);
3780}
3781
Willy Tarreau91861262007-10-17 17:06:05 +02003782/*
3783 * Local variables:
3784 * c-indent-level: 8
3785 * c-basic-offset: 8
3786 * End:
3787 */