blob: 6b4c84e7914177e9e597c6cda083d3b5e78e71a2 [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 Tarreau94981132012-05-21 17:09:48 +0200128 s->si[1].conn.data_ctx = s;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100129 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{
Willy Tarreau94981132012-05-21 17:09:48 +0200420 struct session *s = si->conn.data_ctx;
Simon Hormand9366582011-06-15 15:18:45 +0900421
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{
Willy Tarreau94981132012-05-21 17:09:48 +0200509 struct session *s = si->conn.data_ctx;
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;
Willy Tarreau94981132012-05-21 17:09:48 +0200626 si->conn.data_st = 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 Tarreau94981132012-05-21 17:09:48 +0200748 struct session *s = si->conn.data_ctx;
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;
Willy Tarreau94981132012-05-21 17:09:48 +0200787 si->conn.data_st = 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;
Willy Tarreau94981132012-05-21 17:09:48 +0200793 si->conn.data_st = 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 Tarreau94981132012-05-21 17:09:48 +0200797 si->conn.data_st = 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;
Willy Tarreau94981132012-05-21 17:09:48 +0200822 si->conn.data_st = 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);
Willy Tarreau9580d162012-05-19 19:07:40 +0200992 else if (sv->eweight) {
993 if (px->lbprm.set_server_status_up)
994 px->lbprm.set_server_status_up(sv);
995 }
996 else {
997 if (px->lbprm.set_server_status_down)
998 px->lbprm.set_server_status_down(sv);
999 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001000
1001 return 1;
1002 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001003 else if (strcmp(args[1], "timeout") == 0) {
1004 if (strcmp(args[2], "cli") == 0) {
1005 unsigned timeout;
1006 const char *res;
1007
1008 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001009 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001010 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001011 return 1;
1012 }
1013
1014 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1015 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001016 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001017 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001018 return 1;
1019 }
1020
1021 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1022 return 1;
1023 }
1024 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001025 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001026 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001027 return 1;
1028 }
1029 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001030 else if (strcmp(args[1], "maxconn") == 0) {
1031 if (strcmp(args[2], "frontend") == 0) {
1032 struct proxy *px;
1033 struct listener *l;
1034 int v;
1035
Willy Tarreau532a4502011-09-07 22:37:44 +02001036 px = expect_frontend_admin(s, si, args[3]);
1037 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001038 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001039
1040 if (!*args[4]) {
1041 si->applet.ctx.cli.msg = "Integer value expected.\n";
1042 si->applet.st0 = STAT_CLI_PRINT;
1043 return 1;
1044 }
1045
1046 v = atoi(args[4]);
1047 /* check for unlimited values, we restore default setting (cfg_maxpconn) */
1048 if (v < 1) {
1049 si->applet.ctx.cli.msg = "Value out of range.\n";
1050 si->applet.st0 = STAT_CLI_PRINT;
1051 return 1;
1052 }
1053
1054 /* OK, the value is fine, so we assign it to the proxy and to all of
1055 * its listeners. The blocked ones will be dequeued.
1056 */
1057 px->maxconn = v;
1058 for (l = px->listen; l != NULL; l = l->next) {
1059 l->maxconn = v;
1060 if (l->state == LI_FULL)
1061 resume_listener(l);
1062 }
1063
1064 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1065 dequeue_all_listeners(&s->fe->listener_queue);
1066
1067 return 1;
1068 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001069 else if (strcmp(args[2], "global") == 0) {
1070 int v;
1071
1072 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1073 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1074 si->applet.st0 = STAT_CLI_PRINT;
1075 return 1;
1076 }
1077
1078 if (!*args[3]) {
1079 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1080 si->applet.st0 = STAT_CLI_PRINT;
1081 return 1;
1082 }
1083
1084 v = atoi(args[3]);
1085 if (v > global.hardmaxconn) {
1086 si->applet.ctx.cli.msg = "Value out of range.\n";
1087 si->applet.st0 = STAT_CLI_PRINT;
1088 return 1;
1089 }
1090
1091 /* check for unlimited values */
1092 if (v <= 0)
1093 v = global.hardmaxconn;
1094
1095 global.maxconn = v;
1096
1097 /* Dequeues all of the listeners waiting for a resource */
1098 if (!LIST_ISEMPTY(&global_listener_queue))
1099 dequeue_all_listeners(&global_listener_queue);
1100
1101 return 1;
1102 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001103 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001104 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001105 si->applet.st0 = STAT_CLI_PRINT;
1106 return 1;
1107 }
1108 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001109 else if (strcmp(args[1], "rate-limit") == 0) {
1110 if (strcmp(args[2], "connections") == 0) {
1111 if (strcmp(args[3], "global") == 0) {
1112 int v;
1113
1114 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1115 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1116 si->applet.st0 = STAT_CLI_PRINT;
1117 return 1;
1118 }
1119
1120 if (!*args[4]) {
1121 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1122 si->applet.st0 = STAT_CLI_PRINT;
1123 return 1;
1124 }
1125
1126 v = atoi(args[4]);
1127 if (v < 0) {
1128 si->applet.ctx.cli.msg = "Value out of range.\n";
1129 si->applet.st0 = STAT_CLI_PRINT;
1130 return 1;
1131 }
1132
1133 global.cps_lim = v;
1134
1135 /* Dequeues all of the listeners waiting for a resource */
1136 if (!LIST_ISEMPTY(&global_listener_queue))
1137 dequeue_all_listeners(&global_listener_queue);
1138
1139 return 1;
1140 }
1141 else {
1142 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1143 si->applet.st0 = STAT_CLI_PRINT;
1144 return 1;
1145 }
1146 }
1147 else {
1148 si->applet.ctx.cli.msg = "'set rate-limit' only supports 'connections'.\n";
1149 si->applet.st0 = STAT_CLI_PRINT;
1150 return 1;
1151 }
1152 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001153 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001154 return 0;
1155 }
1156 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001157 else if (strcmp(args[0], "enable") == 0) {
1158 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001159 struct server *sv;
1160
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001161 sv = expect_server_admin(s, si, args[2]);
1162 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001163 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001164
Cyril Bontécd19e512010-01-31 22:34:03 +01001165 if (sv->state & SRV_MAINTAIN) {
1166 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001167 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001168 /* If this server tracks the status of another one,
1169 * we must restore the good status.
1170 */
Willy Tarreau44267702011-10-28 15:35:33 +02001171 if (sv->track->state & SRV_RUNNING) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001172 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001173 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001174 } else {
1175 sv->state &= ~SRV_MAINTAIN;
1176 set_server_down(sv);
1177 }
1178 } else {
1179 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001180 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001181 }
1182 }
1183
Willy Tarreau532a4502011-09-07 22:37:44 +02001184 return 1;
1185 }
1186 else if (strcmp(args[1], "frontend") == 0) {
1187 struct proxy *px;
1188
1189 px = expect_frontend_admin(s, si, args[2]);
1190 if (!px)
1191 return 1;
1192
1193 if (px->state == PR_STSTOPPED) {
1194 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1195 si->applet.st0 = STAT_CLI_PRINT;
1196 return 1;
1197 }
1198
1199 if (px->state != PR_STPAUSED) {
1200 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1201 si->applet.st0 = STAT_CLI_PRINT;
1202 return 1;
1203 }
1204
1205 if (!resume_proxy(px)) {
1206 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1207 si->applet.st0 = STAT_CLI_PRINT;
1208 return 1;
1209 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001210 return 1;
1211 }
1212 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001213 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1214 si->applet.st0 = STAT_CLI_PRINT;
1215 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001216 }
1217 }
1218 else if (strcmp(args[0], "disable") == 0) {
1219 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001220 struct server *sv;
1221
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001222 sv = expect_server_admin(s, si, args[2]);
1223 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001224 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001225
Cyril Bontécd19e512010-01-31 22:34:03 +01001226 if (! (sv->state & SRV_MAINTAIN)) {
1227 /* Not already in maintenance, we can change the server state */
1228 sv->state |= SRV_MAINTAIN;
1229 set_server_down(sv);
1230 }
1231
Willy Tarreau532a4502011-09-07 22:37:44 +02001232 return 1;
1233 }
1234 else if (strcmp(args[1], "frontend") == 0) {
1235 struct proxy *px;
1236
1237 px = expect_frontend_admin(s, si, args[2]);
1238 if (!px)
1239 return 1;
1240
1241 if (px->state == PR_STSTOPPED) {
1242 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1243 si->applet.st0 = STAT_CLI_PRINT;
1244 return 1;
1245 }
1246
1247 if (px->state == PR_STPAUSED) {
1248 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1249 si->applet.st0 = STAT_CLI_PRINT;
1250 return 1;
1251 }
1252
1253 if (!pause_proxy(px)) {
1254 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1255 si->applet.st0 = STAT_CLI_PRINT;
1256 return 1;
1257 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001258 return 1;
1259 }
1260 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001261 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1262 si->applet.st0 = STAT_CLI_PRINT;
1263 return 1;
1264 }
1265 }
1266 else if (strcmp(args[0], "shutdown") == 0) {
1267 if (strcmp(args[1], "frontend") == 0) {
1268 struct proxy *px;
1269
1270 px = expect_frontend_admin(s, si, args[2]);
1271 if (!px)
1272 return 1;
1273
1274 if (px->state == PR_STSTOPPED) {
1275 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1276 si->applet.st0 = STAT_CLI_PRINT;
1277 return 1;
1278 }
1279
1280 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1281 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1282 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1283 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1284 stop_proxy(px);
1285 return 1;
1286 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001287 else if (strcmp(args[1], "session") == 0) {
1288 struct session *sess, *ptr;
1289
1290 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1291 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1292 si->applet.st0 = STAT_CLI_PRINT;
1293 return 1;
1294 }
1295
1296 if (!*args[2]) {
1297 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1298 si->applet.st0 = STAT_CLI_PRINT;
1299 return 1;
1300 }
1301
1302 ptr = (void *)strtoul(args[2], NULL, 0);
1303
1304 /* first, look for the requested session in the session table */
1305 list_for_each_entry(sess, &sessions, list) {
1306 if (sess == ptr)
1307 break;
1308 }
1309
1310 /* do we have the session ? */
1311 if (sess != ptr) {
1312 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1313 si->applet.st0 = STAT_CLI_PRINT;
1314 return 1;
1315 }
1316
1317 session_shutdown(sess, SN_ERR_KILLED);
1318 return 1;
1319 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001320 else if (strcmp(args[1], "sessions") == 0) {
1321 if (strcmp(args[2], "server") == 0) {
1322 struct server *sv;
1323 struct session *sess, *sess_bck;
1324
1325 sv = expect_server_admin(s, si, args[3]);
1326 if (!sv)
1327 return 1;
1328
1329 /* kill all the session that are on this server */
1330 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1331 if (sess->srv_conn == sv)
1332 session_shutdown(sess, SN_ERR_KILLED);
1333
1334 return 1;
1335 }
1336 else {
1337 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1338 si->applet.st0 = STAT_CLI_PRINT;
1339 return 1;
1340 }
1341 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001342 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001343 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001344 si->applet.st0 = STAT_CLI_PRINT;
1345 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001346 }
1347 }
1348 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001349 return 0;
1350 }
1351 return 1;
1352}
1353
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001354/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001355 * used to processes I/O from/to the stats unix socket. The system relies on a
1356 * state machine handling requests and various responses. We read a request,
1357 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001358 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1359 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001360 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001361 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001362static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001363{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001364 struct buffer *req = si->ob;
1365 struct buffer *res = si->ib;
1366 int reql;
1367 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001368
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001369 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1370 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001371
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001372 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001373 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001374 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001375 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001376 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001377 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001378 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001379 /* Let's close for real now. We just close the request
1380 * side, the conditions below will complete if needed.
1381 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001382 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001383 break;
1384 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001385 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001386 /* ensure we have some output room left in the event we
1387 * would want to return some info right after parsing.
1388 */
1389 if (buffer_almost_full(si->ib))
1390 break;
1391
David du Colombier7af46052012-05-16 14:16:48 +02001392 reql = bo_getline(si->ob, trash, trashlen);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001393 if (reql <= 0) { /* closed or EOL not found */
1394 if (reql == 0)
1395 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001396 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001397 continue;
1398 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001399
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001400 /* seek for a possible semi-colon. If we find one, we
1401 * replace it with an LF and skip only this part.
1402 */
1403 for (len = 0; len < reql; len++)
1404 if (trash[len] == ';') {
1405 trash[len] = '\n';
1406 reql = len + 1;
1407 break;
1408 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001409
Willy Tarreau816fc222009-10-04 07:36:58 +02001410 /* now it is time to check that we have a full line,
1411 * remove the trailing \n and possibly \r, then cut the
1412 * line.
1413 */
1414 len = reql - 1;
1415 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001416 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001417 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001418 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001419
Willy Tarreau816fc222009-10-04 07:36:58 +02001420 if (len && trash[len-1] == '\r')
1421 len--;
1422
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001423 trash[len] = '\0';
1424
Willy Tarreaubc4af052011-02-13 13:25:14 +01001425 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001426 if (len) {
1427 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001428 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001429 continue;
1430 }
1431 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001432 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001433 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001434 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001435 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001436 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001437 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001438 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001439 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001440 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001441 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001442 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001443 /* if prompt is disabled, print help on empty lines,
1444 * so that the user at least knows how to enable
1445 * prompt and find help.
1446 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001447 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001448 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001449 }
1450
1451 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001452 bo_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001453 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001454 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001455 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001456 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001457 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001458 continue;
1459 }
1460
Willy Tarreaubc4af052011-02-13 13:25:14 +01001461 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001462 case STAT_CLI_PRINT:
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001463 if (bi_putstr(si->ib, si->applet.ctx.cli.msg) != -1)
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_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001467 if (stats_dump_raw_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_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001471 if (stats_dump_sess_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 Tarreauf5a885f2009-10-04 14:22:18 +02001474 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001475 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001476 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001477 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001478 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001479 if (stats_table_request(si, true))
1480 si->applet.st0 = STAT_CLI_PROMPT;
1481 break;
1482 case STAT_CLI_O_CLR:
1483 if (stats_table_request(si, false))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001484 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001485 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001486 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001487 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001488 break;
1489 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001490
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001491 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001492 if (si->applet.st0 == STAT_CLI_PROMPT) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001493 if (bi_putstr(si->ib, si->applet.st1 ? "\n> " : "\n") != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001494 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001495 }
1496
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001497 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001498 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001499 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001500
1501 /* Now we close the output if one of the writers did so,
1502 * or if we're not in interactive mode and the request
1503 * buffer is empty. This still allows pipelined requests
1504 * to be sent in non-interactive mode.
1505 */
Willy Tarreau2e046c62012-03-01 16:08:30 +01001506 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->applet.st1 && !req->o)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001507 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001508 continue;
1509 }
1510
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001511 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001512 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001513 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001514 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001515
Willy Tarreaubc4af052011-02-13 13:25:14 +01001516 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001517 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1518 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001519 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001520 * and nothing more to consume. This is comparable to a broken pipe, so
1521 * we forward the close to the request side so that it flows upstream to
1522 * the client.
1523 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001524 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001525 }
1526
Willy Tarreaubc4af052011-02-13 13:25:14 +01001527 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001528 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1529 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1530 /* We have no more processing to do, and nothing more to send, and
1531 * the client side has closed. So we'll forward this state downstream
1532 * on the response buffer.
1533 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001534 si_shutr(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001535 res->flags |= BF_READ_NULL;
1536 }
1537
1538 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001539 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001540
1541 /* we don't want to expire timeouts while we're processing requests */
1542 si->ib->rex = TICK_ETERNITY;
1543 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001544
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001545 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001546 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 +02001547 __FUNCTION__, __LINE__,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001548 si->state, req->flags, res->flags, req->i, req->o, res->i, res->o);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001549
1550 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1551 /* check that we have released everything then unregister */
1552 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001553 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001554}
1555
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001556/* This function dumps statistics onto the stream interface's read buffer.
1557 * The data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001558 * It returns 0 as long as it does not complete, non-zero upon completion.
1559 * Some states are not used but it makes the code more similar to other
1560 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001561 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001562static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001563{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001564 struct proxy *px;
1565 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001566 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001567
David du Colombier7af46052012-05-16 14:16:48 +02001568 chunk_init(&msg, trash, trashlen);
Willy Tarreau3e76e722007-10-17 18:57:38 +02001569
Willy Tarreau94981132012-05-21 17:09:48 +02001570 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001571 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001572 /* the function had not been called yet */
Willy Tarreau94981132012-05-21 17:09:48 +02001573 si->conn.data_st = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001574 /* fall through */
1575
Willy Tarreau295a8372011-03-10 11:25:07 +01001576 case STAT_ST_HEAD:
1577 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001578 print_csv_header(&msg);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001579 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001580 return 0;
1581 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001582
Willy Tarreau94981132012-05-21 17:09:48 +02001583 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001584 /* fall through */
1585
Willy Tarreau295a8372011-03-10 11:25:07 +01001586 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001587 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001588 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001589 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001590 "Name: " PRODUCT_NAME "\n"
1591 "Version: " HAPROXY_VERSION "\n"
1592 "Release_date: " HAPROXY_DATE "\n"
1593 "Nbproc: %d\n"
1594 "Process_num: %d\n"
1595 "Pid: %d\n"
1596 "Uptime: %dd %dh%02dm%02ds\n"
1597 "Uptime_sec: %d\n"
1598 "Memmax_MB: %d\n"
1599 "Ulimit-n: %d\n"
1600 "Maxsock: %d\n"
1601 "Maxconn: %d\n"
Willy Tarreau91886b62011-09-07 14:38:31 +02001602 "Hard_maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001603 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001604 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001605 "PipesUsed: %d\n"
1606 "PipesFree: %d\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001607 "ConnRate: %d\n"
1608 "ConnRateLimit: %d\n"
1609 "MaxConnRate: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001610 "Tasks: %d\n"
1611 "Run_queue: %d\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02001612 "Idle_pct: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001613 "node: %s\n"
1614 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001615 "",
1616 global.nbproc,
1617 relative_pid,
1618 pid,
1619 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1620 up,
1621 global.rlimit_memmax,
1622 global.rlimit_nofile,
Willy Tarreau91886b62011-09-07 14:38:31 +02001623 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001624 actconn, pipes_used, pipes_free,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001625 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau45a12512011-09-10 16:56:42 +02001626 nb_tasks_cur, run_queue_cur, idle_pct,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001627 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001628 );
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001629 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001630 return 0;
1631 }
1632
Willy Tarreau295a8372011-03-10 11:25:07 +01001633 si->applet.ctx.stats.px = proxy;
1634 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1635 si->applet.ctx.stats.sv = NULL;
Willy Tarreau94981132012-05-21 17:09:48 +02001636 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001637 /* fall through */
1638
Willy Tarreau295a8372011-03-10 11:25:07 +01001639 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001640 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001641 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1642 while (si->applet.ctx.stats.px) {
1643 px = si->applet.ctx.stats.px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001644 /* skip the disabled proxies and non-networked ones */
1645 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001646 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001647 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001648 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001649 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001650
Willy Tarreau295a8372011-03-10 11:25:07 +01001651 si->applet.ctx.stats.px = px->next;
1652 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001653 }
1654 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001655 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001656
Willy Tarreau94981132012-05-21 17:09:48 +02001657 si->conn.data_st = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001658 /* fall through */
1659
Willy Tarreau295a8372011-03-10 11:25:07 +01001660 case STAT_ST_END:
Willy Tarreau94981132012-05-21 17:09:48 +02001661 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001662 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001663
Willy Tarreau295a8372011-03-10 11:25:07 +01001664 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001665 return 1;
1666
1667 default:
1668 /* unknown state ! */
Willy Tarreau94981132012-05-21 17:09:48 +02001669 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001670 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001671 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001672}
1673
1674
Cyril Bonté70be45d2010-10-12 00:14:35 +02001675/* We don't want to land on the posted stats page because a refresh will
1676 * repost the data. We don't want this to happen on accident so we redirect
1677 * the browse to the stats page with a GET.
1678 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001679static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001680{
Willy Tarreau94981132012-05-21 17:09:48 +02001681 struct session *s = si->conn.data_ctx;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001682 struct chunk msg;
1683
David du Colombier7af46052012-05-16 14:16:48 +02001684 chunk_init(&msg, trash, trashlen);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001685
Willy Tarreau94981132012-05-21 17:09:48 +02001686 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001687 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001688 chunk_printf(&msg,
1689 "HTTP/1.0 303 See Other\r\n"
1690 "Cache-Control: no-cache\r\n"
1691 "Content-Type: text/plain\r\n"
1692 "Connection: close\r\n"
1693 "Location: %s;st=%s",
Cyril Bonté19979e12012-04-04 12:57:21 +02001694 uri->uri_prefix,
1695 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
1696 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
1697 stat_status_codes[si->applet.ctx.stats.st_code]) ?
1698 stat_status_codes[si->applet.ctx.stats.st_code] :
1699 stat_status_codes[STAT_STATUS_UNKN]);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001700 chunk_printf(&msg, "\r\n\r\n");
1701
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001702 if (bi_putchk(si->ib, &msg) == -1)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001703 return 0;
1704
1705 s->txn.status = 303;
1706
1707 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1708 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1709 if (!(s->flags & SN_FINST_MASK))
1710 s->flags |= SN_FINST_R;
1711
Willy Tarreau94981132012-05-21 17:09:48 +02001712 si->conn.data_st = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001713 return 1;
1714 }
1715 return 1;
1716}
1717
1718
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001719/* This I/O handler runs as an applet embedded in a stream interface. It is
1720 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001721 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001722 * automatically unregisters itself once transfer is complete.
1723 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001724static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001725{
Willy Tarreau94981132012-05-21 17:09:48 +02001726 struct session *s = si->conn.data_ctx;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001727 struct buffer *req = si->ob;
1728 struct buffer *res = si->ib;
1729
1730 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1731 goto out;
1732
1733 /* check that the output is not closed */
1734 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001735 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001736
Willy Tarreaubc4af052011-02-13 13:25:14 +01001737 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001738 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001739 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001740 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001741 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001742 }
1743 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001744 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001745 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001746 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001747 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001748 }
1749 }
1750
1751 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
Willy Tarreau73b013b2012-05-21 16:31:45 +02001752 si_shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001753
Willy Tarreaubc4af052011-02-13 13:25:14 +01001754 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001755 si_shutr(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001756 res->flags |= BF_READ_NULL;
1757 }
1758
1759 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001760 si_update(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001761
1762 /* we don't want to expire timeouts while we're processing requests */
1763 si->ib->rex = TICK_ETERNITY;
1764 si->ob->wex = TICK_ETERNITY;
1765
1766 out:
1767 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1768 /* check that we have released everything then unregister */
1769 stream_int_unregister_handler(si);
1770 }
1771}
1772
1773
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001774/* This function dumps statistics in HTTP format onto the stream interface's
1775 * read buffer. The data_ctx must have been zeroed first, and the flags
1776 * properly set. It returns 0 if it had to stop writing data and an I/O is
1777 * needed, 1 if the dump is finished and the session must be closed, or -1
1778 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001779 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001780static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001781{
Willy Tarreau94981132012-05-21 17:09:48 +02001782 struct session *s = si->conn.data_ctx;
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001783 struct buffer *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001784 struct proxy *px;
1785 struct chunk msg;
1786 unsigned int up;
1787
David du Colombier7af46052012-05-16 14:16:48 +02001788 chunk_init(&msg, trash, trashlen);
Willy Tarreau91861262007-10-17 17:06:05 +02001789
Willy Tarreau94981132012-05-21 17:09:48 +02001790 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001791 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001792 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001793 "HTTP/1.0 200 OK\r\n"
1794 "Cache-Control: no-cache\r\n"
1795 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001796 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001797 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001798
Willy Tarreau295a8372011-03-10 11:25:07 +01001799 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001800 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001801 uri->refresh);
1802
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001803 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001804
1805 s->txn.status = 200;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001806 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001807 return 0;
1808
Willy Tarreau91861262007-10-17 17:06:05 +02001809 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1810 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1811 if (!(s->flags & SN_FINST_MASK))
1812 s->flags |= SN_FINST_R;
1813
1814 if (s->txn.meth == HTTP_METH_HEAD) {
1815 /* that's all we return in case of HEAD request */
Willy Tarreau94981132012-05-21 17:09:48 +02001816 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001817 return 1;
1818 }
1819
Willy Tarreau94981132012-05-21 17:09:48 +02001820 si->conn.data_st = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001821 /* fall through */
1822
Willy Tarreau295a8372011-03-10 11:25:07 +01001823 case STAT_ST_HEAD:
1824 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001825 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001826 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001827 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1828 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001829 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001830 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1831 "<style type=\"text/css\"><!--\n"
1832 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001833 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001834 " font-size: 12px;"
1835 " font-weight: normal;"
1836 " color: black;"
1837 " background: white;"
1838 "}\n"
1839 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001840 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001841 "}\n"
1842 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001843 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001844 " margin-bottom: 0.5em;"
1845 "}\n"
1846 "h2 {"
1847 " font-family: helvetica, arial;"
1848 " font-size: x-large;"
1849 " font-weight: bold;"
1850 " font-style: italic;"
1851 " color: #6020a0;"
1852 " margin-top: 0em;"
1853 " margin-bottom: 0em;"
1854 "}\n"
1855 "h3 {"
1856 " font-family: helvetica, arial;"
1857 " font-size: 16px;"
1858 " font-weight: bold;"
1859 " color: #b00040;"
1860 " background: #e8e8d0;"
1861 " margin-top: 0em;"
1862 " margin-bottom: 0em;"
1863 "}\n"
1864 "li {"
1865 " margin-top: 0.25em;"
1866 " margin-right: 2em;"
1867 "}\n"
1868 ".hr {margin-top: 0.25em;"
1869 " border-color: black;"
1870 " border-bottom-style: solid;"
1871 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001872 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001873 ".total {background: #20D0D0;color: #ffff80;}\n"
1874 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001875 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001876 ".backend {background: #e8e8d0;}\n"
1877 ".active0 {background: #ff9090;}\n"
1878 ".active1 {background: #ffd020;}\n"
1879 ".active2 {background: #ffffa0;}\n"
1880 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001881 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1882 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1883 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001884 ".backup0 {background: #ff9090;}\n"
1885 ".backup1 {background: #ff80ff;}\n"
1886 ".backup2 {background: #c060ff;}\n"
1887 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001888 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1889 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1890 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001891 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001892 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001893 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001894 "a.px:link {color: #ffff40; text-decoration: none;}"
1895 "a.px:visited {color: #ffff40; text-decoration: none;}"
1896 "a.px:hover {color: #ffffff; text-decoration: none;}"
1897 "a.lfsb:link {color: #000000; text-decoration: none;}"
1898 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1899 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1900 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001901 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001902 "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"
1903 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001904 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001905 "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 +02001906 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001907 "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 +02001908 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001909 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1910 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1911 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001912 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001913 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001914 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001915 (uri->flags&ST_SHNODE) ? " on " : "",
1916 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001917 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001918 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001919 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001920 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001921 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02001922 return 0;
1923
Willy Tarreau94981132012-05-21 17:09:48 +02001924 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02001925 /* fall through */
1926
Willy Tarreau295a8372011-03-10 11:25:07 +01001927 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02001928 up = (now.tv_sec - start_date.tv_sec);
1929
1930 /* WARNING! this has to fit the first packet too.
1931 * We are around 3.5 kB, add adding entries will
1932 * become tricky if we want to support 4kB buffers !
1933 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001934 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001935 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001936 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1937 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001938 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001939 "<hr width=\"100%%\" class=\"hr\">\n"
1940 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001941 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001942 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001943 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001944 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1945 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001946 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02001947 "Running tasks: %d/%d; idle = %d %%<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001948 "</td><td align=\"center\" nowrap>\n"
1949 "<table class=\"lgd\"><tr>\n"
1950 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1951 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1952 "</tr><tr>\n"
1953 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1954 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1955 "</tr><tr>\n"
1956 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1957 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1958 "</tr><tr>\n"
1959 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001960 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001961 "</tr><tr>\n"
1962 "<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 +02001963 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001964 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001965 "</td>"
1966 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1967 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1968 "",
1969 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001970 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1971 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1972 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001973 up / 86400, (up % 86400) / 3600,
1974 (up % 3600) / 60, (up % 60),
1975 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1976 global.rlimit_memmax ? " MB" : "",
1977 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001978 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001979 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau45a12512011-09-10 16:56:42 +02001980 run_queue_cur, nb_tasks_cur, idle_pct
Willy Tarreau91861262007-10-17 17:06:05 +02001981 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001982
Willy Tarreau295a8372011-03-10 11:25:07 +01001983 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001984 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001985 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1986 uri->uri_prefix,
1987 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01001988 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001989 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001990 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001991 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1992 uri->uri_prefix,
1993 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01001994 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001995
Willy Tarreau55bb8452007-10-17 18:44:57 +02001996 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001997 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001998 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001999 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
2000 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002001 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002002 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002003 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002004 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002005 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
2006 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002007 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002008 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002009 }
Willy Tarreau91861262007-10-17 17:06:05 +02002010
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002011 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002012 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
2013 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002014 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2015 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002016
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002017 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002018 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
2019 uri->uri_prefix,
2020 (uri->refresh > 0) ? ";norefresh" : "");
2021
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002022 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002023 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002024 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
Adrian Bridgettafdb6e52012-03-19 23:36:42 +00002025 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002026 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2027 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2028 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2029 "</ul>"
2030 "</td>"
2031 "</tr></table>\n"
2032 ""
2033 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002034
Willy Tarreau295a8372011-03-10 11:25:07 +01002035 if (si->applet.ctx.stats.st_code) {
Cyril Bonté19979e12012-04-04 12:57:21 +02002036 switch (si->applet.ctx.stats.st_code) {
2037 case STAT_STATUS_DONE:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002038 chunk_printf(&msg,
2039 "<p><div class=active3>"
2040 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2041 "Action processed successfully."
2042 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002043 break;
2044 case STAT_STATUS_NONE:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002045 chunk_printf(&msg,
2046 "<p><div class=active2>"
2047 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2048 "Nothing has changed."
2049 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002050 break;
2051 case STAT_STATUS_PART:
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002052 chunk_printf(&msg,
2053 "<p><div class=active2>"
2054 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2055 "Action partially processed.<br>"
2056 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2057 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002058 break;
2059 case STAT_STATUS_ERRP:
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002060 chunk_printf(&msg,
2061 "<p><div class=active0>"
2062 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2063 "Action not processed because of invalid parameters."
2064 "<ul>"
2065 "<li>The action is maybe unknown.</li>"
2066 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2067 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2068 "</ul>"
2069 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002070 break;
2071 case STAT_STATUS_EXCD:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002072 chunk_printf(&msg,
2073 "<p><div class=active0>"
2074 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2075 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2076 "You should retry with less servers at a time.</b>"
2077 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002078 break;
2079 case STAT_STATUS_DENY:
Cyril Bonté474be412010-10-12 00:14:36 +02002080 chunk_printf(&msg,
2081 "<p><div class=active0>"
2082 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2083 "<b>Action denied.</b>"
2084 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002085 break;
2086 default:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002087 chunk_printf(&msg,
2088 "<p><div class=active6>"
2089 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2090 "Unexpected result."
2091 "</div>\n", uri->uri_prefix);
2092 }
2093 chunk_printf(&msg,"<p>\n");
2094 }
2095
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002096 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002097 return 0;
2098 }
Willy Tarreau91861262007-10-17 17:06:05 +02002099
Willy Tarreau295a8372011-03-10 11:25:07 +01002100 si->applet.ctx.stats.px = proxy;
2101 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau94981132012-05-21 17:09:48 +02002102 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02002103 /* fall through */
2104
Willy Tarreau295a8372011-03-10 11:25:07 +01002105 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02002106 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01002107 while (si->applet.ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002108 if (buffer_almost_full(rep))
2109 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002110 px = si->applet.ctx.stats.px;
Willy Tarreau91861262007-10-17 17:06:05 +02002111 /* skip the disabled proxies and non-networked ones */
2112 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002113 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002114 return 0;
2115
Willy Tarreau295a8372011-03-10 11:25:07 +01002116 si->applet.ctx.stats.px = px->next;
2117 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02002118 }
2119 /* here, we just have reached the last proxy */
2120
Willy Tarreau94981132012-05-21 17:09:48 +02002121 si->conn.data_st = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002122 /* fall through */
2123
Willy Tarreau295a8372011-03-10 11:25:07 +01002124 case STAT_ST_END:
2125 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002126 chunk_printf(&msg, "</body></html>\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002127 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002128 return 0;
2129 }
Willy Tarreau91861262007-10-17 17:06:05 +02002130
Willy Tarreau94981132012-05-21 17:09:48 +02002131 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002132 /* fall through */
2133
Willy Tarreau295a8372011-03-10 11:25:07 +01002134 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002135 return 1;
2136
2137 default:
2138 /* unknown state ! */
Willy Tarreau94981132012-05-21 17:09:48 +02002139 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002140 return -1;
2141 }
2142}
2143
2144
2145/*
2146 * Dumps statistics for a proxy.
2147 * Returns 0 if it had to stop dumping data because of lack of buffer space,
2148 * ot non-zero if everything completed.
2149 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002150static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002151{
Willy Tarreau94981132012-05-21 17:09:48 +02002152 struct session *s = si->conn.data_ctx;
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002153 struct buffer *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002154 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002155 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002156 struct chunk msg;
2157
David du Colombier7af46052012-05-16 14:16:48 +02002158 chunk_init(&msg, trash, trashlen);
Willy Tarreau91861262007-10-17 17:06:05 +02002159
Willy Tarreau295a8372011-03-10 11:25:07 +01002160 switch (si->applet.ctx.stats.px_st) {
2161 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002162 /* we are on a new proxy */
2163
2164 if (uri && uri->scope) {
2165 /* we have a limited scope, we have to check the proxy name */
2166 struct stat_scope *scope;
2167 int len;
2168
2169 len = strlen(px->id);
2170 scope = uri->scope;
2171
2172 while (scope) {
2173 /* match exact proxy name */
2174 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2175 break;
2176
2177 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002178 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002179 break;
2180 scope = scope->next;
2181 }
2182
2183 /* proxy name not found : don't dump anything */
2184 if (scope == NULL)
2185 return 1;
2186 }
2187
Willy Tarreau295a8372011-03-10 11:25:07 +01002188 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
2189 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002190 return 1;
2191
Willy Tarreau295a8372011-03-10 11:25:07 +01002192 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002193 /* fall through */
2194
Willy Tarreau295a8372011-03-10 11:25:07 +01002195 case STAT_PX_ST_TH:
2196 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
2197 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002198 /* A form to enable/disable this proxy servers */
2199 chunk_printf(&msg,
2200 "<form action=\"%s\" method=\"post\">",
2201 uri->uri_prefix);
2202 }
2203
Willy Tarreau55bb8452007-10-17 18:44:57 +02002204 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002205 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002206 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002207 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002208 "<th class=\"pxname\" width=\"10%%\"");
2209
2210 if (uri->flags&ST_SHLGNDS) {
2211 /* cap, mode, id */
2212 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
2213 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2214 px->uuid);
2215
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002216 chunk_printf(&msg, "\"");
2217 }
2218
2219 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002220 ">%s<a name=\"%s\"></a>"
2221 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002222 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002223 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002224 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002225 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002226 "<tr class=\"titre\">",
2227 (uri->flags & ST_SHLGNDS)?"<u>":"",
2228 px->id, px->id, px->id,
2229 (uri->flags & ST_SHLGNDS)?"</u>":"",
2230 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2231
Willy Tarreau295a8372011-03-10 11:25:07 +01002232 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002233 /* Column heading for Enable or Disable server */
2234 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
2235 }
2236
2237 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002238 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002239 "<th colspan=3>Queue</th>"
2240 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002241 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002242 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002243 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002244 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002245 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002246 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002247 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002248 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002249 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002250 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002251 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002252 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2253 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002254 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002255
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002256 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002257 return 0;
2258 }
Willy Tarreau91861262007-10-17 17:06:05 +02002259
Willy Tarreau295a8372011-03-10 11:25:07 +01002260 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002261 /* fall through */
2262
Willy Tarreau295a8372011-03-10 11:25:07 +01002263 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002264 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002265 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002266 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
2267 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002268 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002269 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002270 "<tr class=\"frontend\">");
2271
Willy Tarreau295a8372011-03-10 11:25:07 +01002272 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002273 /* Column sub-heading for Enable or Disable server */
2274 chunk_printf(&msg, "<td></td>");
2275 }
2276
2277 chunk_printf(&msg,
2278 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002279 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002280 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2281 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002282 "",
2283 px->id, px->id);
2284
2285 if (px->mode == PR_MODE_HTTP) {
2286 chunk_printf(&msg,
2287 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01002288 "<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 +01002289 "",
2290 read_freq_ctr(&px->fe_req_per_sec),
2291 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002292 px->fe_counters.p.http.rps_max,
2293 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01002294 LIM2A2(px->fe_sps_lim, "-"));
2295 } else {
2296 chunk_printf(&msg,
2297 /* sessions rate : current, max, limit */
2298 "<td>%s</td><td>%s</td><td>%s</td>"
2299 "",
2300 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002301 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01002302 }
2303
2304 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002305 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002306 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002307 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002308 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002309 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002310
2311 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2312 if (px->mode == PR_MODE_HTTP) {
2313 int i;
2314
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002315 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002316
2317 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002318 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002319
Willy Tarreaueabea072011-09-10 23:29:44 +02002320 chunk_printf(&msg, " other=%lld,", px->fe_counters.p.http.rsp[0]);
2321 chunk_printf(&msg, " intercepted=%lld\"", px->fe_counters.intercepted_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002322 }
2323
2324 chunk_printf(&msg,
2325 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002326 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002327 /* bytes : in, out */
2328 "<td>%s</td><td>%s</td>"
2329 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002330 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002331 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002332 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002333 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002334
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002335 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002336 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002337 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002338 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002339 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002340 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002341 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002342 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002343 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002344 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002345 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002346 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002347 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2348 U2H2(px->fe_counters.failed_req),
Willy Tarreau562515c2011-07-25 08:11:52 +02002349 px->state == PR_STREADY ? "OPEN" :
2350 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002351 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002352 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002353 /* pxid, name, queue cur, queue max, */
2354 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002355 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002356 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002357 /* bytes : in, out */
2358 "%lld,%lld,"
2359 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002360 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002361 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002362 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002363 /* warnings: retries, redispatches */
2364 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002365 /* server status : reflect frontend status */
2366 "%s,"
2367 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002368 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002369 /* pid, iid, sid, throttle, lbtot, tracked, type */
2370 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002371 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002372 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002373 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002374 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002375 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002376 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2377 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2378 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2379 px->fe_counters.failed_req,
Willy Tarreau562515c2011-07-25 08:11:52 +02002380 px->state == PR_STREADY ? "OPEN" :
2381 px->state == PR_STFULL ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002382 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002383 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002384 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002385
2386 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2387 if (px->mode == PR_MODE_HTTP) {
2388 int i;
2389
2390 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002391 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002392
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002393 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002394 } else {
2395 chunk_printf(&msg, ",,,,,,");
2396 }
2397
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002398 /* failed health analyses */
2399 chunk_printf(&msg, ",");
2400
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002401 /* requests : req_rate, req_rate_max, req_tot, */
2402 chunk_printf(&msg, "%u,%u,%lld,",
2403 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002404 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002405
Willy Tarreauae526782010-03-04 20:34:23 +01002406 /* errors: cli_aborts, srv_aborts */
2407 chunk_printf(&msg, ",,");
2408
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002409 /* finish with EOL */
2410 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002411 }
Willy Tarreau91861262007-10-17 17:06:05 +02002412
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002413 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002414 return 0;
2415 }
2416
Willy Tarreau295a8372011-03-10 11:25:07 +01002417 si->applet.ctx.stats.l = px->listen; /* may be NULL */
2418 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002419 /* fall through */
2420
Willy Tarreau295a8372011-03-10 11:25:07 +01002421 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002422 /* stats.l has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002423 for (; si->applet.ctx.stats.l != NULL; si->applet.ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002424 if (buffer_almost_full(rep))
2425 return 0;
2426
Willy Tarreau295a8372011-03-10 11:25:07 +01002427 l = si->applet.ctx.stats.l;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002428 if (!l->counters)
2429 continue;
2430
Willy Tarreau295a8372011-03-10 11:25:07 +01002431 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2432 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002433 break;
2434
Willy Tarreau295a8372011-03-10 11:25:07 +01002435 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002436 continue;
2437 }
2438
Willy Tarreau295a8372011-03-10 11:25:07 +01002439 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002440 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002441 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002442 /* Column sub-heading for Enable or Disable server */
2443 chunk_printf(&msg, "<td></td>");
2444 }
2445 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002446
2447 if (uri->flags&ST_SHLGNDS) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02002448 char str[INET6_ADDRSTRLEN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002449 int port;
2450
Willy Tarreau631f01c2011-09-05 00:36:48 +02002451 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002452
Willy Tarreau631f01c2011-09-05 00:36:48 +02002453 port = get_host_port(&l->addr);
2454 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2455 case AF_INET:
2456 chunk_printf(&msg, "IPv4: %s:%d, ", str, port);
2457 break;
2458 case AF_INET6:
2459 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, port);
2460 break;
2461 case AF_UNIX:
2462 chunk_printf(&msg, "unix, ");
2463 break;
2464 case -1:
2465 chunk_printf(&msg, "(%s), ", strerror(errno));
2466 break;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002467 }
2468
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002469 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002470 chunk_printf(&msg, "id: %d\"", l->luid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002471 }
2472
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002473 chunk_printf(&msg,
2474 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002475 ">%s<a name=\"%s/+%s\"></a>"
2476 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002477 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002478 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002479 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002480 "<td>%s</td><td>%s</td><td>%s</td>"
2481 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002482 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002483 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002484 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002485 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002486 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002487 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002488 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2489 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2490
2491 chunk_printf(&msg,
2492 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002493 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002494 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002495 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002496 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002497 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002498 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002499 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002500 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002501 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002502 "",
2503 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2504 U2H2(l->counters->failed_req),
Willy Tarreaua17c2d92011-07-25 08:16:20 +02002505 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002506 } else {
2507 chunk_printf(&msg,
2508 /* pxid, name, queue cur, queue max, */
2509 "%s,%s,,,"
2510 /* sessions: current, max, limit, total */
2511 "%d,%d,%d,%lld,"
2512 /* bytes: in, out */
2513 "%lld,%lld,"
2514 /* denied: req, resp */
2515 "%lld,%lld,"
2516 /* errors: request, connect, response */
2517 "%lld,,,"
2518 /* warnings: retries, redispatches */
2519 ",,"
2520 /* server status: reflect listener status */
2521 "%s,"
2522 /* rest of server: nothing */
2523 ",,,,,,,,"
2524 /* pid, iid, sid, throttle, lbtot, tracked, type */
2525 "%d,%d,%d,,,,%d,"
2526 /* rate, rate_lim, rate_max */
2527 ",,,"
2528 /* check_status, check_code, check_duration */
2529 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002530 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2531 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002532 /* failed health analyses */
2533 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002534 /* requests : req_rate, req_rate_max, req_tot, */
2535 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002536 /* errors: cli_aborts, srv_aborts */
2537 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002538 "\n",
2539 px->id, l->name,
2540 l->nbconn, l->counters->conn_max,
2541 l->maxconn, l->counters->cum_conn,
2542 l->counters->bytes_in, l->counters->bytes_out,
2543 l->counters->denied_req, l->counters->denied_resp,
2544 l->counters->failed_req,
2545 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2546 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2547 }
2548
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002549 if (bi_putchk(rep, &msg) == -1)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002550 return 0;
2551 }
2552
Willy Tarreau295a8372011-03-10 11:25:07 +01002553 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2554 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002555 /* fall through */
2556
Willy Tarreau295a8372011-03-10 11:25:07 +01002557 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002558 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002559 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002560 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 +02002561
Willy Tarreau4e33d862009-10-11 23:35:10 +02002562 if (buffer_almost_full(rep))
2563 return 0;
2564
Willy Tarreau295a8372011-03-10 11:25:07 +01002565 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002566
Willy Tarreau295a8372011-03-10 11:25:07 +01002567 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2568 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002569 break;
2570
Willy Tarreau295a8372011-03-10 11:25:07 +01002571 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002572 continue;
2573 }
2574
Willy Tarreau44267702011-10-28 15:35:33 +02002575 if (sv->track)
2576 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002577 else
2578 svs = sv;
2579
Willy Tarreau91861262007-10-17 17:06:05 +02002580 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002581 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002582 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002583 else if (svs->state & SRV_RUNNING) {
2584 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002585 sv_state = 3; /* UP */
2586 else
2587 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002588
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002589 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002590 sv_state += 2;
2591 }
Willy Tarreau91861262007-10-17 17:06:05 +02002592 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002593 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002594 sv_state = 1; /* going up */
2595 else
2596 sv_state = 0; /* DOWN */
2597
Willy Tarreau295a8372011-03-10 11:25:07 +01002598 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002599 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002600 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002601 continue;
2602 }
2603
Willy Tarreau295a8372011-03-10 11:25:07 +01002604 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002605 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2606 "UP %d/%d &darr;", "UP",
2607 "NOLB %d/%d &darr;", "NOLB",
2608 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002609 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002610 chunk_printf(&msg,
2611 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002612 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002613 );
2614 }
2615 else {
2616 chunk_printf(&msg,
2617 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002618 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002619 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2620 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002621
Willy Tarreau295a8372011-03-10 11:25:07 +01002622 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002623 chunk_printf(&msg,
2624 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2625 sv->id);
2626 }
2627
2628 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002629
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002630 if (uri->flags&ST_SHLGNDS) {
2631 char str[INET6_ADDRSTRLEN];
2632
Willy Tarreau631f01c2011-09-05 00:36:48 +02002633 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002634
Willy Tarreau631f01c2011-09-05 00:36:48 +02002635 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01002636 case AF_INET:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002637 chunk_printf(&msg, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
David du Colombier6f5ccb12011-03-10 22:26:24 +01002638 break;
2639 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002640 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2641 break;
2642 case AF_UNIX:
2643 chunk_printf(&msg, "unix, ");
2644 break;
2645 case -1:
2646 chunk_printf(&msg, "(%s), ", strerror(errno));
2647 break;
2648 default: /* address family not supported */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002649 break;
2650 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002651
2652 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002653 chunk_printf(&msg, "id: %d", sv->puid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002654
2655 /* cookie */
2656 if (sv->cookie) {
2657 struct chunk src;
2658
2659 chunk_printf(&msg, ", cookie: '");
2660
2661 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2662 chunk_htmlencode(&msg, &src);
2663
2664 chunk_printf(&msg, "'");
2665 }
2666
2667 chunk_printf(&msg, "\"");
2668 }
2669
2670 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002671 ">%s<a name=\"%s/%s\"></a>"
2672 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002673 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002674 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002675 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002676 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002677 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002678 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002679 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002680 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002681 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002682 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002683 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002684 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2685 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002686 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2687
2688 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2689 if (px->mode == PR_MODE_HTTP) {
2690 int i;
2691
2692 chunk_printf(&msg, " title=\"rsp codes:");
2693
2694 for (i = 1; i < 6; i++)
2695 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2696
2697 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2698 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002699
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002700 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002701 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002702 ">%s%s%s</td><td>%s</td>",
2703 (px->mode == PR_MODE_HTTP)?"<u>":"",
2704 U2H0(sv->counters.cum_sess),
2705 (px->mode == PR_MODE_HTTP)?"</u>":"",
2706 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002707
2708 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002709 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002710 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002711 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002712 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002713 /* errors : request, connect */
2714 "<td></td><td>%s</td>"
2715 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002716 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002717 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002718 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002719 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002720 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2721 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002722 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002723 sv->counters.cli_aborts,
2724 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002725 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002726 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002727
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002728 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002729 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002730
Cyril Bontécd19e512010-01-31 22:34:03 +01002731 if (sv->state & SRV_MAINTAIN) {
2732 chunk_printf(&msg, "%s ",
2733 human_time(now.tv_sec - sv->last_change, 1));
2734 chunk_printf(&msg, "MAINT");
2735 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002736 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2737 chunk_printf(&msg, "%s ",
2738 human_time(now.tv_sec - svs->last_change, 1));
2739 chunk_printf(&msg, "MAINT(via)");
2740 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002741 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002742 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002743 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002744
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002745 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002746 srv_hlt_st[sv_state],
2747 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2748 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002749 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002750
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002751 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002752 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002753 get_check_status_description(sv->check_status));
2754
2755 if (*sv->check_desc) {
2756 struct chunk src;
2757
2758 chunk_printf(&msg, ": ");
2759
2760 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2761 chunk_htmlencode(&msg, &src);
2762 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002763
Willy Tarreaue0454092010-02-26 12:29:07 +01002764 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002765 tv_iszero(&sv->check_start)?"":"* ",
2766 get_check_status_info(sv->check_status));
2767
2768 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002769 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002770
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002771 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002772 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002773 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002774 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002775
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002776 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002777 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002778 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002779 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002780 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002781 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002782 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002783 (sv->state & SRV_BACKUP) ? "-" : "Y",
2784 (sv->state & SRV_BACKUP) ? "Y" : "-");
2785
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002786 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002787 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002788 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002789 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2790
2791 if (svs->observe)
2792 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2793
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002794 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002795 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002796 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002797 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002798 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002799 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002800 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002801 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2802 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002803 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002804 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002805 "<td colspan=3></td>");
2806
2807 /* throttle */
2808 if ((sv->state & SRV_WARMINGUP) &&
2809 now.tv_sec < sv->last_change + sv->slowstart &&
2810 now.tv_sec >= sv->last_change) {
2811 unsigned int ratio;
2812 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002813 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002814 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002815 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002816 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002817 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002818 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002819 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002820 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2821 "UP %d/%d,", "UP,",
2822 "NOLB %d/%d,", "NOLB,",
2823 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002824 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002825 /* pxid, name */
2826 "%s,%s,"
2827 /* queue : current, max */
2828 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002829 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002830 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002831 /* bytes : in, out */
2832 "%lld,%lld,"
2833 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002834 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002835 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002836 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002837 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002838 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002839 "",
2840 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002841 sv->nbpend, sv->counters.nbpend_max,
2842 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002843 sv->counters.bytes_in, sv->counters.bytes_out,
2844 sv->counters.failed_secu,
2845 sv->counters.failed_conns, sv->counters.failed_resp,
2846 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002847
Willy Tarreau55bb8452007-10-17 18:44:57 +02002848 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002849 if (sv->state & SRV_MAINTAIN) {
2850 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002851 }
2852 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2853 chunk_printf(&msg, "MAINT(via),");
2854 }
2855 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002856 chunk_printf(&msg,
2857 srv_hlt_st[sv_state],
2858 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2859 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2860 }
Willy Tarreau91861262007-10-17 17:06:05 +02002861
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002862 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002863 /* weight, active, backup */
2864 "%d,%d,%d,"
2865 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002866 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002867 (sv->state & SRV_BACKUP) ? 0 : 1,
2868 (sv->state & SRV_BACKUP) ? 1 : 0);
2869
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002870 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002871 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002872 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002873 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002874 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002875 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002876 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002877 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002878 ",,,,");
2879
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002880 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002881 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002882 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002883 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002884 LIM2A0(sv->maxqueue, ""),
2885 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002886
2887 /* throttle */
2888 if ((sv->state & SRV_WARMINGUP) &&
2889 now.tv_sec < sv->last_change + sv->slowstart &&
2890 now.tv_sec >= sv->last_change) {
2891 unsigned int ratio;
2892 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002893 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002894 }
2895
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002896 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002897 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002898
2899 /* tracked */
Willy Tarreau44267702011-10-28 15:35:33 +02002900 if (sv->track)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002901 chunk_printf(&msg, "%s/%s,",
Willy Tarreau44267702011-10-28 15:35:33 +02002902 sv->track->proxy->id, sv->track->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002903 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002904 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002905
Willy Tarreau7f062c42009-03-05 18:43:00 +01002906 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002907 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002908
2909 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002910 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002911 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002912 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002913
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002914 if (sv->state & SRV_CHECKED) {
2915 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002916 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002917
2918 /* check_code */
2919 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002920 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002921 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002922 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002923
2924 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002925 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002926 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002927 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002928 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002929
2930 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002931 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002932 }
2933
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002934 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2935 if (px->mode == PR_MODE_HTTP) {
2936 int i;
2937
2938 for (i=1; i<6; i++)
2939 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2940
2941 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2942 } else {
2943 chunk_printf(&msg, ",,,,,,");
2944 }
2945
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002946 /* failed health analyses */
2947 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2948
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002949 /* requests : req_rate, req_rate_max, req_tot, */
2950 chunk_printf(&msg, ",,,");
2951
Willy Tarreauae526782010-03-04 20:34:23 +01002952 /* errors: cli_aborts, srv_aborts */
2953 chunk_printf(&msg, "%lld,%lld,",
2954 sv->counters.cli_aborts, sv->counters.srv_aborts);
2955
Willy Tarreau7f062c42009-03-05 18:43:00 +01002956 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002957 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002958 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002959 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002960 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002961 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002962
Willy Tarreau295a8372011-03-10 11:25:07 +01002963 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02002964 /* fall through */
2965
Willy Tarreau295a8372011-03-10 11:25:07 +01002966 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02002967 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002968 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002969 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2970 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002971 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01002972 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002973 /* Column sub-heading for Enable or Disable server */
2974 chunk_printf(&msg, "<td></td>");
2975 }
2976 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002977
2978 if (uri->flags&ST_SHLGNDS) {
2979 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002980 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002981 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002982
2983 /* cookie */
2984 if (px->cookie_name) {
2985 struct chunk src;
2986
2987 chunk_printf(&msg, ", cookie: '");
2988
2989 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2990 chunk_htmlencode(&msg, &src);
2991
2992 chunk_printf(&msg, "'");
2993 }
2994
2995 chunk_printf(&msg, "\"");
2996
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002997 }
2998
2999 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02003000 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01003001 ">%s<a name=\"%s/Backend\"></a>"
3002 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02003003 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003004 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003005 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003006 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003007 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003008 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02003009 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01003010 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003011 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
3012 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02003013
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003014 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003015 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003016 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003017 "<td"
3018 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003019 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003020
3021 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3022 if (px->mode == PR_MODE_HTTP) {
3023 int i;
3024
3025 chunk_printf(&msg, " title=\"rsp codes:");
3026
3027 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003028 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003029
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003030 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003031 }
3032
3033 chunk_printf(&msg,
3034 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01003035 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003036 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003037 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003038 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003039 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003040 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01003041 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003042 U2H7(px->be_counters.cum_lbconn),
3043 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003044
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003045 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02003046 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003047 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01003048 /* errors : request, connect */
3049 "<td></td><td>%s</td>"
3050 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01003051 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003052 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003053 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003054 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02003055 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003056 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02003057 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003058 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3059 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003060 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003061 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
3062 U2H2(px->be_counters.failed_conns),
3063 px->be_counters.cli_aborts,
3064 px->be_counters.srv_aborts,
3065 U2H5(px->be_counters.failed_resp),
3066 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003067 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01003068 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3069 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01003070 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003071 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003072
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003073 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003074 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003075 "<td class=ac>&nbsp;</td><td>%d</td>"
3076 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003077 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003078 "</tr>",
3079 px->down_trans,
3080 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003081 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003082 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02003083 /* pxid, name */
3084 "%s,BACKEND,"
3085 /* queue : current, max */
3086 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003087 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003088 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003089 /* bytes : in, out */
3090 "%lld,%lld,"
3091 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003092 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003093 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003094 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003095 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003096 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003097 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02003098 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003099 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02003100 * active and backups. */
3101 "%s,"
3102 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003103 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003104 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003105 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003106 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003107 /* rate, rate_lim, rate_max, */
3108 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003109 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003110 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02003111 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003112 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3113 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3114 px->be_counters.bytes_in, px->be_counters.bytes_out,
3115 px->be_counters.denied_req, px->be_counters.denied_resp,
3116 px->be_counters.failed_conns, px->be_counters.failed_resp,
3117 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01003118 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01003119 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003120 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003121 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01003122 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003123 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003124 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003125 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003126 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003127
3128 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3129 if (px->mode == PR_MODE_HTTP) {
3130 int i;
3131
3132 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003133 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003134
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003135 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003136 } else {
3137 chunk_printf(&msg, ",,,,,,");
3138 }
3139
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003140 /* failed health analyses */
3141 chunk_printf(&msg, ",");
3142
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003143 /* requests : req_rate, req_rate_max, req_tot, */
3144 chunk_printf(&msg, ",,,");
3145
Willy Tarreauae526782010-03-04 20:34:23 +01003146 /* errors: cli_aborts, srv_aborts */
3147 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003148 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01003149
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003150 /* finish with EOL */
3151 chunk_printf(&msg, "\n");
3152
Willy Tarreau55bb8452007-10-17 18:44:57 +02003153 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003154 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003155 return 0;
3156 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003157
Willy Tarreau295a8372011-03-10 11:25:07 +01003158 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02003159 /* fall through */
3160
Willy Tarreau295a8372011-03-10 11:25:07 +01003161 case STAT_PX_ST_END:
3162 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003163 chunk_printf(&msg, "</table>");
3164
Willy Tarreau295a8372011-03-10 11:25:07 +01003165 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003166 /* close the form used to enable/disable this proxy servers */
3167 chunk_printf(&msg,
3168 "Choose the action to perform on the checked servers : "
3169 "<select name=action>"
3170 "<option value=\"\"></option>"
3171 "<option value=\"disable\">Disable</option>"
3172 "<option value=\"enable\">Enable</option>"
3173 "</select>"
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003174 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003175 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3176 "</form>",
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003177 px->uuid);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003178 }
3179
3180 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02003181
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003182 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003183 return 0;
3184 }
Willy Tarreau91861262007-10-17 17:06:05 +02003185
Willy Tarreau295a8372011-03-10 11:25:07 +01003186 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02003187 /* fall through */
3188
Willy Tarreau295a8372011-03-10 11:25:07 +01003189 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02003190 return 1;
3191
3192 default:
3193 /* unknown state, we should put an abort() here ! */
3194 return 1;
3195 }
3196}
3197
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003198/* This function dumps a complete session state onto the stream intreface's
3199 * read buffer. The data_ctx must have been zeroed first, and the flags
3200 * properly set. The session has to be set in data_ctx.sess.target. It returns
3201 * 0 if the output buffer is full and it needs to be called again, otherwise
3202 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003203 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003204static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003205{
3206 struct tm tm;
3207 struct chunk msg;
3208 struct session *sess;
3209 extern const char *monthname[12];
3210 char pn[INET6_ADDRSTRLEN];
3211
David du Colombier7af46052012-05-16 14:16:48 +02003212 chunk_init(&msg, trash, trashlen);
Willy Tarreau295a8372011-03-10 11:25:07 +01003213 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003214
Willy Tarreau295a8372011-03-10 11:25:07 +01003215 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003216 /* session changed, no need to go any further */
3217 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003218 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003219 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003220 si->applet.ctx.sess.target = NULL;
3221 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003222 return 1;
3223 }
3224
Willy Tarreau295a8372011-03-10 11:25:07 +01003225 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003226 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003227 si->applet.ctx.sess.uid = sess->uniq_id;
3228 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003229 /* fall through */
3230
3231 case 1:
3232 chunk_printf(&msg,
3233 "%p: id=%u, proto=%s",
3234 sess,
3235 sess->uniq_id,
3236 sess->listener->proto->name);
3237
Willy Tarreau6471afb2011-09-23 10:54:59 +02003238 switch (addr_to_str(&sess->si[0].addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003239 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003240 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003241 chunk_printf(&msg, " source=%s:%d\n",
Willy Tarreau6471afb2011-09-23 10:54:59 +02003242 pn, get_host_port(&sess->si[0].addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003243 break;
3244 case AF_UNIX:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003245 chunk_printf(&msg, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003246 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003247 default:
3248 /* no more information to print right now */
3249 chunk_printf(&msg, "\n");
3250 break;
3251 }
3252
3253 chunk_printf(&msg,
3254 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003255 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003256
3257 chunk_printf(&msg,
Daniel Schultze90690c72012-03-23 10:53:36 -07003258 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003259 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3260 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3261 sess->listener ? sess->listener->luid : 0);
3262
Willy Tarreau59b94792012-05-11 16:16:40 +02003263 si_get_to_addr(&sess->si[0]);
Daniel Schultze90690c72012-03-23 10:53:36 -07003264 switch (addr_to_str(&sess->si[0].addr.to, pn, sizeof(pn))) {
3265 case AF_INET:
3266 case AF_INET6:
3267 chunk_printf(&msg, " addr=%s:%d\n",
3268 pn, get_host_port(&sess->si[0].addr.to));
3269 break;
3270 case AF_UNIX:
3271 chunk_printf(&msg, " addr=unix:%d\n", sess->listener->luid);
3272 break;
3273 default:
3274 /* no more information to print right now */
3275 chunk_printf(&msg, "\n");
3276 break;
3277 }
3278
Willy Tarreau50943332011-09-02 17:33:05 +02003279 if (sess->be->cap & PR_CAP_BE)
3280 chunk_printf(&msg,
Daniel Schultze90690c72012-03-23 10:53:36 -07003281 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003282 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003283 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3284 else
3285 chunk_printf(&msg, " backend=<NONE> (id=-1 mode=-)");
3286
Willy Tarreau59b94792012-05-11 16:16:40 +02003287 si_get_from_addr(&sess->si[1]);
Daniel Schultze90690c72012-03-23 10:53:36 -07003288 switch (addr_to_str(&sess->si[1].addr.from, pn, sizeof(pn))) {
3289 case AF_INET:
3290 case AF_INET6:
3291 chunk_printf(&msg, " addr=%s:%d\n",
3292 pn, get_host_port(&sess->si[1].addr.from));
3293 break;
3294 case AF_UNIX:
3295 chunk_printf(&msg, " addr=unix\n");
3296 break;
3297 default:
3298 /* no more information to print right now */
3299 chunk_printf(&msg, "\n");
3300 break;
3301 }
3302
3303 if (sess->be->cap & PR_CAP_BE)
3304 chunk_printf(&msg,
3305 " server=%s (id=%u)",
Willy Tarreau50943332011-09-02 17:33:05 +02003306 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
3307 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
3308 else
Daniel Schultze90690c72012-03-23 10:53:36 -07003309 chunk_printf(&msg, " server=<NONE> (id=-1)");
3310
Willy Tarreau59b94792012-05-11 16:16:40 +02003311 si_get_to_addr(&sess->si[1]);
Daniel Schultze90690c72012-03-23 10:53:36 -07003312 switch (addr_to_str(&sess->si[1].addr.to, pn, sizeof(pn))) {
3313 case AF_INET:
3314 case AF_INET6:
3315 chunk_printf(&msg, " addr=%s:%d\n",
3316 pn, get_host_port(&sess->si[1].addr.to));
3317 break;
3318 case AF_UNIX:
3319 chunk_printf(&msg, " addr=unix\n");
3320 break;
3321 default:
3322 /* no more information to print right now */
3323 chunk_printf(&msg, "\n");
3324 break;
3325 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003326
3327 chunk_printf(&msg,
3328 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
3329 sess->task,
3330 sess->task->state,
3331 sess->task->nice, sess->task->calls,
3332 sess->task->expire ?
3333 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3334 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3335 TICKS_TO_MS(1000)) : "<NEVER>",
3336 task_in_rq(sess->task) ? ", running" : "");
3337
3338 get_localtime(sess->logs.accept_date.tv_sec, &tm);
3339 chunk_printf(&msg,
3340 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
3341 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3342 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3343 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3344
3345 chunk_printf(&msg,
3346 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3347 &sess->si[0],
3348 sess->si[0].state,
3349 sess->si[0].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003350 si_fd(&sess->si[0]),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003351 sess->si[0].exp ?
3352 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3353 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3354 TICKS_TO_MS(1000)) : "<NEVER>",
3355 sess->si[0].err_type);
3356
3357 chunk_printf(&msg,
3358 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3359 &sess->si[1],
3360 sess->si[1].state,
3361 sess->si[1].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003362 si_fd(&sess->si[1]),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003363 sess->si[1].exp ?
3364 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3365 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3366 TICKS_TO_MS(1000)) : "<NEVER>",
3367 sess->si[1].err_type);
3368
3369 chunk_printf(&msg,
3370 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3371 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3372 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3373
3374
3375 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003376 " req=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003377 " an_exp=%s",
3378 sess->req,
3379 sess->req->flags, sess->req->analysers,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003380 sess->req->i, sess->req->o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003381 sess->req->pipe ? sess->req->pipe->data : 0,
3382 sess->req->to_forward,
3383 sess->req->analyse_exp ?
3384 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3385 TICKS_TO_MS(1000)) : "<NEVER>");
3386
3387 chunk_printf(&msg,
3388 " rex=%s",
3389 sess->req->rex ?
3390 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3391 TICKS_TO_MS(1000)) : "<NEVER>");
3392
3393 chunk_printf(&msg,
3394 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003395 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003396 sess->req->wex ?
3397 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3398 TICKS_TO_MS(1000)) : "<NEVER>",
3399 sess->req->data,
Willy Tarreau89fa7062012-03-02 16:13:16 +01003400 (int)(sess->req->p - sess->req->data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003401 sess->txn.req.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003402 sess->req->total);
3403
3404 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003405 " res=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003406 " an_exp=%s",
3407 sess->rep,
3408 sess->rep->flags, sess->rep->analysers,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003409 sess->rep->i, sess->rep->o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003410 sess->rep->pipe ? sess->rep->pipe->data : 0,
3411 sess->rep->to_forward,
3412 sess->rep->analyse_exp ?
3413 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3414 TICKS_TO_MS(1000)) : "<NEVER>");
3415
3416 chunk_printf(&msg,
3417 " rex=%s",
3418 sess->rep->rex ?
3419 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3420 TICKS_TO_MS(1000)) : "<NEVER>");
3421
3422 chunk_printf(&msg,
3423 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003424 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003425 sess->rep->wex ?
3426 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3427 TICKS_TO_MS(1000)) : "<NEVER>",
3428 sess->rep->data,
Willy Tarreau89fa7062012-03-02 16:13:16 +01003429 (int)(sess->rep->p - sess->rep->data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003430 sess->txn.rsp.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003431 sess->rep->total);
3432
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003433 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003434 return 0;
3435
3436 /* use other states to dump the contents */
3437 }
3438 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003439 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003440 return 1;
3441}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003442
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003443/* This function dumps all sessions' states onto the stream intreface's
3444 * read buffer. The data_ctx must have been zeroed first, and the flags
3445 * properly set. It returns 0 if the output buffer is full and it needs
3446 * to be called again, otherwise non-zero. It is designed to be called
3447 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003448 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003449static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003450{
3451 struct chunk msg;
3452
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003453 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003454 /* If we're forced to shut down, we might have to remove our
3455 * reference to the last session being dumped.
3456 */
Willy Tarreau94981132012-05-21 17:09:48 +02003457 if (si->conn.data_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003458 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3459 LIST_DEL(&si->applet.ctx.sess.bref.users);
3460 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003461 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003462 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003463 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003464 }
3465
David du Colombier7af46052012-05-16 14:16:48 +02003466 chunk_init(&msg, trash, trashlen);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003467
Willy Tarreau94981132012-05-21 17:09:48 +02003468 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003469 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003470 /* the function had not been called yet, let's prepare the
3471 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003472 * pointer to the first in the global list. When a target
3473 * session is being destroyed, it is responsible for updating
3474 * this pointer. We know we have reached the end when this
3475 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003476 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003477 LIST_INIT(&si->applet.ctx.sess.bref.users);
3478 si->applet.ctx.sess.bref.ref = sessions.n;
Willy Tarreau94981132012-05-21 17:09:48 +02003479 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003480 /* fall through */
3481
Willy Tarreau295a8372011-03-10 11:25:07 +01003482 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003483 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003484 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3485 LIST_DEL(&si->applet.ctx.sess.bref.users);
3486 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003487 }
3488
3489 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003490 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003491 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003492 struct session *curr_sess;
3493
Willy Tarreau295a8372011-03-10 11:25:07 +01003494 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003495
Willy Tarreau295a8372011-03-10 11:25:07 +01003496 if (si->applet.ctx.sess.target) {
3497 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003498 goto next_sess;
3499
Willy Tarreau295a8372011-03-10 11:25:07 +01003500 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003501 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003502 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003503 return 0;
3504
3505 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003506 LIST_DEL(&si->applet.ctx.sess.bref.users);
3507 LIST_INIT(&si->applet.ctx.sess.bref.users);
3508 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003509 break;
3510 }
3511
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003512 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003513 "%p: proto=%s",
3514 curr_sess,
3515 curr_sess->listener->proto->name);
3516
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003517
Willy Tarreau6471afb2011-09-23 10:54:59 +02003518 switch (addr_to_str(&curr_sess->si[0].addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003519 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003520 case AF_INET6:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003521 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003522 " src=%s:%d fe=%s be=%s srv=%s",
3523 pn,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003524 get_host_port(&curr_sess->si[0].addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003525 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003526 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003527 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003528 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003529 break;
3530 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02003531 chunk_printf(&msg,
3532 " src=unix:%d fe=%s be=%s srv=%s",
3533 curr_sess->listener->luid,
3534 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003535 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003536 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003537 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003538 break;
3539 }
3540
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003541 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003542 " ts=%02x age=%s calls=%d",
3543 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003544 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3545 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003546
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003547 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003548 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003549 curr_sess->req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003550 curr_sess->req->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003551 curr_sess->req->analysers,
3552 curr_sess->req->rex ?
3553 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3554 TICKS_TO_MS(1000)) : "");
3555
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003556 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003557 ",wx=%s",
3558 curr_sess->req->wex ?
3559 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3560 TICKS_TO_MS(1000)) : "");
3561
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003562 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003563 ",ax=%s]",
3564 curr_sess->req->analyse_exp ?
3565 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3566 TICKS_TO_MS(1000)) : "");
3567
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003568 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003569 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003570 curr_sess->rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003571 curr_sess->rep->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003572 curr_sess->rep->analysers,
3573 curr_sess->rep->rex ?
3574 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3575 TICKS_TO_MS(1000)) : "");
3576
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003577 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003578 ",wx=%s",
3579 curr_sess->rep->wex ?
3580 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3581 TICKS_TO_MS(1000)) : "");
3582
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003583 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003584 ",ax=%s]",
3585 curr_sess->rep->analyse_exp ?
3586 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3587 TICKS_TO_MS(1000)) : "");
3588
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003589 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003590 " s0=[%d,%1xh,fd=%d,ex=%s]",
3591 curr_sess->si[0].state,
3592 curr_sess->si[0].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003593 si_fd(&curr_sess->si[0]),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003594 curr_sess->si[0].exp ?
3595 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3596 TICKS_TO_MS(1000)) : "");
3597
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003598 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003599 " s1=[%d,%1xh,fd=%d,ex=%s]",
3600 curr_sess->si[1].state,
3601 curr_sess->si[1].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003602 si_fd(&curr_sess->si[1]),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003603 curr_sess->si[1].exp ?
3604 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3605 TICKS_TO_MS(1000)) : "");
3606
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003607 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003608 " exp=%s",
3609 curr_sess->task->expire ?
3610 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3611 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003612 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003613 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003614
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003615 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003616
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003617 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003618 /* let's try again later from this session. We add ourselves into
3619 * this session's users so that it can remove us upon termination.
3620 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003621 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003622 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003623 }
3624
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003625 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003626 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003627 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003628
Willy Tarreau295a8372011-03-10 11:25:07 +01003629 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003630 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003631 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003632 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3633 else
3634 chunk_printf(&msg, "Session not found.\n");
3635
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003636 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003637 return 0;
3638
Willy Tarreau295a8372011-03-10 11:25:07 +01003639 si->applet.ctx.sess.target = NULL;
3640 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003641 return 1;
3642 }
3643
Willy Tarreau94981132012-05-21 17:09:48 +02003644 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003645 /* fall through */
3646
3647 default:
Willy Tarreau94981132012-05-21 17:09:48 +02003648 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003649 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003650 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003651}
3652
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003653/* This function dumps all tables' states onto the stream intreface's
3654 * read buffer. The data_ctx must have been zeroed first, and the flags
3655 * properly set. It returns 0 if the output buffer is full and it needs
3656 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003657 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003658static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003659{
Willy Tarreau94981132012-05-21 17:09:48 +02003660 struct session *s = si->conn.data_ctx;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003661 struct chunk msg;
3662 struct ebmb_node *eb;
3663 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003664 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003665
3666 /*
Willy Tarreau94981132012-05-21 17:09:48 +02003667 * We have 3 possible states in si->conn.data_st :
Willy Tarreau295a8372011-03-10 11:25:07 +01003668 * - STAT_ST_INIT : the first call
3669 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003670 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003671 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003672 * and the entry pointer points to the next entry to be dumped,
3673 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003674 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003675 * data though.
3676 */
3677
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003678 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003679 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau94981132012-05-21 17:09:48 +02003680 if (si->conn.data_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003681 si->applet.ctx.table.entry->ref_cnt--;
3682 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003683 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003684 return 1;
3685 }
3686
David du Colombier7af46052012-05-16 14:16:48 +02003687 chunk_init(&msg, trash, trashlen);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003688
Willy Tarreau94981132012-05-21 17:09:48 +02003689 while (si->conn.data_st != STAT_ST_FIN) {
3690 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003691 case STAT_ST_INIT:
3692 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3693 if (!si->applet.ctx.table.proxy)
3694 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003695
Willy Tarreau295a8372011-03-10 11:25:07 +01003696 si->applet.ctx.table.entry = NULL;
Willy Tarreau94981132012-05-21 17:09:48 +02003697 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003698 break;
3699
Willy Tarreau295a8372011-03-10 11:25:07 +01003700 case STAT_ST_INFO:
3701 if (!si->applet.ctx.table.proxy ||
3702 (si->applet.ctx.table.target &&
3703 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
Willy Tarreau94981132012-05-21 17:09:48 +02003704 si->conn.data_st = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003705 break;
3706 }
3707
Willy Tarreau295a8372011-03-10 11:25:07 +01003708 if (si->applet.ctx.table.proxy->table.size) {
Simon Hormanc88b8872011-06-15 15:18:49 +09003709 if (show && !stats_dump_table_head_to_buffer(&msg, si, si->applet.ctx.table.proxy,
3710 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003711 return 0;
3712
Willy Tarreau295a8372011-03-10 11:25:07 +01003713 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003714 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
3715 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003716 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003717 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003718 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3719 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau94981132012-05-21 17:09:48 +02003720 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003721 break;
3722 }
3723 }
3724 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003725 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003726 break;
3727
Willy Tarreau295a8372011-03-10 11:25:07 +01003728 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003729 skip_entry = false;
3730
Willy Tarreau295a8372011-03-10 11:25:07 +01003731 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003732 /* we're filtering on some data contents */
3733 void *ptr;
3734 long long data;
3735
Willy Tarreau295a8372011-03-10 11:25:07 +01003736 dt = si->applet.ctx.table.data_type;
3737 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3738 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003739 dt);
3740
3741 data = 0;
3742 switch (stktable_data_types[dt].std_type) {
3743 case STD_T_SINT:
3744 data = stktable_data_cast(ptr, std_t_sint);
3745 break;
3746 case STD_T_UINT:
3747 data = stktable_data_cast(ptr, std_t_uint);
3748 break;
3749 case STD_T_ULL:
3750 data = stktable_data_cast(ptr, std_t_ull);
3751 break;
3752 case STD_T_FRQP:
3753 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003754 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003755 break;
3756 }
3757
3758 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003759 if ((data < si->applet.ctx.table.value &&
3760 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3761 si->applet.ctx.table.data_op == STD_OP_GT ||
3762 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3763 (data == si->applet.ctx.table.value &&
3764 (si->applet.ctx.table.data_op == STD_OP_NE ||
3765 si->applet.ctx.table.data_op == STD_OP_GT ||
3766 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3767 (data > si->applet.ctx.table.value &&
3768 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3769 si->applet.ctx.table.data_op == STD_OP_LT ||
3770 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003771 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003772 }
3773
Simon Hormanc88b8872011-06-15 15:18:49 +09003774 if (show && !skip_entry &&
3775 !stats_dump_table_entry_to_buffer(&msg, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003776 si->applet.ctx.table.entry))
3777 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003778
Willy Tarreau295a8372011-03-10 11:25:07 +01003779 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003780
Willy Tarreau295a8372011-03-10 11:25:07 +01003781 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003782 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003783 struct stksess *old = si->applet.ctx.table.entry;
3784 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01003785 if (show)
3786 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3787 else
3788 stksess_kill(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau295a8372011-03-10 11:25:07 +01003789 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003790 break;
3791 }
3792
Simon Hormanc88b8872011-06-15 15:18:49 +09003793
3794 if (show)
3795 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3796 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3797 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3798
Willy Tarreau295a8372011-03-10 11:25:07 +01003799 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau94981132012-05-21 17:09:48 +02003800 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003801 break;
3802
Willy Tarreau295a8372011-03-10 11:25:07 +01003803 case STAT_ST_END:
Willy Tarreau94981132012-05-21 17:09:48 +02003804 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003805 break;
3806 }
3807 }
3808 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003809}
3810
Willy Tarreaud426a182010-03-05 14:58:26 +01003811/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003812 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3813 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3814 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3815 * lines are respected within the limit of 70 output chars. Lines that are
3816 * continuation of a previous truncated line begin with "+" instead of " "
3817 * after the offset. The new pointer is returned.
3818 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003819static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3820 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003821{
3822 int end;
3823 unsigned char c;
3824
3825 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003826 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003827 return ptr;
3828
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003829 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003830
Willy Tarreaud426a182010-03-05 14:58:26 +01003831 while (ptr < len && ptr < bsize) {
3832 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003833 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003834 if (out->len > end - 2)
3835 break;
3836 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003837 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003838 if (out->len > end - 3)
3839 break;
3840 out->str[out->len++] = '\\';
3841 switch (c) {
3842 case '\t': c = 't'; break;
3843 case '\n': c = 'n'; break;
3844 case '\r': c = 'r'; break;
3845 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003846 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003847 }
3848 out->str[out->len++] = c;
3849 } else {
3850 if (out->len > end - 5)
3851 break;
3852 out->str[out->len++] = '\\';
3853 out->str[out->len++] = 'x';
3854 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3855 out->str[out->len++] = hextab[c & 0xF];
3856 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003857 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003858 /* we had a line break, let's return now */
3859 out->str[out->len++] = '\n';
3860 *line = ptr;
3861 return ptr;
3862 }
3863 }
3864 /* we have an incomplete line, we return it as-is */
3865 out->str[out->len++] = '\n';
3866 return ptr;
3867}
3868
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003869/* This function dumps all captured errors onto the stream intreface's
3870 * read buffer. The data_ctx must have been zeroed first, and the flags
3871 * properly set. It returns 0 if the output buffer is full and it needs
3872 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003873 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003874static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003875{
3876 extern const char *monthname[12];
3877 struct chunk msg;
3878
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003879 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003880 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003881
David du Colombier7af46052012-05-16 14:16:48 +02003882 chunk_init(&msg, trash, trashlen);
Willy Tarreau74808cb2009-03-04 15:53:18 +01003883
Willy Tarreau295a8372011-03-10 11:25:07 +01003884 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003885 /* the function had not been called yet, let's prepare the
3886 * buffer for a response.
3887 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003888 struct tm tm;
3889
3890 get_localtime(date.tv_sec, &tm);
3891 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3892 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3893 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3894 error_snapshot_id);
3895
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003896 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003897 /* Socket buffer full. Let's try again later from the same point */
3898 return 0;
3899 }
3900
Willy Tarreau295a8372011-03-10 11:25:07 +01003901 si->applet.ctx.errors.px = proxy;
3902 si->applet.ctx.errors.buf = 0;
3903 si->applet.ctx.errors.bol = 0;
3904 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003905 }
3906
3907 /* we have two inner loops here, one for the proxy, the other one for
3908 * the buffer.
3909 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003910 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003911 struct error_snapshot *es;
3912
Willy Tarreau295a8372011-03-10 11:25:07 +01003913 if (si->applet.ctx.errors.buf == 0)
3914 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003915 else
Willy Tarreau295a8372011-03-10 11:25:07 +01003916 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003917
3918 if (!es->when.tv_sec)
3919 goto next;
3920
Willy Tarreau295a8372011-03-10 11:25:07 +01003921 if (si->applet.ctx.errors.iid >= 0 &&
3922 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
3923 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003924 goto next;
3925
Willy Tarreau295a8372011-03-10 11:25:07 +01003926 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003927 /* just print headers now */
3928
3929 char pn[INET6_ADDRSTRLEN];
3930 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003931 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003932
3933 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003934 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003935 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003936 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003937
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003938 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
3939 case AF_INET:
3940 case AF_INET6:
3941 port = get_host_port(&es->src);
3942 break;
3943 default:
3944 port = 0;
3945 }
3946
Willy Tarreau295a8372011-03-10 11:25:07 +01003947 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003948 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003949 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003950 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003951 " backend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01003952 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003953 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3954 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01003955 break;
3956 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003957 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003958 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003959 " frontend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01003960 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003961 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01003962 break;
3963 }
3964
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003965 chunk_printf(&msg,
3966 ", server %s (#%d), event #%u\n"
3967 " src %s:%d, session #%d, session flags 0x%08x\n"
3968 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
3969 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
3970 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
3971 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
3972 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
3973 es->ev_id,
3974 pn, port, es->sid, es->s_flags,
3975 es->state, es->m_flags, es->t_flags,
3976 es->m_clen, es->m_blen,
3977 es->b_flags, es->b_out, es->b_tot,
3978 es->len, es->b_wrap, es->pos);
3979
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003980 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003981 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003982 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003983 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003984 si->applet.ctx.errors.ptr = 0;
3985 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003986 }
3987
Willy Tarreau295a8372011-03-10 11:25:07 +01003988 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003989 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003990 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003991 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003992 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02003993 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003994 goto next;
3995 }
3996
3997 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01003998 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003999 int newptr;
4000 int newline;
4001
Willy Tarreau295a8372011-03-10 11:25:07 +01004002 newline = si->applet.ctx.errors.bol;
4003 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
4004 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004005 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004006
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004007 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004008 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004009 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004010 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004011 si->applet.ctx.errors.ptr = newptr;
4012 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004013 };
4014 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01004015 si->applet.ctx.errors.bol = 0;
4016 si->applet.ctx.errors.ptr = -1;
4017 si->applet.ctx.errors.buf++;
4018 if (si->applet.ctx.errors.buf > 1) {
4019 si->applet.ctx.errors.buf = 0;
4020 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004021 }
4022 }
4023
4024 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004025 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004026}
4027
Willy Tarreaub24281b2011-02-13 13:16:36 +01004028struct si_applet http_stats_applet = {
4029 .name = "<STATS>", /* used for logging */
4030 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004031 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004032};
4033
Simon Horman9bd2c732011-06-15 15:18:44 +09004034static struct si_applet cli_applet = {
Willy Tarreaub24281b2011-02-13 13:16:36 +01004035 .name = "<CLI>", /* used for logging */
4036 .fct = cli_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004037 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004038};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004039
Willy Tarreau10522fd2008-07-09 20:12:41 +02004040static struct cfg_kw_list cfg_kws = {{ },{
4041 { CFG_GLOBAL, "stats", stats_parse_global },
4042 { 0, NULL, NULL },
4043}};
4044
4045__attribute__((constructor))
4046static void __dumpstats_module_init(void)
4047{
4048 cfg_register_keywords(&cfg_kws);
4049}
4050
Willy Tarreau91861262007-10-17 17:06:05 +02004051/*
4052 * Local variables:
4053 * c-indent-level: 8
4054 * c-basic-offset: 8
4055 * End:
4056 */