blob: f32e4c57edb47b7bc880ccc1fca16004b83feac0 [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
Emeric Brund88fd822012-05-18 18:30:29 +0200279 global.stats_sock.sock = &sock_raw;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200280 uxst_add_listener(&global.stats_sock);
281 global.maxsock++;
282 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200283 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100284 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200285 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100286
287 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200288 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100289 return -1;
290 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200291
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100292 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200293 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200294 return -1;
295 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200296 if (!global.stats_fe) {
297 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200298 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200299 return -1;
300 }
301 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200302 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200303 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200304 else if (!strcmp(args[1], "maxconn")) {
305 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200306
307 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200308 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200309 return -1;
310 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200311
312 if (!global.stats_fe) {
313 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200314 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200315 return -1;
316 }
317 }
318 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200319 }
320 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200321 memprintf(err, "'%s' only supports 'socket', 'maxconn' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200322 return -1;
323 }
324 return 0;
325}
326
Simon Horman9bd2c732011-06-15 15:18:44 +0900327static int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100328{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200329 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100330 "# pxname,svname,"
331 "qcur,qmax,"
332 "scur,smax,slim,stot,"
333 "bin,bout,"
334 "dreq,dresp,"
335 "ereq,econ,eresp,"
336 "wretr,wredis,"
337 "status,weight,act,bck,"
338 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200339 "pid,iid,sid,throttle,lbtot,tracked,type,"
340 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200341 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100342 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100343 "req_rate,req_rate_max,req_tot,"
344 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100345 "\n");
346}
347
Simon Hormand9366582011-06-15 15:18:45 +0900348/* print a string of text buffer to <out>. The format is :
349 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
350 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
351 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
352 */
353static int dump_text(struct chunk *out, const char *buf, int bsize)
354{
355 unsigned char c;
356 int ptr = 0;
357
358 while (buf[ptr] && ptr < bsize) {
359 c = buf[ptr];
360 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
361 if (out->len > out->size - 1)
362 break;
363 out->str[out->len++] = c;
364 }
365 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
366 if (out->len > out->size - 2)
367 break;
368 out->str[out->len++] = '\\';
369 switch (c) {
370 case ' ': c = ' '; break;
371 case '\t': c = 't'; break;
372 case '\n': c = 'n'; break;
373 case '\r': c = 'r'; break;
374 case '\e': c = 'e'; break;
375 case '\\': c = '\\'; break;
376 }
377 out->str[out->len++] = c;
378 }
379 else {
380 if (out->len > out->size - 4)
381 break;
382 out->str[out->len++] = '\\';
383 out->str[out->len++] = 'x';
384 out->str[out->len++] = hextab[(c >> 4) & 0xF];
385 out->str[out->len++] = hextab[c & 0xF];
386 }
387 ptr++;
388 }
389
390 return ptr;
391}
392
393/* print a buffer in hexa.
394 * Print stopped if <bsize> is reached, or if no more place in the chunk.
395 */
396static int dump_binary(struct chunk *out, const char *buf, int bsize)
397{
398 unsigned char c;
399 int ptr = 0;
400
401 while (ptr < bsize) {
402 c = buf[ptr];
403
404 if (out->len > out->size - 2)
405 break;
406 out->str[out->len++] = hextab[(c >> 4) & 0xF];
407 out->str[out->len++] = hextab[c & 0xF];
408
409 ptr++;
410 }
411 return ptr;
412}
413
414/* Dump the status of a table to a stream interface's
415 * read buffer. It returns 0 if the output buffer is full
416 * and needs to be called again, otherwise non-zero.
417 */
418static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
419 struct proxy *proxy, struct proxy *target)
420{
Willy Tarreau94981132012-05-21 17:09:48 +0200421 struct session *s = si->conn.data_ctx;
Simon Hormand9366582011-06-15 15:18:45 +0900422
423 chunk_printf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
424 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
425
426 /* any other information should be dumped here */
427
428 if (target && s->listener->perm.ux.level < ACCESS_LVL_OPER)
429 chunk_printf(msg, "# contents not dumped due to insufficient privileges\n");
430
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200431 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900432 return 0;
433
434 return 1;
435}
436
437/* Dump the a table entry to a stream interface's
438 * read buffer. It returns 0 if the output buffer is full
439 * and needs to be called again, otherwise non-zero.
440 */
441static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
442 struct proxy *proxy, struct stksess *entry)
443{
444 int dt;
445
446 chunk_printf(msg, "%p:", entry);
447
448 if (proxy->table.type == STKTABLE_TYPE_IP) {
449 char addr[INET_ADDRSTRLEN];
450 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
451 chunk_printf(msg, " key=%s", addr);
452 }
453 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
454 char addr[INET6_ADDRSTRLEN];
455 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
456 chunk_printf(msg, " key=%s", addr);
457 }
458 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
459 chunk_printf(msg, " key=%u", *(unsigned int *)entry->key.key);
460 }
461 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
462 chunk_printf(msg, " key=");
463 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
464 }
465 else {
466 chunk_printf(msg, " key=");
467 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
468 }
469
470 chunk_printf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
471
472 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
473 void *ptr;
474
475 if (proxy->table.data_ofs[dt] == 0)
476 continue;
477 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
478 chunk_printf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
479 else
480 chunk_printf(msg, " %s=", stktable_data_types[dt].name);
481
482 ptr = stktable_data_ptr(&proxy->table, entry, dt);
483 switch (stktable_data_types[dt].std_type) {
484 case STD_T_SINT:
485 chunk_printf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
486 break;
487 case STD_T_UINT:
488 chunk_printf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
489 break;
490 case STD_T_ULL:
491 chunk_printf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
492 break;
493 case STD_T_FRQP:
494 chunk_printf(msg, "%d",
495 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
496 proxy->table.data_arg[dt].u));
497 break;
498 }
499 }
500 chunk_printf(msg, "\n");
501
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200502 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900503 return 0;
504
505 return 1;
506}
507
Willy Tarreaudec98142012-06-06 23:37:08 +0200508static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900509{
Willy Tarreau94981132012-05-21 17:09:48 +0200510 struct session *s = si->conn.data_ctx;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900511 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900512 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900513 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900514 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreaudec98142012-06-06 23:37:08 +0200515 struct chunk msg;
Simon Horman121f3052011-06-15 15:18:46 +0900516
Simon Hormand5b9fd92011-06-15 15:18:48 +0900517 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900518
519 if (!*args[4]) {
520 si->applet.ctx.cli.msg = "Key value expected\n";
521 si->applet.st0 = STAT_CLI_PRINT;
522 return;
523 }
524
Simon Hormanc5b89f62011-06-15 15:18:50 +0900525 switch (px->table.type) {
526 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900527 uint32_key = htonl(inetaddr_host(args[4]));
528 static_table_key.key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900529 break;
530 case STKTABLE_TYPE_IPV6:
531 inet_pton(AF_INET6, args[4], ip6_key);
532 static_table_key.key = &ip6_key;
533 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900534 case STKTABLE_TYPE_INTEGER:
535 {
536 char *endptr;
537 unsigned long val;
538 errno = 0;
539 val = strtoul(args[4], &endptr, 10);
540 if ((errno == ERANGE && val == ULONG_MAX) ||
541 (errno != 0 && val == 0) || endptr == args[4] ||
542 val > 0xffffffff) {
543 si->applet.ctx.cli.msg = "Invalid key\n";
544 si->applet.st0 = STAT_CLI_PRINT;
545 return;
546 }
547 uint32_key = (uint32_t) val;
548 static_table_key.key = &uint32_key;
549 break;
550 }
551 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900552 case STKTABLE_TYPE_STRING:
553 static_table_key.key = args[4];
554 static_table_key.key_len = strlen(args[4]);
555 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900556 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200557 switch (action) {
558 case STAT_CLI_O_TAB:
559 si->applet.ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
560 break;
561 case STAT_CLI_O_CLR:
562 si->applet.ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
563 break;
564 default:
565 si->applet.ctx.cli.msg = "Unknown action\n";
566 break;
567 }
Simon Horman121f3052011-06-15 15:18:46 +0900568 si->applet.st0 = STAT_CLI_PRINT;
569 return;
570 }
571
572 /* check permissions */
573 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
574 si->applet.ctx.cli.msg = stats_permission_denied_msg;
575 si->applet.st0 = STAT_CLI_PRINT;
576 return;
577 }
578
579 ts = stktable_lookup_key(&px->table, &static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900580
Willy Tarreaudec98142012-06-06 23:37:08 +0200581 switch (action) {
582 case STAT_CLI_O_TAB:
583 if (!ts)
584 return;
David du Colombier7af46052012-05-16 14:16:48 +0200585 chunk_init(&msg, trash, trashlen);
Simon Horman17bce342011-06-15 15:18:47 +0900586 if (!stats_dump_table_head_to_buffer(&msg, si, px, px))
587 return;
588 stats_dump_table_entry_to_buffer(&msg, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900589 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200590
591 case STAT_CLI_O_CLR:
592 if (!ts)
593 return;
594 if (ts->ref_cnt) {
595 /* don't delete an entry which is currently referenced */
596 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
597 si->applet.st0 = STAT_CLI_PRINT;
598 return;
599 }
600 stksess_kill(&px->table, ts);
601 break;
Simon Horman17bce342011-06-15 15:18:47 +0900602
Willy Tarreaudec98142012-06-06 23:37:08 +0200603 default:
604 si->applet.ctx.cli.msg = "Unknown action\n";
Simon Horman121f3052011-06-15 15:18:46 +0900605 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200606 break;
Simon Horman121f3052011-06-15 15:18:46 +0900607 }
Simon Horman121f3052011-06-15 15:18:46 +0900608}
609
Simon Hormand5b9fd92011-06-15 15:18:48 +0900610static void stats_sock_table_data_request(struct stream_interface *si, char **args)
611{
612 /* condition on stored data value */
613 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
614 if (si->applet.ctx.table.data_type < 0) {
615 si->applet.ctx.cli.msg = "Unknown data type\n";
616 si->applet.st0 = STAT_CLI_PRINT;
617 return;
618 }
619
620 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
621 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
622 si->applet.st0 = STAT_CLI_PRINT;
623 return;
624 }
625
626 si->applet.ctx.table.data_op = get_std_op(args[4]);
627 if (si->applet.ctx.table.data_op < 0) {
628 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
629 si->applet.st0 = STAT_CLI_PRINT;
630 return;
631 }
632
633 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
634 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
635 si->applet.st0 = STAT_CLI_PRINT;
636 return;
637 }
638}
639
Willy Tarreaudec98142012-06-06 23:37:08 +0200640static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900641{
642 si->applet.ctx.table.data_type = -1;
Willy Tarreau94981132012-05-21 17:09:48 +0200643 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200644 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900645 si->applet.ctx.table.proxy = NULL;
646 si->applet.ctx.table.entry = NULL;
Willy Tarreaudec98142012-06-06 23:37:08 +0200647 si->applet.st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900648
649 if (*args[2]) {
650 si->applet.ctx.table.target = find_stktable(args[2]);
651 if (!si->applet.ctx.table.target) {
652 si->applet.ctx.cli.msg = "No such table\n";
653 si->applet.st0 = STAT_CLI_PRINT;
654 return;
655 }
656 }
657 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200658 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900659 goto err_args;
660 return;
661 }
662
663 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200664 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900665 else if (strncmp(args[3], "data.", 5) == 0)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900666 stats_sock_table_data_request(si, args);
Simon Hormanc88b8872011-06-15 15:18:49 +0900667 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900668 goto err_args;
669
670 return;
671
672err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200673 switch (action) {
674 case STAT_CLI_O_TAB:
Simon Hormand5b9fd92011-06-15 15:18:48 +0900675 si->applet.ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200676 break;
677 case STAT_CLI_O_CLR:
Simon Hormanc88b8872011-06-15 15:18:49 +0900678 si->applet.ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200679 break;
680 default:
681 si->applet.ctx.cli.msg = "Unknown action\n";
682 break;
683 }
Simon Hormand5b9fd92011-06-15 15:18:48 +0900684 si->applet.st0 = STAT_CLI_PRINT;
685}
686
Willy Tarreau532a4502011-09-07 22:37:44 +0200687/* Expects to find a frontend named <arg> and returns it, otherwise displays various
688 * adequate error messages and returns NULL. This function also expects the session
689 * level to be admin.
690 */
691static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
692{
693 struct proxy *px;
694
695 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
696 si->applet.ctx.cli.msg = stats_permission_denied_msg;
697 si->applet.st0 = STAT_CLI_PRINT;
698 return NULL;
699 }
700
701 if (!*arg) {
702 si->applet.ctx.cli.msg = "A frontend name is expected.\n";
703 si->applet.st0 = STAT_CLI_PRINT;
704 return NULL;
705 }
706
707 px = findproxy(arg, PR_CAP_FE);
708 if (!px) {
709 si->applet.ctx.cli.msg = "No such frontend.\n";
710 si->applet.st0 = STAT_CLI_PRINT;
711 return NULL;
712 }
713 return px;
714}
715
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200716/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
717 * and returns the pointer to the server. Otherwise, display adequate error messages
718 * and returns NULL. This function also expects the session level to be admin. Note:
719 * the <arg> is modified to remove the '/'.
720 */
721static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
722{
723 struct proxy *px;
724 struct server *sv;
725 char *line;
726
727 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
728 si->applet.ctx.cli.msg = stats_permission_denied_msg;
729 si->applet.st0 = STAT_CLI_PRINT;
730 return NULL;
731 }
732
733 /* split "backend/server" and make <line> point to server */
734 for (line = arg; *line; line++)
735 if (*line == '/') {
736 *line++ = '\0';
737 break;
738 }
739
740 if (!*line || !*arg) {
741 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
742 si->applet.st0 = STAT_CLI_PRINT;
743 return NULL;
744 }
745
746 if (!get_backend_server(arg, line, &px, &sv)) {
747 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
748 si->applet.st0 = STAT_CLI_PRINT;
749 return NULL;
750 }
751
752 if (px->state == PR_STSTOPPED) {
753 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
754 si->applet.st0 = STAT_CLI_PRINT;
755 return NULL;
756 }
757
758 return sv;
759}
760
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200761/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200762 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100763 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200764 * designating the function which will have to process the request, which can
765 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200766 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900767static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200768{
Willy Tarreau94981132012-05-21 17:09:48 +0200769 struct session *s = si->conn.data_ctx;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200770 char *args[MAX_STATS_ARGS + 1];
771 int arg;
772
773 while (isspace((unsigned char)*line))
774 line++;
775
776 arg = 0;
777 args[arg] = line;
778
779 while (*line && arg < MAX_STATS_ARGS) {
780 if (isspace((unsigned char)*line)) {
781 *line++ = '\0';
782
783 while (isspace((unsigned char)*line))
784 line++;
785
786 args[++arg] = line;
787 continue;
788 }
789
790 line++;
791 }
792
793 while (++arg <= MAX_STATS_ARGS)
794 args[arg] = line;
795
Willy Tarreau295a8372011-03-10 11:25:07 +0100796 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200797 if (strcmp(args[0], "show") == 0) {
798 if (strcmp(args[1], "stat") == 0) {
799 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100800 si->applet.ctx.stats.flags |= STAT_BOUND;
801 si->applet.ctx.stats.iid = atoi(args[2]);
802 si->applet.ctx.stats.type = atoi(args[3]);
803 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200804 }
805
Willy Tarreau295a8372011-03-10 11:25:07 +0100806 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
807 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau94981132012-05-21 17:09:48 +0200808 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100809 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200810 }
811 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100812 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
813 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau94981132012-05-21 17:09:48 +0200814 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100815 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200816 }
817 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau94981132012-05-21 17:09:48 +0200818 si->conn.data_st = STAT_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200819 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100820 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100821 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200822 return 1;
823 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100824 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100825 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100826 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100827 si->applet.ctx.sess.target = NULL;
828 si->applet.ctx.sess.section = 0; /* start with session status */
829 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100830 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200831 }
832 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200833 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100834 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100835 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200836 return 1;
837 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200838 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100839 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200840 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100841 si->applet.ctx.errors.iid = -1;
842 si->applet.ctx.errors.px = NULL;
Willy Tarreau94981132012-05-21 17:09:48 +0200843 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100844 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200845 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200846 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200847 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200848 }
Aman Guptaceafb4a2012-04-02 18:57:54 -0700849 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200850 return 0;
851 }
852 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200853 else if (strcmp(args[0], "clear") == 0) {
854 if (strcmp(args[1], "counters") == 0) {
855 struct proxy *px;
856 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200857 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200858 int clrall = 0;
859
860 if (strcmp(args[2], "all") == 0)
861 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200862
Willy Tarreau6162db22009-10-10 17:13:00 +0200863 /* check permissions */
864 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
865 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100866 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100867 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200868 return 1;
869 }
870
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200871 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100872 if (clrall) {
873 memset(&px->be_counters, 0, sizeof(px->be_counters));
874 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
875 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200876 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100877 px->be_counters.conn_max = 0;
878 px->be_counters.p.http.rps_max = 0;
879 px->be_counters.sps_max = 0;
880 px->be_counters.cps_max = 0;
881 px->be_counters.nbpend_max = 0;
882
883 px->fe_counters.conn_max = 0;
884 px->fe_counters.p.http.rps_max = 0;
885 px->fe_counters.sps_max = 0;
886 px->fe_counters.cps_max = 0;
887 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200888 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200889
890 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200891 if (clrall)
892 memset(&sv->counters, 0, sizeof(sv->counters));
893 else {
894 sv->counters.cur_sess_max = 0;
895 sv->counters.nbpend_max = 0;
896 sv->counters.sps_max = 0;
897 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200898
899 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200900 if (li->counters) {
901 if (clrall)
902 memset(li->counters, 0, sizeof(*li->counters));
903 else
904 li->counters->conn_max = 0;
905 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200906 }
907
Willy Tarreau81c25d02011-09-07 15:17:21 +0200908 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200909 return 1;
910 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200911 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200912 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200913 /* end of processing */
914 return 1;
915 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200916 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200917 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200918 return 0;
919 }
920 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200921 else if (strcmp(args[0], "get") == 0) {
922 if (strcmp(args[1], "weight") == 0) {
923 struct proxy *px;
924 struct server *sv;
925
926 /* split "backend/server" and make <line> point to server */
927 for (line = args[2]; *line; line++)
928 if (*line == '/') {
929 *line++ = '\0';
930 break;
931 }
932
933 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100934 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100935 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200936 return 1;
937 }
938
939 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100940 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100941 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200942 return 1;
943 }
944
945 /* return server's effective weight at the moment */
David du Colombier7af46052012-05-16 14:16:48 +0200946 snprintf(trash, trashlen, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200947 bi_putstr(si->ib, trash);
Willy Tarreau38338fa2009-10-10 18:37:29 +0200948 return 1;
949 }
950 else { /* not "get weight" */
951 return 0;
952 }
953 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200954 else if (strcmp(args[0], "set") == 0) {
955 if (strcmp(args[1], "weight") == 0) {
956 struct proxy *px;
957 struct server *sv;
958 int w;
959
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200960 sv = expect_server_admin(s, si, args[2]);
961 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +0200962 return 1;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200963 px = sv->proxy;
Willy Tarreau4483d432009-10-10 19:30:08 +0200964
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200965 /* if the weight is terminated with '%', it is set relative to
966 * the initial weight, otherwise it is absolute.
967 */
968 if (!*args[3]) {
969 si->applet.ctx.cli.msg = "Require <weight> or <weight%>.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100970 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100971 return 1;
972 }
973
Willy Tarreau4483d432009-10-10 19:30:08 +0200974 w = atoi(args[3]);
975 if (strchr(args[3], '%') != NULL) {
976 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100977 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100978 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200979 return 1;
980 }
981 w = sv->iweight * w / 100;
982 }
983 else {
984 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100985 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100986 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200987 return 1;
988 }
989 }
990
991 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100992 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 +0100993 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200994 return 1;
995 }
996
997 sv->uweight = w;
998
999 if (px->lbprm.algo & BE_LB_PROP_DYN) {
1000 /* we must take care of not pushing the server to full throttle during slow starts */
1001 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
1002 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
1003 else
1004 sv->eweight = BE_WEIGHT_SCALE;
1005 sv->eweight *= sv->uweight;
1006 } else {
1007 sv->eweight = sv->uweight;
1008 }
1009
1010 /* static LB algorithms are a bit harder to update */
1011 if (px->lbprm.update_server_eweight)
1012 px->lbprm.update_server_eweight(sv);
Willy Tarreau9580d162012-05-19 19:07:40 +02001013 else if (sv->eweight) {
1014 if (px->lbprm.set_server_status_up)
1015 px->lbprm.set_server_status_up(sv);
1016 }
1017 else {
1018 if (px->lbprm.set_server_status_down)
1019 px->lbprm.set_server_status_down(sv);
1020 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001021
1022 return 1;
1023 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001024 else if (strcmp(args[1], "timeout") == 0) {
1025 if (strcmp(args[2], "cli") == 0) {
1026 unsigned timeout;
1027 const char *res;
1028
1029 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001030 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001031 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001032 return 1;
1033 }
1034
1035 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1036 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001037 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001038 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001039 return 1;
1040 }
1041
1042 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1043 return 1;
1044 }
1045 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001046 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001047 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001048 return 1;
1049 }
1050 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001051 else if (strcmp(args[1], "maxconn") == 0) {
1052 if (strcmp(args[2], "frontend") == 0) {
1053 struct proxy *px;
1054 struct listener *l;
1055 int v;
1056
Willy Tarreau532a4502011-09-07 22:37:44 +02001057 px = expect_frontend_admin(s, si, args[3]);
1058 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001059 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001060
1061 if (!*args[4]) {
1062 si->applet.ctx.cli.msg = "Integer value expected.\n";
1063 si->applet.st0 = STAT_CLI_PRINT;
1064 return 1;
1065 }
1066
1067 v = atoi(args[4]);
1068 /* check for unlimited values, we restore default setting (cfg_maxpconn) */
1069 if (v < 1) {
1070 si->applet.ctx.cli.msg = "Value out of range.\n";
1071 si->applet.st0 = STAT_CLI_PRINT;
1072 return 1;
1073 }
1074
1075 /* OK, the value is fine, so we assign it to the proxy and to all of
1076 * its listeners. The blocked ones will be dequeued.
1077 */
1078 px->maxconn = v;
1079 for (l = px->listen; l != NULL; l = l->next) {
1080 l->maxconn = v;
1081 if (l->state == LI_FULL)
1082 resume_listener(l);
1083 }
1084
1085 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1086 dequeue_all_listeners(&s->fe->listener_queue);
1087
1088 return 1;
1089 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001090 else if (strcmp(args[2], "global") == 0) {
1091 int v;
1092
1093 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1094 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1095 si->applet.st0 = STAT_CLI_PRINT;
1096 return 1;
1097 }
1098
1099 if (!*args[3]) {
1100 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1101 si->applet.st0 = STAT_CLI_PRINT;
1102 return 1;
1103 }
1104
1105 v = atoi(args[3]);
1106 if (v > global.hardmaxconn) {
1107 si->applet.ctx.cli.msg = "Value out of range.\n";
1108 si->applet.st0 = STAT_CLI_PRINT;
1109 return 1;
1110 }
1111
1112 /* check for unlimited values */
1113 if (v <= 0)
1114 v = global.hardmaxconn;
1115
1116 global.maxconn = v;
1117
1118 /* Dequeues all of the listeners waiting for a resource */
1119 if (!LIST_ISEMPTY(&global_listener_queue))
1120 dequeue_all_listeners(&global_listener_queue);
1121
1122 return 1;
1123 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001124 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001125 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001126 si->applet.st0 = STAT_CLI_PRINT;
1127 return 1;
1128 }
1129 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001130 else if (strcmp(args[1], "rate-limit") == 0) {
1131 if (strcmp(args[2], "connections") == 0) {
1132 if (strcmp(args[3], "global") == 0) {
1133 int v;
1134
1135 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1136 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1137 si->applet.st0 = STAT_CLI_PRINT;
1138 return 1;
1139 }
1140
1141 if (!*args[4]) {
1142 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1143 si->applet.st0 = STAT_CLI_PRINT;
1144 return 1;
1145 }
1146
1147 v = atoi(args[4]);
1148 if (v < 0) {
1149 si->applet.ctx.cli.msg = "Value out of range.\n";
1150 si->applet.st0 = STAT_CLI_PRINT;
1151 return 1;
1152 }
1153
1154 global.cps_lim = v;
1155
1156 /* Dequeues all of the listeners waiting for a resource */
1157 if (!LIST_ISEMPTY(&global_listener_queue))
1158 dequeue_all_listeners(&global_listener_queue);
1159
1160 return 1;
1161 }
1162 else {
1163 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1164 si->applet.st0 = STAT_CLI_PRINT;
1165 return 1;
1166 }
1167 }
1168 else {
1169 si->applet.ctx.cli.msg = "'set rate-limit' only supports 'connections'.\n";
1170 si->applet.st0 = STAT_CLI_PRINT;
1171 return 1;
1172 }
1173 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001174 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001175 return 0;
1176 }
1177 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001178 else if (strcmp(args[0], "enable") == 0) {
1179 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001180 struct server *sv;
1181
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001182 sv = expect_server_admin(s, si, args[2]);
1183 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001184 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001185
Cyril Bontécd19e512010-01-31 22:34:03 +01001186 if (sv->state & SRV_MAINTAIN) {
1187 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001188 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001189 /* If this server tracks the status of another one,
1190 * we must restore the good status.
1191 */
Willy Tarreau44267702011-10-28 15:35:33 +02001192 if (sv->track->state & SRV_RUNNING) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001193 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001194 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001195 } else {
1196 sv->state &= ~SRV_MAINTAIN;
1197 set_server_down(sv);
1198 }
1199 } else {
1200 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001201 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001202 }
1203 }
1204
Willy Tarreau532a4502011-09-07 22:37:44 +02001205 return 1;
1206 }
1207 else if (strcmp(args[1], "frontend") == 0) {
1208 struct proxy *px;
1209
1210 px = expect_frontend_admin(s, si, args[2]);
1211 if (!px)
1212 return 1;
1213
1214 if (px->state == PR_STSTOPPED) {
1215 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1216 si->applet.st0 = STAT_CLI_PRINT;
1217 return 1;
1218 }
1219
1220 if (px->state != PR_STPAUSED) {
1221 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1222 si->applet.st0 = STAT_CLI_PRINT;
1223 return 1;
1224 }
1225
1226 if (!resume_proxy(px)) {
1227 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1228 si->applet.st0 = STAT_CLI_PRINT;
1229 return 1;
1230 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001231 return 1;
1232 }
1233 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001234 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1235 si->applet.st0 = STAT_CLI_PRINT;
1236 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001237 }
1238 }
1239 else if (strcmp(args[0], "disable") == 0) {
1240 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001241 struct server *sv;
1242
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001243 sv = expect_server_admin(s, si, args[2]);
1244 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001245 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001246
Cyril Bontécd19e512010-01-31 22:34:03 +01001247 if (! (sv->state & SRV_MAINTAIN)) {
1248 /* Not already in maintenance, we can change the server state */
1249 sv->state |= SRV_MAINTAIN;
1250 set_server_down(sv);
1251 }
1252
Willy Tarreau532a4502011-09-07 22:37:44 +02001253 return 1;
1254 }
1255 else if (strcmp(args[1], "frontend") == 0) {
1256 struct proxy *px;
1257
1258 px = expect_frontend_admin(s, si, args[2]);
1259 if (!px)
1260 return 1;
1261
1262 if (px->state == PR_STSTOPPED) {
1263 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1264 si->applet.st0 = STAT_CLI_PRINT;
1265 return 1;
1266 }
1267
1268 if (px->state == PR_STPAUSED) {
1269 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1270 si->applet.st0 = STAT_CLI_PRINT;
1271 return 1;
1272 }
1273
1274 if (!pause_proxy(px)) {
1275 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1276 si->applet.st0 = STAT_CLI_PRINT;
1277 return 1;
1278 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001279 return 1;
1280 }
1281 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001282 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1283 si->applet.st0 = STAT_CLI_PRINT;
1284 return 1;
1285 }
1286 }
1287 else if (strcmp(args[0], "shutdown") == 0) {
1288 if (strcmp(args[1], "frontend") == 0) {
1289 struct proxy *px;
1290
1291 px = expect_frontend_admin(s, si, args[2]);
1292 if (!px)
1293 return 1;
1294
1295 if (px->state == PR_STSTOPPED) {
1296 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1297 si->applet.st0 = STAT_CLI_PRINT;
1298 return 1;
1299 }
1300
1301 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1302 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1303 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1304 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1305 stop_proxy(px);
1306 return 1;
1307 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001308 else if (strcmp(args[1], "session") == 0) {
1309 struct session *sess, *ptr;
1310
1311 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1312 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1313 si->applet.st0 = STAT_CLI_PRINT;
1314 return 1;
1315 }
1316
1317 if (!*args[2]) {
1318 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1319 si->applet.st0 = STAT_CLI_PRINT;
1320 return 1;
1321 }
1322
1323 ptr = (void *)strtoul(args[2], NULL, 0);
1324
1325 /* first, look for the requested session in the session table */
1326 list_for_each_entry(sess, &sessions, list) {
1327 if (sess == ptr)
1328 break;
1329 }
1330
1331 /* do we have the session ? */
1332 if (sess != ptr) {
1333 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1334 si->applet.st0 = STAT_CLI_PRINT;
1335 return 1;
1336 }
1337
1338 session_shutdown(sess, SN_ERR_KILLED);
1339 return 1;
1340 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001341 else if (strcmp(args[1], "sessions") == 0) {
1342 if (strcmp(args[2], "server") == 0) {
1343 struct server *sv;
1344 struct session *sess, *sess_bck;
1345
1346 sv = expect_server_admin(s, si, args[3]);
1347 if (!sv)
1348 return 1;
1349
1350 /* kill all the session that are on this server */
1351 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1352 if (sess->srv_conn == sv)
1353 session_shutdown(sess, SN_ERR_KILLED);
1354
1355 return 1;
1356 }
1357 else {
1358 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1359 si->applet.st0 = STAT_CLI_PRINT;
1360 return 1;
1361 }
1362 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001363 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001364 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001365 si->applet.st0 = STAT_CLI_PRINT;
1366 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001367 }
1368 }
1369 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001370 return 0;
1371 }
1372 return 1;
1373}
1374
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001375/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001376 * used to processes I/O from/to the stats unix socket. The system relies on a
1377 * state machine handling requests and various responses. We read a request,
1378 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001379 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1380 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001381 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001382 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001383static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001384{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001385 struct buffer *req = si->ob;
1386 struct buffer *res = si->ib;
1387 int reql;
1388 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001389
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001390 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1391 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001392
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001393 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001394 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001395 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001396 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001397 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001398 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001399 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001400 /* Let's close for real now. We just close the request
1401 * side, the conditions below will complete if needed.
1402 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001403 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001404 break;
1405 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001406 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001407 /* ensure we have some output room left in the event we
1408 * would want to return some info right after parsing.
1409 */
1410 if (buffer_almost_full(si->ib))
1411 break;
1412
David du Colombier7af46052012-05-16 14:16:48 +02001413 reql = bo_getline(si->ob, trash, trashlen);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001414 if (reql <= 0) { /* closed or EOL not found */
1415 if (reql == 0)
1416 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001417 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001418 continue;
1419 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001420
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001421 /* seek for a possible semi-colon. If we find one, we
1422 * replace it with an LF and skip only this part.
1423 */
1424 for (len = 0; len < reql; len++)
1425 if (trash[len] == ';') {
1426 trash[len] = '\n';
1427 reql = len + 1;
1428 break;
1429 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001430
Willy Tarreau816fc222009-10-04 07:36:58 +02001431 /* now it is time to check that we have a full line,
1432 * remove the trailing \n and possibly \r, then cut the
1433 * line.
1434 */
1435 len = reql - 1;
1436 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001437 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001438 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001439 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001440
Willy Tarreau816fc222009-10-04 07:36:58 +02001441 if (len && trash[len-1] == '\r')
1442 len--;
1443
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001444 trash[len] = '\0';
1445
Willy Tarreaubc4af052011-02-13 13:25:14 +01001446 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001447 if (len) {
1448 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001449 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001450 continue;
1451 }
1452 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001453 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001454 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001455 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001456 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001457 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001458 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001459 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001460 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001461 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001462 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001463 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001464 /* if prompt is disabled, print help on empty lines,
1465 * so that the user at least knows how to enable
1466 * prompt and find help.
1467 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001468 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001469 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001470 }
1471
1472 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001473 bo_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001474 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001475 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001476 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001477 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001478 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001479 continue;
1480 }
1481
Willy Tarreaubc4af052011-02-13 13:25:14 +01001482 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001483 case STAT_CLI_PRINT:
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001484 if (bi_putstr(si->ib, si->applet.ctx.cli.msg) != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001485 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001486 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001487 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001488 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001489 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001490 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001491 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001492 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001493 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001494 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001495 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001496 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001497 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001498 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001499 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001500 case STAT_CLI_O_CLR:
Willy Tarreaudec98142012-06-06 23:37:08 +02001501 if (stats_table_request(si, si->applet.st0))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001502 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001503 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001504 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001505 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001506 break;
1507 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001508
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001509 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001510 if (si->applet.st0 == STAT_CLI_PROMPT) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001511 if (bi_putstr(si->ib, si->applet.st1 ? "\n> " : "\n") != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001512 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001513 }
1514
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001515 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001516 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001517 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001518
1519 /* Now we close the output if one of the writers did so,
1520 * or if we're not in interactive mode and the request
1521 * buffer is empty. This still allows pipelined requests
1522 * to be sent in non-interactive mode.
1523 */
Willy Tarreau2e046c62012-03-01 16:08:30 +01001524 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->applet.st1 && !req->o)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001525 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001526 continue;
1527 }
1528
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001529 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001530 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001531 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001532 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001533
Willy Tarreaubc4af052011-02-13 13:25:14 +01001534 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001535 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1536 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001537 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001538 * and nothing more to consume. This is comparable to a broken pipe, so
1539 * we forward the close to the request side so that it flows upstream to
1540 * the client.
1541 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001542 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001543 }
1544
Willy Tarreaubc4af052011-02-13 13:25:14 +01001545 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001546 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1547 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1548 /* We have no more processing to do, and nothing more to send, and
1549 * the client side has closed. So we'll forward this state downstream
1550 * on the response buffer.
1551 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001552 si_shutr(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001553 res->flags |= BF_READ_NULL;
1554 }
1555
1556 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001557 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001558
1559 /* we don't want to expire timeouts while we're processing requests */
1560 si->ib->rex = TICK_ETERNITY;
1561 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001562
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001563 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001564 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 +02001565 __FUNCTION__, __LINE__,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001566 si->state, req->flags, res->flags, req->i, req->o, res->i, res->o);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001567
1568 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1569 /* check that we have released everything then unregister */
1570 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001571 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001572}
1573
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001574/* This function dumps statistics onto the stream interface's read buffer.
1575 * The data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001576 * It returns 0 as long as it does not complete, non-zero upon completion.
1577 * Some states are not used but it makes the code more similar to other
1578 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001579 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001580static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001581{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001582 struct proxy *px;
1583 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001584 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001585
David du Colombier7af46052012-05-16 14:16:48 +02001586 chunk_init(&msg, trash, trashlen);
Willy Tarreau3e76e722007-10-17 18:57:38 +02001587
Willy Tarreau94981132012-05-21 17:09:48 +02001588 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001589 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001590 /* the function had not been called yet */
Willy Tarreau94981132012-05-21 17:09:48 +02001591 si->conn.data_st = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001592 /* fall through */
1593
Willy Tarreau295a8372011-03-10 11:25:07 +01001594 case STAT_ST_HEAD:
1595 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001596 print_csv_header(&msg);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001597 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001598 return 0;
1599 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001600
Willy Tarreau94981132012-05-21 17:09:48 +02001601 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001602 /* fall through */
1603
Willy Tarreau295a8372011-03-10 11:25:07 +01001604 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001605 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001606 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001607 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001608 "Name: " PRODUCT_NAME "\n"
1609 "Version: " HAPROXY_VERSION "\n"
1610 "Release_date: " HAPROXY_DATE "\n"
1611 "Nbproc: %d\n"
1612 "Process_num: %d\n"
1613 "Pid: %d\n"
1614 "Uptime: %dd %dh%02dm%02ds\n"
1615 "Uptime_sec: %d\n"
1616 "Memmax_MB: %d\n"
1617 "Ulimit-n: %d\n"
1618 "Maxsock: %d\n"
1619 "Maxconn: %d\n"
Willy Tarreau91886b62011-09-07 14:38:31 +02001620 "Hard_maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001621 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001622 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001623 "PipesUsed: %d\n"
1624 "PipesFree: %d\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001625 "ConnRate: %d\n"
1626 "ConnRateLimit: %d\n"
1627 "MaxConnRate: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001628 "Tasks: %d\n"
1629 "Run_queue: %d\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02001630 "Idle_pct: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001631 "node: %s\n"
1632 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001633 "",
1634 global.nbproc,
1635 relative_pid,
1636 pid,
1637 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1638 up,
1639 global.rlimit_memmax,
1640 global.rlimit_nofile,
Willy Tarreau91886b62011-09-07 14:38:31 +02001641 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001642 actconn, pipes_used, pipes_free,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001643 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau45a12512011-09-10 16:56:42 +02001644 nb_tasks_cur, run_queue_cur, idle_pct,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001645 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001646 );
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001647 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001648 return 0;
1649 }
1650
Willy Tarreau295a8372011-03-10 11:25:07 +01001651 si->applet.ctx.stats.px = proxy;
1652 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1653 si->applet.ctx.stats.sv = NULL;
Willy Tarreau94981132012-05-21 17:09:48 +02001654 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001655 /* fall through */
1656
Willy Tarreau295a8372011-03-10 11:25:07 +01001657 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001658 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001659 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1660 while (si->applet.ctx.stats.px) {
1661 px = si->applet.ctx.stats.px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001662 /* skip the disabled proxies and non-networked ones */
1663 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001664 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001665 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001666 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001667 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001668
Willy Tarreau295a8372011-03-10 11:25:07 +01001669 si->applet.ctx.stats.px = px->next;
1670 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001671 }
1672 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001673 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001674
Willy Tarreau94981132012-05-21 17:09:48 +02001675 si->conn.data_st = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001676 /* fall through */
1677
Willy Tarreau295a8372011-03-10 11:25:07 +01001678 case STAT_ST_END:
Willy Tarreau94981132012-05-21 17:09:48 +02001679 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001680 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001681
Willy Tarreau295a8372011-03-10 11:25:07 +01001682 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001683 return 1;
1684
1685 default:
1686 /* unknown state ! */
Willy Tarreau94981132012-05-21 17:09:48 +02001687 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001688 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001689 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001690}
1691
1692
Cyril Bonté70be45d2010-10-12 00:14:35 +02001693/* We don't want to land on the posted stats page because a refresh will
1694 * repost the data. We don't want this to happen on accident so we redirect
1695 * the browse to the stats page with a GET.
1696 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001697static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001698{
Willy Tarreau94981132012-05-21 17:09:48 +02001699 struct session *s = si->conn.data_ctx;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001700 struct chunk msg;
1701
David du Colombier7af46052012-05-16 14:16:48 +02001702 chunk_init(&msg, trash, trashlen);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001703
Willy Tarreau94981132012-05-21 17:09:48 +02001704 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001705 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001706 chunk_printf(&msg,
1707 "HTTP/1.0 303 See Other\r\n"
1708 "Cache-Control: no-cache\r\n"
1709 "Content-Type: text/plain\r\n"
1710 "Connection: close\r\n"
1711 "Location: %s;st=%s",
Cyril Bonté19979e12012-04-04 12:57:21 +02001712 uri->uri_prefix,
1713 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
1714 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
1715 stat_status_codes[si->applet.ctx.stats.st_code]) ?
1716 stat_status_codes[si->applet.ctx.stats.st_code] :
1717 stat_status_codes[STAT_STATUS_UNKN]);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001718 chunk_printf(&msg, "\r\n\r\n");
1719
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001720 if (bi_putchk(si->ib, &msg) == -1)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001721 return 0;
1722
1723 s->txn.status = 303;
1724
1725 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1726 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1727 if (!(s->flags & SN_FINST_MASK))
1728 s->flags |= SN_FINST_R;
1729
Willy Tarreau94981132012-05-21 17:09:48 +02001730 si->conn.data_st = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001731 return 1;
1732 }
1733 return 1;
1734}
1735
1736
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001737/* This I/O handler runs as an applet embedded in a stream interface. It is
1738 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001739 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001740 * automatically unregisters itself once transfer is complete.
1741 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001742static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001743{
Willy Tarreau94981132012-05-21 17:09:48 +02001744 struct session *s = si->conn.data_ctx;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001745 struct buffer *req = si->ob;
1746 struct buffer *res = si->ib;
1747
1748 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1749 goto out;
1750
1751 /* check that the output is not closed */
1752 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001753 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001754
Willy Tarreaubc4af052011-02-13 13:25:14 +01001755 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001756 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001757 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001758 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001759 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001760 }
1761 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001762 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001763 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001764 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001765 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001766 }
1767 }
1768
1769 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
Willy Tarreau73b013b2012-05-21 16:31:45 +02001770 si_shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001771
Willy Tarreaubc4af052011-02-13 13:25:14 +01001772 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001773 si_shutr(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001774 res->flags |= BF_READ_NULL;
1775 }
1776
1777 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001778 si_update(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001779
1780 /* we don't want to expire timeouts while we're processing requests */
1781 si->ib->rex = TICK_ETERNITY;
1782 si->ob->wex = TICK_ETERNITY;
1783
1784 out:
1785 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1786 /* check that we have released everything then unregister */
1787 stream_int_unregister_handler(si);
1788 }
1789}
1790
1791
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001792/* This function dumps statistics in HTTP format onto the stream interface's
1793 * read buffer. The data_ctx must have been zeroed first, and the flags
1794 * properly set. It returns 0 if it had to stop writing data and an I/O is
1795 * needed, 1 if the dump is finished and the session must be closed, or -1
1796 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001797 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001798static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001799{
Willy Tarreau94981132012-05-21 17:09:48 +02001800 struct session *s = si->conn.data_ctx;
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001801 struct buffer *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001802 struct proxy *px;
1803 struct chunk msg;
1804 unsigned int up;
1805
David du Colombier7af46052012-05-16 14:16:48 +02001806 chunk_init(&msg, trash, trashlen);
Willy Tarreau91861262007-10-17 17:06:05 +02001807
Willy Tarreau94981132012-05-21 17:09:48 +02001808 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001809 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001810 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001811 "HTTP/1.0 200 OK\r\n"
1812 "Cache-Control: no-cache\r\n"
1813 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001814 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001815 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001816
Willy Tarreau295a8372011-03-10 11:25:07 +01001817 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001818 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001819 uri->refresh);
1820
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001821 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001822
1823 s->txn.status = 200;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001824 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001825 return 0;
1826
Willy Tarreau91861262007-10-17 17:06:05 +02001827 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1828 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1829 if (!(s->flags & SN_FINST_MASK))
1830 s->flags |= SN_FINST_R;
1831
1832 if (s->txn.meth == HTTP_METH_HEAD) {
1833 /* that's all we return in case of HEAD request */
Willy Tarreau94981132012-05-21 17:09:48 +02001834 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001835 return 1;
1836 }
1837
Willy Tarreau94981132012-05-21 17:09:48 +02001838 si->conn.data_st = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001839 /* fall through */
1840
Willy Tarreau295a8372011-03-10 11:25:07 +01001841 case STAT_ST_HEAD:
1842 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001843 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001844 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001845 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1846 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001847 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001848 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1849 "<style type=\"text/css\"><!--\n"
1850 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001851 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001852 " font-size: 12px;"
1853 " font-weight: normal;"
1854 " color: black;"
1855 " background: white;"
1856 "}\n"
1857 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001858 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001859 "}\n"
1860 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001861 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001862 " margin-bottom: 0.5em;"
1863 "}\n"
1864 "h2 {"
1865 " font-family: helvetica, arial;"
1866 " font-size: x-large;"
1867 " font-weight: bold;"
1868 " font-style: italic;"
1869 " color: #6020a0;"
1870 " margin-top: 0em;"
1871 " margin-bottom: 0em;"
1872 "}\n"
1873 "h3 {"
1874 " font-family: helvetica, arial;"
1875 " font-size: 16px;"
1876 " font-weight: bold;"
1877 " color: #b00040;"
1878 " background: #e8e8d0;"
1879 " margin-top: 0em;"
1880 " margin-bottom: 0em;"
1881 "}\n"
1882 "li {"
1883 " margin-top: 0.25em;"
1884 " margin-right: 2em;"
1885 "}\n"
1886 ".hr {margin-top: 0.25em;"
1887 " border-color: black;"
1888 " border-bottom-style: solid;"
1889 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001890 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001891 ".total {background: #20D0D0;color: #ffff80;}\n"
1892 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001893 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001894 ".backend {background: #e8e8d0;}\n"
1895 ".active0 {background: #ff9090;}\n"
1896 ".active1 {background: #ffd020;}\n"
1897 ".active2 {background: #ffffa0;}\n"
1898 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001899 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1900 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1901 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001902 ".backup0 {background: #ff9090;}\n"
1903 ".backup1 {background: #ff80ff;}\n"
1904 ".backup2 {background: #c060ff;}\n"
1905 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001906 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1907 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1908 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001909 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001910 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001911 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001912 "a.px:link {color: #ffff40; text-decoration: none;}"
1913 "a.px:visited {color: #ffff40; text-decoration: none;}"
1914 "a.px:hover {color: #ffffff; text-decoration: none;}"
1915 "a.lfsb:link {color: #000000; text-decoration: none;}"
1916 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1917 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1918 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001919 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001920 "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"
1921 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001922 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001923 "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 +02001924 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001925 "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 +02001926 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001927 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1928 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1929 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001930 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001931 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001932 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001933 (uri->flags&ST_SHNODE) ? " on " : "",
1934 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001935 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001936 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001937 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001938 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001939 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02001940 return 0;
1941
Willy Tarreau94981132012-05-21 17:09:48 +02001942 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02001943 /* fall through */
1944
Willy Tarreau295a8372011-03-10 11:25:07 +01001945 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02001946 up = (now.tv_sec - start_date.tv_sec);
1947
1948 /* WARNING! this has to fit the first packet too.
1949 * We are around 3.5 kB, add adding entries will
1950 * become tricky if we want to support 4kB buffers !
1951 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001952 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001953 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001954 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1955 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001956 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001957 "<hr width=\"100%%\" class=\"hr\">\n"
1958 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001959 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001960 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001961 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001962 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1963 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001964 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02001965 "Running tasks: %d/%d; idle = %d %%<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001966 "</td><td align=\"center\" nowrap>\n"
1967 "<table class=\"lgd\"><tr>\n"
1968 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1969 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1970 "</tr><tr>\n"
1971 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1972 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1973 "</tr><tr>\n"
1974 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1975 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1976 "</tr><tr>\n"
1977 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001978 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001979 "</tr><tr>\n"
1980 "<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 +02001981 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001982 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001983 "</td>"
1984 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1985 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1986 "",
1987 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001988 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1989 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1990 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001991 up / 86400, (up % 86400) / 3600,
1992 (up % 3600) / 60, (up % 60),
1993 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1994 global.rlimit_memmax ? " MB" : "",
1995 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001996 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001997 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau45a12512011-09-10 16:56:42 +02001998 run_queue_cur, nb_tasks_cur, idle_pct
Willy Tarreau91861262007-10-17 17:06:05 +02001999 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002000
Willy Tarreau295a8372011-03-10 11:25:07 +01002001 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002002 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002003 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
2004 uri->uri_prefix,
2005 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01002006 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002007 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002008 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002009 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2010 uri->uri_prefix,
2011 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01002012 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002013
Willy Tarreau55bb8452007-10-17 18:44:57 +02002014 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002015 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002016 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002017 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
2018 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002019 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002020 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002021 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002022 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002023 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
2024 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002025 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002026 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002027 }
Willy Tarreau91861262007-10-17 17:06:05 +02002028
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002029 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002030 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
2031 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002032 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2033 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002034
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002035 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002036 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
2037 uri->uri_prefix,
2038 (uri->refresh > 0) ? ";norefresh" : "");
2039
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002040 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002041 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002042 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
Adrian Bridgettafdb6e52012-03-19 23:36:42 +00002043 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002044 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2045 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2046 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2047 "</ul>"
2048 "</td>"
2049 "</tr></table>\n"
2050 ""
2051 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002052
Willy Tarreau295a8372011-03-10 11:25:07 +01002053 if (si->applet.ctx.stats.st_code) {
Cyril Bonté19979e12012-04-04 12:57:21 +02002054 switch (si->applet.ctx.stats.st_code) {
2055 case STAT_STATUS_DONE:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002056 chunk_printf(&msg,
2057 "<p><div class=active3>"
2058 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2059 "Action processed successfully."
2060 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002061 break;
2062 case STAT_STATUS_NONE:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002063 chunk_printf(&msg,
2064 "<p><div class=active2>"
2065 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2066 "Nothing has changed."
2067 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002068 break;
2069 case STAT_STATUS_PART:
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002070 chunk_printf(&msg,
2071 "<p><div class=active2>"
2072 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2073 "Action partially processed.<br>"
2074 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2075 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002076 break;
2077 case STAT_STATUS_ERRP:
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002078 chunk_printf(&msg,
2079 "<p><div class=active0>"
2080 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2081 "Action not processed because of invalid parameters."
2082 "<ul>"
2083 "<li>The action is maybe unknown.</li>"
2084 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2085 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2086 "</ul>"
2087 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002088 break;
2089 case STAT_STATUS_EXCD:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002090 chunk_printf(&msg,
2091 "<p><div class=active0>"
2092 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2093 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2094 "You should retry with less servers at a time.</b>"
2095 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002096 break;
2097 case STAT_STATUS_DENY:
Cyril Bonté474be412010-10-12 00:14:36 +02002098 chunk_printf(&msg,
2099 "<p><div class=active0>"
2100 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2101 "<b>Action denied.</b>"
2102 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002103 break;
2104 default:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002105 chunk_printf(&msg,
2106 "<p><div class=active6>"
2107 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2108 "Unexpected result."
2109 "</div>\n", uri->uri_prefix);
2110 }
2111 chunk_printf(&msg,"<p>\n");
2112 }
2113
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002114 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002115 return 0;
2116 }
Willy Tarreau91861262007-10-17 17:06:05 +02002117
Willy Tarreau295a8372011-03-10 11:25:07 +01002118 si->applet.ctx.stats.px = proxy;
2119 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau94981132012-05-21 17:09:48 +02002120 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02002121 /* fall through */
2122
Willy Tarreau295a8372011-03-10 11:25:07 +01002123 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02002124 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01002125 while (si->applet.ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002126 if (buffer_almost_full(rep))
2127 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002128 px = si->applet.ctx.stats.px;
Willy Tarreau91861262007-10-17 17:06:05 +02002129 /* skip the disabled proxies and non-networked ones */
2130 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002131 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002132 return 0;
2133
Willy Tarreau295a8372011-03-10 11:25:07 +01002134 si->applet.ctx.stats.px = px->next;
2135 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02002136 }
2137 /* here, we just have reached the last proxy */
2138
Willy Tarreau94981132012-05-21 17:09:48 +02002139 si->conn.data_st = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002140 /* fall through */
2141
Willy Tarreau295a8372011-03-10 11:25:07 +01002142 case STAT_ST_END:
2143 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002144 chunk_printf(&msg, "</body></html>\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002145 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002146 return 0;
2147 }
Willy Tarreau91861262007-10-17 17:06:05 +02002148
Willy Tarreau94981132012-05-21 17:09:48 +02002149 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002150 /* fall through */
2151
Willy Tarreau295a8372011-03-10 11:25:07 +01002152 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002153 return 1;
2154
2155 default:
2156 /* unknown state ! */
Willy Tarreau94981132012-05-21 17:09:48 +02002157 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002158 return -1;
2159 }
2160}
2161
2162
2163/*
2164 * Dumps statistics for a proxy.
2165 * Returns 0 if it had to stop dumping data because of lack of buffer space,
2166 * ot non-zero if everything completed.
2167 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002168static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002169{
Willy Tarreau94981132012-05-21 17:09:48 +02002170 struct session *s = si->conn.data_ctx;
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002171 struct buffer *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002172 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002173 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002174 struct chunk msg;
2175
David du Colombier7af46052012-05-16 14:16:48 +02002176 chunk_init(&msg, trash, trashlen);
Willy Tarreau91861262007-10-17 17:06:05 +02002177
Willy Tarreau295a8372011-03-10 11:25:07 +01002178 switch (si->applet.ctx.stats.px_st) {
2179 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002180 /* we are on a new proxy */
2181
2182 if (uri && uri->scope) {
2183 /* we have a limited scope, we have to check the proxy name */
2184 struct stat_scope *scope;
2185 int len;
2186
2187 len = strlen(px->id);
2188 scope = uri->scope;
2189
2190 while (scope) {
2191 /* match exact proxy name */
2192 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2193 break;
2194
2195 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002196 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002197 break;
2198 scope = scope->next;
2199 }
2200
2201 /* proxy name not found : don't dump anything */
2202 if (scope == NULL)
2203 return 1;
2204 }
2205
Willy Tarreau295a8372011-03-10 11:25:07 +01002206 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
2207 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002208 return 1;
2209
Willy Tarreau295a8372011-03-10 11:25:07 +01002210 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002211 /* fall through */
2212
Willy Tarreau295a8372011-03-10 11:25:07 +01002213 case STAT_PX_ST_TH:
2214 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
2215 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002216 /* A form to enable/disable this proxy servers */
2217 chunk_printf(&msg,
2218 "<form action=\"%s\" method=\"post\">",
2219 uri->uri_prefix);
2220 }
2221
Willy Tarreau55bb8452007-10-17 18:44:57 +02002222 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002223 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002224 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002225 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002226 "<th class=\"pxname\" width=\"10%%\"");
2227
2228 if (uri->flags&ST_SHLGNDS) {
2229 /* cap, mode, id */
2230 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
2231 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2232 px->uuid);
2233
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002234 chunk_printf(&msg, "\"");
2235 }
2236
2237 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002238 ">%s<a name=\"%s\"></a>"
2239 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002240 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002241 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002242 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002243 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002244 "<tr class=\"titre\">",
2245 (uri->flags & ST_SHLGNDS)?"<u>":"",
2246 px->id, px->id, px->id,
2247 (uri->flags & ST_SHLGNDS)?"</u>":"",
2248 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2249
Willy Tarreau295a8372011-03-10 11:25:07 +01002250 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002251 /* Column heading for Enable or Disable server */
2252 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
2253 }
2254
2255 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002256 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002257 "<th colspan=3>Queue</th>"
2258 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002259 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002260 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002261 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002262 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002263 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002264 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002265 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002266 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002267 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002268 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002269 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002270 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2271 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002272 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002273
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002274 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002275 return 0;
2276 }
Willy Tarreau91861262007-10-17 17:06:05 +02002277
Willy Tarreau295a8372011-03-10 11:25:07 +01002278 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002279 /* fall through */
2280
Willy Tarreau295a8372011-03-10 11:25:07 +01002281 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002282 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002283 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002284 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
2285 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002286 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002287 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002288 "<tr class=\"frontend\">");
2289
Willy Tarreau295a8372011-03-10 11:25:07 +01002290 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002291 /* Column sub-heading for Enable or Disable server */
2292 chunk_printf(&msg, "<td></td>");
2293 }
2294
2295 chunk_printf(&msg,
2296 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002297 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002298 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2299 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002300 "",
2301 px->id, px->id);
2302
2303 if (px->mode == PR_MODE_HTTP) {
2304 chunk_printf(&msg,
2305 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01002306 "<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 +01002307 "",
2308 read_freq_ctr(&px->fe_req_per_sec),
2309 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002310 px->fe_counters.p.http.rps_max,
2311 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01002312 LIM2A2(px->fe_sps_lim, "-"));
2313 } else {
2314 chunk_printf(&msg,
2315 /* sessions rate : current, max, limit */
2316 "<td>%s</td><td>%s</td><td>%s</td>"
2317 "",
2318 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002319 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01002320 }
2321
2322 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002323 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002324 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002325 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002326 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002327 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002328
2329 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2330 if (px->mode == PR_MODE_HTTP) {
2331 int i;
2332
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002333 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002334
2335 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002336 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002337
Willy Tarreaueabea072011-09-10 23:29:44 +02002338 chunk_printf(&msg, " other=%lld,", px->fe_counters.p.http.rsp[0]);
2339 chunk_printf(&msg, " intercepted=%lld\"", px->fe_counters.intercepted_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002340 }
2341
2342 chunk_printf(&msg,
2343 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002344 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002345 /* bytes : in, out */
2346 "<td>%s</td><td>%s</td>"
2347 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002348 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002349 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002350 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002351 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002352
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002353 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002354 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002355 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002356 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002357 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002358 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002359 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002360 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002361 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002362 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002363 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002364 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002365 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2366 U2H2(px->fe_counters.failed_req),
Willy Tarreau562515c2011-07-25 08:11:52 +02002367 px->state == PR_STREADY ? "OPEN" :
2368 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002369 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002370 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002371 /* pxid, name, queue cur, queue max, */
2372 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002373 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002374 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002375 /* bytes : in, out */
2376 "%lld,%lld,"
2377 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002378 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002379 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002380 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002381 /* warnings: retries, redispatches */
2382 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002383 /* server status : reflect frontend status */
2384 "%s,"
2385 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002386 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002387 /* pid, iid, sid, throttle, lbtot, tracked, type */
2388 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002389 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002390 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002391 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002392 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002393 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002394 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2395 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2396 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2397 px->fe_counters.failed_req,
Willy Tarreau562515c2011-07-25 08:11:52 +02002398 px->state == PR_STREADY ? "OPEN" :
2399 px->state == PR_STFULL ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002400 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002401 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002402 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002403
2404 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2405 if (px->mode == PR_MODE_HTTP) {
2406 int i;
2407
2408 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002409 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002410
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002411 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002412 } else {
2413 chunk_printf(&msg, ",,,,,,");
2414 }
2415
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002416 /* failed health analyses */
2417 chunk_printf(&msg, ",");
2418
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002419 /* requests : req_rate, req_rate_max, req_tot, */
2420 chunk_printf(&msg, "%u,%u,%lld,",
2421 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002422 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002423
Willy Tarreauae526782010-03-04 20:34:23 +01002424 /* errors: cli_aborts, srv_aborts */
2425 chunk_printf(&msg, ",,");
2426
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002427 /* finish with EOL */
2428 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002429 }
Willy Tarreau91861262007-10-17 17:06:05 +02002430
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002431 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002432 return 0;
2433 }
2434
Willy Tarreau295a8372011-03-10 11:25:07 +01002435 si->applet.ctx.stats.l = px->listen; /* may be NULL */
2436 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002437 /* fall through */
2438
Willy Tarreau295a8372011-03-10 11:25:07 +01002439 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002440 /* stats.l has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002441 for (; si->applet.ctx.stats.l != NULL; si->applet.ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002442 if (buffer_almost_full(rep))
2443 return 0;
2444
Willy Tarreau295a8372011-03-10 11:25:07 +01002445 l = si->applet.ctx.stats.l;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002446 if (!l->counters)
2447 continue;
2448
Willy Tarreau295a8372011-03-10 11:25:07 +01002449 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2450 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002451 break;
2452
Willy Tarreau295a8372011-03-10 11:25:07 +01002453 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002454 continue;
2455 }
2456
Willy Tarreau295a8372011-03-10 11:25:07 +01002457 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002458 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002459 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002460 /* Column sub-heading for Enable or Disable server */
2461 chunk_printf(&msg, "<td></td>");
2462 }
2463 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002464
2465 if (uri->flags&ST_SHLGNDS) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02002466 char str[INET6_ADDRSTRLEN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002467 int port;
2468
Willy Tarreau631f01c2011-09-05 00:36:48 +02002469 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002470
Willy Tarreau631f01c2011-09-05 00:36:48 +02002471 port = get_host_port(&l->addr);
2472 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2473 case AF_INET:
2474 chunk_printf(&msg, "IPv4: %s:%d, ", str, port);
2475 break;
2476 case AF_INET6:
2477 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, port);
2478 break;
2479 case AF_UNIX:
2480 chunk_printf(&msg, "unix, ");
2481 break;
2482 case -1:
2483 chunk_printf(&msg, "(%s), ", strerror(errno));
2484 break;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002485 }
2486
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002487 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002488 chunk_printf(&msg, "id: %d\"", l->luid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002489 }
2490
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002491 chunk_printf(&msg,
2492 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002493 ">%s<a name=\"%s/+%s\"></a>"
2494 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002495 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002496 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002497 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002498 "<td>%s</td><td>%s</td><td>%s</td>"
2499 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002500 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002501 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002502 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002503 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002504 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002505 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002506 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2507 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2508
2509 chunk_printf(&msg,
2510 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002511 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002512 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002513 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002514 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002515 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002516 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002517 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002518 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002519 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002520 "",
2521 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2522 U2H2(l->counters->failed_req),
Willy Tarreaua17c2d92011-07-25 08:16:20 +02002523 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002524 } else {
2525 chunk_printf(&msg,
2526 /* pxid, name, queue cur, queue max, */
2527 "%s,%s,,,"
2528 /* sessions: current, max, limit, total */
2529 "%d,%d,%d,%lld,"
2530 /* bytes: in, out */
2531 "%lld,%lld,"
2532 /* denied: req, resp */
2533 "%lld,%lld,"
2534 /* errors: request, connect, response */
2535 "%lld,,,"
2536 /* warnings: retries, redispatches */
2537 ",,"
2538 /* server status: reflect listener status */
2539 "%s,"
2540 /* rest of server: nothing */
2541 ",,,,,,,,"
2542 /* pid, iid, sid, throttle, lbtot, tracked, type */
2543 "%d,%d,%d,,,,%d,"
2544 /* rate, rate_lim, rate_max */
2545 ",,,"
2546 /* check_status, check_code, check_duration */
2547 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002548 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2549 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002550 /* failed health analyses */
2551 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002552 /* requests : req_rate, req_rate_max, req_tot, */
2553 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002554 /* errors: cli_aborts, srv_aborts */
2555 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002556 "\n",
2557 px->id, l->name,
2558 l->nbconn, l->counters->conn_max,
2559 l->maxconn, l->counters->cum_conn,
2560 l->counters->bytes_in, l->counters->bytes_out,
2561 l->counters->denied_req, l->counters->denied_resp,
2562 l->counters->failed_req,
2563 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2564 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2565 }
2566
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002567 if (bi_putchk(rep, &msg) == -1)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002568 return 0;
2569 }
2570
Willy Tarreau295a8372011-03-10 11:25:07 +01002571 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2572 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002573 /* fall through */
2574
Willy Tarreau295a8372011-03-10 11:25:07 +01002575 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002576 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002577 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002578 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 +02002579
Willy Tarreau4e33d862009-10-11 23:35:10 +02002580 if (buffer_almost_full(rep))
2581 return 0;
2582
Willy Tarreau295a8372011-03-10 11:25:07 +01002583 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002584
Willy Tarreau295a8372011-03-10 11:25:07 +01002585 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2586 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002587 break;
2588
Willy Tarreau295a8372011-03-10 11:25:07 +01002589 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002590 continue;
2591 }
2592
Willy Tarreau44267702011-10-28 15:35:33 +02002593 if (sv->track)
2594 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002595 else
2596 svs = sv;
2597
Willy Tarreau91861262007-10-17 17:06:05 +02002598 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002599 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002600 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002601 else if (svs->state & SRV_RUNNING) {
2602 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002603 sv_state = 3; /* UP */
2604 else
2605 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002606
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002607 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002608 sv_state += 2;
2609 }
Willy Tarreau91861262007-10-17 17:06:05 +02002610 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002611 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002612 sv_state = 1; /* going up */
2613 else
2614 sv_state = 0; /* DOWN */
2615
Willy Tarreau295a8372011-03-10 11:25:07 +01002616 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002617 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002618 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002619 continue;
2620 }
2621
Willy Tarreau295a8372011-03-10 11:25:07 +01002622 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002623 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2624 "UP %d/%d &darr;", "UP",
2625 "NOLB %d/%d &darr;", "NOLB",
2626 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002627 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002628 chunk_printf(&msg,
2629 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002630 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002631 );
2632 }
2633 else {
2634 chunk_printf(&msg,
2635 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002636 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002637 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2638 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002639
Willy Tarreau295a8372011-03-10 11:25:07 +01002640 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002641 chunk_printf(&msg,
2642 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2643 sv->id);
2644 }
2645
2646 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002647
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002648 if (uri->flags&ST_SHLGNDS) {
2649 char str[INET6_ADDRSTRLEN];
2650
Willy Tarreau631f01c2011-09-05 00:36:48 +02002651 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002652
Willy Tarreau631f01c2011-09-05 00:36:48 +02002653 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01002654 case AF_INET:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002655 chunk_printf(&msg, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
David du Colombier6f5ccb12011-03-10 22:26:24 +01002656 break;
2657 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002658 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2659 break;
2660 case AF_UNIX:
2661 chunk_printf(&msg, "unix, ");
2662 break;
2663 case -1:
2664 chunk_printf(&msg, "(%s), ", strerror(errno));
2665 break;
2666 default: /* address family not supported */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002667 break;
2668 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002669
2670 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002671 chunk_printf(&msg, "id: %d", sv->puid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002672
2673 /* cookie */
2674 if (sv->cookie) {
2675 struct chunk src;
2676
2677 chunk_printf(&msg, ", cookie: '");
2678
2679 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2680 chunk_htmlencode(&msg, &src);
2681
2682 chunk_printf(&msg, "'");
2683 }
2684
2685 chunk_printf(&msg, "\"");
2686 }
2687
2688 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002689 ">%s<a name=\"%s/%s\"></a>"
2690 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002691 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002692 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002693 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002694 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002695 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002696 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002697 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002698 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002699 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002700 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002701 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002702 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2703 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002704 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2705
2706 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2707 if (px->mode == PR_MODE_HTTP) {
2708 int i;
2709
2710 chunk_printf(&msg, " title=\"rsp codes:");
2711
2712 for (i = 1; i < 6; i++)
2713 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2714
2715 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2716 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002717
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002718 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002719 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002720 ">%s%s%s</td><td>%s</td>",
2721 (px->mode == PR_MODE_HTTP)?"<u>":"",
2722 U2H0(sv->counters.cum_sess),
2723 (px->mode == PR_MODE_HTTP)?"</u>":"",
2724 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002725
2726 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002727 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002728 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002729 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002730 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002731 /* errors : request, connect */
2732 "<td></td><td>%s</td>"
2733 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002734 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002735 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002736 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002737 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002738 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2739 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002740 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002741 sv->counters.cli_aborts,
2742 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002743 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002744 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002745
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002746 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002747 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002748
Cyril Bontécd19e512010-01-31 22:34:03 +01002749 if (sv->state & SRV_MAINTAIN) {
2750 chunk_printf(&msg, "%s ",
2751 human_time(now.tv_sec - sv->last_change, 1));
2752 chunk_printf(&msg, "MAINT");
2753 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002754 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2755 chunk_printf(&msg, "%s ",
2756 human_time(now.tv_sec - svs->last_change, 1));
2757 chunk_printf(&msg, "MAINT(via)");
2758 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002759 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002760 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002761 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002762
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002763 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002764 srv_hlt_st[sv_state],
2765 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2766 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002767 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002768
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002769 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002770 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002771 get_check_status_description(sv->check_status));
2772
2773 if (*sv->check_desc) {
2774 struct chunk src;
2775
2776 chunk_printf(&msg, ": ");
2777
2778 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2779 chunk_htmlencode(&msg, &src);
2780 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002781
Willy Tarreaue0454092010-02-26 12:29:07 +01002782 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002783 tv_iszero(&sv->check_start)?"":"* ",
2784 get_check_status_info(sv->check_status));
2785
2786 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002787 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002788
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002789 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002790 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002791 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002792 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002793
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002794 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002795 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002796 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002797 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002798 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002799 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002800 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002801 (sv->state & SRV_BACKUP) ? "-" : "Y",
2802 (sv->state & SRV_BACKUP) ? "Y" : "-");
2803
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002804 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002805 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002806 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002807 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2808
2809 if (svs->observe)
2810 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2811
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002812 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002813 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002814 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002815 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002816 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002817 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002818 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002819 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2820 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002821 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002822 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002823 "<td colspan=3></td>");
2824
2825 /* throttle */
2826 if ((sv->state & SRV_WARMINGUP) &&
2827 now.tv_sec < sv->last_change + sv->slowstart &&
2828 now.tv_sec >= sv->last_change) {
2829 unsigned int ratio;
2830 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002831 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002832 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002833 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002834 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002835 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002836 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002837 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002838 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2839 "UP %d/%d,", "UP,",
2840 "NOLB %d/%d,", "NOLB,",
2841 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002842 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002843 /* pxid, name */
2844 "%s,%s,"
2845 /* queue : current, max */
2846 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002847 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002848 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002849 /* bytes : in, out */
2850 "%lld,%lld,"
2851 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002852 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002853 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002854 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002855 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002856 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002857 "",
2858 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002859 sv->nbpend, sv->counters.nbpend_max,
2860 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002861 sv->counters.bytes_in, sv->counters.bytes_out,
2862 sv->counters.failed_secu,
2863 sv->counters.failed_conns, sv->counters.failed_resp,
2864 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002865
Willy Tarreau55bb8452007-10-17 18:44:57 +02002866 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002867 if (sv->state & SRV_MAINTAIN) {
2868 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002869 }
2870 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2871 chunk_printf(&msg, "MAINT(via),");
2872 }
2873 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002874 chunk_printf(&msg,
2875 srv_hlt_st[sv_state],
2876 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2877 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2878 }
Willy Tarreau91861262007-10-17 17:06:05 +02002879
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002880 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002881 /* weight, active, backup */
2882 "%d,%d,%d,"
2883 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002884 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002885 (sv->state & SRV_BACKUP) ? 0 : 1,
2886 (sv->state & SRV_BACKUP) ? 1 : 0);
2887
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002888 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002889 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002890 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002891 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002892 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002893 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002894 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002895 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002896 ",,,,");
2897
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002898 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002899 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002900 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002901 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002902 LIM2A0(sv->maxqueue, ""),
2903 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002904
2905 /* throttle */
2906 if ((sv->state & SRV_WARMINGUP) &&
2907 now.tv_sec < sv->last_change + sv->slowstart &&
2908 now.tv_sec >= sv->last_change) {
2909 unsigned int ratio;
2910 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002911 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002912 }
2913
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002914 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002915 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002916
2917 /* tracked */
Willy Tarreau44267702011-10-28 15:35:33 +02002918 if (sv->track)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002919 chunk_printf(&msg, "%s/%s,",
Willy Tarreau44267702011-10-28 15:35:33 +02002920 sv->track->proxy->id, sv->track->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002921 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002922 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002923
Willy Tarreau7f062c42009-03-05 18:43:00 +01002924 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002925 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002926
2927 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002928 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002929 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002930 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002931
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002932 if (sv->state & SRV_CHECKED) {
2933 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002934 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002935
2936 /* check_code */
2937 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002938 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002939 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002940 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002941
2942 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002943 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002944 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002945 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002946 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002947
2948 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002949 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002950 }
2951
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002952 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2953 if (px->mode == PR_MODE_HTTP) {
2954 int i;
2955
2956 for (i=1; i<6; i++)
2957 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2958
2959 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2960 } else {
2961 chunk_printf(&msg, ",,,,,,");
2962 }
2963
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002964 /* failed health analyses */
2965 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2966
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002967 /* requests : req_rate, req_rate_max, req_tot, */
2968 chunk_printf(&msg, ",,,");
2969
Willy Tarreauae526782010-03-04 20:34:23 +01002970 /* errors: cli_aborts, srv_aborts */
2971 chunk_printf(&msg, "%lld,%lld,",
2972 sv->counters.cli_aborts, sv->counters.srv_aborts);
2973
Willy Tarreau7f062c42009-03-05 18:43:00 +01002974 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002975 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002976 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002977 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002978 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002979 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002980
Willy Tarreau295a8372011-03-10 11:25:07 +01002981 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02002982 /* fall through */
2983
Willy Tarreau295a8372011-03-10 11:25:07 +01002984 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02002985 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002986 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002987 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2988 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002989 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01002990 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002991 /* Column sub-heading for Enable or Disable server */
2992 chunk_printf(&msg, "<td></td>");
2993 }
2994 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002995
2996 if (uri->flags&ST_SHLGNDS) {
2997 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002998 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002999 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003000
3001 /* cookie */
3002 if (px->cookie_name) {
3003 struct chunk src;
3004
3005 chunk_printf(&msg, ", cookie: '");
3006
3007 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3008 chunk_htmlencode(&msg, &src);
3009
3010 chunk_printf(&msg, "'");
3011 }
3012
3013 chunk_printf(&msg, "\"");
3014
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003015 }
3016
3017 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02003018 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01003019 ">%s<a name=\"%s/Backend\"></a>"
3020 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02003021 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003022 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003023 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003024 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003025 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003026 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02003027 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01003028 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003029 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
3030 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02003031
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003032 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003033 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003034 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003035 "<td"
3036 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003037 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003038
3039 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3040 if (px->mode == PR_MODE_HTTP) {
3041 int i;
3042
3043 chunk_printf(&msg, " title=\"rsp codes:");
3044
3045 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003046 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003047
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003048 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003049 }
3050
3051 chunk_printf(&msg,
3052 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01003053 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003054 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003055 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003056 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003057 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003058 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01003059 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003060 U2H7(px->be_counters.cum_lbconn),
3061 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003062
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003063 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02003064 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003065 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01003066 /* errors : request, connect */
3067 "<td></td><td>%s</td>"
3068 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01003069 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003070 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003071 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003072 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02003073 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003074 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02003075 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003076 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3077 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003078 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003079 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
3080 U2H2(px->be_counters.failed_conns),
3081 px->be_counters.cli_aborts,
3082 px->be_counters.srv_aborts,
3083 U2H5(px->be_counters.failed_resp),
3084 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003085 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01003086 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3087 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01003088 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003089 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003090
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003091 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003092 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003093 "<td class=ac>&nbsp;</td><td>%d</td>"
3094 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003095 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003096 "</tr>",
3097 px->down_trans,
3098 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003099 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003100 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02003101 /* pxid, name */
3102 "%s,BACKEND,"
3103 /* queue : current, max */
3104 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003105 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003106 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003107 /* bytes : in, out */
3108 "%lld,%lld,"
3109 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003110 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003111 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003112 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003113 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003114 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003115 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02003116 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003117 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02003118 * active and backups. */
3119 "%s,"
3120 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003121 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003122 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003123 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003124 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003125 /* rate, rate_lim, rate_max, */
3126 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003127 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003128 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02003129 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003130 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3131 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3132 px->be_counters.bytes_in, px->be_counters.bytes_out,
3133 px->be_counters.denied_req, px->be_counters.denied_resp,
3134 px->be_counters.failed_conns, px->be_counters.failed_resp,
3135 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01003136 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01003137 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003138 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003139 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01003140 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003141 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003142 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003143 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003144 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003145
3146 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3147 if (px->mode == PR_MODE_HTTP) {
3148 int i;
3149
3150 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003151 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003152
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003153 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003154 } else {
3155 chunk_printf(&msg, ",,,,,,");
3156 }
3157
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003158 /* failed health analyses */
3159 chunk_printf(&msg, ",");
3160
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003161 /* requests : req_rate, req_rate_max, req_tot, */
3162 chunk_printf(&msg, ",,,");
3163
Willy Tarreauae526782010-03-04 20:34:23 +01003164 /* errors: cli_aborts, srv_aborts */
3165 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003166 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01003167
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003168 /* finish with EOL */
3169 chunk_printf(&msg, "\n");
3170
Willy Tarreau55bb8452007-10-17 18:44:57 +02003171 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003172 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003173 return 0;
3174 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003175
Willy Tarreau295a8372011-03-10 11:25:07 +01003176 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02003177 /* fall through */
3178
Willy Tarreau295a8372011-03-10 11:25:07 +01003179 case STAT_PX_ST_END:
3180 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003181 chunk_printf(&msg, "</table>");
3182
Willy Tarreau295a8372011-03-10 11:25:07 +01003183 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003184 /* close the form used to enable/disable this proxy servers */
3185 chunk_printf(&msg,
3186 "Choose the action to perform on the checked servers : "
3187 "<select name=action>"
3188 "<option value=\"\"></option>"
3189 "<option value=\"disable\">Disable</option>"
3190 "<option value=\"enable\">Enable</option>"
Willy Tarreaud7282242012-06-04 00:22:44 +02003191 "<option value=\"stop\">Soft Stop</option>"
3192 "<option value=\"start\">Soft Start</option>"
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003193 "<option value=\"shutdown\">Kill Sessions</option>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003194 "</select>"
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003195 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003196 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3197 "</form>",
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003198 px->uuid);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003199 }
3200
3201 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02003202
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003203 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003204 return 0;
3205 }
Willy Tarreau91861262007-10-17 17:06:05 +02003206
Willy Tarreau295a8372011-03-10 11:25:07 +01003207 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02003208 /* fall through */
3209
Willy Tarreau295a8372011-03-10 11:25:07 +01003210 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02003211 return 1;
3212
3213 default:
3214 /* unknown state, we should put an abort() here ! */
3215 return 1;
3216 }
3217}
3218
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003219/* This function dumps a complete session state onto the stream intreface's
3220 * read buffer. The data_ctx must have been zeroed first, and the flags
3221 * properly set. The session has to be set in data_ctx.sess.target. It returns
3222 * 0 if the output buffer is full and it needs to be called again, otherwise
3223 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003224 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003225static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003226{
3227 struct tm tm;
3228 struct chunk msg;
3229 struct session *sess;
3230 extern const char *monthname[12];
3231 char pn[INET6_ADDRSTRLEN];
3232
David du Colombier7af46052012-05-16 14:16:48 +02003233 chunk_init(&msg, trash, trashlen);
Willy Tarreau295a8372011-03-10 11:25:07 +01003234 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003235
Willy Tarreau295a8372011-03-10 11:25:07 +01003236 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003237 /* session changed, no need to go any further */
3238 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003239 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003240 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003241 si->applet.ctx.sess.target = NULL;
3242 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003243 return 1;
3244 }
3245
Willy Tarreau295a8372011-03-10 11:25:07 +01003246 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003247 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003248 si->applet.ctx.sess.uid = sess->uniq_id;
3249 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003250 /* fall through */
3251
3252 case 1:
3253 chunk_printf(&msg,
3254 "%p: id=%u, proto=%s",
3255 sess,
3256 sess->uniq_id,
3257 sess->listener->proto->name);
3258
Willy Tarreau6471afb2011-09-23 10:54:59 +02003259 switch (addr_to_str(&sess->si[0].addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003260 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003261 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003262 chunk_printf(&msg, " source=%s:%d\n",
Willy Tarreau6471afb2011-09-23 10:54:59 +02003263 pn, get_host_port(&sess->si[0].addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003264 break;
3265 case AF_UNIX:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003266 chunk_printf(&msg, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003267 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003268 default:
3269 /* no more information to print right now */
3270 chunk_printf(&msg, "\n");
3271 break;
3272 }
3273
3274 chunk_printf(&msg,
3275 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003276 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003277
3278 chunk_printf(&msg,
Daniel Schultze90690c72012-03-23 10:53:36 -07003279 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003280 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3281 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3282 sess->listener ? sess->listener->luid : 0);
3283
Willy Tarreau59b94792012-05-11 16:16:40 +02003284 si_get_to_addr(&sess->si[0]);
Daniel Schultze90690c72012-03-23 10:53:36 -07003285 switch (addr_to_str(&sess->si[0].addr.to, pn, sizeof(pn))) {
3286 case AF_INET:
3287 case AF_INET6:
3288 chunk_printf(&msg, " addr=%s:%d\n",
3289 pn, get_host_port(&sess->si[0].addr.to));
3290 break;
3291 case AF_UNIX:
3292 chunk_printf(&msg, " addr=unix:%d\n", sess->listener->luid);
3293 break;
3294 default:
3295 /* no more information to print right now */
3296 chunk_printf(&msg, "\n");
3297 break;
3298 }
3299
Willy Tarreau50943332011-09-02 17:33:05 +02003300 if (sess->be->cap & PR_CAP_BE)
3301 chunk_printf(&msg,
Daniel Schultze90690c72012-03-23 10:53:36 -07003302 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003303 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003304 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3305 else
3306 chunk_printf(&msg, " backend=<NONE> (id=-1 mode=-)");
3307
Willy Tarreau59b94792012-05-11 16:16:40 +02003308 si_get_from_addr(&sess->si[1]);
Daniel Schultze90690c72012-03-23 10:53:36 -07003309 switch (addr_to_str(&sess->si[1].addr.from, pn, sizeof(pn))) {
3310 case AF_INET:
3311 case AF_INET6:
3312 chunk_printf(&msg, " addr=%s:%d\n",
3313 pn, get_host_port(&sess->si[1].addr.from));
3314 break;
3315 case AF_UNIX:
3316 chunk_printf(&msg, " addr=unix\n");
3317 break;
3318 default:
3319 /* no more information to print right now */
3320 chunk_printf(&msg, "\n");
3321 break;
3322 }
3323
3324 if (sess->be->cap & PR_CAP_BE)
3325 chunk_printf(&msg,
3326 " server=%s (id=%u)",
Willy Tarreau50943332011-09-02 17:33:05 +02003327 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
3328 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
3329 else
Daniel Schultze90690c72012-03-23 10:53:36 -07003330 chunk_printf(&msg, " server=<NONE> (id=-1)");
3331
Willy Tarreau59b94792012-05-11 16:16:40 +02003332 si_get_to_addr(&sess->si[1]);
Daniel Schultze90690c72012-03-23 10:53:36 -07003333 switch (addr_to_str(&sess->si[1].addr.to, pn, sizeof(pn))) {
3334 case AF_INET:
3335 case AF_INET6:
3336 chunk_printf(&msg, " addr=%s:%d\n",
3337 pn, get_host_port(&sess->si[1].addr.to));
3338 break;
3339 case AF_UNIX:
3340 chunk_printf(&msg, " addr=unix\n");
3341 break;
3342 default:
3343 /* no more information to print right now */
3344 chunk_printf(&msg, "\n");
3345 break;
3346 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003347
3348 chunk_printf(&msg,
3349 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
3350 sess->task,
3351 sess->task->state,
3352 sess->task->nice, sess->task->calls,
3353 sess->task->expire ?
3354 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3355 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3356 TICKS_TO_MS(1000)) : "<NEVER>",
3357 task_in_rq(sess->task) ? ", running" : "");
3358
3359 get_localtime(sess->logs.accept_date.tv_sec, &tm);
3360 chunk_printf(&msg,
3361 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
3362 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3363 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3364 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3365
3366 chunk_printf(&msg,
3367 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3368 &sess->si[0],
3369 sess->si[0].state,
3370 sess->si[0].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003371 si_fd(&sess->si[0]),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003372 sess->si[0].exp ?
3373 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3374 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3375 TICKS_TO_MS(1000)) : "<NEVER>",
3376 sess->si[0].err_type);
3377
3378 chunk_printf(&msg,
3379 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3380 &sess->si[1],
3381 sess->si[1].state,
3382 sess->si[1].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003383 si_fd(&sess->si[1]),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003384 sess->si[1].exp ?
3385 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3386 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3387 TICKS_TO_MS(1000)) : "<NEVER>",
3388 sess->si[1].err_type);
3389
3390 chunk_printf(&msg,
3391 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3392 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3393 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3394
3395
3396 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003397 " req=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003398 " an_exp=%s",
3399 sess->req,
3400 sess->req->flags, sess->req->analysers,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003401 sess->req->i, sess->req->o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003402 sess->req->pipe ? sess->req->pipe->data : 0,
3403 sess->req->to_forward,
3404 sess->req->analyse_exp ?
3405 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3406 TICKS_TO_MS(1000)) : "<NEVER>");
3407
3408 chunk_printf(&msg,
3409 " rex=%s",
3410 sess->req->rex ?
3411 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3412 TICKS_TO_MS(1000)) : "<NEVER>");
3413
3414 chunk_printf(&msg,
3415 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003416 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003417 sess->req->wex ?
3418 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3419 TICKS_TO_MS(1000)) : "<NEVER>",
3420 sess->req->data,
Willy Tarreau89fa7062012-03-02 16:13:16 +01003421 (int)(sess->req->p - sess->req->data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003422 sess->txn.req.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003423 sess->req->total);
3424
3425 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003426 " res=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003427 " an_exp=%s",
3428 sess->rep,
3429 sess->rep->flags, sess->rep->analysers,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003430 sess->rep->i, sess->rep->o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003431 sess->rep->pipe ? sess->rep->pipe->data : 0,
3432 sess->rep->to_forward,
3433 sess->rep->analyse_exp ?
3434 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3435 TICKS_TO_MS(1000)) : "<NEVER>");
3436
3437 chunk_printf(&msg,
3438 " rex=%s",
3439 sess->rep->rex ?
3440 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3441 TICKS_TO_MS(1000)) : "<NEVER>");
3442
3443 chunk_printf(&msg,
3444 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003445 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003446 sess->rep->wex ?
3447 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3448 TICKS_TO_MS(1000)) : "<NEVER>",
3449 sess->rep->data,
Willy Tarreau89fa7062012-03-02 16:13:16 +01003450 (int)(sess->rep->p - sess->rep->data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003451 sess->txn.rsp.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003452 sess->rep->total);
3453
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003454 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003455 return 0;
3456
3457 /* use other states to dump the contents */
3458 }
3459 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003460 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003461 return 1;
3462}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003463
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003464/* This function dumps all sessions' states onto the stream intreface's
3465 * read buffer. The data_ctx must have been zeroed first, and the flags
3466 * properly set. It returns 0 if the output buffer is full and it needs
3467 * to be called again, otherwise non-zero. It is designed to be called
3468 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003469 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003470static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003471{
3472 struct chunk msg;
3473
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003474 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003475 /* If we're forced to shut down, we might have to remove our
3476 * reference to the last session being dumped.
3477 */
Willy Tarreau94981132012-05-21 17:09:48 +02003478 if (si->conn.data_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003479 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3480 LIST_DEL(&si->applet.ctx.sess.bref.users);
3481 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003482 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003483 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003484 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003485 }
3486
David du Colombier7af46052012-05-16 14:16:48 +02003487 chunk_init(&msg, trash, trashlen);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003488
Willy Tarreau94981132012-05-21 17:09:48 +02003489 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003490 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003491 /* the function had not been called yet, let's prepare the
3492 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003493 * pointer to the first in the global list. When a target
3494 * session is being destroyed, it is responsible for updating
3495 * this pointer. We know we have reached the end when this
3496 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003497 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003498 LIST_INIT(&si->applet.ctx.sess.bref.users);
3499 si->applet.ctx.sess.bref.ref = sessions.n;
Willy Tarreau94981132012-05-21 17:09:48 +02003500 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003501 /* fall through */
3502
Willy Tarreau295a8372011-03-10 11:25:07 +01003503 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003504 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003505 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3506 LIST_DEL(&si->applet.ctx.sess.bref.users);
3507 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003508 }
3509
3510 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003511 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003512 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003513 struct session *curr_sess;
3514
Willy Tarreau295a8372011-03-10 11:25:07 +01003515 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003516
Willy Tarreau295a8372011-03-10 11:25:07 +01003517 if (si->applet.ctx.sess.target) {
3518 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003519 goto next_sess;
3520
Willy Tarreau295a8372011-03-10 11:25:07 +01003521 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003522 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003523 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003524 return 0;
3525
3526 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003527 LIST_DEL(&si->applet.ctx.sess.bref.users);
3528 LIST_INIT(&si->applet.ctx.sess.bref.users);
3529 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003530 break;
3531 }
3532
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003533 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003534 "%p: proto=%s",
3535 curr_sess,
3536 curr_sess->listener->proto->name);
3537
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003538
Willy Tarreau6471afb2011-09-23 10:54:59 +02003539 switch (addr_to_str(&curr_sess->si[0].addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003540 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003541 case AF_INET6:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003542 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003543 " src=%s:%d fe=%s be=%s srv=%s",
3544 pn,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003545 get_host_port(&curr_sess->si[0].addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003546 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003547 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003548 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003549 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003550 break;
3551 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02003552 chunk_printf(&msg,
3553 " src=unix:%d fe=%s be=%s srv=%s",
3554 curr_sess->listener->luid,
3555 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003556 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003557 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003558 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003559 break;
3560 }
3561
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003562 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003563 " ts=%02x age=%s calls=%d",
3564 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003565 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3566 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003567
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003568 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003569 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003570 curr_sess->req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003571 curr_sess->req->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003572 curr_sess->req->analysers,
3573 curr_sess->req->rex ?
3574 human_time(TICKS_TO_MS(curr_sess->req->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->req->wex ?
3580 human_time(TICKS_TO_MS(curr_sess->req->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->req->analyse_exp ?
3586 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3587 TICKS_TO_MS(1000)) : "");
3588
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003589 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003590 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003591 curr_sess->rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003592 curr_sess->rep->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003593 curr_sess->rep->analysers,
3594 curr_sess->rep->rex ?
3595 human_time(TICKS_TO_MS(curr_sess->rep->rex - 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 ",wx=%s",
3600 curr_sess->rep->wex ?
3601 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3602 TICKS_TO_MS(1000)) : "");
3603
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003604 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003605 ",ax=%s]",
3606 curr_sess->rep->analyse_exp ?
3607 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3608 TICKS_TO_MS(1000)) : "");
3609
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003610 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003611 " s0=[%d,%1xh,fd=%d,ex=%s]",
3612 curr_sess->si[0].state,
3613 curr_sess->si[0].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003614 si_fd(&curr_sess->si[0]),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003615 curr_sess->si[0].exp ?
3616 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3617 TICKS_TO_MS(1000)) : "");
3618
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003619 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003620 " s1=[%d,%1xh,fd=%d,ex=%s]",
3621 curr_sess->si[1].state,
3622 curr_sess->si[1].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003623 si_fd(&curr_sess->si[1]),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003624 curr_sess->si[1].exp ?
3625 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3626 TICKS_TO_MS(1000)) : "");
3627
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003628 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003629 " exp=%s",
3630 curr_sess->task->expire ?
3631 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3632 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003633 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003634 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003635
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003636 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003637
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003638 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003639 /* let's try again later from this session. We add ourselves into
3640 * this session's users so that it can remove us upon termination.
3641 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003642 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003643 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003644 }
3645
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003646 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003647 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003648 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003649
Willy Tarreau295a8372011-03-10 11:25:07 +01003650 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003651 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003652 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003653 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3654 else
3655 chunk_printf(&msg, "Session not found.\n");
3656
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003657 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003658 return 0;
3659
Willy Tarreau295a8372011-03-10 11:25:07 +01003660 si->applet.ctx.sess.target = NULL;
3661 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003662 return 1;
3663 }
3664
Willy Tarreau94981132012-05-21 17:09:48 +02003665 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003666 /* fall through */
3667
3668 default:
Willy Tarreau94981132012-05-21 17:09:48 +02003669 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003670 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003671 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003672}
3673
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003674/* This function dumps all tables' states onto the stream intreface's
3675 * read buffer. The data_ctx must have been zeroed first, and the flags
3676 * properly set. It returns 0 if the output buffer is full and it needs
3677 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003678 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003679static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003680{
Willy Tarreau94981132012-05-21 17:09:48 +02003681 struct session *s = si->conn.data_ctx;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003682 struct chunk msg;
3683 struct ebmb_node *eb;
3684 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003685 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003686
3687 /*
Willy Tarreau94981132012-05-21 17:09:48 +02003688 * We have 3 possible states in si->conn.data_st :
Willy Tarreau295a8372011-03-10 11:25:07 +01003689 * - STAT_ST_INIT : the first call
3690 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003691 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003692 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003693 * and the entry pointer points to the next entry to be dumped,
3694 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003695 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003696 * data though.
3697 */
3698
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003699 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003700 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau94981132012-05-21 17:09:48 +02003701 if (si->conn.data_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003702 si->applet.ctx.table.entry->ref_cnt--;
3703 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003704 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003705 return 1;
3706 }
3707
David du Colombier7af46052012-05-16 14:16:48 +02003708 chunk_init(&msg, trash, trashlen);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003709
Willy Tarreau94981132012-05-21 17:09:48 +02003710 while (si->conn.data_st != STAT_ST_FIN) {
3711 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003712 case STAT_ST_INIT:
3713 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3714 if (!si->applet.ctx.table.proxy)
3715 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003716
Willy Tarreau295a8372011-03-10 11:25:07 +01003717 si->applet.ctx.table.entry = NULL;
Willy Tarreau94981132012-05-21 17:09:48 +02003718 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003719 break;
3720
Willy Tarreau295a8372011-03-10 11:25:07 +01003721 case STAT_ST_INFO:
3722 if (!si->applet.ctx.table.proxy ||
3723 (si->applet.ctx.table.target &&
3724 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
Willy Tarreau94981132012-05-21 17:09:48 +02003725 si->conn.data_st = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003726 break;
3727 }
3728
Willy Tarreau295a8372011-03-10 11:25:07 +01003729 if (si->applet.ctx.table.proxy->table.size) {
Simon Hormanc88b8872011-06-15 15:18:49 +09003730 if (show && !stats_dump_table_head_to_buffer(&msg, si, si->applet.ctx.table.proxy,
3731 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003732 return 0;
3733
Willy Tarreau295a8372011-03-10 11:25:07 +01003734 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003735 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
3736 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003737 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003738 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003739 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3740 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau94981132012-05-21 17:09:48 +02003741 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003742 break;
3743 }
3744 }
3745 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003746 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003747 break;
3748
Willy Tarreau295a8372011-03-10 11:25:07 +01003749 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003750 skip_entry = false;
3751
Willy Tarreau295a8372011-03-10 11:25:07 +01003752 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003753 /* we're filtering on some data contents */
3754 void *ptr;
3755 long long data;
3756
Willy Tarreau295a8372011-03-10 11:25:07 +01003757 dt = si->applet.ctx.table.data_type;
3758 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3759 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003760 dt);
3761
3762 data = 0;
3763 switch (stktable_data_types[dt].std_type) {
3764 case STD_T_SINT:
3765 data = stktable_data_cast(ptr, std_t_sint);
3766 break;
3767 case STD_T_UINT:
3768 data = stktable_data_cast(ptr, std_t_uint);
3769 break;
3770 case STD_T_ULL:
3771 data = stktable_data_cast(ptr, std_t_ull);
3772 break;
3773 case STD_T_FRQP:
3774 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003775 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003776 break;
3777 }
3778
3779 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003780 if ((data < si->applet.ctx.table.value &&
3781 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3782 si->applet.ctx.table.data_op == STD_OP_GT ||
3783 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3784 (data == si->applet.ctx.table.value &&
3785 (si->applet.ctx.table.data_op == STD_OP_NE ||
3786 si->applet.ctx.table.data_op == STD_OP_GT ||
3787 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3788 (data > si->applet.ctx.table.value &&
3789 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3790 si->applet.ctx.table.data_op == STD_OP_LT ||
3791 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003792 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003793 }
3794
Simon Hormanc88b8872011-06-15 15:18:49 +09003795 if (show && !skip_entry &&
3796 !stats_dump_table_entry_to_buffer(&msg, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003797 si->applet.ctx.table.entry))
3798 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003799
Willy Tarreau295a8372011-03-10 11:25:07 +01003800 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003801
Willy Tarreau295a8372011-03-10 11:25:07 +01003802 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003803 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003804 struct stksess *old = si->applet.ctx.table.entry;
3805 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01003806 if (show)
3807 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3808 else
3809 stksess_kill(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau295a8372011-03-10 11:25:07 +01003810 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003811 break;
3812 }
3813
Simon Hormanc88b8872011-06-15 15:18:49 +09003814
3815 if (show)
3816 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3817 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3818 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3819
Willy Tarreau295a8372011-03-10 11:25:07 +01003820 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau94981132012-05-21 17:09:48 +02003821 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003822 break;
3823
Willy Tarreau295a8372011-03-10 11:25:07 +01003824 case STAT_ST_END:
Willy Tarreau94981132012-05-21 17:09:48 +02003825 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003826 break;
3827 }
3828 }
3829 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003830}
3831
Willy Tarreaud426a182010-03-05 14:58:26 +01003832/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003833 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3834 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3835 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3836 * lines are respected within the limit of 70 output chars. Lines that are
3837 * continuation of a previous truncated line begin with "+" instead of " "
3838 * after the offset. The new pointer is returned.
3839 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003840static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3841 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003842{
3843 int end;
3844 unsigned char c;
3845
3846 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003847 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003848 return ptr;
3849
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003850 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003851
Willy Tarreaud426a182010-03-05 14:58:26 +01003852 while (ptr < len && ptr < bsize) {
3853 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003854 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003855 if (out->len > end - 2)
3856 break;
3857 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003858 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003859 if (out->len > end - 3)
3860 break;
3861 out->str[out->len++] = '\\';
3862 switch (c) {
3863 case '\t': c = 't'; break;
3864 case '\n': c = 'n'; break;
3865 case '\r': c = 'r'; break;
3866 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003867 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003868 }
3869 out->str[out->len++] = c;
3870 } else {
3871 if (out->len > end - 5)
3872 break;
3873 out->str[out->len++] = '\\';
3874 out->str[out->len++] = 'x';
3875 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3876 out->str[out->len++] = hextab[c & 0xF];
3877 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003878 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003879 /* we had a line break, let's return now */
3880 out->str[out->len++] = '\n';
3881 *line = ptr;
3882 return ptr;
3883 }
3884 }
3885 /* we have an incomplete line, we return it as-is */
3886 out->str[out->len++] = '\n';
3887 return ptr;
3888}
3889
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003890/* This function dumps all captured errors onto the stream intreface's
3891 * read buffer. The data_ctx must have been zeroed first, and the flags
3892 * properly set. It returns 0 if the output buffer is full and it needs
3893 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003894 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003895static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003896{
3897 extern const char *monthname[12];
3898 struct chunk msg;
3899
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003900 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003901 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003902
David du Colombier7af46052012-05-16 14:16:48 +02003903 chunk_init(&msg, trash, trashlen);
Willy Tarreau74808cb2009-03-04 15:53:18 +01003904
Willy Tarreau295a8372011-03-10 11:25:07 +01003905 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003906 /* the function had not been called yet, let's prepare the
3907 * buffer for a response.
3908 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003909 struct tm tm;
3910
3911 get_localtime(date.tv_sec, &tm);
3912 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3913 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3914 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3915 error_snapshot_id);
3916
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003917 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003918 /* Socket buffer full. Let's try again later from the same point */
3919 return 0;
3920 }
3921
Willy Tarreau295a8372011-03-10 11:25:07 +01003922 si->applet.ctx.errors.px = proxy;
3923 si->applet.ctx.errors.buf = 0;
3924 si->applet.ctx.errors.bol = 0;
3925 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003926 }
3927
3928 /* we have two inner loops here, one for the proxy, the other one for
3929 * the buffer.
3930 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003931 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003932 struct error_snapshot *es;
3933
Willy Tarreau295a8372011-03-10 11:25:07 +01003934 if (si->applet.ctx.errors.buf == 0)
3935 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003936 else
Willy Tarreau295a8372011-03-10 11:25:07 +01003937 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003938
3939 if (!es->when.tv_sec)
3940 goto next;
3941
Willy Tarreau295a8372011-03-10 11:25:07 +01003942 if (si->applet.ctx.errors.iid >= 0 &&
3943 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
3944 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003945 goto next;
3946
Willy Tarreau295a8372011-03-10 11:25:07 +01003947 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003948 /* just print headers now */
3949
3950 char pn[INET6_ADDRSTRLEN];
3951 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003952 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003953
3954 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003955 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003956 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003957 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003958
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003959 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
3960 case AF_INET:
3961 case AF_INET6:
3962 port = get_host_port(&es->src);
3963 break;
3964 default:
3965 port = 0;
3966 }
3967
Willy Tarreau295a8372011-03-10 11:25:07 +01003968 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003969 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003970 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003971 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003972 " backend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01003973 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003974 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3975 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01003976 break;
3977 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003978 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003979 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003980 " frontend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01003981 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003982 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01003983 break;
3984 }
3985
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003986 chunk_printf(&msg,
3987 ", server %s (#%d), event #%u\n"
3988 " src %s:%d, session #%d, session flags 0x%08x\n"
3989 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
3990 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
3991 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
3992 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
3993 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
3994 es->ev_id,
3995 pn, port, es->sid, es->s_flags,
3996 es->state, es->m_flags, es->t_flags,
3997 es->m_clen, es->m_blen,
3998 es->b_flags, es->b_out, es->b_tot,
3999 es->len, es->b_wrap, es->pos);
4000
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004001 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004002 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004003 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004004 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004005 si->applet.ctx.errors.ptr = 0;
4006 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004007 }
4008
Willy Tarreau295a8372011-03-10 11:25:07 +01004009 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004010 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004011 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004012 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004013 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004014 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004015 goto next;
4016 }
4017
4018 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01004019 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004020 int newptr;
4021 int newline;
4022
Willy Tarreau295a8372011-03-10 11:25:07 +01004023 newline = si->applet.ctx.errors.bol;
4024 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
4025 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004026 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004027
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004028 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004029 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004030 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004031 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004032 si->applet.ctx.errors.ptr = newptr;
4033 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004034 };
4035 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01004036 si->applet.ctx.errors.bol = 0;
4037 si->applet.ctx.errors.ptr = -1;
4038 si->applet.ctx.errors.buf++;
4039 if (si->applet.ctx.errors.buf > 1) {
4040 si->applet.ctx.errors.buf = 0;
4041 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004042 }
4043 }
4044
4045 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004046 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004047}
4048
Willy Tarreaub24281b2011-02-13 13:16:36 +01004049struct si_applet http_stats_applet = {
4050 .name = "<STATS>", /* used for logging */
4051 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004052 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004053};
4054
Simon Horman9bd2c732011-06-15 15:18:44 +09004055static struct si_applet cli_applet = {
Willy Tarreaub24281b2011-02-13 13:16:36 +01004056 .name = "<CLI>", /* used for logging */
4057 .fct = cli_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004058 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004059};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004060
Willy Tarreau10522fd2008-07-09 20:12:41 +02004061static struct cfg_kw_list cfg_kws = {{ },{
4062 { CFG_GLOBAL, "stats", stats_parse_global },
4063 { 0, NULL, NULL },
4064}};
4065
4066__attribute__((constructor))
4067static void __dumpstats_module_init(void)
4068{
4069 cfg_register_keywords(&cfg_kws);
4070}
4071
Willy Tarreau91861262007-10-17 17:06:05 +02004072/*
4073 * Local variables:
4074 * c-indent-level: 8
4075 * c-basic-offset: 8
4076 * End:
4077 */