blob: c9c40557a08a21b42008faab7ba8f233fbf58758 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaueb472682010-05-28 18:46:57 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
Simon Horman17bce342011-06-15 15:18:47 +090017#include <stdbool.h>
Simon Hormancec9a222011-06-15 15:18:51 +090018#include <stdint.h>
Willy Tarreau91861262007-10-17 17:06:05 +020019#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020022#include <pwd.h>
23#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020024
25#include <sys/socket.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28
Willy Tarreau10522fd2008-07-09 20:12:41 +020029#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020030#include <common/compat.h>
31#include <common/config.h>
32#include <common/debug.h>
33#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
40
Willy Tarreau91861262007-10-17 17:06:05 +020041#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020042
43#include <proto/backend.h>
44#include <proto/buffers.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020045#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020046#include <proto/dumpstats.h>
47#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010048#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020049#include <proto/log.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010050#include <proto/pipe.h>
Willy Tarreau2a0f4d22011-08-02 11:49:05 +020051#include <proto/protocols.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020052#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020053#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020054#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020055#include <proto/server.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010056#include <proto/stream_interface.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020057#include <proto/stream_sock.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010058#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020059
Simon Horman9bd2c732011-06-15 15:18:44 +090060static int stats_dump_raw_to_buffer(struct stream_interface *si);
61static int stats_dump_full_sess_to_buffer(struct stream_interface *si);
62static int stats_dump_sess_to_buffer(struct stream_interface *si);
63static int stats_dump_errors_to_buffer(struct stream_interface *si);
Simon Hormanc88b8872011-06-15 15:18:49 +090064static int stats_table_request(struct stream_interface *si, bool show);
Simon Horman9bd2c732011-06-15 15:18:44 +090065static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
66static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri);
67
68static struct si_applet cli_applet;
69
70static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020071 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +020072 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +020073 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020074 " help : this message\n"
75 " prompt : toggle interactive mode with prompt\n"
76 " quit : disconnect\n"
77 " show info : report information about the running process\n"
78 " show stat : report counters for each proxy and server\n"
79 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +010080 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +020081 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +020082 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +020083 " set weight : change a server's weight\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +010084 " set timeout : change a timeout setting\n"
Cyril Bontécd19e512010-01-31 22:34:03 +010085 " disable server : set a server in maintenance mode\n"
86 " enable server : re-enable a server that was previously in maintenance mode\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +020087 " set maxconn : change a maxconn setting\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020088 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020089
Simon Horman9bd2c732011-06-15 15:18:44 +090090static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +020091 "Permission denied\n"
92 "";
93
Willy Tarreau295a8372011-03-10 11:25:07 +010094/* data transmission states for the stats responses */
95enum {
96 STAT_ST_INIT = 0,
97 STAT_ST_HEAD,
98 STAT_ST_INFO,
99 STAT_ST_LIST,
100 STAT_ST_END,
101 STAT_ST_FIN,
102};
103
104/* data transmission states for the stats responses inside a proxy */
105enum {
106 STAT_PX_ST_INIT = 0,
107 STAT_PX_ST_TH,
108 STAT_PX_ST_FE,
109 STAT_PX_ST_LI,
110 STAT_PX_ST_SV,
111 STAT_PX_ST_BE,
112 STAT_PX_ST_END,
113 STAT_PX_ST_FIN,
114};
115
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200116/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +0200117 * a new stats socket. It returns a positive value upon success, 0 if the connection
118 * needs to be closed and ignored, or a negative value upon critical failure.
119 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900120static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200121{
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200122 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100123 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200124 copy_target(&s->target, &s->si[1].target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +0100125 s->si[1].applet.private = s;
126 s->si[1].applet.st1 = 0;
127 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200128
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200129 tv_zero(&s->logs.tv_request);
130 s->logs.t_queue = 0;
131 s->logs.t_connect = 0;
132 s->logs.t_data = 0;
133 s->logs.t_close = 0;
134 s->logs.bytes_in = s->logs.bytes_out = 0;
135 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
136 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200137
Willy Tarreaueb472682010-05-28 18:46:57 +0200138 s->req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
139
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200140 if (s->listener->timeout) {
141 s->req->rto = *s->listener->timeout;
142 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200143 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200144 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200145}
146
Willy Tarreau07e9e642010-08-17 21:48:17 +0200147/* allocate a new stats frontend named <name>, and return it
148 * (or NULL in case of lack of memory).
149 */
150static struct proxy *alloc_stats_fe(const char *name)
151{
152 struct proxy *fe;
153
154 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
155 if (!fe)
156 return NULL;
157
Willy Tarreau237250c2011-07-29 01:49:03 +0200158 init_new_proxy(fe);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200159
160 fe->last_change = now.tv_sec;
161 fe->id = strdup("GLOBAL");
162 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200163 fe->maxconn = 10; /* default to 10 concurrent connections */
164 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
165
Willy Tarreau07e9e642010-08-17 21:48:17 +0200166 return fe;
167}
168
Willy Tarreaufbee7132007-10-18 13:53:22 +0200169/* This function parses a "stats" statement in the "global" section. It returns
170 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
171 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
172 * zero included. The trailing '\n' must not be written. The function must be
173 * called with <args> pointing to the first word after "stats".
174 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200175static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
176 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200177{
Willy Tarreau10522fd2008-07-09 20:12:41 +0200178 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200179 if (!strcmp(args[0], "socket")) {
Andreas Kohn16171e22011-01-19 20:29:32 +0100180 struct sockaddr_un *su;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200181 int cur_arg;
182
183 if (*args[1] == 0) {
184 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
185 return -1;
186 }
187
188 if (global.stats_sock.state != LI_NEW) {
189 snprintf(err, errlen, "'stats socket' already specified in global section");
190 return -1;
191 }
192
Andreas Kohn16171e22011-01-19 20:29:32 +0100193 su = str2sun(args[1]);
194 if (!su) {
195 snprintf(err, errlen, "'stats socket' path would require truncation");
196 return -1;
197 }
198 memcpy(&global.stats_sock.addr, su, sizeof(struct sockaddr_un)); // guaranteed to fit
Willy Tarreaufbee7132007-10-18 13:53:22 +0200199
Willy Tarreau89a63132009-08-16 17:41:45 +0200200 if (!global.stats_fe) {
Willy Tarreau07e9e642010-08-17 21:48:17 +0200201 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau89a63132009-08-16 17:41:45 +0200202 snprintf(err, errlen, "out of memory");
203 return -1;
204 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200205 }
206
Willy Tarreaufbee7132007-10-18 13:53:22 +0200207 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100208 global.stats_sock.options = LI_O_NONE;
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200209 global.stats_sock.accept = session_accept;
210 global.stats_fe->accept = stats_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200211 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200212 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200213 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreaueb472682010-05-28 18:46:57 +0200214 global.stats_sock.frontend = global.stats_fe;
Willy Tarreau6162db22009-10-10 17:13:00 +0200215 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200216 global.stats_sock.maxconn = global.stats_fe->maxconn;
Willy Tarreau89a63132009-08-16 17:41:45 +0200217 global.stats_sock.timeout = &global.stats_fe->timeout.client;
218
219 global.stats_sock.next = global.stats_fe->listen;
220 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200221
222 cur_arg = 2;
223 while (*args[cur_arg]) {
224 if (!strcmp(args[cur_arg], "uid")) {
225 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
226 cur_arg += 2;
227 }
228 else if (!strcmp(args[cur_arg], "gid")) {
229 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
230 cur_arg += 2;
231 }
232 else if (!strcmp(args[cur_arg], "mode")) {
233 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
234 cur_arg += 2;
235 }
236 else if (!strcmp(args[cur_arg], "user")) {
237 struct passwd *user;
238 user = getpwnam(args[cur_arg + 1]);
239 if (!user) {
240 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
241 args[cur_arg + 1]);
242 return -1;
243 }
244 global.stats_sock.perm.ux.uid = user->pw_uid;
245 cur_arg += 2;
246 }
247 else if (!strcmp(args[cur_arg], "group")) {
248 struct group *group;
249 group = getgrnam(args[cur_arg + 1]);
250 if (!group) {
251 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
252 args[cur_arg + 1]);
253 return -1;
254 }
255 global.stats_sock.perm.ux.gid = group->gr_gid;
256 cur_arg += 2;
257 }
Willy Tarreau6162db22009-10-10 17:13:00 +0200258 else if (!strcmp(args[cur_arg], "level")) {
259 if (!strcmp(args[cur_arg+1], "user"))
260 global.stats_sock.perm.ux.level = ACCESS_LVL_USER;
261 else if (!strcmp(args[cur_arg+1], "operator"))
262 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER;
263 else if (!strcmp(args[cur_arg+1], "admin"))
264 global.stats_sock.perm.ux.level = ACCESS_LVL_ADMIN;
265 else {
266 snprintf(err, errlen, "'stats socket level' only supports 'user', 'operator', and 'admin'");
267 return -1;
268 }
269 cur_arg += 2;
270 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200271 else {
Willy Tarreau6162db22009-10-10 17:13:00 +0200272 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', 'level', and 'mode'");
Willy Tarreaufbee7132007-10-18 13:53:22 +0200273 return -1;
274 }
275 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100276
Willy Tarreaufbee7132007-10-18 13:53:22 +0200277 uxst_add_listener(&global.stats_sock);
278 global.maxsock++;
279 }
280 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100281 unsigned timeout;
282 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
283
284 if (res) {
285 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
286 return -1;
287 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200288
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100289 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200290 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
291 return -1;
292 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200293 if (!global.stats_fe) {
294 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
295 snprintf(err, errlen, "out of memory");
296 return -1;
297 }
298 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200299 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200300 }
301 else if (!strcmp(args[0], "maxconn")) {
302 int maxconn = atol(args[1]);
303
304 if (maxconn <= 0) {
305 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
306 return -1;
307 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200308
309 if (!global.stats_fe) {
310 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
311 snprintf(err, errlen, "out of memory");
312 return -1;
313 }
314 }
315 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200316 }
317 else {
318 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
319 return -1;
320 }
321 return 0;
322}
323
Simon Horman9bd2c732011-06-15 15:18:44 +0900324static int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100325{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200326 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100327 "# pxname,svname,"
328 "qcur,qmax,"
329 "scur,smax,slim,stot,"
330 "bin,bout,"
331 "dreq,dresp,"
332 "ereq,econ,eresp,"
333 "wretr,wredis,"
334 "status,weight,act,bck,"
335 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200336 "pid,iid,sid,throttle,lbtot,tracked,type,"
337 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200338 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100339 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100340 "req_rate,req_rate_max,req_tot,"
341 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100342 "\n");
343}
344
Simon Hormand9366582011-06-15 15:18:45 +0900345/* print a string of text buffer to <out>. The format is :
346 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
347 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
348 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
349 */
350static int dump_text(struct chunk *out, const char *buf, int bsize)
351{
352 unsigned char c;
353 int ptr = 0;
354
355 while (buf[ptr] && ptr < bsize) {
356 c = buf[ptr];
357 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
358 if (out->len > out->size - 1)
359 break;
360 out->str[out->len++] = c;
361 }
362 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
363 if (out->len > out->size - 2)
364 break;
365 out->str[out->len++] = '\\';
366 switch (c) {
367 case ' ': c = ' '; break;
368 case '\t': c = 't'; break;
369 case '\n': c = 'n'; break;
370 case '\r': c = 'r'; break;
371 case '\e': c = 'e'; break;
372 case '\\': c = '\\'; break;
373 }
374 out->str[out->len++] = c;
375 }
376 else {
377 if (out->len > out->size - 4)
378 break;
379 out->str[out->len++] = '\\';
380 out->str[out->len++] = 'x';
381 out->str[out->len++] = hextab[(c >> 4) & 0xF];
382 out->str[out->len++] = hextab[c & 0xF];
383 }
384 ptr++;
385 }
386
387 return ptr;
388}
389
390/* print a buffer in hexa.
391 * Print stopped if <bsize> is reached, or if no more place in the chunk.
392 */
393static int dump_binary(struct chunk *out, const char *buf, int bsize)
394{
395 unsigned char c;
396 int ptr = 0;
397
398 while (ptr < bsize) {
399 c = buf[ptr];
400
401 if (out->len > out->size - 2)
402 break;
403 out->str[out->len++] = hextab[(c >> 4) & 0xF];
404 out->str[out->len++] = hextab[c & 0xF];
405
406 ptr++;
407 }
408 return ptr;
409}
410
411/* Dump the status of a table to a stream interface's
412 * read buffer. It returns 0 if the output buffer is full
413 * and needs to be called again, otherwise non-zero.
414 */
415static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
416 struct proxy *proxy, struct proxy *target)
417{
418 struct session *s = si->applet.private;
419
420 chunk_printf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
421 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
422
423 /* any other information should be dumped here */
424
425 if (target && s->listener->perm.ux.level < ACCESS_LVL_OPER)
426 chunk_printf(msg, "# contents not dumped due to insufficient privileges\n");
427
428 if (buffer_feed_chunk(si->ib, msg) >= 0)
429 return 0;
430
431 return 1;
432}
433
434/* Dump the a table entry to a stream interface's
435 * read buffer. It returns 0 if the output buffer is full
436 * and needs to be called again, otherwise non-zero.
437 */
438static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
439 struct proxy *proxy, struct stksess *entry)
440{
441 int dt;
442
443 chunk_printf(msg, "%p:", entry);
444
445 if (proxy->table.type == STKTABLE_TYPE_IP) {
446 char addr[INET_ADDRSTRLEN];
447 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
448 chunk_printf(msg, " key=%s", addr);
449 }
450 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
451 char addr[INET6_ADDRSTRLEN];
452 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
453 chunk_printf(msg, " key=%s", addr);
454 }
455 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
456 chunk_printf(msg, " key=%u", *(unsigned int *)entry->key.key);
457 }
458 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
459 chunk_printf(msg, " key=");
460 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
461 }
462 else {
463 chunk_printf(msg, " key=");
464 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
465 }
466
467 chunk_printf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
468
469 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
470 void *ptr;
471
472 if (proxy->table.data_ofs[dt] == 0)
473 continue;
474 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
475 chunk_printf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
476 else
477 chunk_printf(msg, " %s=", stktable_data_types[dt].name);
478
479 ptr = stktable_data_ptr(&proxy->table, entry, dt);
480 switch (stktable_data_types[dt].std_type) {
481 case STD_T_SINT:
482 chunk_printf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
483 break;
484 case STD_T_UINT:
485 chunk_printf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
486 break;
487 case STD_T_ULL:
488 chunk_printf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
489 break;
490 case STD_T_FRQP:
491 chunk_printf(msg, "%d",
492 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
493 proxy->table.data_arg[dt].u));
494 break;
495 }
496 }
497 chunk_printf(msg, "\n");
498
499 if (buffer_feed_chunk(si->ib, msg) >= 0)
500 return 0;
501
502 return 1;
503}
504
Simon Horman17bce342011-06-15 15:18:47 +0900505static void stats_sock_table_key_request(struct stream_interface *si, char **args, bool show)
Simon Horman121f3052011-06-15 15:18:46 +0900506{
507 struct session *s = si->applet.private;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900508 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900509 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900510 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900511 unsigned char ip6_key[sizeof(struct in6_addr)];
Simon Horman121f3052011-06-15 15:18:46 +0900512
Simon Hormand5b9fd92011-06-15 15:18:48 +0900513 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900514
515 if (!*args[4]) {
516 si->applet.ctx.cli.msg = "Key value expected\n";
517 si->applet.st0 = STAT_CLI_PRINT;
518 return;
519 }
520
Simon Hormanc5b89f62011-06-15 15:18:50 +0900521 switch (px->table.type) {
522 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900523 uint32_key = htonl(inetaddr_host(args[4]));
524 static_table_key.key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900525 break;
526 case STKTABLE_TYPE_IPV6:
527 inet_pton(AF_INET6, args[4], ip6_key);
528 static_table_key.key = &ip6_key;
529 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900530 case STKTABLE_TYPE_INTEGER:
531 {
532 char *endptr;
533 unsigned long val;
534 errno = 0;
535 val = strtoul(args[4], &endptr, 10);
536 if ((errno == ERANGE && val == ULONG_MAX) ||
537 (errno != 0 && val == 0) || endptr == args[4] ||
538 val > 0xffffffff) {
539 si->applet.ctx.cli.msg = "Invalid key\n";
540 si->applet.st0 = STAT_CLI_PRINT;
541 return;
542 }
543 uint32_key = (uint32_t) val;
544 static_table_key.key = &uint32_key;
545 break;
546 }
547 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900548 case STKTABLE_TYPE_STRING:
549 static_table_key.key = args[4];
550 static_table_key.key_len = strlen(args[4]);
551 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900552 default:
Simon Horman17bce342011-06-15 15:18:47 +0900553 if (show)
Simon Hormancec9a222011-06-15 15:18:51 +0900554 si->applet.ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6 and integer is not supported\n";
Simon Horman17bce342011-06-15 15:18:47 +0900555 else
Simon Hormancec9a222011-06-15 15:18:51 +0900556 si->applet.ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6 and integer is not supported\n";
Simon Horman121f3052011-06-15 15:18:46 +0900557 si->applet.st0 = STAT_CLI_PRINT;
558 return;
559 }
560
561 /* check permissions */
562 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
563 si->applet.ctx.cli.msg = stats_permission_denied_msg;
564 si->applet.st0 = STAT_CLI_PRINT;
565 return;
566 }
567
568 ts = stktable_lookup_key(&px->table, &static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900569 if (!ts)
570 return;
571
572 if (show) {
573 struct chunk msg;
574 chunk_init(&msg, trash, sizeof(trash));
575 if (!stats_dump_table_head_to_buffer(&msg, si, px, px))
576 return;
577 stats_dump_table_entry_to_buffer(&msg, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900578 return;
579 }
Simon Horman17bce342011-06-15 15:18:47 +0900580
581 if (ts->ref_cnt) {
Simon Horman121f3052011-06-15 15:18:46 +0900582 /* don't delete an entry which is currently referenced */
583 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
584 si->applet.st0 = STAT_CLI_PRINT;
585 return;
586 }
587
588 stksess_kill(&px->table, ts);
589}
590
Simon Hormand5b9fd92011-06-15 15:18:48 +0900591static void stats_sock_table_data_request(struct stream_interface *si, char **args)
592{
593 /* condition on stored data value */
594 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
595 if (si->applet.ctx.table.data_type < 0) {
596 si->applet.ctx.cli.msg = "Unknown data type\n";
597 si->applet.st0 = STAT_CLI_PRINT;
598 return;
599 }
600
601 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
602 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
603 si->applet.st0 = STAT_CLI_PRINT;
604 return;
605 }
606
607 si->applet.ctx.table.data_op = get_std_op(args[4]);
608 if (si->applet.ctx.table.data_op < 0) {
609 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
610 si->applet.st0 = STAT_CLI_PRINT;
611 return;
612 }
613
614 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
615 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
616 si->applet.st0 = STAT_CLI_PRINT;
617 return;
618 }
619}
620
621static void stats_sock_table_request(struct stream_interface *si, char **args, bool show)
622{
623 si->applet.ctx.table.data_type = -1;
624 si->applet.state = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200625 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900626 si->applet.ctx.table.proxy = NULL;
627 si->applet.ctx.table.entry = NULL;
Simon Hormanc88b8872011-06-15 15:18:49 +0900628 if (show)
629 si->applet.st0 = STAT_CLI_O_TAB;
630 else
631 si->applet.st0 = STAT_CLI_O_CLR;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900632
633 if (*args[2]) {
634 si->applet.ctx.table.target = find_stktable(args[2]);
635 if (!si->applet.ctx.table.target) {
636 si->applet.ctx.cli.msg = "No such table\n";
637 si->applet.st0 = STAT_CLI_PRINT;
638 return;
639 }
640 }
641 else {
642 if (!show)
643 goto err_args;
644 return;
645 }
646
647 if (strcmp(args[3], "key") == 0)
648 stats_sock_table_key_request(si, args, show);
Simon Hormanc88b8872011-06-15 15:18:49 +0900649 else if (strncmp(args[3], "data.", 5) == 0)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900650 stats_sock_table_data_request(si, args);
Simon Hormanc88b8872011-06-15 15:18:49 +0900651 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900652 goto err_args;
653
654 return;
655
656err_args:
657 if (show)
658 si->applet.ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
659 else
Simon Hormanc88b8872011-06-15 15:18:49 +0900660 si->applet.ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
Simon Hormand5b9fd92011-06-15 15:18:48 +0900661 si->applet.st0 = STAT_CLI_PRINT;
662}
663
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200664/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200665 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100666 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200667 * designating the function which will have to process the request, which can
668 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200669 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900670static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200671{
Willy Tarreaubc4af052011-02-13 13:25:14 +0100672 struct session *s = si->applet.private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200673 char *args[MAX_STATS_ARGS + 1];
674 int arg;
675
676 while (isspace((unsigned char)*line))
677 line++;
678
679 arg = 0;
680 args[arg] = line;
681
682 while (*line && arg < MAX_STATS_ARGS) {
683 if (isspace((unsigned char)*line)) {
684 *line++ = '\0';
685
686 while (isspace((unsigned char)*line))
687 line++;
688
689 args[++arg] = line;
690 continue;
691 }
692
693 line++;
694 }
695
696 while (++arg <= MAX_STATS_ARGS)
697 args[arg] = line;
698
Willy Tarreau295a8372011-03-10 11:25:07 +0100699 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200700 if (strcmp(args[0], "show") == 0) {
701 if (strcmp(args[1], "stat") == 0) {
702 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100703 si->applet.ctx.stats.flags |= STAT_BOUND;
704 si->applet.ctx.stats.iid = atoi(args[2]);
705 si->applet.ctx.stats.type = atoi(args[3]);
706 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200707 }
708
Willy Tarreau295a8372011-03-10 11:25:07 +0100709 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
710 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
711 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100712 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200713 }
714 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100715 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
716 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
717 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100718 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200719 }
720 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100721 si->applet.state = STAT_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200722 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100723 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100724 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200725 return 1;
726 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100727 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100728 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100729 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100730 si->applet.ctx.sess.target = NULL;
731 si->applet.ctx.sess.section = 0; /* start with session status */
732 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100733 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200734 }
735 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200736 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100737 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100738 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200739 return 1;
740 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200741 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100742 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200743 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100744 si->applet.ctx.errors.iid = -1;
745 si->applet.ctx.errors.px = NULL;
746 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100747 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200748 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200749 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900750 stats_sock_table_request(si, args, true);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200751 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200752 else { /* neither "stat" nor "info" nor "sess" nor "errors" no "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200753 return 0;
754 }
755 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200756 else if (strcmp(args[0], "clear") == 0) {
757 if (strcmp(args[1], "counters") == 0) {
758 struct proxy *px;
759 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200760 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200761 int clrall = 0;
762
763 if (strcmp(args[2], "all") == 0)
764 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200765
Willy Tarreau6162db22009-10-10 17:13:00 +0200766 /* check permissions */
767 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
768 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100769 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100770 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200771 return 1;
772 }
773
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200774 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100775 if (clrall) {
776 memset(&px->be_counters, 0, sizeof(px->be_counters));
777 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
778 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200779 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100780 px->be_counters.conn_max = 0;
781 px->be_counters.p.http.rps_max = 0;
782 px->be_counters.sps_max = 0;
783 px->be_counters.cps_max = 0;
784 px->be_counters.nbpend_max = 0;
785
786 px->fe_counters.conn_max = 0;
787 px->fe_counters.p.http.rps_max = 0;
788 px->fe_counters.sps_max = 0;
789 px->fe_counters.cps_max = 0;
790 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200791 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200792
793 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200794 if (clrall)
795 memset(&sv->counters, 0, sizeof(sv->counters));
796 else {
797 sv->counters.cur_sess_max = 0;
798 sv->counters.nbpend_max = 0;
799 sv->counters.sps_max = 0;
800 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200801
802 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200803 if (li->counters) {
804 if (clrall)
805 memset(li->counters, 0, sizeof(*li->counters));
806 else
807 li->counters->conn_max = 0;
808 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200809 }
810
811 return 1;
812 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200813 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900814 stats_sock_table_request(si, args, false);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200815 /* end of processing */
816 return 1;
817 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200818 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200819 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200820 return 0;
821 }
822 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200823 else if (strcmp(args[0], "get") == 0) {
824 if (strcmp(args[1], "weight") == 0) {
825 struct proxy *px;
826 struct server *sv;
827
828 /* split "backend/server" and make <line> point to server */
829 for (line = args[2]; *line; line++)
830 if (*line == '/') {
831 *line++ = '\0';
832 break;
833 }
834
835 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100836 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100837 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200838 return 1;
839 }
840
841 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100842 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100843 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200844 return 1;
845 }
846
847 /* return server's effective weight at the moment */
848 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
849 buffer_feed(si->ib, trash);
850 return 1;
851 }
852 else { /* not "get weight" */
853 return 0;
854 }
855 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200856 else if (strcmp(args[0], "set") == 0) {
857 if (strcmp(args[1], "weight") == 0) {
858 struct proxy *px;
859 struct server *sv;
860 int w;
861
862 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100863 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100864 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200865 return 1;
866 }
867
868 /* split "backend/server" and make <line> point to server */
869 for (line = args[2]; *line; line++)
870 if (*line == '/') {
871 *line++ = '\0';
872 break;
873 }
874
875 if (!*line || !*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100876 si->applet.ctx.cli.msg = "Require 'backend/server' and 'weight' or 'weight%'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100877 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200878 return 1;
879 }
880
881 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100882 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100883 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200884 return 1;
885 }
886
Cyril Bonté613f0df2011-03-03 20:49:04 +0100887 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100888 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100889 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100890 return 1;
891 }
892
Willy Tarreau4483d432009-10-10 19:30:08 +0200893 /* if the weight is terminated with '%', it is set relative to
894 * the initial weight, otherwise it is absolute.
895 */
896 w = atoi(args[3]);
897 if (strchr(args[3], '%') != NULL) {
898 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100899 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100900 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200901 return 1;
902 }
903 w = sv->iweight * w / 100;
904 }
905 else {
906 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100907 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100908 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200909 return 1;
910 }
911 }
912
913 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100914 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 +0100915 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200916 return 1;
917 }
918
919 sv->uweight = w;
920
921 if (px->lbprm.algo & BE_LB_PROP_DYN) {
922 /* we must take care of not pushing the server to full throttle during slow starts */
923 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
924 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
925 else
926 sv->eweight = BE_WEIGHT_SCALE;
927 sv->eweight *= sv->uweight;
928 } else {
929 sv->eweight = sv->uweight;
930 }
931
932 /* static LB algorithms are a bit harder to update */
933 if (px->lbprm.update_server_eweight)
934 px->lbprm.update_server_eweight(sv);
935 else if (sv->eweight)
936 px->lbprm.set_server_status_up(sv);
937 else
938 px->lbprm.set_server_status_down(sv);
939
940 return 1;
941 }
Willy Tarreau7aabd112010-01-26 10:59:06 +0100942 else if (strcmp(args[1], "timeout") == 0) {
943 if (strcmp(args[2], "cli") == 0) {
944 unsigned timeout;
945 const char *res;
946
947 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100948 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100949 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100950 return 1;
951 }
952
953 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
954 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100955 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100956 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100957 return 1;
958 }
959
960 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
961 return 1;
962 }
963 else {
Willy Tarreau295a8372011-03-10 11:25:07 +0100964 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100965 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100966 return 1;
967 }
968 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200969 else if (strcmp(args[1], "maxconn") == 0) {
970 if (strcmp(args[2], "frontend") == 0) {
971 struct proxy *px;
972 struct listener *l;
973 int v;
974
975 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
976 si->applet.ctx.cli.msg = stats_permission_denied_msg;
977 si->applet.st0 = STAT_CLI_PRINT;
978 return 1;
979 }
980
981 if (!*args[3]) {
982 si->applet.ctx.cli.msg = "Frontend name expected.\n";
983 si->applet.st0 = STAT_CLI_PRINT;
984 return 1;
985 }
986
987 px = findproxy(args[3], PR_CAP_FE);
988 if (!px) {
989 si->applet.ctx.cli.msg = "No such frontend.\n";
990 si->applet.st0 = STAT_CLI_PRINT;
991 return 1;
992 }
993
994 if (!*args[4]) {
995 si->applet.ctx.cli.msg = "Integer value expected.\n";
996 si->applet.st0 = STAT_CLI_PRINT;
997 return 1;
998 }
999
1000 v = atoi(args[4]);
1001 /* check for unlimited values, we restore default setting (cfg_maxpconn) */
1002 if (v < 1) {
1003 si->applet.ctx.cli.msg = "Value out of range.\n";
1004 si->applet.st0 = STAT_CLI_PRINT;
1005 return 1;
1006 }
1007
1008 /* OK, the value is fine, so we assign it to the proxy and to all of
1009 * its listeners. The blocked ones will be dequeued.
1010 */
1011 px->maxconn = v;
1012 for (l = px->listen; l != NULL; l = l->next) {
1013 l->maxconn = v;
1014 if (l->state == LI_FULL)
1015 resume_listener(l);
1016 }
1017
1018 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1019 dequeue_all_listeners(&s->fe->listener_queue);
1020
1021 return 1;
1022 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001023 else if (strcmp(args[2], "global") == 0) {
1024 int v;
1025
1026 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1027 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1028 si->applet.st0 = STAT_CLI_PRINT;
1029 return 1;
1030 }
1031
1032 if (!*args[3]) {
1033 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1034 si->applet.st0 = STAT_CLI_PRINT;
1035 return 1;
1036 }
1037
1038 v = atoi(args[3]);
1039 if (v > global.hardmaxconn) {
1040 si->applet.ctx.cli.msg = "Value out of range.\n";
1041 si->applet.st0 = STAT_CLI_PRINT;
1042 return 1;
1043 }
1044
1045 /* check for unlimited values */
1046 if (v <= 0)
1047 v = global.hardmaxconn;
1048
1049 global.maxconn = v;
1050
1051 /* Dequeues all of the listeners waiting for a resource */
1052 if (!LIST_ISEMPTY(&global_listener_queue))
1053 dequeue_all_listeners(&global_listener_queue);
1054
1055 return 1;
1056 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001057 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001058 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001059 si->applet.st0 = STAT_CLI_PRINT;
1060 return 1;
1061 }
1062 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001063 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001064 return 0;
1065 }
1066 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001067 else if (strcmp(args[0], "enable") == 0) {
1068 if (strcmp(args[1], "server") == 0) {
1069 struct proxy *px;
1070 struct server *sv;
1071
1072 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001073 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001074 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001075 return 1;
1076 }
1077
1078 /* split "backend/server" and make <line> point to server */
1079 for (line = args[2]; *line; line++)
1080 if (*line == '/') {
1081 *line++ = '\0';
1082 break;
1083 }
1084
1085 if (!*line || !*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001086 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001087 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001088 return 1;
1089 }
1090
1091 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001092 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001093 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001094 return 1;
1095 }
1096
Cyril Bonté613f0df2011-03-03 20:49:04 +01001097 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001098 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001099 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001100 return 1;
1101 }
1102
Cyril Bontécd19e512010-01-31 22:34:03 +01001103 if (sv->state & SRV_MAINTAIN) {
1104 /* The server is really in maintenance, we can change the server state */
1105 if (sv->tracked) {
1106 /* If this server tracks the status of another one,
1107 * we must restore the good status.
1108 */
1109 if (sv->tracked->state & SRV_RUNNING) {
1110 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001111 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001112 } else {
1113 sv->state &= ~SRV_MAINTAIN;
1114 set_server_down(sv);
1115 }
1116 } else {
1117 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001118 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001119 }
1120 }
1121
1122 return 1;
1123 }
1124 else { /* unknown "enable" parameter */
1125 return 0;
1126 }
1127 }
1128 else if (strcmp(args[0], "disable") == 0) {
1129 if (strcmp(args[1], "server") == 0) {
1130 struct proxy *px;
1131 struct server *sv;
1132
1133 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001134 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001135 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001136 return 1;
1137 }
1138
1139 /* split "backend/server" and make <line> point to server */
1140 for (line = args[2]; *line; line++)
1141 if (*line == '/') {
1142 *line++ = '\0';
1143 break;
1144 }
1145
1146 if (!*line || !*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001147 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001148 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001149 return 1;
1150 }
1151
1152 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001153 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001154 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +01001155 return 1;
1156 }
1157
Cyril Bonté613f0df2011-03-03 20:49:04 +01001158 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001159 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001160 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001161 return 1;
1162 }
1163
Cyril Bontécd19e512010-01-31 22:34:03 +01001164 if (! (sv->state & SRV_MAINTAIN)) {
1165 /* Not already in maintenance, we can change the server state */
1166 sv->state |= SRV_MAINTAIN;
1167 set_server_down(sv);
1168 }
1169
1170 return 1;
1171 }
1172 else { /* unknown "disable" parameter */
1173 return 0;
1174 }
1175 }
1176 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001177 return 0;
1178 }
1179 return 1;
1180}
1181
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001182/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001183 * used to processes I/O from/to the stats unix socket. The system relies on a
1184 * state machine handling requests and various responses. We read a request,
1185 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001186 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1187 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001188 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001189 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001190static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001191{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001192 struct buffer *req = si->ob;
1193 struct buffer *res = si->ib;
1194 int reql;
1195 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001196
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001197 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1198 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001199
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001200 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001201 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001202 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001203 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001204 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001205 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001206 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001207 /* Let's close for real now. We just close the request
1208 * side, the conditions below will complete if needed.
1209 */
1210 si->shutw(si);
1211 break;
1212 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001213 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001214 /* ensure we have some output room left in the event we
1215 * would want to return some info right after parsing.
1216 */
1217 if (buffer_almost_full(si->ib))
1218 break;
1219
Willy Tarreau74b08c92010-09-08 17:04:31 +02001220 reql = buffer_get_line(si->ob, trash, sizeof(trash));
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001221 if (reql <= 0) { /* closed or EOL not found */
1222 if (reql == 0)
1223 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001224 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001225 continue;
1226 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001227
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001228 /* seek for a possible semi-colon. If we find one, we
1229 * replace it with an LF and skip only this part.
1230 */
1231 for (len = 0; len < reql; len++)
1232 if (trash[len] == ';') {
1233 trash[len] = '\n';
1234 reql = len + 1;
1235 break;
1236 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001237
Willy Tarreau816fc222009-10-04 07:36:58 +02001238 /* now it is time to check that we have a full line,
1239 * remove the trailing \n and possibly \r, then cut the
1240 * line.
1241 */
1242 len = reql - 1;
1243 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001244 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001245 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001246 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001247
Willy Tarreau816fc222009-10-04 07:36:58 +02001248 if (len && trash[len-1] == '\r')
1249 len--;
1250
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001251 trash[len] = '\0';
1252
Willy Tarreaubc4af052011-02-13 13:25:14 +01001253 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001254 if (len) {
1255 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001256 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001257 continue;
1258 }
1259 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001260 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001261 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001262 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001263 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001264 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001265 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001266 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001267 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001268 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001269 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001270 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001271 /* if prompt is disabled, print help on empty lines,
1272 * so that the user at least knows how to enable
1273 * prompt and find help.
1274 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001275 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001276 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001277 }
1278
1279 /* re-adjust req buffer */
1280 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001281 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001282 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001283 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001284 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001285 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001286 continue;
1287 }
1288
Willy Tarreaubc4af052011-02-13 13:25:14 +01001289 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001290 case STAT_CLI_PRINT:
Willy Tarreau295a8372011-03-10 11:25:07 +01001291 if (buffer_feed(si->ib, si->applet.ctx.cli.msg) < 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001292 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001293 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001294 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001295 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001296 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001297 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001298 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001299 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001300 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001301 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001302 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001303 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001304 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001305 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001306 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001307 if (stats_table_request(si, true))
1308 si->applet.st0 = STAT_CLI_PROMPT;
1309 break;
1310 case STAT_CLI_O_CLR:
1311 if (stats_table_request(si, false))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001312 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001313 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001314 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001315 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001316 break;
1317 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001318
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001319 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001320 if (si->applet.st0 == STAT_CLI_PROMPT) {
1321 if (buffer_feed(si->ib, si->applet.st1 ? "\n> " : "\n") < 0)
1322 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001323 }
1324
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001325 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001326 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001327 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001328
1329 /* Now we close the output if one of the writers did so,
1330 * or if we're not in interactive mode and the request
1331 * buffer is empty. This still allows pipelined requests
1332 * to be sent in non-interactive mode.
1333 */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001334 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->applet.st1 && !req->send_max)) {
1335 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001336 continue;
1337 }
1338
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001339 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001340 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001341 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001342 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001343
Willy Tarreaubc4af052011-02-13 13:25:14 +01001344 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001345 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1346 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1347 /* Other size has closed, let's abort if we have no more processing to do
1348 * and nothing more to consume. This is comparable to a broken pipe, so
1349 * we forward the close to the request side so that it flows upstream to
1350 * the client.
1351 */
1352 si->shutw(si);
1353 }
1354
Willy Tarreaubc4af052011-02-13 13:25:14 +01001355 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001356 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1357 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1358 /* We have no more processing to do, and nothing more to send, and
1359 * the client side has closed. So we'll forward this state downstream
1360 * on the response buffer.
1361 */
1362 si->shutr(si);
1363 res->flags |= BF_READ_NULL;
1364 }
1365
1366 /* update all other flags and resync with the other side */
1367 si->update(si);
1368
1369 /* we don't want to expire timeouts while we're processing requests */
1370 si->ib->rex = TICK_ETERNITY;
1371 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001372
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001373 out:
1374 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
1375 __FUNCTION__, __LINE__,
1376 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
1377
1378 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1379 /* check that we have released everything then unregister */
1380 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001381 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001382}
1383
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001384/* This function dumps statistics onto the stream interface's read buffer.
1385 * The data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001386 * It returns 0 as long as it does not complete, non-zero upon completion.
1387 * Some states are not used but it makes the code more similar to other
1388 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001389 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001390static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001391{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001392 struct proxy *px;
1393 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001394 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001395
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001396 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +02001397
Willy Tarreau295a8372011-03-10 11:25:07 +01001398 switch (si->applet.state) {
1399 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001400 /* the function had not been called yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001401 si->applet.state = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001402 /* fall through */
1403
Willy Tarreau295a8372011-03-10 11:25:07 +01001404 case STAT_ST_HEAD:
1405 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001406 print_csv_header(&msg);
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001407 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001408 return 0;
1409 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001410
Willy Tarreau295a8372011-03-10 11:25:07 +01001411 si->applet.state = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001412 /* fall through */
1413
Willy Tarreau295a8372011-03-10 11:25:07 +01001414 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001415 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001416 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001417 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001418 "Name: " PRODUCT_NAME "\n"
1419 "Version: " HAPROXY_VERSION "\n"
1420 "Release_date: " HAPROXY_DATE "\n"
1421 "Nbproc: %d\n"
1422 "Process_num: %d\n"
1423 "Pid: %d\n"
1424 "Uptime: %dd %dh%02dm%02ds\n"
1425 "Uptime_sec: %d\n"
1426 "Memmax_MB: %d\n"
1427 "Ulimit-n: %d\n"
1428 "Maxsock: %d\n"
1429 "Maxconn: %d\n"
Willy Tarreau91886b62011-09-07 14:38:31 +02001430 "Hard_maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001431 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001432 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001433 "PipesUsed: %d\n"
1434 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001435 "Tasks: %d\n"
1436 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001437 "node: %s\n"
1438 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001439 "",
1440 global.nbproc,
1441 relative_pid,
1442 pid,
1443 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1444 up,
1445 global.rlimit_memmax,
1446 global.rlimit_nofile,
Willy Tarreau91886b62011-09-07 14:38:31 +02001447 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001448 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001449 nb_tasks_cur, run_queue_cur,
1450 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001451 );
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001452 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001453 return 0;
1454 }
1455
Willy Tarreau295a8372011-03-10 11:25:07 +01001456 si->applet.ctx.stats.px = proxy;
1457 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1458 si->applet.ctx.stats.sv = NULL;
1459 si->applet.state = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001460 /* fall through */
1461
Willy Tarreau295a8372011-03-10 11:25:07 +01001462 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001463 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001464 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1465 while (si->applet.ctx.stats.px) {
1466 px = si->applet.ctx.stats.px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001467 /* skip the disabled proxies and non-networked ones */
1468 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001469 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001470 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001471 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001472 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001473
Willy Tarreau295a8372011-03-10 11:25:07 +01001474 si->applet.ctx.stats.px = px->next;
1475 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001476 }
1477 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001478 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001479
Willy Tarreau295a8372011-03-10 11:25:07 +01001480 si->applet.state = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001481 /* fall through */
1482
Willy Tarreau295a8372011-03-10 11:25:07 +01001483 case STAT_ST_END:
1484 si->applet.state = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001485 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001486
Willy Tarreau295a8372011-03-10 11:25:07 +01001487 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001488 return 1;
1489
1490 default:
1491 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001492 si->applet.state = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001493 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001494 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001495}
1496
1497
Cyril Bonté70be45d2010-10-12 00:14:35 +02001498/* We don't want to land on the posted stats page because a refresh will
1499 * repost the data. We don't want this to happen on accident so we redirect
1500 * the browse to the stats page with a GET.
1501 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001502static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001503{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001504 struct session *s = si->applet.private;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001505 struct chunk msg;
1506
1507 chunk_init(&msg, trash, sizeof(trash));
1508
Willy Tarreau295a8372011-03-10 11:25:07 +01001509 switch (si->applet.state) {
1510 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001511 chunk_printf(&msg,
1512 "HTTP/1.0 303 See Other\r\n"
1513 "Cache-Control: no-cache\r\n"
1514 "Content-Type: text/plain\r\n"
1515 "Connection: close\r\n"
1516 "Location: %s;st=%s",
Willy Tarreau295a8372011-03-10 11:25:07 +01001517 uri->uri_prefix, si->applet.ctx.stats.st_code);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001518 chunk_printf(&msg, "\r\n\r\n");
1519
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001520 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001521 return 0;
1522
1523 s->txn.status = 303;
1524
1525 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1526 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1527 if (!(s->flags & SN_FINST_MASK))
1528 s->flags |= SN_FINST_R;
1529
Willy Tarreau295a8372011-03-10 11:25:07 +01001530 si->applet.state = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001531 return 1;
1532 }
1533 return 1;
1534}
1535
1536
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001537/* This I/O handler runs as an applet embedded in a stream interface. It is
1538 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001539 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001540 * automatically unregisters itself once transfer is complete.
1541 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001542static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001543{
Willy Tarreaubc4af052011-02-13 13:25:14 +01001544 struct session *s = si->applet.private;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001545 struct buffer *req = si->ob;
1546 struct buffer *res = si->ib;
1547
1548 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1549 goto out;
1550
1551 /* check that the output is not closed */
1552 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001553 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001554
Willy Tarreaubc4af052011-02-13 13:25:14 +01001555 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001556 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001557 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001558 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001559 si->shutw(si);
1560 }
1561 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001562 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001563 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001564 si->shutw(si);
1565 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001566 }
1567 }
1568
1569 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
1570 si->shutw(si);
1571
Willy Tarreaubc4af052011-02-13 13:25:14 +01001572 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001573 si->shutr(si);
1574 res->flags |= BF_READ_NULL;
1575 }
1576
1577 /* update all other flags and resync with the other side */
1578 si->update(si);
1579
1580 /* we don't want to expire timeouts while we're processing requests */
1581 si->ib->rex = TICK_ETERNITY;
1582 si->ob->wex = TICK_ETERNITY;
1583
1584 out:
1585 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1586 /* check that we have released everything then unregister */
1587 stream_int_unregister_handler(si);
1588 }
1589}
1590
1591
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001592/* This function dumps statistics in HTTP format onto the stream interface's
1593 * read buffer. The data_ctx must have been zeroed first, and the flags
1594 * properly set. It returns 0 if it had to stop writing data and an I/O is
1595 * needed, 1 if the dump is finished and the session must be closed, or -1
1596 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001597 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001598static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001599{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001600 struct session *s = si->applet.private;
1601 struct buffer *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001602 struct proxy *px;
1603 struct chunk msg;
1604 unsigned int up;
1605
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001606 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001607
Willy Tarreau295a8372011-03-10 11:25:07 +01001608 switch (si->applet.state) {
1609 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001610 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001611 "HTTP/1.0 200 OK\r\n"
1612 "Cache-Control: no-cache\r\n"
1613 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001614 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001615 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001616
Willy Tarreau295a8372011-03-10 11:25:07 +01001617 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001618 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001619 uri->refresh);
1620
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001621 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001622
1623 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001624 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001625 return 0;
1626
Willy Tarreau91861262007-10-17 17:06:05 +02001627 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1628 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1629 if (!(s->flags & SN_FINST_MASK))
1630 s->flags |= SN_FINST_R;
1631
1632 if (s->txn.meth == HTTP_METH_HEAD) {
1633 /* that's all we return in case of HEAD request */
Willy Tarreau295a8372011-03-10 11:25:07 +01001634 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001635 return 1;
1636 }
1637
Willy Tarreau295a8372011-03-10 11:25:07 +01001638 si->applet.state = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001639 /* fall through */
1640
Willy Tarreau295a8372011-03-10 11:25:07 +01001641 case STAT_ST_HEAD:
1642 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001643 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001644 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001645 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1646 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001647 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001648 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1649 "<style type=\"text/css\"><!--\n"
1650 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001651 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001652 " font-size: 12px;"
1653 " font-weight: normal;"
1654 " color: black;"
1655 " background: white;"
1656 "}\n"
1657 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001658 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001659 "}\n"
1660 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001661 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001662 " margin-bottom: 0.5em;"
1663 "}\n"
1664 "h2 {"
1665 " font-family: helvetica, arial;"
1666 " font-size: x-large;"
1667 " font-weight: bold;"
1668 " font-style: italic;"
1669 " color: #6020a0;"
1670 " margin-top: 0em;"
1671 " margin-bottom: 0em;"
1672 "}\n"
1673 "h3 {"
1674 " font-family: helvetica, arial;"
1675 " font-size: 16px;"
1676 " font-weight: bold;"
1677 " color: #b00040;"
1678 " background: #e8e8d0;"
1679 " margin-top: 0em;"
1680 " margin-bottom: 0em;"
1681 "}\n"
1682 "li {"
1683 " margin-top: 0.25em;"
1684 " margin-right: 2em;"
1685 "}\n"
1686 ".hr {margin-top: 0.25em;"
1687 " border-color: black;"
1688 " border-bottom-style: solid;"
1689 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001690 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001691 ".total {background: #20D0D0;color: #ffff80;}\n"
1692 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001693 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001694 ".backend {background: #e8e8d0;}\n"
1695 ".active0 {background: #ff9090;}\n"
1696 ".active1 {background: #ffd020;}\n"
1697 ".active2 {background: #ffffa0;}\n"
1698 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001699 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1700 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1701 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001702 ".backup0 {background: #ff9090;}\n"
1703 ".backup1 {background: #ff80ff;}\n"
1704 ".backup2 {background: #c060ff;}\n"
1705 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001706 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1707 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1708 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001709 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001710 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001711 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001712 "a.px:link {color: #ffff40; text-decoration: none;}"
1713 "a.px:visited {color: #ffff40; text-decoration: none;}"
1714 "a.px:hover {color: #ffffff; text-decoration: none;}"
1715 "a.lfsb:link {color: #000000; text-decoration: none;}"
1716 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1717 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1718 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001719 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001720 "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"
1721 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001722 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001723 "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 +02001724 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001725 "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 +02001726 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001727 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1728 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1729 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001730 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001731 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001732 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001733 (uri->flags&ST_SHNODE) ? " on " : "",
1734 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001735 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001736 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001737 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001738 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001739 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001740 return 0;
1741
Willy Tarreau295a8372011-03-10 11:25:07 +01001742 si->applet.state = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02001743 /* fall through */
1744
Willy Tarreau295a8372011-03-10 11:25:07 +01001745 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02001746 up = (now.tv_sec - start_date.tv_sec);
1747
1748 /* WARNING! this has to fit the first packet too.
1749 * We are around 3.5 kB, add adding entries will
1750 * become tricky if we want to support 4kB buffers !
1751 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001752 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001753 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001754 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1755 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001756 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001757 "<hr width=\"100%%\" class=\"hr\">\n"
1758 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001759 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001760 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001761 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001762 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1763 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
1764 "current conns = %d; current pipes = %d/%d<br>\n"
1765 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001766 "</td><td align=\"center\" nowrap>\n"
1767 "<table class=\"lgd\"><tr>\n"
1768 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1769 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1770 "</tr><tr>\n"
1771 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1772 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1773 "</tr><tr>\n"
1774 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1775 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1776 "</tr><tr>\n"
1777 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001778 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001779 "</tr><tr>\n"
1780 "<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 +02001781 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001782 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001783 "</td>"
1784 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1785 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1786 "",
1787 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001788 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1789 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1790 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001791 up / 86400, (up % 86400) / 3600,
1792 (up % 3600) / 60, (up % 60),
1793 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1794 global.rlimit_memmax ? " MB" : "",
1795 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001796 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001797 actconn, pipes_used, pipes_used+pipes_free,
1798 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +02001799 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001800
Willy Tarreau295a8372011-03-10 11:25:07 +01001801 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001802 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001803 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1804 uri->uri_prefix,
1805 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01001806 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001807 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001808 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001809 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1810 uri->uri_prefix,
1811 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01001812 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001813
Willy Tarreau55bb8452007-10-17 18:44:57 +02001814 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001815 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001816 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001817 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1818 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001819 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001820 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001821 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001822 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001823 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1824 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001825 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001826 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001827 }
Willy Tarreau91861262007-10-17 17:06:05 +02001828
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001829 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001830 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
1831 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001832 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
1833 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001834
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001835 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02001836 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
1837 uri->uri_prefix,
1838 (uri->refresh > 0) ? ";norefresh" : "");
1839
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001840 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001841 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001842 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1843 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
1844 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
1845 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
1846 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
1847 "</ul>"
1848 "</td>"
1849 "</tr></table>\n"
1850 ""
1851 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001852
Willy Tarreau295a8372011-03-10 11:25:07 +01001853 if (si->applet.ctx.stats.st_code) {
1854 if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001855 chunk_printf(&msg,
1856 "<p><div class=active3>"
1857 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1858 "Action processed successfully."
1859 "</div>\n", uri->uri_prefix);
1860 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001861 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_NONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001862 chunk_printf(&msg,
1863 "<p><div class=active2>"
1864 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1865 "Nothing has changed."
1866 "</div>\n", uri->uri_prefix);
1867 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001868 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_EXCD) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001869 chunk_printf(&msg,
1870 "<p><div class=active0>"
1871 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1872 "<b>Action not processed : the buffer couldn't store all the data.<br>"
1873 "You should retry with less servers at a time.</b>"
1874 "</div>\n", uri->uri_prefix);
1875 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001876 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DENY) == 0) {
Cyril Bonté474be412010-10-12 00:14:36 +02001877 chunk_printf(&msg,
1878 "<p><div class=active0>"
1879 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1880 "<b>Action denied.</b>"
1881 "</div>\n", uri->uri_prefix);
1882 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001883 else {
1884 chunk_printf(&msg,
1885 "<p><div class=active6>"
1886 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1887 "Unexpected result."
1888 "</div>\n", uri->uri_prefix);
1889 }
1890 chunk_printf(&msg,"<p>\n");
1891 }
1892
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001893 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001894 return 0;
1895 }
Willy Tarreau91861262007-10-17 17:06:05 +02001896
Willy Tarreau295a8372011-03-10 11:25:07 +01001897 si->applet.ctx.stats.px = proxy;
1898 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1899 si->applet.state = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02001900 /* fall through */
1901
Willy Tarreau295a8372011-03-10 11:25:07 +01001902 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02001903 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001904 while (si->applet.ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001905 if (buffer_almost_full(rep))
1906 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01001907 px = si->applet.ctx.stats.px;
Willy Tarreau91861262007-10-17 17:06:05 +02001908 /* skip the disabled proxies and non-networked ones */
1909 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001910 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001911 return 0;
1912
Willy Tarreau295a8372011-03-10 11:25:07 +01001913 si->applet.ctx.stats.px = px->next;
1914 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02001915 }
1916 /* here, we just have reached the last proxy */
1917
Willy Tarreau295a8372011-03-10 11:25:07 +01001918 si->applet.state = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02001919 /* fall through */
1920
Willy Tarreau295a8372011-03-10 11:25:07 +01001921 case STAT_ST_END:
1922 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001923 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001924 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001925 return 0;
1926 }
Willy Tarreau91861262007-10-17 17:06:05 +02001927
Willy Tarreau295a8372011-03-10 11:25:07 +01001928 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001929 /* fall through */
1930
Willy Tarreau295a8372011-03-10 11:25:07 +01001931 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02001932 return 1;
1933
1934 default:
1935 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001936 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001937 return -1;
1938 }
1939}
1940
1941
1942/*
1943 * Dumps statistics for a proxy.
1944 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1945 * ot non-zero if everything completed.
1946 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001947static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001948{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001949 struct session *s = si->applet.private;
1950 struct buffer *rep = si->ib;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001951 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001952 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02001953 struct chunk msg;
1954
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001955 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001956
Willy Tarreau295a8372011-03-10 11:25:07 +01001957 switch (si->applet.ctx.stats.px_st) {
1958 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02001959 /* we are on a new proxy */
1960
1961 if (uri && uri->scope) {
1962 /* we have a limited scope, we have to check the proxy name */
1963 struct stat_scope *scope;
1964 int len;
1965
1966 len = strlen(px->id);
1967 scope = uri->scope;
1968
1969 while (scope) {
1970 /* match exact proxy name */
1971 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1972 break;
1973
1974 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001975 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001976 break;
1977 scope = scope->next;
1978 }
1979
1980 /* proxy name not found : don't dump anything */
1981 if (scope == NULL)
1982 return 1;
1983 }
1984
Willy Tarreau295a8372011-03-10 11:25:07 +01001985 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
1986 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001987 return 1;
1988
Willy Tarreau295a8372011-03-10 11:25:07 +01001989 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02001990 /* fall through */
1991
Willy Tarreau295a8372011-03-10 11:25:07 +01001992 case STAT_PX_ST_TH:
1993 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
1994 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001995 /* A form to enable/disable this proxy servers */
1996 chunk_printf(&msg,
1997 "<form action=\"%s\" method=\"post\">",
1998 uri->uri_prefix);
1999 }
2000
Willy Tarreau55bb8452007-10-17 18:44:57 +02002001 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002002 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002003 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002004 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002005 "<th class=\"pxname\" width=\"10%%\"");
2006
2007 if (uri->flags&ST_SHLGNDS) {
2008 /* cap, mode, id */
2009 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
2010 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2011 px->uuid);
2012
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002013 chunk_printf(&msg, "\"");
2014 }
2015
2016 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002017 ">%s<a name=\"%s\"></a>"
2018 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002019 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002020 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002021 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002022 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002023 "<tr class=\"titre\">",
2024 (uri->flags & ST_SHLGNDS)?"<u>":"",
2025 px->id, px->id, px->id,
2026 (uri->flags & ST_SHLGNDS)?"</u>":"",
2027 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
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 heading for Enable or Disable server */
2031 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
2032 }
2033
2034 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002035 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002036 "<th colspan=3>Queue</th>"
2037 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002038 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002039 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002040 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002041 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002042 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002043 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002044 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002045 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002046 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002047 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002048 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002049 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2050 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002051 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002052
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002053 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002054 return 0;
2055 }
Willy Tarreau91861262007-10-17 17:06:05 +02002056
Willy Tarreau295a8372011-03-10 11:25:07 +01002057 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002058 /* fall through */
2059
Willy Tarreau295a8372011-03-10 11:25:07 +01002060 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002061 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002062 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002063 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
2064 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002065 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002066 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002067 "<tr class=\"frontend\">");
2068
Willy Tarreau295a8372011-03-10 11:25:07 +01002069 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002070 /* Column sub-heading for Enable or Disable server */
2071 chunk_printf(&msg, "<td></td>");
2072 }
2073
2074 chunk_printf(&msg,
2075 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002076 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002077 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2078 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002079 "",
2080 px->id, px->id);
2081
2082 if (px->mode == PR_MODE_HTTP) {
2083 chunk_printf(&msg,
2084 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01002085 "<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 +01002086 "",
2087 read_freq_ctr(&px->fe_req_per_sec),
2088 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002089 px->fe_counters.p.http.rps_max,
2090 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01002091 LIM2A2(px->fe_sps_lim, "-"));
2092 } else {
2093 chunk_printf(&msg,
2094 /* sessions rate : current, max, limit */
2095 "<td>%s</td><td>%s</td><td>%s</td>"
2096 "",
2097 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002098 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01002099 }
2100
2101 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002102 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002103 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002104 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002105 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002106 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002107
2108 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2109 if (px->mode == PR_MODE_HTTP) {
2110 int i;
2111
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002112 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002113
2114 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002115 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002116
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002117 chunk_printf(&msg, " other=%lld\"", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002118 }
2119
2120 chunk_printf(&msg,
2121 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002122 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002123 /* bytes : in, out */
2124 "<td>%s</td><td>%s</td>"
2125 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002126 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002127 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002128 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002129 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002130
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002131 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002132 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002133 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002134 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002135 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002136 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002137 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002138 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002139 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002140 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002141 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002142 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002143 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2144 U2H2(px->fe_counters.failed_req),
Willy Tarreau562515c2011-07-25 08:11:52 +02002145 px->state == PR_STREADY ? "OPEN" :
2146 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002147 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002148 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002149 /* pxid, name, queue cur, queue max, */
2150 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002151 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002152 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002153 /* bytes : in, out */
2154 "%lld,%lld,"
2155 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002156 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002157 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002158 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002159 /* warnings: retries, redispatches */
2160 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002161 /* server status : reflect frontend status */
2162 "%s,"
2163 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002164 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002165 /* pid, iid, sid, throttle, lbtot, tracked, type */
2166 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002167 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002168 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002169 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002170 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002171 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002172 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2173 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2174 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2175 px->fe_counters.failed_req,
Willy Tarreau562515c2011-07-25 08:11:52 +02002176 px->state == PR_STREADY ? "OPEN" :
2177 px->state == PR_STFULL ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002178 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002179 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002180 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002181
2182 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2183 if (px->mode == PR_MODE_HTTP) {
2184 int i;
2185
2186 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002187 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002188
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002189 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002190 } else {
2191 chunk_printf(&msg, ",,,,,,");
2192 }
2193
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002194 /* failed health analyses */
2195 chunk_printf(&msg, ",");
2196
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002197 /* requests : req_rate, req_rate_max, req_tot, */
2198 chunk_printf(&msg, "%u,%u,%lld,",
2199 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002200 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002201
Willy Tarreauae526782010-03-04 20:34:23 +01002202 /* errors: cli_aborts, srv_aborts */
2203 chunk_printf(&msg, ",,");
2204
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002205 /* finish with EOL */
2206 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002207 }
Willy Tarreau91861262007-10-17 17:06:05 +02002208
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002209 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002210 return 0;
2211 }
2212
Willy Tarreau295a8372011-03-10 11:25:07 +01002213 si->applet.ctx.stats.l = px->listen; /* may be NULL */
2214 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002215 /* fall through */
2216
Willy Tarreau295a8372011-03-10 11:25:07 +01002217 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002218 /* stats.l has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002219 for (; si->applet.ctx.stats.l != NULL; si->applet.ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002220 if (buffer_almost_full(rep))
2221 return 0;
2222
Willy Tarreau295a8372011-03-10 11:25:07 +01002223 l = si->applet.ctx.stats.l;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002224 if (!l->counters)
2225 continue;
2226
Willy Tarreau295a8372011-03-10 11:25:07 +01002227 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2228 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002229 break;
2230
Willy Tarreau295a8372011-03-10 11:25:07 +01002231 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002232 continue;
2233 }
2234
Willy Tarreau295a8372011-03-10 11:25:07 +01002235 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002236 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002237 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002238 /* Column sub-heading for Enable or Disable server */
2239 chunk_printf(&msg, "<td></td>");
2240 }
2241 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002242
2243 if (uri->flags&ST_SHLGNDS) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02002244 char str[INET6_ADDRSTRLEN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002245 int port;
2246
Willy Tarreau631f01c2011-09-05 00:36:48 +02002247 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002248
Willy Tarreau631f01c2011-09-05 00:36:48 +02002249 port = get_host_port(&l->addr);
2250 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2251 case AF_INET:
2252 chunk_printf(&msg, "IPv4: %s:%d, ", str, port);
2253 break;
2254 case AF_INET6:
2255 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, port);
2256 break;
2257 case AF_UNIX:
2258 chunk_printf(&msg, "unix, ");
2259 break;
2260 case -1:
2261 chunk_printf(&msg, "(%s), ", strerror(errno));
2262 break;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002263 }
2264
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002265 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002266 chunk_printf(&msg, "id: %d\"", l->luid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002267 }
2268
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002269 chunk_printf(&msg,
2270 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002271 ">%s<a name=\"%s/+%s\"></a>"
2272 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002273 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002274 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002275 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002276 "<td>%s</td><td>%s</td><td>%s</td>"
2277 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002278 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002279 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002280 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002281 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002282 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002283 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002284 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2285 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2286
2287 chunk_printf(&msg,
2288 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002289 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002290 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002291 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002292 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002293 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002294 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002295 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002296 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002297 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002298 "",
2299 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2300 U2H2(l->counters->failed_req),
Willy Tarreaua17c2d92011-07-25 08:16:20 +02002301 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002302 } else {
2303 chunk_printf(&msg,
2304 /* pxid, name, queue cur, queue max, */
2305 "%s,%s,,,"
2306 /* sessions: current, max, limit, total */
2307 "%d,%d,%d,%lld,"
2308 /* bytes: in, out */
2309 "%lld,%lld,"
2310 /* denied: req, resp */
2311 "%lld,%lld,"
2312 /* errors: request, connect, response */
2313 "%lld,,,"
2314 /* warnings: retries, redispatches */
2315 ",,"
2316 /* server status: reflect listener status */
2317 "%s,"
2318 /* rest of server: nothing */
2319 ",,,,,,,,"
2320 /* pid, iid, sid, throttle, lbtot, tracked, type */
2321 "%d,%d,%d,,,,%d,"
2322 /* rate, rate_lim, rate_max */
2323 ",,,"
2324 /* check_status, check_code, check_duration */
2325 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002326 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2327 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002328 /* failed health analyses */
2329 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002330 /* requests : req_rate, req_rate_max, req_tot, */
2331 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002332 /* errors: cli_aborts, srv_aborts */
2333 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002334 "\n",
2335 px->id, l->name,
2336 l->nbconn, l->counters->conn_max,
2337 l->maxconn, l->counters->cum_conn,
2338 l->counters->bytes_in, l->counters->bytes_out,
2339 l->counters->denied_req, l->counters->denied_resp,
2340 l->counters->failed_req,
2341 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2342 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2343 }
2344
2345 if (buffer_feed_chunk(rep, &msg) >= 0)
2346 return 0;
2347 }
2348
Willy Tarreau295a8372011-03-10 11:25:07 +01002349 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2350 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002351 /* fall through */
2352
Willy Tarreau295a8372011-03-10 11:25:07 +01002353 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002354 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002355 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002356 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 +02002357
Willy Tarreau4e33d862009-10-11 23:35:10 +02002358 if (buffer_almost_full(rep))
2359 return 0;
2360
Willy Tarreau295a8372011-03-10 11:25:07 +01002361 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002362
Willy Tarreau295a8372011-03-10 11:25:07 +01002363 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2364 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002365 break;
2366
Willy Tarreau295a8372011-03-10 11:25:07 +01002367 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002368 continue;
2369 }
2370
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002371 if (sv->tracked)
2372 svs = sv->tracked;
2373 else
2374 svs = sv;
2375
Willy Tarreau91861262007-10-17 17:06:05 +02002376 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002377 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002378 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002379 else if (svs->state & SRV_RUNNING) {
2380 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002381 sv_state = 3; /* UP */
2382 else
2383 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002384
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002385 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002386 sv_state += 2;
2387 }
Willy Tarreau91861262007-10-17 17:06:05 +02002388 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002389 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002390 sv_state = 1; /* going up */
2391 else
2392 sv_state = 0; /* DOWN */
2393
Willy Tarreau295a8372011-03-10 11:25:07 +01002394 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002395 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002396 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002397 continue;
2398 }
2399
Willy Tarreau295a8372011-03-10 11:25:07 +01002400 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002401 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2402 "UP %d/%d &darr;", "UP",
2403 "NOLB %d/%d &darr;", "NOLB",
2404 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002405 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002406 chunk_printf(&msg,
2407 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002408 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002409 );
2410 }
2411 else {
2412 chunk_printf(&msg,
2413 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002414 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002415 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2416 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002417
Willy Tarreau295a8372011-03-10 11:25:07 +01002418 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002419 chunk_printf(&msg,
2420 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2421 sv->id);
2422 }
2423
2424 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002425
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002426 if (uri->flags&ST_SHLGNDS) {
2427 char str[INET6_ADDRSTRLEN];
2428
Willy Tarreau631f01c2011-09-05 00:36:48 +02002429 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002430
Willy Tarreau631f01c2011-09-05 00:36:48 +02002431 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01002432 case AF_INET:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002433 chunk_printf(&msg, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
David du Colombier6f5ccb12011-03-10 22:26:24 +01002434 break;
2435 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002436 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2437 break;
2438 case AF_UNIX:
2439 chunk_printf(&msg, "unix, ");
2440 break;
2441 case -1:
2442 chunk_printf(&msg, "(%s), ", strerror(errno));
2443 break;
2444 default: /* address family not supported */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002445 break;
2446 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002447
2448 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002449 chunk_printf(&msg, "id: %d", sv->puid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002450
2451 /* cookie */
2452 if (sv->cookie) {
2453 struct chunk src;
2454
2455 chunk_printf(&msg, ", cookie: '");
2456
2457 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2458 chunk_htmlencode(&msg, &src);
2459
2460 chunk_printf(&msg, "'");
2461 }
2462
2463 chunk_printf(&msg, "\"");
2464 }
2465
2466 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002467 ">%s<a name=\"%s/%s\"></a>"
2468 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002469 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002470 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002471 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002472 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002473 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002474 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002475 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002476 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002477 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002478 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002479 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002480 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2481 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002482 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2483
2484 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2485 if (px->mode == PR_MODE_HTTP) {
2486 int i;
2487
2488 chunk_printf(&msg, " title=\"rsp codes:");
2489
2490 for (i = 1; i < 6; i++)
2491 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2492
2493 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2494 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002495
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002496 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002497 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002498 ">%s%s%s</td><td>%s</td>",
2499 (px->mode == PR_MODE_HTTP)?"<u>":"",
2500 U2H0(sv->counters.cum_sess),
2501 (px->mode == PR_MODE_HTTP)?"</u>":"",
2502 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002503
2504 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002505 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002506 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002507 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002508 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002509 /* errors : request, connect */
2510 "<td></td><td>%s</td>"
2511 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002512 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002513 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002514 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002515 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002516 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2517 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002518 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002519 sv->counters.cli_aborts,
2520 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002521 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002522 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002523
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002524 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002525 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002526
Cyril Bontécd19e512010-01-31 22:34:03 +01002527 if (sv->state & SRV_MAINTAIN) {
2528 chunk_printf(&msg, "%s ",
2529 human_time(now.tv_sec - sv->last_change, 1));
2530 chunk_printf(&msg, "MAINT");
2531 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002532 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2533 chunk_printf(&msg, "%s ",
2534 human_time(now.tv_sec - svs->last_change, 1));
2535 chunk_printf(&msg, "MAINT(via)");
2536 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002537 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002538 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002539 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002540
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002541 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002542 srv_hlt_st[sv_state],
2543 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2544 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002545 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002546
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002547 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002548 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002549 get_check_status_description(sv->check_status));
2550
2551 if (*sv->check_desc) {
2552 struct chunk src;
2553
2554 chunk_printf(&msg, ": ");
2555
2556 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2557 chunk_htmlencode(&msg, &src);
2558 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002559
Willy Tarreaue0454092010-02-26 12:29:07 +01002560 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002561 tv_iszero(&sv->check_start)?"":"* ",
2562 get_check_status_info(sv->check_status));
2563
2564 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002565 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002566
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002567 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002568 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002569 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002570 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002571
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002572 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002573 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002574 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002575 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002576 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002577 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002578 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002579 (sv->state & SRV_BACKUP) ? "-" : "Y",
2580 (sv->state & SRV_BACKUP) ? "Y" : "-");
2581
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002582 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002583 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002584 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002585 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2586
2587 if (svs->observe)
2588 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2589
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002590 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002591 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002592 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002593 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002594 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002595 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002596 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002597 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2598 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002599 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002600 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002601 "<td colspan=3></td>");
2602
2603 /* throttle */
2604 if ((sv->state & SRV_WARMINGUP) &&
2605 now.tv_sec < sv->last_change + sv->slowstart &&
2606 now.tv_sec >= sv->last_change) {
2607 unsigned int ratio;
2608 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002609 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002610 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002611 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002612 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002613 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002614 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002615 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002616 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2617 "UP %d/%d,", "UP,",
2618 "NOLB %d/%d,", "NOLB,",
2619 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002620 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002621 /* pxid, name */
2622 "%s,%s,"
2623 /* queue : current, max */
2624 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002625 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002626 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002627 /* bytes : in, out */
2628 "%lld,%lld,"
2629 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002630 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002631 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002632 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002633 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002634 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002635 "",
2636 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002637 sv->nbpend, sv->counters.nbpend_max,
2638 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002639 sv->counters.bytes_in, sv->counters.bytes_out,
2640 sv->counters.failed_secu,
2641 sv->counters.failed_conns, sv->counters.failed_resp,
2642 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002643
Willy Tarreau55bb8452007-10-17 18:44:57 +02002644 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002645 if (sv->state & SRV_MAINTAIN) {
2646 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002647 }
2648 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2649 chunk_printf(&msg, "MAINT(via),");
2650 }
2651 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002652 chunk_printf(&msg,
2653 srv_hlt_st[sv_state],
2654 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2655 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2656 }
Willy Tarreau91861262007-10-17 17:06:05 +02002657
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002658 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002659 /* weight, active, backup */
2660 "%d,%d,%d,"
2661 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002662 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002663 (sv->state & SRV_BACKUP) ? 0 : 1,
2664 (sv->state & SRV_BACKUP) ? 1 : 0);
2665
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002666 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002667 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002668 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002669 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002670 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002671 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002672 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002673 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002674 ",,,,");
2675
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002676 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002677 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002678 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002679 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002680 LIM2A0(sv->maxqueue, ""),
2681 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002682
2683 /* throttle */
2684 if ((sv->state & SRV_WARMINGUP) &&
2685 now.tv_sec < sv->last_change + sv->slowstart &&
2686 now.tv_sec >= sv->last_change) {
2687 unsigned int ratio;
2688 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002689 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002690 }
2691
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002692 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002693 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002694
2695 /* tracked */
2696 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002697 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002698 sv->tracked->proxy->id, sv->tracked->id);
2699 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002700 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002701
Willy Tarreau7f062c42009-03-05 18:43:00 +01002702 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002703 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002704
2705 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002706 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002707 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002708 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002709
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002710 if (sv->state & SRV_CHECKED) {
2711 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002712 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002713
2714 /* check_code */
2715 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002716 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002717 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002718 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002719
2720 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002721 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002722 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002723 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002724 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002725
2726 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002727 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002728 }
2729
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002730 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2731 if (px->mode == PR_MODE_HTTP) {
2732 int i;
2733
2734 for (i=1; i<6; i++)
2735 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2736
2737 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2738 } else {
2739 chunk_printf(&msg, ",,,,,,");
2740 }
2741
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002742 /* failed health analyses */
2743 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2744
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002745 /* requests : req_rate, req_rate_max, req_tot, */
2746 chunk_printf(&msg, ",,,");
2747
Willy Tarreauae526782010-03-04 20:34:23 +01002748 /* errors: cli_aborts, srv_aborts */
2749 chunk_printf(&msg, "%lld,%lld,",
2750 sv->counters.cli_aborts, sv->counters.srv_aborts);
2751
Willy Tarreau7f062c42009-03-05 18:43:00 +01002752 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002753 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002754 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002755 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002756 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002757 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002758
Willy Tarreau295a8372011-03-10 11:25:07 +01002759 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02002760 /* fall through */
2761
Willy Tarreau295a8372011-03-10 11:25:07 +01002762 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02002763 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002764 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002765 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2766 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002767 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01002768 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002769 /* Column sub-heading for Enable or Disable server */
2770 chunk_printf(&msg, "<td></td>");
2771 }
2772 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002773
2774 if (uri->flags&ST_SHLGNDS) {
2775 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002776 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002777 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002778
2779 /* cookie */
2780 if (px->cookie_name) {
2781 struct chunk src;
2782
2783 chunk_printf(&msg, ", cookie: '");
2784
2785 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2786 chunk_htmlencode(&msg, &src);
2787
2788 chunk_printf(&msg, "'");
2789 }
2790
2791 chunk_printf(&msg, "\"");
2792
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002793 }
2794
2795 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002796 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01002797 ">%s<a name=\"%s/Backend\"></a>"
2798 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002799 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002800 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002801 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002802 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002803 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002804 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002805 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002806 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002807 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
2808 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02002809
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002810 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002811 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002812 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002813 "<td"
2814 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002815 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002816
2817 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2818 if (px->mode == PR_MODE_HTTP) {
2819 int i;
2820
2821 chunk_printf(&msg, " title=\"rsp codes:");
2822
2823 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002824 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002825
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002826 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002827 }
2828
2829 chunk_printf(&msg,
2830 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002831 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002832 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002833 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002834 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002835 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002836 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01002837 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002838 U2H7(px->be_counters.cum_lbconn),
2839 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002840
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002841 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002842 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002843 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002844 /* errors : request, connect */
2845 "<td></td><td>%s</td>"
2846 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002847 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002848 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002849 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002850 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02002851 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002852 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02002853 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002854 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2855 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002856 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002857 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
2858 U2H2(px->be_counters.failed_conns),
2859 px->be_counters.cli_aborts,
2860 px->be_counters.srv_aborts,
2861 U2H5(px->be_counters.failed_resp),
2862 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002863 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01002864 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2865 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01002866 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002867 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002868
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002869 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002870 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002871 "<td class=ac>&nbsp;</td><td>%d</td>"
2872 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002873 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002874 "</tr>",
2875 px->down_trans,
2876 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002877 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002878 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002879 /* pxid, name */
2880 "%s,BACKEND,"
2881 /* queue : current, max */
2882 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002883 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002884 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002885 /* bytes : in, out */
2886 "%lld,%lld,"
2887 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002888 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002889 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002890 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002891 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002892 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002893 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02002894 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002895 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02002896 * active and backups. */
2897 "%s,"
2898 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002899 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002900 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002901 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002902 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002903 /* rate, rate_lim, rate_max, */
2904 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002905 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002906 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002907 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002908 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2909 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2910 px->be_counters.bytes_in, px->be_counters.bytes_out,
2911 px->be_counters.denied_req, px->be_counters.denied_resp,
2912 px->be_counters.failed_conns, px->be_counters.failed_resp,
2913 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01002914 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01002915 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002916 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002917 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01002918 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002919 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002920 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002921 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002922 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002923
2924 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2925 if (px->mode == PR_MODE_HTTP) {
2926 int i;
2927
2928 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002929 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002930
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002931 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002932 } else {
2933 chunk_printf(&msg, ",,,,,,");
2934 }
2935
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002936 /* failed health analyses */
2937 chunk_printf(&msg, ",");
2938
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002939 /* requests : req_rate, req_rate_max, req_tot, */
2940 chunk_printf(&msg, ",,,");
2941
Willy Tarreauae526782010-03-04 20:34:23 +01002942 /* errors: cli_aborts, srv_aborts */
2943 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002944 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01002945
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002946 /* finish with EOL */
2947 chunk_printf(&msg, "\n");
2948
Willy Tarreau55bb8452007-10-17 18:44:57 +02002949 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002950 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002951 return 0;
2952 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002953
Willy Tarreau295a8372011-03-10 11:25:07 +01002954 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002955 /* fall through */
2956
Willy Tarreau295a8372011-03-10 11:25:07 +01002957 case STAT_PX_ST_END:
2958 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002959 chunk_printf(&msg, "</table>");
2960
Willy Tarreau295a8372011-03-10 11:25:07 +01002961 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002962 /* close the form used to enable/disable this proxy servers */
2963 chunk_printf(&msg,
2964 "Choose the action to perform on the checked servers : "
2965 "<select name=action>"
2966 "<option value=\"\"></option>"
2967 "<option value=\"disable\">Disable</option>"
2968 "<option value=\"enable\">Enable</option>"
2969 "</select>"
2970 "<input type=\"hidden\" name=\"b\" value=\"%s\">"
2971 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2972 "</form>",
2973 px->id);
2974 }
2975
2976 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02002977
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002978 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002979 return 0;
2980 }
Willy Tarreau91861262007-10-17 17:06:05 +02002981
Willy Tarreau295a8372011-03-10 11:25:07 +01002982 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002983 /* fall through */
2984
Willy Tarreau295a8372011-03-10 11:25:07 +01002985 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002986 return 1;
2987
2988 default:
2989 /* unknown state, we should put an abort() here ! */
2990 return 1;
2991 }
2992}
2993
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002994/* This function dumps a complete session state onto the stream intreface's
2995 * read buffer. The data_ctx must have been zeroed first, and the flags
2996 * properly set. The session has to be set in data_ctx.sess.target. It returns
2997 * 0 if the output buffer is full and it needs to be called again, otherwise
2998 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002999 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003000static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003001{
3002 struct tm tm;
3003 struct chunk msg;
3004 struct session *sess;
3005 extern const char *monthname[12];
3006 char pn[INET6_ADDRSTRLEN];
3007
3008 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau295a8372011-03-10 11:25:07 +01003009 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003010
Willy Tarreau295a8372011-03-10 11:25:07 +01003011 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003012 /* session changed, no need to go any further */
3013 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003014 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003015 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003016 si->applet.ctx.sess.target = NULL;
3017 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003018 return 1;
3019 }
3020
Willy Tarreau295a8372011-03-10 11:25:07 +01003021 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003022 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003023 si->applet.ctx.sess.uid = sess->uniq_id;
3024 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003025 /* fall through */
3026
3027 case 1:
3028 chunk_printf(&msg,
3029 "%p: id=%u, proto=%s",
3030 sess,
3031 sess->uniq_id,
3032 sess->listener->proto->name);
3033
Willy Tarreau631f01c2011-09-05 00:36:48 +02003034 switch (addr_to_str(&sess->si[0].addr.c.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003035 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003036 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003037 chunk_printf(&msg, " source=%s:%d\n",
3038 pn, get_host_port(&sess->si[0].addr.c.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003039 break;
3040 case AF_UNIX:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003041 chunk_printf(&msg, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003042 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003043 default:
3044 /* no more information to print right now */
3045 chunk_printf(&msg, "\n");
3046 break;
3047 }
3048
3049 chunk_printf(&msg,
3050 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003051 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003052
3053 chunk_printf(&msg,
3054 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)\n",
3055 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3056 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3057 sess->listener ? sess->listener->luid : 0);
3058
Willy Tarreau50943332011-09-02 17:33:05 +02003059 if (sess->be->cap & PR_CAP_BE)
3060 chunk_printf(&msg,
3061 " backend=%s (id=%u mode=%s) server=%s (id=%u)\n",
3062 sess->be->id,
3063 sess->be->uuid, sess->be->mode ? "http" : "tcp",
3064 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
3065 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
3066 else
3067 chunk_printf(&msg, " backend=<NONE> (id=-1 mode=-) server=<NONE> (id=-1)\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003068
3069 chunk_printf(&msg,
3070 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
3071 sess->task,
3072 sess->task->state,
3073 sess->task->nice, sess->task->calls,
3074 sess->task->expire ?
3075 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3076 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3077 TICKS_TO_MS(1000)) : "<NEVER>",
3078 task_in_rq(sess->task) ? ", running" : "");
3079
3080 get_localtime(sess->logs.accept_date.tv_sec, &tm);
3081 chunk_printf(&msg,
3082 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
3083 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3084 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3085 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3086
3087 chunk_printf(&msg,
3088 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3089 &sess->si[0],
3090 sess->si[0].state,
3091 sess->si[0].flags,
3092 sess->si[0].fd,
3093 sess->si[0].exp ?
3094 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3095 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3096 TICKS_TO_MS(1000)) : "<NEVER>",
3097 sess->si[0].err_type);
3098
3099 chunk_printf(&msg,
3100 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3101 &sess->si[1],
3102 sess->si[1].state,
3103 sess->si[1].flags,
3104 sess->si[1].fd,
3105 sess->si[1].exp ?
3106 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3107 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3108 TICKS_TO_MS(1000)) : "<NEVER>",
3109 sess->si[1].err_type);
3110
3111 chunk_printf(&msg,
3112 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3113 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3114 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3115
3116
3117 chunk_printf(&msg,
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02003118 " req=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003119 " an_exp=%s",
3120 sess->req,
3121 sess->req->flags, sess->req->analysers,
3122 sess->req->l, sess->req->send_max,
3123 sess->req->pipe ? sess->req->pipe->data : 0,
3124 sess->req->to_forward,
3125 sess->req->analyse_exp ?
3126 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3127 TICKS_TO_MS(1000)) : "<NEVER>");
3128
3129 chunk_printf(&msg,
3130 " rex=%s",
3131 sess->req->rex ?
3132 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3133 TICKS_TO_MS(1000)) : "<NEVER>");
3134
3135 chunk_printf(&msg,
3136 " wex=%s\n"
3137 " data=%p r=%d w=%d lr=%d total=%lld\n",
3138 sess->req->wex ?
3139 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3140 TICKS_TO_MS(1000)) : "<NEVER>",
3141 sess->req->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01003142 (int)(sess->req->r - sess->req->data),
3143 (int)(sess->req->w - sess->req->data),
3144 (int)(sess->req->lr - sess->req->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003145 sess->req->total);
3146
3147 chunk_printf(&msg,
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02003148 " res=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003149 " an_exp=%s",
3150 sess->rep,
3151 sess->rep->flags, sess->rep->analysers,
3152 sess->rep->l, sess->rep->send_max,
3153 sess->rep->pipe ? sess->rep->pipe->data : 0,
3154 sess->rep->to_forward,
3155 sess->rep->analyse_exp ?
3156 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3157 TICKS_TO_MS(1000)) : "<NEVER>");
3158
3159 chunk_printf(&msg,
3160 " rex=%s",
3161 sess->rep->rex ?
3162 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3163 TICKS_TO_MS(1000)) : "<NEVER>");
3164
3165 chunk_printf(&msg,
3166 " wex=%s\n"
3167 " data=%p r=%d w=%d lr=%d total=%lld\n",
3168 sess->rep->wex ?
3169 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3170 TICKS_TO_MS(1000)) : "<NEVER>",
3171 sess->rep->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01003172 (int)(sess->rep->r - sess->rep->data),
3173 (int)(sess->rep->w - sess->rep->data),
3174 (int)(sess->rep->lr - sess->rep->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003175 sess->rep->total);
3176
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003177 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003178 return 0;
3179
3180 /* use other states to dump the contents */
3181 }
3182 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003183 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003184 return 1;
3185}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003186
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003187/* This function dumps all sessions' states onto the stream intreface's
3188 * read buffer. The data_ctx must have been zeroed first, and the flags
3189 * properly set. It returns 0 if the output buffer is full and it needs
3190 * to be called again, otherwise non-zero. It is designed to be called
3191 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003192 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003193static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003194{
3195 struct chunk msg;
3196
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003197 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003198 /* If we're forced to shut down, we might have to remove our
3199 * reference to the last session being dumped.
3200 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003201 if (si->applet.state == STAT_ST_LIST) {
3202 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3203 LIST_DEL(&si->applet.ctx.sess.bref.users);
3204 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003205 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003206 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003207 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003208 }
3209
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003210 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003211
Willy Tarreau295a8372011-03-10 11:25:07 +01003212 switch (si->applet.state) {
3213 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003214 /* the function had not been called yet, let's prepare the
3215 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003216 * pointer to the first in the global list. When a target
3217 * session is being destroyed, it is responsible for updating
3218 * this pointer. We know we have reached the end when this
3219 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003220 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003221 LIST_INIT(&si->applet.ctx.sess.bref.users);
3222 si->applet.ctx.sess.bref.ref = sessions.n;
3223 si->applet.state = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003224 /* fall through */
3225
Willy Tarreau295a8372011-03-10 11:25:07 +01003226 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003227 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003228 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3229 LIST_DEL(&si->applet.ctx.sess.bref.users);
3230 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003231 }
3232
3233 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003234 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003235 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003236 struct session *curr_sess;
3237
Willy Tarreau295a8372011-03-10 11:25:07 +01003238 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003239
Willy Tarreau295a8372011-03-10 11:25:07 +01003240 if (si->applet.ctx.sess.target) {
3241 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003242 goto next_sess;
3243
Willy Tarreau295a8372011-03-10 11:25:07 +01003244 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003245 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003246 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003247 return 0;
3248
3249 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003250 LIST_DEL(&si->applet.ctx.sess.bref.users);
3251 LIST_INIT(&si->applet.ctx.sess.bref.users);
3252 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003253 break;
3254 }
3255
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003256 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003257 "%p: proto=%s",
3258 curr_sess,
3259 curr_sess->listener->proto->name);
3260
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003261
Willy Tarreau631f01c2011-09-05 00:36:48 +02003262 switch (addr_to_str(&curr_sess->si[0].addr.c.from, pn, sizeof(pn))) {
3263 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003264 case AF_INET6:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003265 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003266 " src=%s:%d fe=%s be=%s srv=%s",
3267 pn,
Willy Tarreau631f01c2011-09-05 00:36:48 +02003268 get_host_port(&curr_sess->si[0].addr.c.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003269 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003270 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003271 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003272 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003273 break;
3274 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02003275 chunk_printf(&msg,
3276 " src=unix:%d fe=%s be=%s srv=%s",
3277 curr_sess->listener->luid,
3278 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003279 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003280 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003281 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003282 break;
3283 }
3284
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003285 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003286 " ts=%02x age=%s calls=%d",
3287 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003288 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3289 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003290
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003291 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003292 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
3293 curr_sess->req->flags,
3294 curr_sess->req->l,
3295 curr_sess->req->analysers,
3296 curr_sess->req->rex ?
3297 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3298 TICKS_TO_MS(1000)) : "");
3299
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003300 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003301 ",wx=%s",
3302 curr_sess->req->wex ?
3303 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3304 TICKS_TO_MS(1000)) : "");
3305
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003306 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003307 ",ax=%s]",
3308 curr_sess->req->analyse_exp ?
3309 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3310 TICKS_TO_MS(1000)) : "");
3311
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003312 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003313 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
3314 curr_sess->rep->flags,
3315 curr_sess->rep->l,
3316 curr_sess->rep->analysers,
3317 curr_sess->rep->rex ?
3318 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3319 TICKS_TO_MS(1000)) : "");
3320
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003321 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003322 ",wx=%s",
3323 curr_sess->rep->wex ?
3324 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3325 TICKS_TO_MS(1000)) : "");
3326
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003327 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003328 ",ax=%s]",
3329 curr_sess->rep->analyse_exp ?
3330 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3331 TICKS_TO_MS(1000)) : "");
3332
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003333 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003334 " s0=[%d,%1xh,fd=%d,ex=%s]",
3335 curr_sess->si[0].state,
3336 curr_sess->si[0].flags,
3337 curr_sess->si[0].fd,
3338 curr_sess->si[0].exp ?
3339 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3340 TICKS_TO_MS(1000)) : "");
3341
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003342 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003343 " s1=[%d,%1xh,fd=%d,ex=%s]",
3344 curr_sess->si[1].state,
3345 curr_sess->si[1].flags,
3346 curr_sess->si[1].fd,
3347 curr_sess->si[1].exp ?
3348 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3349 TICKS_TO_MS(1000)) : "");
3350
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003351 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003352 " exp=%s",
3353 curr_sess->task->expire ?
3354 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3355 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003356 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003357 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003358
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003359 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003360
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003361 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003362 /* let's try again later from this session. We add ourselves into
3363 * this session's users so that it can remove us upon termination.
3364 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003365 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003366 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003367 }
3368
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003369 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003370 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003371 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003372
Willy Tarreau295a8372011-03-10 11:25:07 +01003373 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003374 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003375 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003376 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3377 else
3378 chunk_printf(&msg, "Session not found.\n");
3379
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003380 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003381 return 0;
3382
Willy Tarreau295a8372011-03-10 11:25:07 +01003383 si->applet.ctx.sess.target = NULL;
3384 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003385 return 1;
3386 }
3387
Willy Tarreau295a8372011-03-10 11:25:07 +01003388 si->applet.state = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003389 /* fall through */
3390
3391 default:
Willy Tarreau295a8372011-03-10 11:25:07 +01003392 si->applet.state = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003393 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003394 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003395}
3396
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003397/* This function dumps all tables' states onto the stream intreface's
3398 * read buffer. The data_ctx must have been zeroed first, and the flags
3399 * properly set. It returns 0 if the output buffer is full and it needs
3400 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003401 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003402static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003403{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003404 struct session *s = si->applet.private;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003405 struct chunk msg;
3406 struct ebmb_node *eb;
3407 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003408 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003409
3410 /*
Willy Tarreau295a8372011-03-10 11:25:07 +01003411 * We have 3 possible states in si->applet.state :
3412 * - STAT_ST_INIT : the first call
3413 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003414 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003415 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003416 * and the entry pointer points to the next entry to be dumped,
3417 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003418 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003419 * data though.
3420 */
3421
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003422 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003423 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau295a8372011-03-10 11:25:07 +01003424 if (si->applet.state == STAT_ST_LIST) {
3425 si->applet.ctx.table.entry->ref_cnt--;
3426 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003427 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003428 return 1;
3429 }
3430
3431 chunk_init(&msg, trash, sizeof(trash));
3432
Willy Tarreau295a8372011-03-10 11:25:07 +01003433 while (si->applet.state != STAT_ST_FIN) {
3434 switch (si->applet.state) {
3435 case STAT_ST_INIT:
3436 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3437 if (!si->applet.ctx.table.proxy)
3438 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003439
Willy Tarreau295a8372011-03-10 11:25:07 +01003440 si->applet.ctx.table.entry = NULL;
3441 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003442 break;
3443
Willy Tarreau295a8372011-03-10 11:25:07 +01003444 case STAT_ST_INFO:
3445 if (!si->applet.ctx.table.proxy ||
3446 (si->applet.ctx.table.target &&
3447 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
3448 si->applet.state = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003449 break;
3450 }
3451
Willy Tarreau295a8372011-03-10 11:25:07 +01003452 if (si->applet.ctx.table.proxy->table.size) {
Simon Hormanc88b8872011-06-15 15:18:49 +09003453 if (show && !stats_dump_table_head_to_buffer(&msg, si, si->applet.ctx.table.proxy,
3454 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003455 return 0;
3456
Willy Tarreau295a8372011-03-10 11:25:07 +01003457 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003458 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
3459 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003460 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003461 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003462 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3463 si->applet.ctx.table.entry->ref_cnt++;
3464 si->applet.state = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003465 break;
3466 }
3467 }
3468 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003469 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003470 break;
3471
Willy Tarreau295a8372011-03-10 11:25:07 +01003472 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003473 skip_entry = false;
3474
Willy Tarreau295a8372011-03-10 11:25:07 +01003475 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003476 /* we're filtering on some data contents */
3477 void *ptr;
3478 long long data;
3479
Willy Tarreau295a8372011-03-10 11:25:07 +01003480 dt = si->applet.ctx.table.data_type;
3481 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3482 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003483 dt);
3484
3485 data = 0;
3486 switch (stktable_data_types[dt].std_type) {
3487 case STD_T_SINT:
3488 data = stktable_data_cast(ptr, std_t_sint);
3489 break;
3490 case STD_T_UINT:
3491 data = stktable_data_cast(ptr, std_t_uint);
3492 break;
3493 case STD_T_ULL:
3494 data = stktable_data_cast(ptr, std_t_ull);
3495 break;
3496 case STD_T_FRQP:
3497 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003498 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003499 break;
3500 }
3501
3502 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003503 if ((data < si->applet.ctx.table.value &&
3504 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3505 si->applet.ctx.table.data_op == STD_OP_GT ||
3506 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3507 (data == si->applet.ctx.table.value &&
3508 (si->applet.ctx.table.data_op == STD_OP_NE ||
3509 si->applet.ctx.table.data_op == STD_OP_GT ||
3510 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3511 (data > si->applet.ctx.table.value &&
3512 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3513 si->applet.ctx.table.data_op == STD_OP_LT ||
3514 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003515 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003516 }
3517
Simon Hormanc88b8872011-06-15 15:18:49 +09003518 if (show && !skip_entry &&
3519 !stats_dump_table_entry_to_buffer(&msg, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003520 si->applet.ctx.table.entry))
3521 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003522
Willy Tarreau295a8372011-03-10 11:25:07 +01003523 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003524
Willy Tarreau295a8372011-03-10 11:25:07 +01003525 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003526 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003527 struct stksess *old = si->applet.ctx.table.entry;
3528 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3529 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3530 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003531 break;
3532 }
3533
Simon Hormanc88b8872011-06-15 15:18:49 +09003534
3535 if (show)
3536 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3537 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3538 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3539
Willy Tarreau295a8372011-03-10 11:25:07 +01003540 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
3541 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003542 break;
3543
Willy Tarreau295a8372011-03-10 11:25:07 +01003544 case STAT_ST_END:
3545 si->applet.state = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003546 break;
3547 }
3548 }
3549 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003550}
3551
Willy Tarreaud426a182010-03-05 14:58:26 +01003552/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003553 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3554 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3555 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3556 * lines are respected within the limit of 70 output chars. Lines that are
3557 * continuation of a previous truncated line begin with "+" instead of " "
3558 * after the offset. The new pointer is returned.
3559 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003560static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3561 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003562{
3563 int end;
3564 unsigned char c;
3565
3566 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003567 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003568 return ptr;
3569
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003570 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003571
Willy Tarreaud426a182010-03-05 14:58:26 +01003572 while (ptr < len && ptr < bsize) {
3573 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003574 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003575 if (out->len > end - 2)
3576 break;
3577 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003578 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003579 if (out->len > end - 3)
3580 break;
3581 out->str[out->len++] = '\\';
3582 switch (c) {
3583 case '\t': c = 't'; break;
3584 case '\n': c = 'n'; break;
3585 case '\r': c = 'r'; break;
3586 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003587 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003588 }
3589 out->str[out->len++] = c;
3590 } else {
3591 if (out->len > end - 5)
3592 break;
3593 out->str[out->len++] = '\\';
3594 out->str[out->len++] = 'x';
3595 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3596 out->str[out->len++] = hextab[c & 0xF];
3597 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003598 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003599 /* we had a line break, let's return now */
3600 out->str[out->len++] = '\n';
3601 *line = ptr;
3602 return ptr;
3603 }
3604 }
3605 /* we have an incomplete line, we return it as-is */
3606 out->str[out->len++] = '\n';
3607 return ptr;
3608}
3609
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003610/* This function dumps all captured errors onto the stream intreface's
3611 * read buffer. The data_ctx must have been zeroed first, and the flags
3612 * properly set. It returns 0 if the output buffer is full and it needs
3613 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003614 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003615static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003616{
3617 extern const char *monthname[12];
3618 struct chunk msg;
3619
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003620 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003621 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003622
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003623 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003624
Willy Tarreau295a8372011-03-10 11:25:07 +01003625 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003626 /* the function had not been called yet, let's prepare the
3627 * buffer for a response.
3628 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003629 struct tm tm;
3630
3631 get_localtime(date.tv_sec, &tm);
3632 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3633 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3634 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3635 error_snapshot_id);
3636
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003637 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003638 /* Socket buffer full. Let's try again later from the same point */
3639 return 0;
3640 }
3641
Willy Tarreau295a8372011-03-10 11:25:07 +01003642 si->applet.ctx.errors.px = proxy;
3643 si->applet.ctx.errors.buf = 0;
3644 si->applet.ctx.errors.bol = 0;
3645 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003646 }
3647
3648 /* we have two inner loops here, one for the proxy, the other one for
3649 * the buffer.
3650 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003651 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003652 struct error_snapshot *es;
3653
Willy Tarreau295a8372011-03-10 11:25:07 +01003654 if (si->applet.ctx.errors.buf == 0)
3655 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003656 else
Willy Tarreau295a8372011-03-10 11:25:07 +01003657 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003658
3659 if (!es->when.tv_sec)
3660 goto next;
3661
Willy Tarreau295a8372011-03-10 11:25:07 +01003662 if (si->applet.ctx.errors.iid >= 0 &&
3663 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
3664 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003665 goto next;
3666
Willy Tarreau295a8372011-03-10 11:25:07 +01003667 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003668 /* just print headers now */
3669
3670 char pn[INET6_ADDRSTRLEN];
3671 struct tm tm;
3672
3673 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003674 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003675 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003676 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003677
Willy Tarreau631f01c2011-09-05 00:36:48 +02003678 addr_to_str(&es->src, pn, sizeof(pn));
Willy Tarreau295a8372011-03-10 11:25:07 +01003679 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003680 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003681 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003682 " frontend %s (#%d): invalid request\n"
3683 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003684 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003685 " request length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003686 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau3fd6cec2010-03-25 06:45:07 +01003687 pn, es->sid, (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3688 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003689 es->srv ? es->srv->id : "<NONE>",
3690 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003691 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003692 es->len, es->pos);
3693 break;
3694 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003695 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003696 " backend %s (#%d) : invalid response\n"
3697 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003698 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003699 " response length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003700 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003701 pn, es->sid, es->oe->id, es->oe->uuid,
3702 es->srv ? es->srv->id : "<NONE>",
3703 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003704 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003705 es->len, es->pos);
3706 break;
3707 }
3708
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003709 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003710 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003711 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003712 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003713 si->applet.ctx.errors.ptr = 0;
3714 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003715 }
3716
Willy Tarreau295a8372011-03-10 11:25:07 +01003717 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003718 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003719 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003720 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003721 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau61b34732009-10-03 23:49:35 +02003722 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003723 goto next;
3724 }
3725
3726 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01003727 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003728 int newptr;
3729 int newline;
3730
Willy Tarreau295a8372011-03-10 11:25:07 +01003731 newline = si->applet.ctx.errors.bol;
3732 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
3733 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02003734 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003735
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003736 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003737 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003738 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003739 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003740 si->applet.ctx.errors.ptr = newptr;
3741 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003742 };
3743 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01003744 si->applet.ctx.errors.bol = 0;
3745 si->applet.ctx.errors.ptr = -1;
3746 si->applet.ctx.errors.buf++;
3747 if (si->applet.ctx.errors.buf > 1) {
3748 si->applet.ctx.errors.buf = 0;
3749 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003750 }
3751 }
3752
3753 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02003754 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003755}
3756
Willy Tarreaub24281b2011-02-13 13:16:36 +01003757struct si_applet http_stats_applet = {
3758 .name = "<STATS>", /* used for logging */
3759 .fct = http_stats_io_handler,
3760};
3761
Simon Horman9bd2c732011-06-15 15:18:44 +09003762static struct si_applet cli_applet = {
Willy Tarreaub24281b2011-02-13 13:16:36 +01003763 .name = "<CLI>", /* used for logging */
3764 .fct = cli_io_handler,
3765};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003766
Willy Tarreau10522fd2008-07-09 20:12:41 +02003767static struct cfg_kw_list cfg_kws = {{ },{
3768 { CFG_GLOBAL, "stats", stats_parse_global },
3769 { 0, NULL, NULL },
3770}};
3771
3772__attribute__((constructor))
3773static void __dumpstats_module_init(void)
3774{
3775 cfg_register_keywords(&cfg_kws);
3776}
3777
Willy Tarreau91861262007-10-17 17:06:05 +02003778/*
3779 * Local variables:
3780 * c-indent-level: 8
3781 * c-basic-offset: 8
3782 * End:
3783 */