blob: b95418e94bffc04382d0ff4e35aa6ac2fa0063af [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>
Willy Tarreau91861262007-10-17 17:06:05 +020018#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020021#include <pwd.h>
22#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020023
24#include <sys/socket.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27
Willy Tarreau10522fd2008-07-09 20:12:41 +020028#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020029#include <common/compat.h>
30#include <common/config.h>
31#include <common/debug.h>
32#include <common/memory.h>
33#include <common/mini-clist.h>
34#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020035#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020036#include <common/time.h>
37#include <common/uri_auth.h>
38#include <common/version.h>
39
Willy Tarreau91861262007-10-17 17:06:05 +020040#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020041
42#include <proto/backend.h>
43#include <proto/buffers.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020044#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020045#include <proto/dumpstats.h>
46#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010047#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020048#include <proto/log.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010049#include <proto/pipe.h>
Willy Tarreau2a0f4d22011-08-02 11:49:05 +020050#include <proto/protocols.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020051#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020052#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020053#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020054#include <proto/server.h>
Willy Tarreauc63190d2012-05-11 14:23:52 +020055#include <proto/sock_raw.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010056#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010057#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020058
Simon Horman9bd2c732011-06-15 15:18:44 +090059static int stats_dump_raw_to_buffer(struct stream_interface *si);
60static int stats_dump_full_sess_to_buffer(struct stream_interface *si);
61static int stats_dump_sess_to_buffer(struct stream_interface *si);
62static int stats_dump_errors_to_buffer(struct stream_interface *si);
Simon Hormanc88b8872011-06-15 15:18:49 +090063static int stats_table_request(struct stream_interface *si, bool show);
Simon Horman9bd2c732011-06-15 15:18:44 +090064static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
65static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri);
66
67static struct si_applet cli_applet;
68
69static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020070 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +020071 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +020072 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020073 " help : this message\n"
74 " prompt : toggle interactive mode with prompt\n"
75 " quit : disconnect\n"
76 " show info : report information about the running process\n"
77 " show stat : report counters for each proxy and server\n"
78 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +010079 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +020080 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +020081 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +020082 " set weight : change a server's weight\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +010083 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +020084 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +020085 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +020086 " disable : put a server or frontend in maintenance mode\n"
87 " enable : re-enable a server or frontend which is in maintenance mode\n"
88 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020089 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020090
Simon Horman9bd2c732011-06-15 15:18:44 +090091static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +020092 "Permission denied\n"
93 "";
94
Willy Tarreau295a8372011-03-10 11:25:07 +010095/* data transmission states for the stats responses */
96enum {
97 STAT_ST_INIT = 0,
98 STAT_ST_HEAD,
99 STAT_ST_INFO,
100 STAT_ST_LIST,
101 STAT_ST_END,
102 STAT_ST_FIN,
103};
104
105/* data transmission states for the stats responses inside a proxy */
106enum {
107 STAT_PX_ST_INIT = 0,
108 STAT_PX_ST_TH,
109 STAT_PX_ST_FE,
110 STAT_PX_ST_LI,
111 STAT_PX_ST_SV,
112 STAT_PX_ST_BE,
113 STAT_PX_ST_END,
114 STAT_PX_ST_FIN,
115};
116
Cyril Bonté19979e12012-04-04 12:57:21 +0200117extern const char *stat_status_codes[];
118
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200119/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +0200120 * a new stats socket. It returns a positive value upon success, 0 if the connection
121 * needs to be closed and ignored, or a negative value upon critical failure.
122 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900123static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200124{
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200125 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100126 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200127 copy_target(&s->target, &s->si[1].target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +0100128 s->si[1].applet.private = s;
129 s->si[1].applet.st1 = 0;
130 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200131
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200132 tv_zero(&s->logs.tv_request);
133 s->logs.t_queue = 0;
134 s->logs.t_connect = 0;
135 s->logs.t_data = 0;
136 s->logs.t_close = 0;
137 s->logs.bytes_in = s->logs.bytes_out = 0;
138 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
139 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200140
Willy Tarreaueb472682010-05-28 18:46:57 +0200141 s->req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
142
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200143 if (s->listener->timeout) {
144 s->req->rto = *s->listener->timeout;
145 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200146 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200147 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200148}
149
Willy Tarreau07e9e642010-08-17 21:48:17 +0200150/* allocate a new stats frontend named <name>, and return it
151 * (or NULL in case of lack of memory).
152 */
153static struct proxy *alloc_stats_fe(const char *name)
154{
155 struct proxy *fe;
156
157 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
158 if (!fe)
159 return NULL;
160
Willy Tarreau237250c2011-07-29 01:49:03 +0200161 init_new_proxy(fe);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200162
163 fe->last_change = now.tv_sec;
164 fe->id = strdup("GLOBAL");
165 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200166 fe->maxconn = 10; /* default to 10 concurrent connections */
167 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
168
Willy Tarreau07e9e642010-08-17 21:48:17 +0200169 return fe;
170}
171
Willy Tarreaufbee7132007-10-18 13:53:22 +0200172/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200173 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
174 * error message into the <err> buffer which will be preallocated. The trailing
175 * '\n' must not be written. The function must be called with <args> pointing to
176 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200177 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200178static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200179 struct proxy *defpx, char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200180{
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200181 if (!strcmp(args[1], "socket")) {
Andreas Kohn16171e22011-01-19 20:29:32 +0100182 struct sockaddr_un *su;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200183 int cur_arg;
184
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200185 if (*args[2] == 0) {
186 memprintf(err, "'%s %s' in global section expects a path to a UNIX socket", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200187 return -1;
188 }
189
190 if (global.stats_sock.state != LI_NEW) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200191 memprintf(err, "'%s %s' already specified in global section", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200192 return -1;
193 }
194
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200195 su = str2sun(args[2]);
Andreas Kohn16171e22011-01-19 20:29:32 +0100196 if (!su) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200197 memprintf(err, "'%s %s' : path would require truncation", args[0], args[1]);
Andreas Kohn16171e22011-01-19 20:29:32 +0100198 return -1;
199 }
200 memcpy(&global.stats_sock.addr, su, sizeof(struct sockaddr_un)); // guaranteed to fit
Willy Tarreaufbee7132007-10-18 13:53:22 +0200201
Willy Tarreau89a63132009-08-16 17:41:45 +0200202 if (!global.stats_fe) {
Willy Tarreau07e9e642010-08-17 21:48:17 +0200203 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200204 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200205 return -1;
206 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200207 }
208
Willy Tarreaufbee7132007-10-18 13:53:22 +0200209 global.stats_sock.state = LI_INIT;
Willy Tarreau3c63fd82011-09-07 18:00:47 +0200210 global.stats_sock.options = LI_O_UNLIMITED;
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200211 global.stats_sock.accept = session_accept;
212 global.stats_fe->accept = stats_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200213 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200214 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200215 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreaueb472682010-05-28 18:46:57 +0200216 global.stats_sock.frontend = global.stats_fe;
Willy Tarreau6162db22009-10-10 17:13:00 +0200217 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200218 global.stats_sock.maxconn = global.stats_fe->maxconn;
Willy Tarreau89a63132009-08-16 17:41:45 +0200219 global.stats_sock.timeout = &global.stats_fe->timeout.client;
220
221 global.stats_sock.next = global.stats_fe->listen;
222 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200223
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200224 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200225 while (*args[cur_arg]) {
226 if (!strcmp(args[cur_arg], "uid")) {
227 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
228 cur_arg += 2;
229 }
230 else if (!strcmp(args[cur_arg], "gid")) {
231 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
232 cur_arg += 2;
233 }
234 else if (!strcmp(args[cur_arg], "mode")) {
235 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
236 cur_arg += 2;
237 }
238 else if (!strcmp(args[cur_arg], "user")) {
239 struct passwd *user;
240 user = getpwnam(args[cur_arg + 1]);
241 if (!user) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200242 memprintf(err, "'%s %s' : unknown user '%s'", args[0], args[1], args[cur_arg + 1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200243 return -1;
244 }
245 global.stats_sock.perm.ux.uid = user->pw_uid;
246 cur_arg += 2;
247 }
248 else if (!strcmp(args[cur_arg], "group")) {
249 struct group *group;
250 group = getgrnam(args[cur_arg + 1]);
251 if (!group) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200252 memprintf(err, "'%s %s' : unknown group '%s'", args[0], args[1], args[cur_arg + 1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200253 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 {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200266 memprintf(err, "'%s %s' : '%s' only supports 'user', 'operator', and 'admin' (got '%s')",
267 args[0], args[1], args[cur_arg], args[cur_arg+1]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200268 return -1;
269 }
270 cur_arg += 2;
271 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200272 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200273 memprintf(err, "'%s %s' only supports 'user', 'uid', 'group', 'gid', 'level', and 'mode' (got '%s')",
274 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200275 return -1;
276 }
277 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100278
Willy Tarreaufbee7132007-10-18 13:53:22 +0200279 uxst_add_listener(&global.stats_sock);
280 global.maxsock++;
281 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200282 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100283 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200284 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100285
286 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200287 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100288 return -1;
289 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200290
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100291 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200292 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200293 return -1;
294 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200295 if (!global.stats_fe) {
296 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200297 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200298 return -1;
299 }
300 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200301 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200302 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200303 else if (!strcmp(args[1], "maxconn")) {
304 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200305
306 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200307 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200308 return -1;
309 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200310
311 if (!global.stats_fe) {
312 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200313 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200314 return -1;
315 }
316 }
317 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200318 }
319 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200320 memprintf(err, "'%s' only supports 'socket', 'maxconn' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200321 return -1;
322 }
323 return 0;
324}
325
Simon Horman9bd2c732011-06-15 15:18:44 +0900326static int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100327{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200328 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100329 "# pxname,svname,"
330 "qcur,qmax,"
331 "scur,smax,slim,stot,"
332 "bin,bout,"
333 "dreq,dresp,"
334 "ereq,econ,eresp,"
335 "wretr,wredis,"
336 "status,weight,act,bck,"
337 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200338 "pid,iid,sid,throttle,lbtot,tracked,type,"
339 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200340 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100341 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100342 "req_rate,req_rate_max,req_tot,"
343 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100344 "\n");
345}
346
Simon Hormand9366582011-06-15 15:18:45 +0900347/* print a string of text buffer to <out>. The format is :
348 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
349 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
350 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
351 */
352static int dump_text(struct chunk *out, const char *buf, int bsize)
353{
354 unsigned char c;
355 int ptr = 0;
356
357 while (buf[ptr] && ptr < bsize) {
358 c = buf[ptr];
359 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
360 if (out->len > out->size - 1)
361 break;
362 out->str[out->len++] = c;
363 }
364 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
365 if (out->len > out->size - 2)
366 break;
367 out->str[out->len++] = '\\';
368 switch (c) {
369 case ' ': c = ' '; break;
370 case '\t': c = 't'; break;
371 case '\n': c = 'n'; break;
372 case '\r': c = 'r'; break;
373 case '\e': c = 'e'; break;
374 case '\\': c = '\\'; break;
375 }
376 out->str[out->len++] = c;
377 }
378 else {
379 if (out->len > out->size - 4)
380 break;
381 out->str[out->len++] = '\\';
382 out->str[out->len++] = 'x';
383 out->str[out->len++] = hextab[(c >> 4) & 0xF];
384 out->str[out->len++] = hextab[c & 0xF];
385 }
386 ptr++;
387 }
388
389 return ptr;
390}
391
392/* print a buffer in hexa.
393 * Print stopped if <bsize> is reached, or if no more place in the chunk.
394 */
395static int dump_binary(struct chunk *out, const char *buf, int bsize)
396{
397 unsigned char c;
398 int ptr = 0;
399
400 while (ptr < bsize) {
401 c = buf[ptr];
402
403 if (out->len > out->size - 2)
404 break;
405 out->str[out->len++] = hextab[(c >> 4) & 0xF];
406 out->str[out->len++] = hextab[c & 0xF];
407
408 ptr++;
409 }
410 return ptr;
411}
412
413/* Dump the status of a table to a stream interface's
414 * read buffer. It returns 0 if the output buffer is full
415 * and needs to be called again, otherwise non-zero.
416 */
417static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
418 struct proxy *proxy, struct proxy *target)
419{
420 struct session *s = si->applet.private;
421
422 chunk_printf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
423 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
424
425 /* any other information should be dumped here */
426
427 if (target && s->listener->perm.ux.level < ACCESS_LVL_OPER)
428 chunk_printf(msg, "# contents not dumped due to insufficient privileges\n");
429
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200430 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900431 return 0;
432
433 return 1;
434}
435
436/* Dump the a table entry to a stream interface's
437 * read buffer. It returns 0 if the output buffer is full
438 * and needs to be called again, otherwise non-zero.
439 */
440static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
441 struct proxy *proxy, struct stksess *entry)
442{
443 int dt;
444
445 chunk_printf(msg, "%p:", entry);
446
447 if (proxy->table.type == STKTABLE_TYPE_IP) {
448 char addr[INET_ADDRSTRLEN];
449 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
450 chunk_printf(msg, " key=%s", addr);
451 }
452 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
453 char addr[INET6_ADDRSTRLEN];
454 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
455 chunk_printf(msg, " key=%s", addr);
456 }
457 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
458 chunk_printf(msg, " key=%u", *(unsigned int *)entry->key.key);
459 }
460 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
461 chunk_printf(msg, " key=");
462 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
463 }
464 else {
465 chunk_printf(msg, " key=");
466 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
467 }
468
469 chunk_printf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
470
471 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
472 void *ptr;
473
474 if (proxy->table.data_ofs[dt] == 0)
475 continue;
476 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
477 chunk_printf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
478 else
479 chunk_printf(msg, " %s=", stktable_data_types[dt].name);
480
481 ptr = stktable_data_ptr(&proxy->table, entry, dt);
482 switch (stktable_data_types[dt].std_type) {
483 case STD_T_SINT:
484 chunk_printf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
485 break;
486 case STD_T_UINT:
487 chunk_printf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
488 break;
489 case STD_T_ULL:
490 chunk_printf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
491 break;
492 case STD_T_FRQP:
493 chunk_printf(msg, "%d",
494 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
495 proxy->table.data_arg[dt].u));
496 break;
497 }
498 }
499 chunk_printf(msg, "\n");
500
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200501 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900502 return 0;
503
504 return 1;
505}
506
Simon Horman17bce342011-06-15 15:18:47 +0900507static void stats_sock_table_key_request(struct stream_interface *si, char **args, bool show)
Simon Horman121f3052011-06-15 15:18:46 +0900508{
509 struct session *s = si->applet.private;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900510 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900511 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900512 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900513 unsigned char ip6_key[sizeof(struct in6_addr)];
Simon Horman121f3052011-06-15 15:18:46 +0900514
Simon Hormand5b9fd92011-06-15 15:18:48 +0900515 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900516
517 if (!*args[4]) {
518 si->applet.ctx.cli.msg = "Key value expected\n";
519 si->applet.st0 = STAT_CLI_PRINT;
520 return;
521 }
522
Simon Hormanc5b89f62011-06-15 15:18:50 +0900523 switch (px->table.type) {
524 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900525 uint32_key = htonl(inetaddr_host(args[4]));
526 static_table_key.key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900527 break;
528 case STKTABLE_TYPE_IPV6:
529 inet_pton(AF_INET6, args[4], ip6_key);
530 static_table_key.key = &ip6_key;
531 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900532 case STKTABLE_TYPE_INTEGER:
533 {
534 char *endptr;
535 unsigned long val;
536 errno = 0;
537 val = strtoul(args[4], &endptr, 10);
538 if ((errno == ERANGE && val == ULONG_MAX) ||
539 (errno != 0 && val == 0) || endptr == args[4] ||
540 val > 0xffffffff) {
541 si->applet.ctx.cli.msg = "Invalid key\n";
542 si->applet.st0 = STAT_CLI_PRINT;
543 return;
544 }
545 uint32_key = (uint32_t) val;
546 static_table_key.key = &uint32_key;
547 break;
548 }
549 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900550 case STKTABLE_TYPE_STRING:
551 static_table_key.key = args[4];
552 static_table_key.key_len = strlen(args[4]);
553 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900554 default:
Simon Horman17bce342011-06-15 15:18:47 +0900555 if (show)
Simon Hormancec9a222011-06-15 15:18:51 +0900556 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 +0900557 else
Simon Hormancec9a222011-06-15 15:18:51 +0900558 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 +0900559 si->applet.st0 = STAT_CLI_PRINT;
560 return;
561 }
562
563 /* check permissions */
564 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
565 si->applet.ctx.cli.msg = stats_permission_denied_msg;
566 si->applet.st0 = STAT_CLI_PRINT;
567 return;
568 }
569
570 ts = stktable_lookup_key(&px->table, &static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900571 if (!ts)
572 return;
573
574 if (show) {
575 struct chunk msg;
David du Colombier7af46052012-05-16 14:16:48 +0200576 chunk_init(&msg, trash, trashlen);
Simon Horman17bce342011-06-15 15:18:47 +0900577 if (!stats_dump_table_head_to_buffer(&msg, si, px, px))
578 return;
579 stats_dump_table_entry_to_buffer(&msg, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900580 return;
581 }
Simon Horman17bce342011-06-15 15:18:47 +0900582
583 if (ts->ref_cnt) {
Simon Horman121f3052011-06-15 15:18:46 +0900584 /* don't delete an entry which is currently referenced */
585 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
586 si->applet.st0 = STAT_CLI_PRINT;
587 return;
588 }
589
590 stksess_kill(&px->table, ts);
591}
592
Simon Hormand5b9fd92011-06-15 15:18:48 +0900593static void stats_sock_table_data_request(struct stream_interface *si, char **args)
594{
595 /* condition on stored data value */
596 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
597 if (si->applet.ctx.table.data_type < 0) {
598 si->applet.ctx.cli.msg = "Unknown data type\n";
599 si->applet.st0 = STAT_CLI_PRINT;
600 return;
601 }
602
603 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
604 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
605 si->applet.st0 = STAT_CLI_PRINT;
606 return;
607 }
608
609 si->applet.ctx.table.data_op = get_std_op(args[4]);
610 if (si->applet.ctx.table.data_op < 0) {
611 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
612 si->applet.st0 = STAT_CLI_PRINT;
613 return;
614 }
615
616 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
617 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
618 si->applet.st0 = STAT_CLI_PRINT;
619 return;
620 }
621}
622
623static void stats_sock_table_request(struct stream_interface *si, char **args, bool show)
624{
625 si->applet.ctx.table.data_type = -1;
626 si->applet.state = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200627 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900628 si->applet.ctx.table.proxy = NULL;
629 si->applet.ctx.table.entry = NULL;
Simon Hormanc88b8872011-06-15 15:18:49 +0900630 if (show)
631 si->applet.st0 = STAT_CLI_O_TAB;
632 else
633 si->applet.st0 = STAT_CLI_O_CLR;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900634
635 if (*args[2]) {
636 si->applet.ctx.table.target = find_stktable(args[2]);
637 if (!si->applet.ctx.table.target) {
638 si->applet.ctx.cli.msg = "No such table\n";
639 si->applet.st0 = STAT_CLI_PRINT;
640 return;
641 }
642 }
643 else {
644 if (!show)
645 goto err_args;
646 return;
647 }
648
649 if (strcmp(args[3], "key") == 0)
650 stats_sock_table_key_request(si, args, show);
Simon Hormanc88b8872011-06-15 15:18:49 +0900651 else if (strncmp(args[3], "data.", 5) == 0)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900652 stats_sock_table_data_request(si, args);
Simon Hormanc88b8872011-06-15 15:18:49 +0900653 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900654 goto err_args;
655
656 return;
657
658err_args:
659 if (show)
660 si->applet.ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
661 else
Simon Hormanc88b8872011-06-15 15:18:49 +0900662 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 +0900663 si->applet.st0 = STAT_CLI_PRINT;
664}
665
Willy Tarreau532a4502011-09-07 22:37:44 +0200666/* Expects to find a frontend named <arg> and returns it, otherwise displays various
667 * adequate error messages and returns NULL. This function also expects the session
668 * level to be admin.
669 */
670static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
671{
672 struct proxy *px;
673
674 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
675 si->applet.ctx.cli.msg = stats_permission_denied_msg;
676 si->applet.st0 = STAT_CLI_PRINT;
677 return NULL;
678 }
679
680 if (!*arg) {
681 si->applet.ctx.cli.msg = "A frontend name is expected.\n";
682 si->applet.st0 = STAT_CLI_PRINT;
683 return NULL;
684 }
685
686 px = findproxy(arg, PR_CAP_FE);
687 if (!px) {
688 si->applet.ctx.cli.msg = "No such frontend.\n";
689 si->applet.st0 = STAT_CLI_PRINT;
690 return NULL;
691 }
692 return px;
693}
694
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200695/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
696 * and returns the pointer to the server. Otherwise, display adequate error messages
697 * and returns NULL. This function also expects the session level to be admin. Note:
698 * the <arg> is modified to remove the '/'.
699 */
700static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
701{
702 struct proxy *px;
703 struct server *sv;
704 char *line;
705
706 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
707 si->applet.ctx.cli.msg = stats_permission_denied_msg;
708 si->applet.st0 = STAT_CLI_PRINT;
709 return NULL;
710 }
711
712 /* split "backend/server" and make <line> point to server */
713 for (line = arg; *line; line++)
714 if (*line == '/') {
715 *line++ = '\0';
716 break;
717 }
718
719 if (!*line || !*arg) {
720 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
721 si->applet.st0 = STAT_CLI_PRINT;
722 return NULL;
723 }
724
725 if (!get_backend_server(arg, line, &px, &sv)) {
726 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
727 si->applet.st0 = STAT_CLI_PRINT;
728 return NULL;
729 }
730
731 if (px->state == PR_STSTOPPED) {
732 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
733 si->applet.st0 = STAT_CLI_PRINT;
734 return NULL;
735 }
736
737 return sv;
738}
739
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200740/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200741 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100742 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200743 * designating the function which will have to process the request, which can
744 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200745 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900746static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200747{
Willy Tarreaubc4af052011-02-13 13:25:14 +0100748 struct session *s = si->applet.private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200749 char *args[MAX_STATS_ARGS + 1];
750 int arg;
751
752 while (isspace((unsigned char)*line))
753 line++;
754
755 arg = 0;
756 args[arg] = line;
757
758 while (*line && arg < MAX_STATS_ARGS) {
759 if (isspace((unsigned char)*line)) {
760 *line++ = '\0';
761
762 while (isspace((unsigned char)*line))
763 line++;
764
765 args[++arg] = line;
766 continue;
767 }
768
769 line++;
770 }
771
772 while (++arg <= MAX_STATS_ARGS)
773 args[arg] = line;
774
Willy Tarreau295a8372011-03-10 11:25:07 +0100775 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200776 if (strcmp(args[0], "show") == 0) {
777 if (strcmp(args[1], "stat") == 0) {
778 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100779 si->applet.ctx.stats.flags |= STAT_BOUND;
780 si->applet.ctx.stats.iid = atoi(args[2]);
781 si->applet.ctx.stats.type = atoi(args[3]);
782 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200783 }
784
Willy Tarreau295a8372011-03-10 11:25:07 +0100785 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
786 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
787 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100788 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200789 }
790 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100791 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
792 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
793 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100794 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200795 }
796 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100797 si->applet.state = STAT_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200798 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100799 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100800 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200801 return 1;
802 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100803 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100804 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100805 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100806 si->applet.ctx.sess.target = NULL;
807 si->applet.ctx.sess.section = 0; /* start with session status */
808 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100809 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200810 }
811 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200812 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100813 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100814 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200815 return 1;
816 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200817 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100818 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200819 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100820 si->applet.ctx.errors.iid = -1;
821 si->applet.ctx.errors.px = NULL;
822 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100823 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200824 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200825 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900826 stats_sock_table_request(si, args, true);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200827 }
Aman Guptaceafb4a2012-04-02 18:57:54 -0700828 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200829 return 0;
830 }
831 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200832 else if (strcmp(args[0], "clear") == 0) {
833 if (strcmp(args[1], "counters") == 0) {
834 struct proxy *px;
835 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200836 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200837 int clrall = 0;
838
839 if (strcmp(args[2], "all") == 0)
840 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200841
Willy Tarreau6162db22009-10-10 17:13:00 +0200842 /* check permissions */
843 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
844 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100845 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100846 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200847 return 1;
848 }
849
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200850 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100851 if (clrall) {
852 memset(&px->be_counters, 0, sizeof(px->be_counters));
853 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
854 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200855 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100856 px->be_counters.conn_max = 0;
857 px->be_counters.p.http.rps_max = 0;
858 px->be_counters.sps_max = 0;
859 px->be_counters.cps_max = 0;
860 px->be_counters.nbpend_max = 0;
861
862 px->fe_counters.conn_max = 0;
863 px->fe_counters.p.http.rps_max = 0;
864 px->fe_counters.sps_max = 0;
865 px->fe_counters.cps_max = 0;
866 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200867 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200868
869 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200870 if (clrall)
871 memset(&sv->counters, 0, sizeof(sv->counters));
872 else {
873 sv->counters.cur_sess_max = 0;
874 sv->counters.nbpend_max = 0;
875 sv->counters.sps_max = 0;
876 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200877
878 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200879 if (li->counters) {
880 if (clrall)
881 memset(li->counters, 0, sizeof(*li->counters));
882 else
883 li->counters->conn_max = 0;
884 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200885 }
886
Willy Tarreau81c25d02011-09-07 15:17:21 +0200887 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200888 return 1;
889 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200890 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900891 stats_sock_table_request(si, args, false);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200892 /* end of processing */
893 return 1;
894 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200895 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200896 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200897 return 0;
898 }
899 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200900 else if (strcmp(args[0], "get") == 0) {
901 if (strcmp(args[1], "weight") == 0) {
902 struct proxy *px;
903 struct server *sv;
904
905 /* split "backend/server" and make <line> point to server */
906 for (line = args[2]; *line; line++)
907 if (*line == '/') {
908 *line++ = '\0';
909 break;
910 }
911
912 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100913 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100914 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200915 return 1;
916 }
917
918 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100919 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100920 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200921 return 1;
922 }
923
924 /* return server's effective weight at the moment */
David du Colombier7af46052012-05-16 14:16:48 +0200925 snprintf(trash, trashlen, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200926 bi_putstr(si->ib, trash);
Willy Tarreau38338fa2009-10-10 18:37:29 +0200927 return 1;
928 }
929 else { /* not "get weight" */
930 return 0;
931 }
932 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200933 else if (strcmp(args[0], "set") == 0) {
934 if (strcmp(args[1], "weight") == 0) {
935 struct proxy *px;
936 struct server *sv;
937 int w;
938
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200939 sv = expect_server_admin(s, si, args[2]);
940 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +0200941 return 1;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200942 px = sv->proxy;
Willy Tarreau4483d432009-10-10 19:30:08 +0200943
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200944 /* if the weight is terminated with '%', it is set relative to
945 * the initial weight, otherwise it is absolute.
946 */
947 if (!*args[3]) {
948 si->applet.ctx.cli.msg = "Require <weight> or <weight%>.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100949 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100950 return 1;
951 }
952
Willy Tarreau4483d432009-10-10 19:30:08 +0200953 w = atoi(args[3]);
954 if (strchr(args[3], '%') != NULL) {
955 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100956 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100957 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200958 return 1;
959 }
960 w = sv->iweight * w / 100;
961 }
962 else {
963 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100964 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100965 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200966 return 1;
967 }
968 }
969
970 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100971 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 +0100972 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200973 return 1;
974 }
975
976 sv->uweight = w;
977
978 if (px->lbprm.algo & BE_LB_PROP_DYN) {
979 /* we must take care of not pushing the server to full throttle during slow starts */
980 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
981 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
982 else
983 sv->eweight = BE_WEIGHT_SCALE;
984 sv->eweight *= sv->uweight;
985 } else {
986 sv->eweight = sv->uweight;
987 }
988
989 /* static LB algorithms are a bit harder to update */
990 if (px->lbprm.update_server_eweight)
991 px->lbprm.update_server_eweight(sv);
992 else if (sv->eweight)
993 px->lbprm.set_server_status_up(sv);
994 else
995 px->lbprm.set_server_status_down(sv);
996
997 return 1;
998 }
Willy Tarreau7aabd112010-01-26 10:59:06 +0100999 else if (strcmp(args[1], "timeout") == 0) {
1000 if (strcmp(args[2], "cli") == 0) {
1001 unsigned timeout;
1002 const char *res;
1003
1004 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001005 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001006 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001007 return 1;
1008 }
1009
1010 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1011 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001012 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001013 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001014 return 1;
1015 }
1016
1017 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1018 return 1;
1019 }
1020 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001021 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001022 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001023 return 1;
1024 }
1025 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001026 else if (strcmp(args[1], "maxconn") == 0) {
1027 if (strcmp(args[2], "frontend") == 0) {
1028 struct proxy *px;
1029 struct listener *l;
1030 int v;
1031
Willy Tarreau532a4502011-09-07 22:37:44 +02001032 px = expect_frontend_admin(s, si, args[3]);
1033 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001034 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001035
1036 if (!*args[4]) {
1037 si->applet.ctx.cli.msg = "Integer value expected.\n";
1038 si->applet.st0 = STAT_CLI_PRINT;
1039 return 1;
1040 }
1041
1042 v = atoi(args[4]);
1043 /* check for unlimited values, we restore default setting (cfg_maxpconn) */
1044 if (v < 1) {
1045 si->applet.ctx.cli.msg = "Value out of range.\n";
1046 si->applet.st0 = STAT_CLI_PRINT;
1047 return 1;
1048 }
1049
1050 /* OK, the value is fine, so we assign it to the proxy and to all of
1051 * its listeners. The blocked ones will be dequeued.
1052 */
1053 px->maxconn = v;
1054 for (l = px->listen; l != NULL; l = l->next) {
1055 l->maxconn = v;
1056 if (l->state == LI_FULL)
1057 resume_listener(l);
1058 }
1059
1060 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1061 dequeue_all_listeners(&s->fe->listener_queue);
1062
1063 return 1;
1064 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001065 else if (strcmp(args[2], "global") == 0) {
1066 int v;
1067
1068 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1069 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1070 si->applet.st0 = STAT_CLI_PRINT;
1071 return 1;
1072 }
1073
1074 if (!*args[3]) {
1075 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1076 si->applet.st0 = STAT_CLI_PRINT;
1077 return 1;
1078 }
1079
1080 v = atoi(args[3]);
1081 if (v > global.hardmaxconn) {
1082 si->applet.ctx.cli.msg = "Value out of range.\n";
1083 si->applet.st0 = STAT_CLI_PRINT;
1084 return 1;
1085 }
1086
1087 /* check for unlimited values */
1088 if (v <= 0)
1089 v = global.hardmaxconn;
1090
1091 global.maxconn = v;
1092
1093 /* Dequeues all of the listeners waiting for a resource */
1094 if (!LIST_ISEMPTY(&global_listener_queue))
1095 dequeue_all_listeners(&global_listener_queue);
1096
1097 return 1;
1098 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001099 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001100 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001101 si->applet.st0 = STAT_CLI_PRINT;
1102 return 1;
1103 }
1104 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001105 else if (strcmp(args[1], "rate-limit") == 0) {
1106 if (strcmp(args[2], "connections") == 0) {
1107 if (strcmp(args[3], "global") == 0) {
1108 int v;
1109
1110 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1111 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1112 si->applet.st0 = STAT_CLI_PRINT;
1113 return 1;
1114 }
1115
1116 if (!*args[4]) {
1117 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1118 si->applet.st0 = STAT_CLI_PRINT;
1119 return 1;
1120 }
1121
1122 v = atoi(args[4]);
1123 if (v < 0) {
1124 si->applet.ctx.cli.msg = "Value out of range.\n";
1125 si->applet.st0 = STAT_CLI_PRINT;
1126 return 1;
1127 }
1128
1129 global.cps_lim = v;
1130
1131 /* Dequeues all of the listeners waiting for a resource */
1132 if (!LIST_ISEMPTY(&global_listener_queue))
1133 dequeue_all_listeners(&global_listener_queue);
1134
1135 return 1;
1136 }
1137 else {
1138 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1139 si->applet.st0 = STAT_CLI_PRINT;
1140 return 1;
1141 }
1142 }
1143 else {
1144 si->applet.ctx.cli.msg = "'set rate-limit' only supports 'connections'.\n";
1145 si->applet.st0 = STAT_CLI_PRINT;
1146 return 1;
1147 }
1148 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001149 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001150 return 0;
1151 }
1152 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001153 else if (strcmp(args[0], "enable") == 0) {
1154 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001155 struct server *sv;
1156
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001157 sv = expect_server_admin(s, si, args[2]);
1158 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001159 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001160
Cyril Bontécd19e512010-01-31 22:34:03 +01001161 if (sv->state & SRV_MAINTAIN) {
1162 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001163 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001164 /* If this server tracks the status of another one,
1165 * we must restore the good status.
1166 */
Willy Tarreau44267702011-10-28 15:35:33 +02001167 if (sv->track->state & SRV_RUNNING) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001168 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001169 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001170 } else {
1171 sv->state &= ~SRV_MAINTAIN;
1172 set_server_down(sv);
1173 }
1174 } else {
1175 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001176 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001177 }
1178 }
1179
Willy Tarreau532a4502011-09-07 22:37:44 +02001180 return 1;
1181 }
1182 else if (strcmp(args[1], "frontend") == 0) {
1183 struct proxy *px;
1184
1185 px = expect_frontend_admin(s, si, args[2]);
1186 if (!px)
1187 return 1;
1188
1189 if (px->state == PR_STSTOPPED) {
1190 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1191 si->applet.st0 = STAT_CLI_PRINT;
1192 return 1;
1193 }
1194
1195 if (px->state != PR_STPAUSED) {
1196 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1197 si->applet.st0 = STAT_CLI_PRINT;
1198 return 1;
1199 }
1200
1201 if (!resume_proxy(px)) {
1202 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1203 si->applet.st0 = STAT_CLI_PRINT;
1204 return 1;
1205 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001206 return 1;
1207 }
1208 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001209 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1210 si->applet.st0 = STAT_CLI_PRINT;
1211 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001212 }
1213 }
1214 else if (strcmp(args[0], "disable") == 0) {
1215 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001216 struct server *sv;
1217
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001218 sv = expect_server_admin(s, si, args[2]);
1219 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001220 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001221
Cyril Bontécd19e512010-01-31 22:34:03 +01001222 if (! (sv->state & SRV_MAINTAIN)) {
1223 /* Not already in maintenance, we can change the server state */
1224 sv->state |= SRV_MAINTAIN;
1225 set_server_down(sv);
1226 }
1227
Willy Tarreau532a4502011-09-07 22:37:44 +02001228 return 1;
1229 }
1230 else if (strcmp(args[1], "frontend") == 0) {
1231 struct proxy *px;
1232
1233 px = expect_frontend_admin(s, si, args[2]);
1234 if (!px)
1235 return 1;
1236
1237 if (px->state == PR_STSTOPPED) {
1238 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1239 si->applet.st0 = STAT_CLI_PRINT;
1240 return 1;
1241 }
1242
1243 if (px->state == PR_STPAUSED) {
1244 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1245 si->applet.st0 = STAT_CLI_PRINT;
1246 return 1;
1247 }
1248
1249 if (!pause_proxy(px)) {
1250 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1251 si->applet.st0 = STAT_CLI_PRINT;
1252 return 1;
1253 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001254 return 1;
1255 }
1256 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001257 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1258 si->applet.st0 = STAT_CLI_PRINT;
1259 return 1;
1260 }
1261 }
1262 else if (strcmp(args[0], "shutdown") == 0) {
1263 if (strcmp(args[1], "frontend") == 0) {
1264 struct proxy *px;
1265
1266 px = expect_frontend_admin(s, si, args[2]);
1267 if (!px)
1268 return 1;
1269
1270 if (px->state == PR_STSTOPPED) {
1271 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1272 si->applet.st0 = STAT_CLI_PRINT;
1273 return 1;
1274 }
1275
1276 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1277 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1278 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1279 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1280 stop_proxy(px);
1281 return 1;
1282 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001283 else if (strcmp(args[1], "session") == 0) {
1284 struct session *sess, *ptr;
1285
1286 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1287 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1288 si->applet.st0 = STAT_CLI_PRINT;
1289 return 1;
1290 }
1291
1292 if (!*args[2]) {
1293 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1294 si->applet.st0 = STAT_CLI_PRINT;
1295 return 1;
1296 }
1297
1298 ptr = (void *)strtoul(args[2], NULL, 0);
1299
1300 /* first, look for the requested session in the session table */
1301 list_for_each_entry(sess, &sessions, list) {
1302 if (sess == ptr)
1303 break;
1304 }
1305
1306 /* do we have the session ? */
1307 if (sess != ptr) {
1308 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1309 si->applet.st0 = STAT_CLI_PRINT;
1310 return 1;
1311 }
1312
1313 session_shutdown(sess, SN_ERR_KILLED);
1314 return 1;
1315 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001316 else if (strcmp(args[1], "sessions") == 0) {
1317 if (strcmp(args[2], "server") == 0) {
1318 struct server *sv;
1319 struct session *sess, *sess_bck;
1320
1321 sv = expect_server_admin(s, si, args[3]);
1322 if (!sv)
1323 return 1;
1324
1325 /* kill all the session that are on this server */
1326 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1327 if (sess->srv_conn == sv)
1328 session_shutdown(sess, SN_ERR_KILLED);
1329
1330 return 1;
1331 }
1332 else {
1333 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1334 si->applet.st0 = STAT_CLI_PRINT;
1335 return 1;
1336 }
1337 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001338 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001339 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001340 si->applet.st0 = STAT_CLI_PRINT;
1341 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001342 }
1343 }
1344 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001345 return 0;
1346 }
1347 return 1;
1348}
1349
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001350/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001351 * used to processes I/O from/to the stats unix socket. The system relies on a
1352 * state machine handling requests and various responses. We read a request,
1353 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001354 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1355 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001356 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001357 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001358static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001359{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001360 struct buffer *req = si->ob;
1361 struct buffer *res = si->ib;
1362 int reql;
1363 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001364
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001365 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1366 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001367
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001368 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001369 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001370 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001371 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001372 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001373 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001374 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001375 /* Let's close for real now. We just close the request
1376 * side, the conditions below will complete if needed.
1377 */
Willy Tarreau060781f2012-05-07 16:50:03 +02001378 si->sock.shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001379 break;
1380 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001381 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001382 /* ensure we have some output room left in the event we
1383 * would want to return some info right after parsing.
1384 */
1385 if (buffer_almost_full(si->ib))
1386 break;
1387
David du Colombier7af46052012-05-16 14:16:48 +02001388 reql = bo_getline(si->ob, trash, trashlen);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001389 if (reql <= 0) { /* closed or EOL not found */
1390 if (reql == 0)
1391 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001392 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001393 continue;
1394 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001395
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001396 /* seek for a possible semi-colon. If we find one, we
1397 * replace it with an LF and skip only this part.
1398 */
1399 for (len = 0; len < reql; len++)
1400 if (trash[len] == ';') {
1401 trash[len] = '\n';
1402 reql = len + 1;
1403 break;
1404 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001405
Willy Tarreau816fc222009-10-04 07:36:58 +02001406 /* now it is time to check that we have a full line,
1407 * remove the trailing \n and possibly \r, then cut the
1408 * line.
1409 */
1410 len = reql - 1;
1411 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001412 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001413 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001414 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001415
Willy Tarreau816fc222009-10-04 07:36:58 +02001416 if (len && trash[len-1] == '\r')
1417 len--;
1418
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001419 trash[len] = '\0';
1420
Willy Tarreaubc4af052011-02-13 13:25:14 +01001421 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001422 if (len) {
1423 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001424 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001425 continue;
1426 }
1427 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001428 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001429 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001430 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001431 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001432 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001433 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001434 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001435 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001436 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001437 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001438 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001439 /* if prompt is disabled, print help on empty lines,
1440 * so that the user at least knows how to enable
1441 * prompt and find help.
1442 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001443 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001444 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001445 }
1446
1447 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001448 bo_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001449 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001450 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001451 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001452 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001453 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001454 continue;
1455 }
1456
Willy Tarreaubc4af052011-02-13 13:25:14 +01001457 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001458 case STAT_CLI_PRINT:
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001459 if (bi_putstr(si->ib, si->applet.ctx.cli.msg) != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001460 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001461 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001462 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001463 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001464 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001465 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001466 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001467 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001468 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001469 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001470 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001471 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001472 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001473 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001474 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001475 if (stats_table_request(si, true))
1476 si->applet.st0 = STAT_CLI_PROMPT;
1477 break;
1478 case STAT_CLI_O_CLR:
1479 if (stats_table_request(si, false))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001480 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001481 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001482 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001483 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001484 break;
1485 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001486
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001487 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001488 if (si->applet.st0 == STAT_CLI_PROMPT) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001489 if (bi_putstr(si->ib, si->applet.st1 ? "\n> " : "\n") != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001490 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001491 }
1492
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001493 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001494 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001495 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001496
1497 /* Now we close the output if one of the writers did so,
1498 * or if we're not in interactive mode and the request
1499 * buffer is empty. This still allows pipelined requests
1500 * to be sent in non-interactive mode.
1501 */
Willy Tarreau2e046c62012-03-01 16:08:30 +01001502 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->applet.st1 && !req->o)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001503 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001504 continue;
1505 }
1506
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001507 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001508 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001509 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001510 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001511
Willy Tarreaubc4af052011-02-13 13:25:14 +01001512 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001513 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1514 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001515 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001516 * and nothing more to consume. This is comparable to a broken pipe, so
1517 * we forward the close to the request side so that it flows upstream to
1518 * the client.
1519 */
Willy Tarreau060781f2012-05-07 16:50:03 +02001520 si->sock.shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001521 }
1522
Willy Tarreaubc4af052011-02-13 13:25:14 +01001523 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001524 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1525 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1526 /* We have no more processing to do, and nothing more to send, and
1527 * the client side has closed. So we'll forward this state downstream
1528 * on the response buffer.
1529 */
Willy Tarreau060781f2012-05-07 16:50:03 +02001530 si->sock.shutr(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001531 res->flags |= BF_READ_NULL;
1532 }
1533
1534 /* update all other flags and resync with the other side */
Willy Tarreau060781f2012-05-07 16:50:03 +02001535 si->sock.update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001536
1537 /* we don't want to expire timeouts while we're processing requests */
1538 si->ib->rex = TICK_ETERNITY;
1539 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001540
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001541 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001542 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rqh=%d, rqs=%d, rh=%d, rs=%d\n",
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001543 __FUNCTION__, __LINE__,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001544 si->state, req->flags, res->flags, req->i, req->o, res->i, res->o);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001545
1546 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1547 /* check that we have released everything then unregister */
1548 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001549 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001550}
1551
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001552/* This function dumps statistics onto the stream interface's read buffer.
1553 * The data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001554 * It returns 0 as long as it does not complete, non-zero upon completion.
1555 * Some states are not used but it makes the code more similar to other
1556 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001557 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001558static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001559{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001560 struct proxy *px;
1561 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001562 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001563
David du Colombier7af46052012-05-16 14:16:48 +02001564 chunk_init(&msg, trash, trashlen);
Willy Tarreau3e76e722007-10-17 18:57:38 +02001565
Willy Tarreau295a8372011-03-10 11:25:07 +01001566 switch (si->applet.state) {
1567 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001568 /* the function had not been called yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001569 si->applet.state = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001570 /* fall through */
1571
Willy Tarreau295a8372011-03-10 11:25:07 +01001572 case STAT_ST_HEAD:
1573 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001574 print_csv_header(&msg);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001575 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001576 return 0;
1577 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001578
Willy Tarreau295a8372011-03-10 11:25:07 +01001579 si->applet.state = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001580 /* fall through */
1581
Willy Tarreau295a8372011-03-10 11:25:07 +01001582 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001583 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001584 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001585 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001586 "Name: " PRODUCT_NAME "\n"
1587 "Version: " HAPROXY_VERSION "\n"
1588 "Release_date: " HAPROXY_DATE "\n"
1589 "Nbproc: %d\n"
1590 "Process_num: %d\n"
1591 "Pid: %d\n"
1592 "Uptime: %dd %dh%02dm%02ds\n"
1593 "Uptime_sec: %d\n"
1594 "Memmax_MB: %d\n"
1595 "Ulimit-n: %d\n"
1596 "Maxsock: %d\n"
1597 "Maxconn: %d\n"
Willy Tarreau91886b62011-09-07 14:38:31 +02001598 "Hard_maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001599 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001600 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001601 "PipesUsed: %d\n"
1602 "PipesFree: %d\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001603 "ConnRate: %d\n"
1604 "ConnRateLimit: %d\n"
1605 "MaxConnRate: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001606 "Tasks: %d\n"
1607 "Run_queue: %d\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02001608 "Idle_pct: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001609 "node: %s\n"
1610 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001611 "",
1612 global.nbproc,
1613 relative_pid,
1614 pid,
1615 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1616 up,
1617 global.rlimit_memmax,
1618 global.rlimit_nofile,
Willy Tarreau91886b62011-09-07 14:38:31 +02001619 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001620 actconn, pipes_used, pipes_free,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001621 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau45a12512011-09-10 16:56:42 +02001622 nb_tasks_cur, run_queue_cur, idle_pct,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001623 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001624 );
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001625 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001626 return 0;
1627 }
1628
Willy Tarreau295a8372011-03-10 11:25:07 +01001629 si->applet.ctx.stats.px = proxy;
1630 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1631 si->applet.ctx.stats.sv = NULL;
1632 si->applet.state = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001633 /* fall through */
1634
Willy Tarreau295a8372011-03-10 11:25:07 +01001635 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001636 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001637 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1638 while (si->applet.ctx.stats.px) {
1639 px = si->applet.ctx.stats.px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001640 /* skip the disabled proxies and non-networked ones */
1641 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001642 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001643 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001644 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001645 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001646
Willy Tarreau295a8372011-03-10 11:25:07 +01001647 si->applet.ctx.stats.px = px->next;
1648 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001649 }
1650 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001651 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001652
Willy Tarreau295a8372011-03-10 11:25:07 +01001653 si->applet.state = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001654 /* fall through */
1655
Willy Tarreau295a8372011-03-10 11:25:07 +01001656 case STAT_ST_END:
1657 si->applet.state = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001658 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001659
Willy Tarreau295a8372011-03-10 11:25:07 +01001660 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001661 return 1;
1662
1663 default:
1664 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001665 si->applet.state = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001666 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001667 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001668}
1669
1670
Cyril Bonté70be45d2010-10-12 00:14:35 +02001671/* We don't want to land on the posted stats page because a refresh will
1672 * repost the data. We don't want this to happen on accident so we redirect
1673 * the browse to the stats page with a GET.
1674 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001675static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001676{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001677 struct session *s = si->applet.private;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001678 struct chunk msg;
1679
David du Colombier7af46052012-05-16 14:16:48 +02001680 chunk_init(&msg, trash, trashlen);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001681
Willy Tarreau295a8372011-03-10 11:25:07 +01001682 switch (si->applet.state) {
1683 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001684 chunk_printf(&msg,
1685 "HTTP/1.0 303 See Other\r\n"
1686 "Cache-Control: no-cache\r\n"
1687 "Content-Type: text/plain\r\n"
1688 "Connection: close\r\n"
1689 "Location: %s;st=%s",
Cyril Bonté19979e12012-04-04 12:57:21 +02001690 uri->uri_prefix,
1691 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
1692 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
1693 stat_status_codes[si->applet.ctx.stats.st_code]) ?
1694 stat_status_codes[si->applet.ctx.stats.st_code] :
1695 stat_status_codes[STAT_STATUS_UNKN]);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001696 chunk_printf(&msg, "\r\n\r\n");
1697
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001698 if (bi_putchk(si->ib, &msg) == -1)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001699 return 0;
1700
1701 s->txn.status = 303;
1702
1703 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1704 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1705 if (!(s->flags & SN_FINST_MASK))
1706 s->flags |= SN_FINST_R;
1707
Willy Tarreau295a8372011-03-10 11:25:07 +01001708 si->applet.state = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001709 return 1;
1710 }
1711 return 1;
1712}
1713
1714
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001715/* This I/O handler runs as an applet embedded in a stream interface. It is
1716 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001717 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001718 * automatically unregisters itself once transfer is complete.
1719 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001720static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001721{
Willy Tarreaubc4af052011-02-13 13:25:14 +01001722 struct session *s = si->applet.private;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001723 struct buffer *req = si->ob;
1724 struct buffer *res = si->ib;
1725
1726 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1727 goto out;
1728
1729 /* check that the output is not closed */
1730 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001731 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001732
Willy Tarreaubc4af052011-02-13 13:25:14 +01001733 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001734 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001735 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001736 si->applet.st0 = 1;
Willy Tarreau060781f2012-05-07 16:50:03 +02001737 si->sock.shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001738 }
1739 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001740 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001741 si->applet.st0 = 1;
Willy Tarreau060781f2012-05-07 16:50:03 +02001742 si->sock.shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001743 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001744 }
1745 }
1746
1747 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
Willy Tarreau060781f2012-05-07 16:50:03 +02001748 si->sock.shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001749
Willy Tarreaubc4af052011-02-13 13:25:14 +01001750 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreau060781f2012-05-07 16:50:03 +02001751 si->sock.shutr(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001752 res->flags |= BF_READ_NULL;
1753 }
1754
1755 /* update all other flags and resync with the other side */
Willy Tarreau060781f2012-05-07 16:50:03 +02001756 si->sock.update(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001757
1758 /* we don't want to expire timeouts while we're processing requests */
1759 si->ib->rex = TICK_ETERNITY;
1760 si->ob->wex = TICK_ETERNITY;
1761
1762 out:
1763 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1764 /* check that we have released everything then unregister */
1765 stream_int_unregister_handler(si);
1766 }
1767}
1768
1769
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001770/* This function dumps statistics in HTTP format onto the stream interface's
1771 * read buffer. The data_ctx must have been zeroed first, and the flags
1772 * properly set. It returns 0 if it had to stop writing data and an I/O is
1773 * needed, 1 if the dump is finished and the session must be closed, or -1
1774 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001775 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001776static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001777{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001778 struct session *s = si->applet.private;
1779 struct buffer *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001780 struct proxy *px;
1781 struct chunk msg;
1782 unsigned int up;
1783
David du Colombier7af46052012-05-16 14:16:48 +02001784 chunk_init(&msg, trash, trashlen);
Willy Tarreau91861262007-10-17 17:06:05 +02001785
Willy Tarreau295a8372011-03-10 11:25:07 +01001786 switch (si->applet.state) {
1787 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001788 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001789 "HTTP/1.0 200 OK\r\n"
1790 "Cache-Control: no-cache\r\n"
1791 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001792 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001793 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001794
Willy Tarreau295a8372011-03-10 11:25:07 +01001795 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001796 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001797 uri->refresh);
1798
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001799 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001800
1801 s->txn.status = 200;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001802 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001803 return 0;
1804
Willy Tarreau91861262007-10-17 17:06:05 +02001805 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1806 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1807 if (!(s->flags & SN_FINST_MASK))
1808 s->flags |= SN_FINST_R;
1809
1810 if (s->txn.meth == HTTP_METH_HEAD) {
1811 /* that's all we return in case of HEAD request */
Willy Tarreau295a8372011-03-10 11:25:07 +01001812 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001813 return 1;
1814 }
1815
Willy Tarreau295a8372011-03-10 11:25:07 +01001816 si->applet.state = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001817 /* fall through */
1818
Willy Tarreau295a8372011-03-10 11:25:07 +01001819 case STAT_ST_HEAD:
1820 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001821 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001822 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001823 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1824 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001825 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001826 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1827 "<style type=\"text/css\"><!--\n"
1828 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001829 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001830 " font-size: 12px;"
1831 " font-weight: normal;"
1832 " color: black;"
1833 " background: white;"
1834 "}\n"
1835 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001836 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001837 "}\n"
1838 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001839 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001840 " margin-bottom: 0.5em;"
1841 "}\n"
1842 "h2 {"
1843 " font-family: helvetica, arial;"
1844 " font-size: x-large;"
1845 " font-weight: bold;"
1846 " font-style: italic;"
1847 " color: #6020a0;"
1848 " margin-top: 0em;"
1849 " margin-bottom: 0em;"
1850 "}\n"
1851 "h3 {"
1852 " font-family: helvetica, arial;"
1853 " font-size: 16px;"
1854 " font-weight: bold;"
1855 " color: #b00040;"
1856 " background: #e8e8d0;"
1857 " margin-top: 0em;"
1858 " margin-bottom: 0em;"
1859 "}\n"
1860 "li {"
1861 " margin-top: 0.25em;"
1862 " margin-right: 2em;"
1863 "}\n"
1864 ".hr {margin-top: 0.25em;"
1865 " border-color: black;"
1866 " border-bottom-style: solid;"
1867 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001868 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001869 ".total {background: #20D0D0;color: #ffff80;}\n"
1870 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001871 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001872 ".backend {background: #e8e8d0;}\n"
1873 ".active0 {background: #ff9090;}\n"
1874 ".active1 {background: #ffd020;}\n"
1875 ".active2 {background: #ffffa0;}\n"
1876 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001877 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1878 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1879 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001880 ".backup0 {background: #ff9090;}\n"
1881 ".backup1 {background: #ff80ff;}\n"
1882 ".backup2 {background: #c060ff;}\n"
1883 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001884 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1885 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1886 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001887 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001888 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001889 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001890 "a.px:link {color: #ffff40; text-decoration: none;}"
1891 "a.px:visited {color: #ffff40; text-decoration: none;}"
1892 "a.px:hover {color: #ffffff; text-decoration: none;}"
1893 "a.lfsb:link {color: #000000; text-decoration: none;}"
1894 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1895 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1896 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001897 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001898 "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"
1899 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001900 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001901 "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 +02001902 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001903 "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 +02001904 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001905 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1906 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1907 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001908 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001909 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001910 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001911 (uri->flags&ST_SHNODE) ? " on " : "",
1912 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001913 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001914 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001915 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001916 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001917 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02001918 return 0;
1919
Willy Tarreau295a8372011-03-10 11:25:07 +01001920 si->applet.state = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02001921 /* fall through */
1922
Willy Tarreau295a8372011-03-10 11:25:07 +01001923 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02001924 up = (now.tv_sec - start_date.tv_sec);
1925
1926 /* WARNING! this has to fit the first packet too.
1927 * We are around 3.5 kB, add adding entries will
1928 * become tricky if we want to support 4kB buffers !
1929 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001930 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001931 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001932 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1933 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001934 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001935 "<hr width=\"100%%\" class=\"hr\">\n"
1936 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001937 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001938 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001939 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001940 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1941 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001942 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02001943 "Running tasks: %d/%d; idle = %d %%<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001944 "</td><td align=\"center\" nowrap>\n"
1945 "<table class=\"lgd\"><tr>\n"
1946 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1947 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1948 "</tr><tr>\n"
1949 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1950 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1951 "</tr><tr>\n"
1952 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1953 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1954 "</tr><tr>\n"
1955 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001956 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001957 "</tr><tr>\n"
1958 "<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 +02001959 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001960 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001961 "</td>"
1962 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1963 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1964 "",
1965 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001966 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1967 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1968 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001969 up / 86400, (up % 86400) / 3600,
1970 (up % 3600) / 60, (up % 60),
1971 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1972 global.rlimit_memmax ? " MB" : "",
1973 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001974 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001975 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau45a12512011-09-10 16:56:42 +02001976 run_queue_cur, nb_tasks_cur, idle_pct
Willy Tarreau91861262007-10-17 17:06:05 +02001977 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001978
Willy Tarreau295a8372011-03-10 11:25:07 +01001979 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001980 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001981 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1982 uri->uri_prefix,
1983 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01001984 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001985 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001986 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001987 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1988 uri->uri_prefix,
1989 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01001990 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001991
Willy Tarreau55bb8452007-10-17 18:44:57 +02001992 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001993 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001994 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001995 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1996 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001997 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001998 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001999 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002000 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002001 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
2002 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002003 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002004 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002005 }
Willy Tarreau91861262007-10-17 17:06:05 +02002006
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002007 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002008 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
2009 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002010 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2011 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002012
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002013 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002014 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
2015 uri->uri_prefix,
2016 (uri->refresh > 0) ? ";norefresh" : "");
2017
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002018 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002019 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002020 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
Adrian Bridgettafdb6e52012-03-19 23:36:42 +00002021 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002022 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2023 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2024 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2025 "</ul>"
2026 "</td>"
2027 "</tr></table>\n"
2028 ""
2029 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002030
Willy Tarreau295a8372011-03-10 11:25:07 +01002031 if (si->applet.ctx.stats.st_code) {
Cyril Bonté19979e12012-04-04 12:57:21 +02002032 switch (si->applet.ctx.stats.st_code) {
2033 case STAT_STATUS_DONE:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002034 chunk_printf(&msg,
2035 "<p><div class=active3>"
2036 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2037 "Action processed successfully."
2038 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002039 break;
2040 case STAT_STATUS_NONE:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002041 chunk_printf(&msg,
2042 "<p><div class=active2>"
2043 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2044 "Nothing has changed."
2045 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002046 break;
2047 case STAT_STATUS_PART:
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002048 chunk_printf(&msg,
2049 "<p><div class=active2>"
2050 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2051 "Action partially processed.<br>"
2052 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2053 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002054 break;
2055 case STAT_STATUS_ERRP:
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002056 chunk_printf(&msg,
2057 "<p><div class=active0>"
2058 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2059 "Action not processed because of invalid parameters."
2060 "<ul>"
2061 "<li>The action is maybe unknown.</li>"
2062 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2063 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2064 "</ul>"
2065 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002066 break;
2067 case STAT_STATUS_EXCD:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002068 chunk_printf(&msg,
2069 "<p><div class=active0>"
2070 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2071 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2072 "You should retry with less servers at a time.</b>"
2073 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002074 break;
2075 case STAT_STATUS_DENY:
Cyril Bonté474be412010-10-12 00:14:36 +02002076 chunk_printf(&msg,
2077 "<p><div class=active0>"
2078 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2079 "<b>Action denied.</b>"
2080 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002081 break;
2082 default:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002083 chunk_printf(&msg,
2084 "<p><div class=active6>"
2085 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2086 "Unexpected result."
2087 "</div>\n", uri->uri_prefix);
2088 }
2089 chunk_printf(&msg,"<p>\n");
2090 }
2091
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002092 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002093 return 0;
2094 }
Willy Tarreau91861262007-10-17 17:06:05 +02002095
Willy Tarreau295a8372011-03-10 11:25:07 +01002096 si->applet.ctx.stats.px = proxy;
2097 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
2098 si->applet.state = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02002099 /* fall through */
2100
Willy Tarreau295a8372011-03-10 11:25:07 +01002101 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02002102 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01002103 while (si->applet.ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002104 if (buffer_almost_full(rep))
2105 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002106 px = si->applet.ctx.stats.px;
Willy Tarreau91861262007-10-17 17:06:05 +02002107 /* skip the disabled proxies and non-networked ones */
2108 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002109 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002110 return 0;
2111
Willy Tarreau295a8372011-03-10 11:25:07 +01002112 si->applet.ctx.stats.px = px->next;
2113 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02002114 }
2115 /* here, we just have reached the last proxy */
2116
Willy Tarreau295a8372011-03-10 11:25:07 +01002117 si->applet.state = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002118 /* fall through */
2119
Willy Tarreau295a8372011-03-10 11:25:07 +01002120 case STAT_ST_END:
2121 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002122 chunk_printf(&msg, "</body></html>\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002123 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002124 return 0;
2125 }
Willy Tarreau91861262007-10-17 17:06:05 +02002126
Willy Tarreau295a8372011-03-10 11:25:07 +01002127 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002128 /* fall through */
2129
Willy Tarreau295a8372011-03-10 11:25:07 +01002130 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002131 return 1;
2132
2133 default:
2134 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01002135 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002136 return -1;
2137 }
2138}
2139
2140
2141/*
2142 * Dumps statistics for a proxy.
2143 * Returns 0 if it had to stop dumping data because of lack of buffer space,
2144 * ot non-zero if everything completed.
2145 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002146static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002147{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002148 struct session *s = si->applet.private;
2149 struct buffer *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002150 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002151 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002152 struct chunk msg;
2153
David du Colombier7af46052012-05-16 14:16:48 +02002154 chunk_init(&msg, trash, trashlen);
Willy Tarreau91861262007-10-17 17:06:05 +02002155
Willy Tarreau295a8372011-03-10 11:25:07 +01002156 switch (si->applet.ctx.stats.px_st) {
2157 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002158 /* we are on a new proxy */
2159
2160 if (uri && uri->scope) {
2161 /* we have a limited scope, we have to check the proxy name */
2162 struct stat_scope *scope;
2163 int len;
2164
2165 len = strlen(px->id);
2166 scope = uri->scope;
2167
2168 while (scope) {
2169 /* match exact proxy name */
2170 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2171 break;
2172
2173 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002174 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002175 break;
2176 scope = scope->next;
2177 }
2178
2179 /* proxy name not found : don't dump anything */
2180 if (scope == NULL)
2181 return 1;
2182 }
2183
Willy Tarreau295a8372011-03-10 11:25:07 +01002184 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
2185 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002186 return 1;
2187
Willy Tarreau295a8372011-03-10 11:25:07 +01002188 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002189 /* fall through */
2190
Willy Tarreau295a8372011-03-10 11:25:07 +01002191 case STAT_PX_ST_TH:
2192 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
2193 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002194 /* A form to enable/disable this proxy servers */
2195 chunk_printf(&msg,
2196 "<form action=\"%s\" method=\"post\">",
2197 uri->uri_prefix);
2198 }
2199
Willy Tarreau55bb8452007-10-17 18:44:57 +02002200 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002201 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002202 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002203 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002204 "<th class=\"pxname\" width=\"10%%\"");
2205
2206 if (uri->flags&ST_SHLGNDS) {
2207 /* cap, mode, id */
2208 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
2209 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2210 px->uuid);
2211
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002212 chunk_printf(&msg, "\"");
2213 }
2214
2215 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002216 ">%s<a name=\"%s\"></a>"
2217 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002218 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002219 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002220 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002221 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002222 "<tr class=\"titre\">",
2223 (uri->flags & ST_SHLGNDS)?"<u>":"",
2224 px->id, px->id, px->id,
2225 (uri->flags & ST_SHLGNDS)?"</u>":"",
2226 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2227
Willy Tarreau295a8372011-03-10 11:25:07 +01002228 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002229 /* Column heading for Enable or Disable server */
2230 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
2231 }
2232
2233 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002234 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002235 "<th colspan=3>Queue</th>"
2236 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002237 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002238 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002239 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002240 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002241 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002242 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002243 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002244 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002245 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002246 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002247 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002248 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2249 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002250 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002251
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002252 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002253 return 0;
2254 }
Willy Tarreau91861262007-10-17 17:06:05 +02002255
Willy Tarreau295a8372011-03-10 11:25:07 +01002256 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002257 /* fall through */
2258
Willy Tarreau295a8372011-03-10 11:25:07 +01002259 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002260 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002261 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002262 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
2263 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002264 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002265 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002266 "<tr class=\"frontend\">");
2267
Willy Tarreau295a8372011-03-10 11:25:07 +01002268 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002269 /* Column sub-heading for Enable or Disable server */
2270 chunk_printf(&msg, "<td></td>");
2271 }
2272
2273 chunk_printf(&msg,
2274 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002275 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002276 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2277 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002278 "",
2279 px->id, px->id);
2280
2281 if (px->mode == PR_MODE_HTTP) {
2282 chunk_printf(&msg,
2283 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01002284 "<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 +01002285 "",
2286 read_freq_ctr(&px->fe_req_per_sec),
2287 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002288 px->fe_counters.p.http.rps_max,
2289 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01002290 LIM2A2(px->fe_sps_lim, "-"));
2291 } else {
2292 chunk_printf(&msg,
2293 /* sessions rate : current, max, limit */
2294 "<td>%s</td><td>%s</td><td>%s</td>"
2295 "",
2296 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002297 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01002298 }
2299
2300 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002301 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002302 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002303 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002304 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002305 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002306
2307 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2308 if (px->mode == PR_MODE_HTTP) {
2309 int i;
2310
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002311 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002312
2313 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002314 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002315
Willy Tarreaueabea072011-09-10 23:29:44 +02002316 chunk_printf(&msg, " other=%lld,", px->fe_counters.p.http.rsp[0]);
2317 chunk_printf(&msg, " intercepted=%lld\"", px->fe_counters.intercepted_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002318 }
2319
2320 chunk_printf(&msg,
2321 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002322 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002323 /* bytes : in, out */
2324 "<td>%s</td><td>%s</td>"
2325 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002326 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002327 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002328 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002329 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002330
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002331 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002332 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002333 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002334 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002335 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002336 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002337 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002338 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002339 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002340 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002341 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002342 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002343 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2344 U2H2(px->fe_counters.failed_req),
Willy Tarreau562515c2011-07-25 08:11:52 +02002345 px->state == PR_STREADY ? "OPEN" :
2346 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002347 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002348 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002349 /* pxid, name, queue cur, queue max, */
2350 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002351 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002352 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002353 /* bytes : in, out */
2354 "%lld,%lld,"
2355 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002356 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002357 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002358 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002359 /* warnings: retries, redispatches */
2360 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002361 /* server status : reflect frontend status */
2362 "%s,"
2363 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002364 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002365 /* pid, iid, sid, throttle, lbtot, tracked, type */
2366 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002367 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002368 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002369 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002370 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002371 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002372 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2373 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2374 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2375 px->fe_counters.failed_req,
Willy Tarreau562515c2011-07-25 08:11:52 +02002376 px->state == PR_STREADY ? "OPEN" :
2377 px->state == PR_STFULL ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002378 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002379 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002380 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002381
2382 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2383 if (px->mode == PR_MODE_HTTP) {
2384 int i;
2385
2386 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002387 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002388
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002389 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002390 } else {
2391 chunk_printf(&msg, ",,,,,,");
2392 }
2393
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002394 /* failed health analyses */
2395 chunk_printf(&msg, ",");
2396
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002397 /* requests : req_rate, req_rate_max, req_tot, */
2398 chunk_printf(&msg, "%u,%u,%lld,",
2399 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002400 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002401
Willy Tarreauae526782010-03-04 20:34:23 +01002402 /* errors: cli_aborts, srv_aborts */
2403 chunk_printf(&msg, ",,");
2404
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002405 /* finish with EOL */
2406 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002407 }
Willy Tarreau91861262007-10-17 17:06:05 +02002408
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002409 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002410 return 0;
2411 }
2412
Willy Tarreau295a8372011-03-10 11:25:07 +01002413 si->applet.ctx.stats.l = px->listen; /* may be NULL */
2414 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002415 /* fall through */
2416
Willy Tarreau295a8372011-03-10 11:25:07 +01002417 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002418 /* stats.l has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002419 for (; si->applet.ctx.stats.l != NULL; si->applet.ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002420 if (buffer_almost_full(rep))
2421 return 0;
2422
Willy Tarreau295a8372011-03-10 11:25:07 +01002423 l = si->applet.ctx.stats.l;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002424 if (!l->counters)
2425 continue;
2426
Willy Tarreau295a8372011-03-10 11:25:07 +01002427 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2428 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002429 break;
2430
Willy Tarreau295a8372011-03-10 11:25:07 +01002431 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002432 continue;
2433 }
2434
Willy Tarreau295a8372011-03-10 11:25:07 +01002435 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002436 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002437 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002438 /* Column sub-heading for Enable or Disable server */
2439 chunk_printf(&msg, "<td></td>");
2440 }
2441 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002442
2443 if (uri->flags&ST_SHLGNDS) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02002444 char str[INET6_ADDRSTRLEN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002445 int port;
2446
Willy Tarreau631f01c2011-09-05 00:36:48 +02002447 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002448
Willy Tarreau631f01c2011-09-05 00:36:48 +02002449 port = get_host_port(&l->addr);
2450 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2451 case AF_INET:
2452 chunk_printf(&msg, "IPv4: %s:%d, ", str, port);
2453 break;
2454 case AF_INET6:
2455 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, port);
2456 break;
2457 case AF_UNIX:
2458 chunk_printf(&msg, "unix, ");
2459 break;
2460 case -1:
2461 chunk_printf(&msg, "(%s), ", strerror(errno));
2462 break;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002463 }
2464
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002465 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002466 chunk_printf(&msg, "id: %d\"", l->luid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002467 }
2468
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002469 chunk_printf(&msg,
2470 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002471 ">%s<a name=\"%s/+%s\"></a>"
2472 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002473 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002474 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002475 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002476 "<td>%s</td><td>%s</td><td>%s</td>"
2477 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002478 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002479 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002480 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002481 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002482 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002483 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002484 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2485 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2486
2487 chunk_printf(&msg,
2488 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002489 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002490 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002491 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002492 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002493 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002494 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002495 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002496 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002497 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002498 "",
2499 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2500 U2H2(l->counters->failed_req),
Willy Tarreaua17c2d92011-07-25 08:16:20 +02002501 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002502 } else {
2503 chunk_printf(&msg,
2504 /* pxid, name, queue cur, queue max, */
2505 "%s,%s,,,"
2506 /* sessions: current, max, limit, total */
2507 "%d,%d,%d,%lld,"
2508 /* bytes: in, out */
2509 "%lld,%lld,"
2510 /* denied: req, resp */
2511 "%lld,%lld,"
2512 /* errors: request, connect, response */
2513 "%lld,,,"
2514 /* warnings: retries, redispatches */
2515 ",,"
2516 /* server status: reflect listener status */
2517 "%s,"
2518 /* rest of server: nothing */
2519 ",,,,,,,,"
2520 /* pid, iid, sid, throttle, lbtot, tracked, type */
2521 "%d,%d,%d,,,,%d,"
2522 /* rate, rate_lim, rate_max */
2523 ",,,"
2524 /* check_status, check_code, check_duration */
2525 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002526 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2527 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002528 /* failed health analyses */
2529 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002530 /* requests : req_rate, req_rate_max, req_tot, */
2531 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002532 /* errors: cli_aborts, srv_aborts */
2533 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002534 "\n",
2535 px->id, l->name,
2536 l->nbconn, l->counters->conn_max,
2537 l->maxconn, l->counters->cum_conn,
2538 l->counters->bytes_in, l->counters->bytes_out,
2539 l->counters->denied_req, l->counters->denied_resp,
2540 l->counters->failed_req,
2541 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2542 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2543 }
2544
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002545 if (bi_putchk(rep, &msg) == -1)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002546 return 0;
2547 }
2548
Willy Tarreau295a8372011-03-10 11:25:07 +01002549 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2550 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002551 /* fall through */
2552
Willy Tarreau295a8372011-03-10 11:25:07 +01002553 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002554 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002555 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002556 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 +02002557
Willy Tarreau4e33d862009-10-11 23:35:10 +02002558 if (buffer_almost_full(rep))
2559 return 0;
2560
Willy Tarreau295a8372011-03-10 11:25:07 +01002561 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002562
Willy Tarreau295a8372011-03-10 11:25:07 +01002563 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2564 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002565 break;
2566
Willy Tarreau295a8372011-03-10 11:25:07 +01002567 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002568 continue;
2569 }
2570
Willy Tarreau44267702011-10-28 15:35:33 +02002571 if (sv->track)
2572 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002573 else
2574 svs = sv;
2575
Willy Tarreau91861262007-10-17 17:06:05 +02002576 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002577 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002578 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002579 else if (svs->state & SRV_RUNNING) {
2580 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002581 sv_state = 3; /* UP */
2582 else
2583 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002584
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002585 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002586 sv_state += 2;
2587 }
Willy Tarreau91861262007-10-17 17:06:05 +02002588 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002589 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002590 sv_state = 1; /* going up */
2591 else
2592 sv_state = 0; /* DOWN */
2593
Willy Tarreau295a8372011-03-10 11:25:07 +01002594 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002595 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002596 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002597 continue;
2598 }
2599
Willy Tarreau295a8372011-03-10 11:25:07 +01002600 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002601 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2602 "UP %d/%d &darr;", "UP",
2603 "NOLB %d/%d &darr;", "NOLB",
2604 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002605 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002606 chunk_printf(&msg,
2607 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002608 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002609 );
2610 }
2611 else {
2612 chunk_printf(&msg,
2613 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002614 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002615 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2616 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002617
Willy Tarreau295a8372011-03-10 11:25:07 +01002618 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002619 chunk_printf(&msg,
2620 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2621 sv->id);
2622 }
2623
2624 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002625
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002626 if (uri->flags&ST_SHLGNDS) {
2627 char str[INET6_ADDRSTRLEN];
2628
Willy Tarreau631f01c2011-09-05 00:36:48 +02002629 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002630
Willy Tarreau631f01c2011-09-05 00:36:48 +02002631 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01002632 case AF_INET:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002633 chunk_printf(&msg, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
David du Colombier6f5ccb12011-03-10 22:26:24 +01002634 break;
2635 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002636 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2637 break;
2638 case AF_UNIX:
2639 chunk_printf(&msg, "unix, ");
2640 break;
2641 case -1:
2642 chunk_printf(&msg, "(%s), ", strerror(errno));
2643 break;
2644 default: /* address family not supported */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002645 break;
2646 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002647
2648 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002649 chunk_printf(&msg, "id: %d", sv->puid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002650
2651 /* cookie */
2652 if (sv->cookie) {
2653 struct chunk src;
2654
2655 chunk_printf(&msg, ", cookie: '");
2656
2657 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2658 chunk_htmlencode(&msg, &src);
2659
2660 chunk_printf(&msg, "'");
2661 }
2662
2663 chunk_printf(&msg, "\"");
2664 }
2665
2666 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002667 ">%s<a name=\"%s/%s\"></a>"
2668 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002669 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002670 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002671 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002672 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002673 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002674 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002675 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002676 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002677 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002678 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002679 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002680 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2681 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002682 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2683
2684 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2685 if (px->mode == PR_MODE_HTTP) {
2686 int i;
2687
2688 chunk_printf(&msg, " title=\"rsp codes:");
2689
2690 for (i = 1; i < 6; i++)
2691 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2692
2693 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2694 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002695
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002696 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002697 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002698 ">%s%s%s</td><td>%s</td>",
2699 (px->mode == PR_MODE_HTTP)?"<u>":"",
2700 U2H0(sv->counters.cum_sess),
2701 (px->mode == PR_MODE_HTTP)?"</u>":"",
2702 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002703
2704 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002705 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002706 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002707 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002708 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002709 /* errors : request, connect */
2710 "<td></td><td>%s</td>"
2711 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002712 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002713 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002714 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002715 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002716 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2717 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002718 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002719 sv->counters.cli_aborts,
2720 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002721 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002722 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002723
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002724 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002725 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002726
Cyril Bontécd19e512010-01-31 22:34:03 +01002727 if (sv->state & SRV_MAINTAIN) {
2728 chunk_printf(&msg, "%s ",
2729 human_time(now.tv_sec - sv->last_change, 1));
2730 chunk_printf(&msg, "MAINT");
2731 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002732 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2733 chunk_printf(&msg, "%s ",
2734 human_time(now.tv_sec - svs->last_change, 1));
2735 chunk_printf(&msg, "MAINT(via)");
2736 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002737 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002738 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002739 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002740
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002741 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002742 srv_hlt_st[sv_state],
2743 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2744 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002745 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002746
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002747 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002748 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002749 get_check_status_description(sv->check_status));
2750
2751 if (*sv->check_desc) {
2752 struct chunk src;
2753
2754 chunk_printf(&msg, ": ");
2755
2756 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2757 chunk_htmlencode(&msg, &src);
2758 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002759
Willy Tarreaue0454092010-02-26 12:29:07 +01002760 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002761 tv_iszero(&sv->check_start)?"":"* ",
2762 get_check_status_info(sv->check_status));
2763
2764 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002765 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002766
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002767 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002768 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002769 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002770 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002771
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002772 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002773 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002774 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002775 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002776 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002777 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002778 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002779 (sv->state & SRV_BACKUP) ? "-" : "Y",
2780 (sv->state & SRV_BACKUP) ? "Y" : "-");
2781
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002782 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002783 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002784 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002785 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2786
2787 if (svs->observe)
2788 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2789
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002790 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002791 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002792 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002793 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002794 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002795 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002796 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002797 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2798 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002799 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002800 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002801 "<td colspan=3></td>");
2802
2803 /* throttle */
2804 if ((sv->state & SRV_WARMINGUP) &&
2805 now.tv_sec < sv->last_change + sv->slowstart &&
2806 now.tv_sec >= sv->last_change) {
2807 unsigned int ratio;
2808 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002809 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002810 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002811 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002812 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002813 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002814 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002815 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002816 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2817 "UP %d/%d,", "UP,",
2818 "NOLB %d/%d,", "NOLB,",
2819 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002820 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002821 /* pxid, name */
2822 "%s,%s,"
2823 /* queue : current, max */
2824 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002825 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002826 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002827 /* bytes : in, out */
2828 "%lld,%lld,"
2829 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002830 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002831 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002832 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002833 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002834 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002835 "",
2836 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002837 sv->nbpend, sv->counters.nbpend_max,
2838 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002839 sv->counters.bytes_in, sv->counters.bytes_out,
2840 sv->counters.failed_secu,
2841 sv->counters.failed_conns, sv->counters.failed_resp,
2842 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002843
Willy Tarreau55bb8452007-10-17 18:44:57 +02002844 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002845 if (sv->state & SRV_MAINTAIN) {
2846 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002847 }
2848 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2849 chunk_printf(&msg, "MAINT(via),");
2850 }
2851 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002852 chunk_printf(&msg,
2853 srv_hlt_st[sv_state],
2854 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2855 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2856 }
Willy Tarreau91861262007-10-17 17:06:05 +02002857
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002858 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002859 /* weight, active, backup */
2860 "%d,%d,%d,"
2861 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002862 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002863 (sv->state & SRV_BACKUP) ? 0 : 1,
2864 (sv->state & SRV_BACKUP) ? 1 : 0);
2865
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002866 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002867 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002868 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002869 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002870 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002871 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002872 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002873 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002874 ",,,,");
2875
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002876 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002877 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002878 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002879 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002880 LIM2A0(sv->maxqueue, ""),
2881 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002882
2883 /* throttle */
2884 if ((sv->state & SRV_WARMINGUP) &&
2885 now.tv_sec < sv->last_change + sv->slowstart &&
2886 now.tv_sec >= sv->last_change) {
2887 unsigned int ratio;
2888 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002889 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002890 }
2891
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002892 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002893 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002894
2895 /* tracked */
Willy Tarreau44267702011-10-28 15:35:33 +02002896 if (sv->track)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002897 chunk_printf(&msg, "%s/%s,",
Willy Tarreau44267702011-10-28 15:35:33 +02002898 sv->track->proxy->id, sv->track->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002899 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002900 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002901
Willy Tarreau7f062c42009-03-05 18:43:00 +01002902 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002903 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002904
2905 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002906 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002907 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002908 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002909
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002910 if (sv->state & SRV_CHECKED) {
2911 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002912 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002913
2914 /* check_code */
2915 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002916 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002917 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002918 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002919
2920 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002921 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002922 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002923 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002924 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002925
2926 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002927 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002928 }
2929
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002930 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2931 if (px->mode == PR_MODE_HTTP) {
2932 int i;
2933
2934 for (i=1; i<6; i++)
2935 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2936
2937 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2938 } else {
2939 chunk_printf(&msg, ",,,,,,");
2940 }
2941
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002942 /* failed health analyses */
2943 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2944
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002945 /* requests : req_rate, req_rate_max, req_tot, */
2946 chunk_printf(&msg, ",,,");
2947
Willy Tarreauae526782010-03-04 20:34:23 +01002948 /* errors: cli_aborts, srv_aborts */
2949 chunk_printf(&msg, "%lld,%lld,",
2950 sv->counters.cli_aborts, sv->counters.srv_aborts);
2951
Willy Tarreau7f062c42009-03-05 18:43:00 +01002952 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002953 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002954 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002955 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002956 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002957 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002958
Willy Tarreau295a8372011-03-10 11:25:07 +01002959 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02002960 /* fall through */
2961
Willy Tarreau295a8372011-03-10 11:25:07 +01002962 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02002963 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002964 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002965 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2966 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002967 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01002968 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002969 /* Column sub-heading for Enable or Disable server */
2970 chunk_printf(&msg, "<td></td>");
2971 }
2972 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002973
2974 if (uri->flags&ST_SHLGNDS) {
2975 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002976 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002977 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002978
2979 /* cookie */
2980 if (px->cookie_name) {
2981 struct chunk src;
2982
2983 chunk_printf(&msg, ", cookie: '");
2984
2985 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2986 chunk_htmlencode(&msg, &src);
2987
2988 chunk_printf(&msg, "'");
2989 }
2990
2991 chunk_printf(&msg, "\"");
2992
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002993 }
2994
2995 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002996 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01002997 ">%s<a name=\"%s/Backend\"></a>"
2998 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002999 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003000 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003001 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003002 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003003 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003004 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02003005 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01003006 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003007 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
3008 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02003009
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003010 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003011 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003012 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003013 "<td"
3014 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003015 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003016
3017 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3018 if (px->mode == PR_MODE_HTTP) {
3019 int i;
3020
3021 chunk_printf(&msg, " title=\"rsp codes:");
3022
3023 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003024 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003025
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003026 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003027 }
3028
3029 chunk_printf(&msg,
3030 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01003031 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003032 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003033 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003034 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003035 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003036 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01003037 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003038 U2H7(px->be_counters.cum_lbconn),
3039 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003040
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003041 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02003042 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003043 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01003044 /* errors : request, connect */
3045 "<td></td><td>%s</td>"
3046 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01003047 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003048 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003049 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003050 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02003051 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003052 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02003053 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003054 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3055 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003056 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003057 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
3058 U2H2(px->be_counters.failed_conns),
3059 px->be_counters.cli_aborts,
3060 px->be_counters.srv_aborts,
3061 U2H5(px->be_counters.failed_resp),
3062 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003063 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01003064 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3065 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01003066 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003067 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003068
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003069 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003070 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003071 "<td class=ac>&nbsp;</td><td>%d</td>"
3072 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003073 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003074 "</tr>",
3075 px->down_trans,
3076 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003077 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003078 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02003079 /* pxid, name */
3080 "%s,BACKEND,"
3081 /* queue : current, max */
3082 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003083 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003084 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003085 /* bytes : in, out */
3086 "%lld,%lld,"
3087 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003088 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003089 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003090 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003091 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003092 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003093 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02003094 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003095 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02003096 * active and backups. */
3097 "%s,"
3098 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003099 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003100 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003101 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003102 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003103 /* rate, rate_lim, rate_max, */
3104 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003105 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003106 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02003107 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003108 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3109 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3110 px->be_counters.bytes_in, px->be_counters.bytes_out,
3111 px->be_counters.denied_req, px->be_counters.denied_resp,
3112 px->be_counters.failed_conns, px->be_counters.failed_resp,
3113 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01003114 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01003115 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003116 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003117 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01003118 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003119 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003120 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003121 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003122 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003123
3124 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3125 if (px->mode == PR_MODE_HTTP) {
3126 int i;
3127
3128 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003129 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003130
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003131 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003132 } else {
3133 chunk_printf(&msg, ",,,,,,");
3134 }
3135
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003136 /* failed health analyses */
3137 chunk_printf(&msg, ",");
3138
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003139 /* requests : req_rate, req_rate_max, req_tot, */
3140 chunk_printf(&msg, ",,,");
3141
Willy Tarreauae526782010-03-04 20:34:23 +01003142 /* errors: cli_aborts, srv_aborts */
3143 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003144 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01003145
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003146 /* finish with EOL */
3147 chunk_printf(&msg, "\n");
3148
Willy Tarreau55bb8452007-10-17 18:44:57 +02003149 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003150 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003151 return 0;
3152 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003153
Willy Tarreau295a8372011-03-10 11:25:07 +01003154 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02003155 /* fall through */
3156
Willy Tarreau295a8372011-03-10 11:25:07 +01003157 case STAT_PX_ST_END:
3158 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003159 chunk_printf(&msg, "</table>");
3160
Willy Tarreau295a8372011-03-10 11:25:07 +01003161 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003162 /* close the form used to enable/disable this proxy servers */
3163 chunk_printf(&msg,
3164 "Choose the action to perform on the checked servers : "
3165 "<select name=action>"
3166 "<option value=\"\"></option>"
3167 "<option value=\"disable\">Disable</option>"
3168 "<option value=\"enable\">Enable</option>"
3169 "</select>"
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003170 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003171 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3172 "</form>",
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003173 px->uuid);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003174 }
3175
3176 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02003177
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003178 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003179 return 0;
3180 }
Willy Tarreau91861262007-10-17 17:06:05 +02003181
Willy Tarreau295a8372011-03-10 11:25:07 +01003182 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02003183 /* fall through */
3184
Willy Tarreau295a8372011-03-10 11:25:07 +01003185 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02003186 return 1;
3187
3188 default:
3189 /* unknown state, we should put an abort() here ! */
3190 return 1;
3191 }
3192}
3193
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003194/* This function dumps a complete session state onto the stream intreface's
3195 * read buffer. The data_ctx must have been zeroed first, and the flags
3196 * properly set. The session has to be set in data_ctx.sess.target. It returns
3197 * 0 if the output buffer is full and it needs to be called again, otherwise
3198 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003199 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003200static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003201{
3202 struct tm tm;
3203 struct chunk msg;
3204 struct session *sess;
3205 extern const char *monthname[12];
3206 char pn[INET6_ADDRSTRLEN];
3207
David du Colombier7af46052012-05-16 14:16:48 +02003208 chunk_init(&msg, trash, trashlen);
Willy Tarreau295a8372011-03-10 11:25:07 +01003209 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003210
Willy Tarreau295a8372011-03-10 11:25:07 +01003211 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003212 /* session changed, no need to go any further */
3213 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003214 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003215 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003216 si->applet.ctx.sess.target = NULL;
3217 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003218 return 1;
3219 }
3220
Willy Tarreau295a8372011-03-10 11:25:07 +01003221 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003222 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003223 si->applet.ctx.sess.uid = sess->uniq_id;
3224 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003225 /* fall through */
3226
3227 case 1:
3228 chunk_printf(&msg,
3229 "%p: id=%u, proto=%s",
3230 sess,
3231 sess->uniq_id,
3232 sess->listener->proto->name);
3233
Willy Tarreau6471afb2011-09-23 10:54:59 +02003234 switch (addr_to_str(&sess->si[0].addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003235 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003236 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003237 chunk_printf(&msg, " source=%s:%d\n",
Willy Tarreau6471afb2011-09-23 10:54:59 +02003238 pn, get_host_port(&sess->si[0].addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003239 break;
3240 case AF_UNIX:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003241 chunk_printf(&msg, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003242 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003243 default:
3244 /* no more information to print right now */
3245 chunk_printf(&msg, "\n");
3246 break;
3247 }
3248
3249 chunk_printf(&msg,
3250 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003251 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003252
3253 chunk_printf(&msg,
Daniel Schultze90690c72012-03-23 10:53:36 -07003254 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003255 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3256 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3257 sess->listener ? sess->listener->luid : 0);
3258
Willy Tarreau59b94792012-05-11 16:16:40 +02003259 si_get_to_addr(&sess->si[0]);
Daniel Schultze90690c72012-03-23 10:53:36 -07003260 switch (addr_to_str(&sess->si[0].addr.to, pn, sizeof(pn))) {
3261 case AF_INET:
3262 case AF_INET6:
3263 chunk_printf(&msg, " addr=%s:%d\n",
3264 pn, get_host_port(&sess->si[0].addr.to));
3265 break;
3266 case AF_UNIX:
3267 chunk_printf(&msg, " addr=unix:%d\n", sess->listener->luid);
3268 break;
3269 default:
3270 /* no more information to print right now */
3271 chunk_printf(&msg, "\n");
3272 break;
3273 }
3274
Willy Tarreau50943332011-09-02 17:33:05 +02003275 if (sess->be->cap & PR_CAP_BE)
3276 chunk_printf(&msg,
Daniel Schultze90690c72012-03-23 10:53:36 -07003277 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003278 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003279 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3280 else
3281 chunk_printf(&msg, " backend=<NONE> (id=-1 mode=-)");
3282
Willy Tarreau59b94792012-05-11 16:16:40 +02003283 si_get_from_addr(&sess->si[1]);
Daniel Schultze90690c72012-03-23 10:53:36 -07003284 switch (addr_to_str(&sess->si[1].addr.from, pn, sizeof(pn))) {
3285 case AF_INET:
3286 case AF_INET6:
3287 chunk_printf(&msg, " addr=%s:%d\n",
3288 pn, get_host_port(&sess->si[1].addr.from));
3289 break;
3290 case AF_UNIX:
3291 chunk_printf(&msg, " addr=unix\n");
3292 break;
3293 default:
3294 /* no more information to print right now */
3295 chunk_printf(&msg, "\n");
3296 break;
3297 }
3298
3299 if (sess->be->cap & PR_CAP_BE)
3300 chunk_printf(&msg,
3301 " server=%s (id=%u)",
Willy Tarreau50943332011-09-02 17:33:05 +02003302 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
3303 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
3304 else
Daniel Schultze90690c72012-03-23 10:53:36 -07003305 chunk_printf(&msg, " server=<NONE> (id=-1)");
3306
Willy Tarreau59b94792012-05-11 16:16:40 +02003307 si_get_to_addr(&sess->si[1]);
Daniel Schultze90690c72012-03-23 10:53:36 -07003308 switch (addr_to_str(&sess->si[1].addr.to, pn, sizeof(pn))) {
3309 case AF_INET:
3310 case AF_INET6:
3311 chunk_printf(&msg, " addr=%s:%d\n",
3312 pn, get_host_port(&sess->si[1].addr.to));
3313 break;
3314 case AF_UNIX:
3315 chunk_printf(&msg, " addr=unix\n");
3316 break;
3317 default:
3318 /* no more information to print right now */
3319 chunk_printf(&msg, "\n");
3320 break;
3321 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003322
3323 chunk_printf(&msg,
3324 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
3325 sess->task,
3326 sess->task->state,
3327 sess->task->nice, sess->task->calls,
3328 sess->task->expire ?
3329 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3330 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3331 TICKS_TO_MS(1000)) : "<NEVER>",
3332 task_in_rq(sess->task) ? ", running" : "");
3333
3334 get_localtime(sess->logs.accept_date.tv_sec, &tm);
3335 chunk_printf(&msg,
3336 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
3337 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3338 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3339 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3340
3341 chunk_printf(&msg,
3342 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3343 &sess->si[0],
3344 sess->si[0].state,
3345 sess->si[0].flags,
3346 sess->si[0].fd,
3347 sess->si[0].exp ?
3348 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3349 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3350 TICKS_TO_MS(1000)) : "<NEVER>",
3351 sess->si[0].err_type);
3352
3353 chunk_printf(&msg,
3354 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3355 &sess->si[1],
3356 sess->si[1].state,
3357 sess->si[1].flags,
3358 sess->si[1].fd,
3359 sess->si[1].exp ?
3360 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3361 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3362 TICKS_TO_MS(1000)) : "<NEVER>",
3363 sess->si[1].err_type);
3364
3365 chunk_printf(&msg,
3366 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3367 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3368 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3369
3370
3371 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003372 " req=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003373 " an_exp=%s",
3374 sess->req,
3375 sess->req->flags, sess->req->analysers,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003376 sess->req->i, sess->req->o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003377 sess->req->pipe ? sess->req->pipe->data : 0,
3378 sess->req->to_forward,
3379 sess->req->analyse_exp ?
3380 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3381 TICKS_TO_MS(1000)) : "<NEVER>");
3382
3383 chunk_printf(&msg,
3384 " rex=%s",
3385 sess->req->rex ?
3386 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3387 TICKS_TO_MS(1000)) : "<NEVER>");
3388
3389 chunk_printf(&msg,
3390 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003391 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003392 sess->req->wex ?
3393 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3394 TICKS_TO_MS(1000)) : "<NEVER>",
3395 sess->req->data,
Willy Tarreau89fa7062012-03-02 16:13:16 +01003396 (int)(sess->req->p - sess->req->data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003397 sess->txn.req.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003398 sess->req->total);
3399
3400 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003401 " res=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003402 " an_exp=%s",
3403 sess->rep,
3404 sess->rep->flags, sess->rep->analysers,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003405 sess->rep->i, sess->rep->o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003406 sess->rep->pipe ? sess->rep->pipe->data : 0,
3407 sess->rep->to_forward,
3408 sess->rep->analyse_exp ?
3409 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3410 TICKS_TO_MS(1000)) : "<NEVER>");
3411
3412 chunk_printf(&msg,
3413 " rex=%s",
3414 sess->rep->rex ?
3415 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3416 TICKS_TO_MS(1000)) : "<NEVER>");
3417
3418 chunk_printf(&msg,
3419 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003420 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003421 sess->rep->wex ?
3422 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3423 TICKS_TO_MS(1000)) : "<NEVER>",
3424 sess->rep->data,
Willy Tarreau89fa7062012-03-02 16:13:16 +01003425 (int)(sess->rep->p - sess->rep->data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003426 sess->txn.rsp.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003427 sess->rep->total);
3428
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003429 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003430 return 0;
3431
3432 /* use other states to dump the contents */
3433 }
3434 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003435 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003436 return 1;
3437}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003438
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003439/* This function dumps all sessions' states onto the stream intreface's
3440 * read buffer. The data_ctx must have been zeroed first, and the flags
3441 * properly set. It returns 0 if the output buffer is full and it needs
3442 * to be called again, otherwise non-zero. It is designed to be called
3443 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003444 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003445static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003446{
3447 struct chunk msg;
3448
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003449 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003450 /* If we're forced to shut down, we might have to remove our
3451 * reference to the last session being dumped.
3452 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003453 if (si->applet.state == STAT_ST_LIST) {
3454 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3455 LIST_DEL(&si->applet.ctx.sess.bref.users);
3456 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003457 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003458 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003459 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003460 }
3461
David du Colombier7af46052012-05-16 14:16:48 +02003462 chunk_init(&msg, trash, trashlen);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003463
Willy Tarreau295a8372011-03-10 11:25:07 +01003464 switch (si->applet.state) {
3465 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003466 /* the function had not been called yet, let's prepare the
3467 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003468 * pointer to the first in the global list. When a target
3469 * session is being destroyed, it is responsible for updating
3470 * this pointer. We know we have reached the end when this
3471 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003472 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003473 LIST_INIT(&si->applet.ctx.sess.bref.users);
3474 si->applet.ctx.sess.bref.ref = sessions.n;
3475 si->applet.state = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003476 /* fall through */
3477
Willy Tarreau295a8372011-03-10 11:25:07 +01003478 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003479 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003480 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3481 LIST_DEL(&si->applet.ctx.sess.bref.users);
3482 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003483 }
3484
3485 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003486 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003487 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003488 struct session *curr_sess;
3489
Willy Tarreau295a8372011-03-10 11:25:07 +01003490 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003491
Willy Tarreau295a8372011-03-10 11:25:07 +01003492 if (si->applet.ctx.sess.target) {
3493 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003494 goto next_sess;
3495
Willy Tarreau295a8372011-03-10 11:25:07 +01003496 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003497 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003498 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003499 return 0;
3500
3501 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003502 LIST_DEL(&si->applet.ctx.sess.bref.users);
3503 LIST_INIT(&si->applet.ctx.sess.bref.users);
3504 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003505 break;
3506 }
3507
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003508 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003509 "%p: proto=%s",
3510 curr_sess,
3511 curr_sess->listener->proto->name);
3512
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003513
Willy Tarreau6471afb2011-09-23 10:54:59 +02003514 switch (addr_to_str(&curr_sess->si[0].addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003515 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003516 case AF_INET6:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003517 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003518 " src=%s:%d fe=%s be=%s srv=%s",
3519 pn,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003520 get_host_port(&curr_sess->si[0].addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003521 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003522 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003523 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003524 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003525 break;
3526 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02003527 chunk_printf(&msg,
3528 " src=unix:%d fe=%s be=%s srv=%s",
3529 curr_sess->listener->luid,
3530 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003531 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003532 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003533 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003534 break;
3535 }
3536
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003537 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003538 " ts=%02x age=%s calls=%d",
3539 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003540 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3541 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003542
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003543 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003544 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003545 curr_sess->req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003546 curr_sess->req->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003547 curr_sess->req->analysers,
3548 curr_sess->req->rex ?
3549 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3550 TICKS_TO_MS(1000)) : "");
3551
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003552 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003553 ",wx=%s",
3554 curr_sess->req->wex ?
3555 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3556 TICKS_TO_MS(1000)) : "");
3557
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003558 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003559 ",ax=%s]",
3560 curr_sess->req->analyse_exp ?
3561 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3562 TICKS_TO_MS(1000)) : "");
3563
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003564 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003565 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003566 curr_sess->rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003567 curr_sess->rep->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003568 curr_sess->rep->analysers,
3569 curr_sess->rep->rex ?
3570 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3571 TICKS_TO_MS(1000)) : "");
3572
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003573 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003574 ",wx=%s",
3575 curr_sess->rep->wex ?
3576 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3577 TICKS_TO_MS(1000)) : "");
3578
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003579 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003580 ",ax=%s]",
3581 curr_sess->rep->analyse_exp ?
3582 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3583 TICKS_TO_MS(1000)) : "");
3584
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003585 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003586 " s0=[%d,%1xh,fd=%d,ex=%s]",
3587 curr_sess->si[0].state,
3588 curr_sess->si[0].flags,
3589 curr_sess->si[0].fd,
3590 curr_sess->si[0].exp ?
3591 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3592 TICKS_TO_MS(1000)) : "");
3593
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003594 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003595 " s1=[%d,%1xh,fd=%d,ex=%s]",
3596 curr_sess->si[1].state,
3597 curr_sess->si[1].flags,
3598 curr_sess->si[1].fd,
3599 curr_sess->si[1].exp ?
3600 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3601 TICKS_TO_MS(1000)) : "");
3602
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003603 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003604 " exp=%s",
3605 curr_sess->task->expire ?
3606 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3607 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003608 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003609 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003610
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003611 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003612
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003613 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003614 /* let's try again later from this session. We add ourselves into
3615 * this session's users so that it can remove us upon termination.
3616 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003617 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003618 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003619 }
3620
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003621 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003622 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003623 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003624
Willy Tarreau295a8372011-03-10 11:25:07 +01003625 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003626 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003627 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003628 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3629 else
3630 chunk_printf(&msg, "Session not found.\n");
3631
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003632 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003633 return 0;
3634
Willy Tarreau295a8372011-03-10 11:25:07 +01003635 si->applet.ctx.sess.target = NULL;
3636 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003637 return 1;
3638 }
3639
Willy Tarreau295a8372011-03-10 11:25:07 +01003640 si->applet.state = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003641 /* fall through */
3642
3643 default:
Willy Tarreau295a8372011-03-10 11:25:07 +01003644 si->applet.state = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003645 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003646 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003647}
3648
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003649/* This function dumps all tables' states onto the stream intreface's
3650 * read buffer. The data_ctx must have been zeroed first, and the flags
3651 * properly set. It returns 0 if the output buffer is full and it needs
3652 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003653 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003654static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003655{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003656 struct session *s = si->applet.private;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003657 struct chunk msg;
3658 struct ebmb_node *eb;
3659 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003660 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003661
3662 /*
Willy Tarreau295a8372011-03-10 11:25:07 +01003663 * We have 3 possible states in si->applet.state :
3664 * - STAT_ST_INIT : the first call
3665 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003666 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003667 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003668 * and the entry pointer points to the next entry to be dumped,
3669 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003670 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003671 * data though.
3672 */
3673
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003674 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003675 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau295a8372011-03-10 11:25:07 +01003676 if (si->applet.state == STAT_ST_LIST) {
3677 si->applet.ctx.table.entry->ref_cnt--;
3678 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003679 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003680 return 1;
3681 }
3682
David du Colombier7af46052012-05-16 14:16:48 +02003683 chunk_init(&msg, trash, trashlen);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003684
Willy Tarreau295a8372011-03-10 11:25:07 +01003685 while (si->applet.state != STAT_ST_FIN) {
3686 switch (si->applet.state) {
3687 case STAT_ST_INIT:
3688 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3689 if (!si->applet.ctx.table.proxy)
3690 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003691
Willy Tarreau295a8372011-03-10 11:25:07 +01003692 si->applet.ctx.table.entry = NULL;
3693 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003694 break;
3695
Willy Tarreau295a8372011-03-10 11:25:07 +01003696 case STAT_ST_INFO:
3697 if (!si->applet.ctx.table.proxy ||
3698 (si->applet.ctx.table.target &&
3699 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
3700 si->applet.state = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003701 break;
3702 }
3703
Willy Tarreau295a8372011-03-10 11:25:07 +01003704 if (si->applet.ctx.table.proxy->table.size) {
Simon Hormanc88b8872011-06-15 15:18:49 +09003705 if (show && !stats_dump_table_head_to_buffer(&msg, si, si->applet.ctx.table.proxy,
3706 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003707 return 0;
3708
Willy Tarreau295a8372011-03-10 11:25:07 +01003709 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003710 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
3711 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003712 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003713 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003714 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3715 si->applet.ctx.table.entry->ref_cnt++;
3716 si->applet.state = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003717 break;
3718 }
3719 }
3720 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003721 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003722 break;
3723
Willy Tarreau295a8372011-03-10 11:25:07 +01003724 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003725 skip_entry = false;
3726
Willy Tarreau295a8372011-03-10 11:25:07 +01003727 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003728 /* we're filtering on some data contents */
3729 void *ptr;
3730 long long data;
3731
Willy Tarreau295a8372011-03-10 11:25:07 +01003732 dt = si->applet.ctx.table.data_type;
3733 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3734 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003735 dt);
3736
3737 data = 0;
3738 switch (stktable_data_types[dt].std_type) {
3739 case STD_T_SINT:
3740 data = stktable_data_cast(ptr, std_t_sint);
3741 break;
3742 case STD_T_UINT:
3743 data = stktable_data_cast(ptr, std_t_uint);
3744 break;
3745 case STD_T_ULL:
3746 data = stktable_data_cast(ptr, std_t_ull);
3747 break;
3748 case STD_T_FRQP:
3749 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003750 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003751 break;
3752 }
3753
3754 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003755 if ((data < si->applet.ctx.table.value &&
3756 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3757 si->applet.ctx.table.data_op == STD_OP_GT ||
3758 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3759 (data == si->applet.ctx.table.value &&
3760 (si->applet.ctx.table.data_op == STD_OP_NE ||
3761 si->applet.ctx.table.data_op == STD_OP_GT ||
3762 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3763 (data > si->applet.ctx.table.value &&
3764 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3765 si->applet.ctx.table.data_op == STD_OP_LT ||
3766 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003767 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003768 }
3769
Simon Hormanc88b8872011-06-15 15:18:49 +09003770 if (show && !skip_entry &&
3771 !stats_dump_table_entry_to_buffer(&msg, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003772 si->applet.ctx.table.entry))
3773 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003774
Willy Tarreau295a8372011-03-10 11:25:07 +01003775 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003776
Willy Tarreau295a8372011-03-10 11:25:07 +01003777 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003778 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003779 struct stksess *old = si->applet.ctx.table.entry;
3780 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01003781 if (show)
3782 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3783 else
3784 stksess_kill(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau295a8372011-03-10 11:25:07 +01003785 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003786 break;
3787 }
3788
Simon Hormanc88b8872011-06-15 15:18:49 +09003789
3790 if (show)
3791 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3792 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3793 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3794
Willy Tarreau295a8372011-03-10 11:25:07 +01003795 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
3796 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003797 break;
3798
Willy Tarreau295a8372011-03-10 11:25:07 +01003799 case STAT_ST_END:
3800 si->applet.state = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003801 break;
3802 }
3803 }
3804 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003805}
3806
Willy Tarreaud426a182010-03-05 14:58:26 +01003807/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003808 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3809 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3810 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3811 * lines are respected within the limit of 70 output chars. Lines that are
3812 * continuation of a previous truncated line begin with "+" instead of " "
3813 * after the offset. The new pointer is returned.
3814 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003815static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3816 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003817{
3818 int end;
3819 unsigned char c;
3820
3821 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003822 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003823 return ptr;
3824
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003825 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003826
Willy Tarreaud426a182010-03-05 14:58:26 +01003827 while (ptr < len && ptr < bsize) {
3828 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003829 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003830 if (out->len > end - 2)
3831 break;
3832 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003833 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003834 if (out->len > end - 3)
3835 break;
3836 out->str[out->len++] = '\\';
3837 switch (c) {
3838 case '\t': c = 't'; break;
3839 case '\n': c = 'n'; break;
3840 case '\r': c = 'r'; break;
3841 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003842 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003843 }
3844 out->str[out->len++] = c;
3845 } else {
3846 if (out->len > end - 5)
3847 break;
3848 out->str[out->len++] = '\\';
3849 out->str[out->len++] = 'x';
3850 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3851 out->str[out->len++] = hextab[c & 0xF];
3852 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003853 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003854 /* we had a line break, let's return now */
3855 out->str[out->len++] = '\n';
3856 *line = ptr;
3857 return ptr;
3858 }
3859 }
3860 /* we have an incomplete line, we return it as-is */
3861 out->str[out->len++] = '\n';
3862 return ptr;
3863}
3864
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003865/* This function dumps all captured errors onto the stream intreface's
3866 * read buffer. The data_ctx must have been zeroed first, and the flags
3867 * properly set. It returns 0 if the output buffer is full and it needs
3868 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003869 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003870static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003871{
3872 extern const char *monthname[12];
3873 struct chunk msg;
3874
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003875 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003876 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003877
David du Colombier7af46052012-05-16 14:16:48 +02003878 chunk_init(&msg, trash, trashlen);
Willy Tarreau74808cb2009-03-04 15:53:18 +01003879
Willy Tarreau295a8372011-03-10 11:25:07 +01003880 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003881 /* the function had not been called yet, let's prepare the
3882 * buffer for a response.
3883 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003884 struct tm tm;
3885
3886 get_localtime(date.tv_sec, &tm);
3887 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3888 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3889 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3890 error_snapshot_id);
3891
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003892 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003893 /* Socket buffer full. Let's try again later from the same point */
3894 return 0;
3895 }
3896
Willy Tarreau295a8372011-03-10 11:25:07 +01003897 si->applet.ctx.errors.px = proxy;
3898 si->applet.ctx.errors.buf = 0;
3899 si->applet.ctx.errors.bol = 0;
3900 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003901 }
3902
3903 /* we have two inner loops here, one for the proxy, the other one for
3904 * the buffer.
3905 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003906 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003907 struct error_snapshot *es;
3908
Willy Tarreau295a8372011-03-10 11:25:07 +01003909 if (si->applet.ctx.errors.buf == 0)
3910 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003911 else
Willy Tarreau295a8372011-03-10 11:25:07 +01003912 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003913
3914 if (!es->when.tv_sec)
3915 goto next;
3916
Willy Tarreau295a8372011-03-10 11:25:07 +01003917 if (si->applet.ctx.errors.iid >= 0 &&
3918 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
3919 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003920 goto next;
3921
Willy Tarreau295a8372011-03-10 11:25:07 +01003922 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003923 /* just print headers now */
3924
3925 char pn[INET6_ADDRSTRLEN];
3926 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003927 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003928
3929 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003930 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003931 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003932 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003933
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003934 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
3935 case AF_INET:
3936 case AF_INET6:
3937 port = get_host_port(&es->src);
3938 break;
3939 default:
3940 port = 0;
3941 }
3942
Willy Tarreau295a8372011-03-10 11:25:07 +01003943 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003944 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003945 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003946 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003947 " backend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01003948 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003949 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3950 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01003951 break;
3952 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003953 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003954 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003955 " frontend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01003956 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003957 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01003958 break;
3959 }
3960
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003961 chunk_printf(&msg,
3962 ", server %s (#%d), event #%u\n"
3963 " src %s:%d, session #%d, session flags 0x%08x\n"
3964 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
3965 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
3966 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
3967 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
3968 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
3969 es->ev_id,
3970 pn, port, es->sid, es->s_flags,
3971 es->state, es->m_flags, es->t_flags,
3972 es->m_clen, es->m_blen,
3973 es->b_flags, es->b_out, es->b_tot,
3974 es->len, es->b_wrap, es->pos);
3975
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003976 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003977 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003978 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003979 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003980 si->applet.ctx.errors.ptr = 0;
3981 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003982 }
3983
Willy Tarreau295a8372011-03-10 11:25:07 +01003984 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003985 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003986 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003987 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003988 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02003989 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003990 goto next;
3991 }
3992
3993 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01003994 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003995 int newptr;
3996 int newline;
3997
Willy Tarreau295a8372011-03-10 11:25:07 +01003998 newline = si->applet.ctx.errors.bol;
3999 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
4000 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004001 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004002
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004003 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004004 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004005 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004006 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004007 si->applet.ctx.errors.ptr = newptr;
4008 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004009 };
4010 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01004011 si->applet.ctx.errors.bol = 0;
4012 si->applet.ctx.errors.ptr = -1;
4013 si->applet.ctx.errors.buf++;
4014 if (si->applet.ctx.errors.buf > 1) {
4015 si->applet.ctx.errors.buf = 0;
4016 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004017 }
4018 }
4019
4020 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004021 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004022}
4023
Willy Tarreaub24281b2011-02-13 13:16:36 +01004024struct si_applet http_stats_applet = {
4025 .name = "<STATS>", /* used for logging */
4026 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004027 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004028};
4029
Simon Horman9bd2c732011-06-15 15:18:44 +09004030static struct si_applet cli_applet = {
Willy Tarreaub24281b2011-02-13 13:16:36 +01004031 .name = "<CLI>", /* used for logging */
4032 .fct = cli_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004033 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004034};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004035
Willy Tarreau10522fd2008-07-09 20:12:41 +02004036static struct cfg_kw_list cfg_kws = {{ },{
4037 { CFG_GLOBAL, "stats", stats_parse_global },
4038 { 0, NULL, NULL },
4039}};
4040
4041__attribute__((constructor))
4042static void __dumpstats_module_init(void)
4043{
4044 cfg_register_keywords(&cfg_kws);
4045}
4046
Willy Tarreau91861262007-10-17 17:06:05 +02004047/*
4048 * Local variables:
4049 * c-indent-level: 8
4050 * c-basic-offset: 8
4051 * End:
4052 */