blob: 14d50e1c67c76a82d07c67c23a1e8edb7b976a9d [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
Simon Horman17bce342011-06-15 15:18:47 +0900508static void stats_sock_table_key_request(struct stream_interface *si, char **args, bool show)
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)];
Simon Horman121f3052011-06-15 15:18:46 +0900515
Simon Hormand5b9fd92011-06-15 15:18:48 +0900516 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900517
518 if (!*args[4]) {
519 si->applet.ctx.cli.msg = "Key value expected\n";
520 si->applet.st0 = STAT_CLI_PRINT;
521 return;
522 }
523
Simon Hormanc5b89f62011-06-15 15:18:50 +0900524 switch (px->table.type) {
525 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900526 uint32_key = htonl(inetaddr_host(args[4]));
527 static_table_key.key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900528 break;
529 case STKTABLE_TYPE_IPV6:
530 inet_pton(AF_INET6, args[4], ip6_key);
531 static_table_key.key = &ip6_key;
532 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900533 case STKTABLE_TYPE_INTEGER:
534 {
535 char *endptr;
536 unsigned long val;
537 errno = 0;
538 val = strtoul(args[4], &endptr, 10);
539 if ((errno == ERANGE && val == ULONG_MAX) ||
540 (errno != 0 && val == 0) || endptr == args[4] ||
541 val > 0xffffffff) {
542 si->applet.ctx.cli.msg = "Invalid key\n";
543 si->applet.st0 = STAT_CLI_PRINT;
544 return;
545 }
546 uint32_key = (uint32_t) val;
547 static_table_key.key = &uint32_key;
548 break;
549 }
550 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900551 case STKTABLE_TYPE_STRING:
552 static_table_key.key = args[4];
553 static_table_key.key_len = strlen(args[4]);
554 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900555 default:
Simon Horman17bce342011-06-15 15:18:47 +0900556 if (show)
Simon Hormancec9a222011-06-15 15:18:51 +0900557 si->applet.ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6 and integer is not supported\n";
Simon Horman17bce342011-06-15 15:18:47 +0900558 else
Simon Hormancec9a222011-06-15 15:18:51 +0900559 si->applet.ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6 and integer is not supported\n";
Simon Horman121f3052011-06-15 15:18:46 +0900560 si->applet.st0 = STAT_CLI_PRINT;
561 return;
562 }
563
564 /* check permissions */
565 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
566 si->applet.ctx.cli.msg = stats_permission_denied_msg;
567 si->applet.st0 = STAT_CLI_PRINT;
568 return;
569 }
570
571 ts = stktable_lookup_key(&px->table, &static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900572 if (!ts)
573 return;
574
575 if (show) {
576 struct chunk msg;
David du Colombier7af46052012-05-16 14:16:48 +0200577 chunk_init(&msg, trash, trashlen);
Simon Horman17bce342011-06-15 15:18:47 +0900578 if (!stats_dump_table_head_to_buffer(&msg, si, px, px))
579 return;
580 stats_dump_table_entry_to_buffer(&msg, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900581 return;
582 }
Simon Horman17bce342011-06-15 15:18:47 +0900583
584 if (ts->ref_cnt) {
Simon Horman121f3052011-06-15 15:18:46 +0900585 /* don't delete an entry which is currently referenced */
586 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
587 si->applet.st0 = STAT_CLI_PRINT;
588 return;
589 }
590
591 stksess_kill(&px->table, ts);
592}
593
Simon Hormand5b9fd92011-06-15 15:18:48 +0900594static void stats_sock_table_data_request(struct stream_interface *si, char **args)
595{
596 /* condition on stored data value */
597 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
598 if (si->applet.ctx.table.data_type < 0) {
599 si->applet.ctx.cli.msg = "Unknown data type\n";
600 si->applet.st0 = STAT_CLI_PRINT;
601 return;
602 }
603
604 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
605 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
606 si->applet.st0 = STAT_CLI_PRINT;
607 return;
608 }
609
610 si->applet.ctx.table.data_op = get_std_op(args[4]);
611 if (si->applet.ctx.table.data_op < 0) {
612 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
613 si->applet.st0 = STAT_CLI_PRINT;
614 return;
615 }
616
617 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
618 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
619 si->applet.st0 = STAT_CLI_PRINT;
620 return;
621 }
622}
623
624static void stats_sock_table_request(struct stream_interface *si, char **args, bool show)
625{
626 si->applet.ctx.table.data_type = -1;
Willy Tarreau94981132012-05-21 17:09:48 +0200627 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200628 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900629 si->applet.ctx.table.proxy = NULL;
630 si->applet.ctx.table.entry = NULL;
Simon Hormanc88b8872011-06-15 15:18:49 +0900631 if (show)
632 si->applet.st0 = STAT_CLI_O_TAB;
633 else
634 si->applet.st0 = STAT_CLI_O_CLR;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900635
636 if (*args[2]) {
637 si->applet.ctx.table.target = find_stktable(args[2]);
638 if (!si->applet.ctx.table.target) {
639 si->applet.ctx.cli.msg = "No such table\n";
640 si->applet.st0 = STAT_CLI_PRINT;
641 return;
642 }
643 }
644 else {
645 if (!show)
646 goto err_args;
647 return;
648 }
649
650 if (strcmp(args[3], "key") == 0)
651 stats_sock_table_key_request(si, args, show);
Simon Hormanc88b8872011-06-15 15:18:49 +0900652 else if (strncmp(args[3], "data.", 5) == 0)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900653 stats_sock_table_data_request(si, args);
Simon Hormanc88b8872011-06-15 15:18:49 +0900654 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900655 goto err_args;
656
657 return;
658
659err_args:
660 if (show)
661 si->applet.ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
662 else
Simon Hormanc88b8872011-06-15 15:18:49 +0900663 si->applet.ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
Simon Hormand5b9fd92011-06-15 15:18:48 +0900664 si->applet.st0 = STAT_CLI_PRINT;
665}
666
Willy Tarreau532a4502011-09-07 22:37:44 +0200667/* Expects to find a frontend named <arg> and returns it, otherwise displays various
668 * adequate error messages and returns NULL. This function also expects the session
669 * level to be admin.
670 */
671static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
672{
673 struct proxy *px;
674
675 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
676 si->applet.ctx.cli.msg = stats_permission_denied_msg;
677 si->applet.st0 = STAT_CLI_PRINT;
678 return NULL;
679 }
680
681 if (!*arg) {
682 si->applet.ctx.cli.msg = "A frontend name is expected.\n";
683 si->applet.st0 = STAT_CLI_PRINT;
684 return NULL;
685 }
686
687 px = findproxy(arg, PR_CAP_FE);
688 if (!px) {
689 si->applet.ctx.cli.msg = "No such frontend.\n";
690 si->applet.st0 = STAT_CLI_PRINT;
691 return NULL;
692 }
693 return px;
694}
695
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200696/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
697 * and returns the pointer to the server. Otherwise, display adequate error messages
698 * and returns NULL. This function also expects the session level to be admin. Note:
699 * the <arg> is modified to remove the '/'.
700 */
701static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
702{
703 struct proxy *px;
704 struct server *sv;
705 char *line;
706
707 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
708 si->applet.ctx.cli.msg = stats_permission_denied_msg;
709 si->applet.st0 = STAT_CLI_PRINT;
710 return NULL;
711 }
712
713 /* split "backend/server" and make <line> point to server */
714 for (line = arg; *line; line++)
715 if (*line == '/') {
716 *line++ = '\0';
717 break;
718 }
719
720 if (!*line || !*arg) {
721 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
722 si->applet.st0 = STAT_CLI_PRINT;
723 return NULL;
724 }
725
726 if (!get_backend_server(arg, line, &px, &sv)) {
727 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
728 si->applet.st0 = STAT_CLI_PRINT;
729 return NULL;
730 }
731
732 if (px->state == PR_STSTOPPED) {
733 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
734 si->applet.st0 = STAT_CLI_PRINT;
735 return NULL;
736 }
737
738 return sv;
739}
740
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200741/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200742 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100743 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200744 * designating the function which will have to process the request, which can
745 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200746 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900747static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200748{
Willy Tarreau94981132012-05-21 17:09:48 +0200749 struct session *s = si->conn.data_ctx;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200750 char *args[MAX_STATS_ARGS + 1];
751 int arg;
752
753 while (isspace((unsigned char)*line))
754 line++;
755
756 arg = 0;
757 args[arg] = line;
758
759 while (*line && arg < MAX_STATS_ARGS) {
760 if (isspace((unsigned char)*line)) {
761 *line++ = '\0';
762
763 while (isspace((unsigned char)*line))
764 line++;
765
766 args[++arg] = line;
767 continue;
768 }
769
770 line++;
771 }
772
773 while (++arg <= MAX_STATS_ARGS)
774 args[arg] = line;
775
Willy Tarreau295a8372011-03-10 11:25:07 +0100776 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200777 if (strcmp(args[0], "show") == 0) {
778 if (strcmp(args[1], "stat") == 0) {
779 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100780 si->applet.ctx.stats.flags |= STAT_BOUND;
781 si->applet.ctx.stats.iid = atoi(args[2]);
782 si->applet.ctx.stats.type = atoi(args[3]);
783 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200784 }
785
Willy Tarreau295a8372011-03-10 11:25:07 +0100786 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
787 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau94981132012-05-21 17:09:48 +0200788 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100789 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200790 }
791 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100792 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
793 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau94981132012-05-21 17:09:48 +0200794 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100795 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200796 }
797 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau94981132012-05-21 17:09:48 +0200798 si->conn.data_st = STAT_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200799 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100800 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100801 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200802 return 1;
803 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100804 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100805 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100806 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100807 si->applet.ctx.sess.target = NULL;
808 si->applet.ctx.sess.section = 0; /* start with session status */
809 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100810 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200811 }
812 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200813 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100814 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100815 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200816 return 1;
817 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200818 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100819 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200820 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100821 si->applet.ctx.errors.iid = -1;
822 si->applet.ctx.errors.px = NULL;
Willy Tarreau94981132012-05-21 17:09:48 +0200823 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100824 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200825 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200826 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900827 stats_sock_table_request(si, args, true);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200828 }
Aman Guptaceafb4a2012-04-02 18:57:54 -0700829 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200830 return 0;
831 }
832 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200833 else if (strcmp(args[0], "clear") == 0) {
834 if (strcmp(args[1], "counters") == 0) {
835 struct proxy *px;
836 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200837 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200838 int clrall = 0;
839
840 if (strcmp(args[2], "all") == 0)
841 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200842
Willy Tarreau6162db22009-10-10 17:13:00 +0200843 /* check permissions */
844 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
845 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100846 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100847 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200848 return 1;
849 }
850
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200851 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100852 if (clrall) {
853 memset(&px->be_counters, 0, sizeof(px->be_counters));
854 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
855 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200856 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100857 px->be_counters.conn_max = 0;
858 px->be_counters.p.http.rps_max = 0;
859 px->be_counters.sps_max = 0;
860 px->be_counters.cps_max = 0;
861 px->be_counters.nbpend_max = 0;
862
863 px->fe_counters.conn_max = 0;
864 px->fe_counters.p.http.rps_max = 0;
865 px->fe_counters.sps_max = 0;
866 px->fe_counters.cps_max = 0;
867 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200868 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200869
870 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200871 if (clrall)
872 memset(&sv->counters, 0, sizeof(sv->counters));
873 else {
874 sv->counters.cur_sess_max = 0;
875 sv->counters.nbpend_max = 0;
876 sv->counters.sps_max = 0;
877 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200878
879 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200880 if (li->counters) {
881 if (clrall)
882 memset(li->counters, 0, sizeof(*li->counters));
883 else
884 li->counters->conn_max = 0;
885 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200886 }
887
Willy Tarreau81c25d02011-09-07 15:17:21 +0200888 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200889 return 1;
890 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200891 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900892 stats_sock_table_request(si, args, false);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200893 /* end of processing */
894 return 1;
895 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200896 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200897 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200898 return 0;
899 }
900 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200901 else if (strcmp(args[0], "get") == 0) {
902 if (strcmp(args[1], "weight") == 0) {
903 struct proxy *px;
904 struct server *sv;
905
906 /* split "backend/server" and make <line> point to server */
907 for (line = args[2]; *line; line++)
908 if (*line == '/') {
909 *line++ = '\0';
910 break;
911 }
912
913 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100914 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100915 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200916 return 1;
917 }
918
919 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100920 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100921 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200922 return 1;
923 }
924
925 /* return server's effective weight at the moment */
David du Colombier7af46052012-05-16 14:16:48 +0200926 snprintf(trash, trashlen, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200927 bi_putstr(si->ib, trash);
Willy Tarreau38338fa2009-10-10 18:37:29 +0200928 return 1;
929 }
930 else { /* not "get weight" */
931 return 0;
932 }
933 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200934 else if (strcmp(args[0], "set") == 0) {
935 if (strcmp(args[1], "weight") == 0) {
936 struct proxy *px;
937 struct server *sv;
938 int w;
939
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200940 sv = expect_server_admin(s, si, args[2]);
941 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +0200942 return 1;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200943 px = sv->proxy;
Willy Tarreau4483d432009-10-10 19:30:08 +0200944
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200945 /* if the weight is terminated with '%', it is set relative to
946 * the initial weight, otherwise it is absolute.
947 */
948 if (!*args[3]) {
949 si->applet.ctx.cli.msg = "Require <weight> or <weight%>.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100950 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100951 return 1;
952 }
953
Willy Tarreau4483d432009-10-10 19:30:08 +0200954 w = atoi(args[3]);
955 if (strchr(args[3], '%') != NULL) {
956 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100957 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100958 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200959 return 1;
960 }
961 w = sv->iweight * w / 100;
962 }
963 else {
964 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100965 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100966 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200967 return 1;
968 }
969 }
970
971 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100972 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 +0100973 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200974 return 1;
975 }
976
977 sv->uweight = w;
978
979 if (px->lbprm.algo & BE_LB_PROP_DYN) {
980 /* we must take care of not pushing the server to full throttle during slow starts */
981 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
982 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
983 else
984 sv->eweight = BE_WEIGHT_SCALE;
985 sv->eweight *= sv->uweight;
986 } else {
987 sv->eweight = sv->uweight;
988 }
989
990 /* static LB algorithms are a bit harder to update */
991 if (px->lbprm.update_server_eweight)
992 px->lbprm.update_server_eweight(sv);
Willy Tarreau9580d162012-05-19 19:07:40 +0200993 else if (sv->eweight) {
994 if (px->lbprm.set_server_status_up)
995 px->lbprm.set_server_status_up(sv);
996 }
997 else {
998 if (px->lbprm.set_server_status_down)
999 px->lbprm.set_server_status_down(sv);
1000 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001001
1002 return 1;
1003 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001004 else if (strcmp(args[1], "timeout") == 0) {
1005 if (strcmp(args[2], "cli") == 0) {
1006 unsigned timeout;
1007 const char *res;
1008
1009 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001010 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001011 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001012 return 1;
1013 }
1014
1015 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1016 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001017 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001018 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001019 return 1;
1020 }
1021
1022 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1023 return 1;
1024 }
1025 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001026 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001027 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001028 return 1;
1029 }
1030 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001031 else if (strcmp(args[1], "maxconn") == 0) {
1032 if (strcmp(args[2], "frontend") == 0) {
1033 struct proxy *px;
1034 struct listener *l;
1035 int v;
1036
Willy Tarreau532a4502011-09-07 22:37:44 +02001037 px = expect_frontend_admin(s, si, args[3]);
1038 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001039 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001040
1041 if (!*args[4]) {
1042 si->applet.ctx.cli.msg = "Integer value expected.\n";
1043 si->applet.st0 = STAT_CLI_PRINT;
1044 return 1;
1045 }
1046
1047 v = atoi(args[4]);
1048 /* check for unlimited values, we restore default setting (cfg_maxpconn) */
1049 if (v < 1) {
1050 si->applet.ctx.cli.msg = "Value out of range.\n";
1051 si->applet.st0 = STAT_CLI_PRINT;
1052 return 1;
1053 }
1054
1055 /* OK, the value is fine, so we assign it to the proxy and to all of
1056 * its listeners. The blocked ones will be dequeued.
1057 */
1058 px->maxconn = v;
1059 for (l = px->listen; l != NULL; l = l->next) {
1060 l->maxconn = v;
1061 if (l->state == LI_FULL)
1062 resume_listener(l);
1063 }
1064
1065 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1066 dequeue_all_listeners(&s->fe->listener_queue);
1067
1068 return 1;
1069 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001070 else if (strcmp(args[2], "global") == 0) {
1071 int v;
1072
1073 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1074 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1075 si->applet.st0 = STAT_CLI_PRINT;
1076 return 1;
1077 }
1078
1079 if (!*args[3]) {
1080 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1081 si->applet.st0 = STAT_CLI_PRINT;
1082 return 1;
1083 }
1084
1085 v = atoi(args[3]);
1086 if (v > global.hardmaxconn) {
1087 si->applet.ctx.cli.msg = "Value out of range.\n";
1088 si->applet.st0 = STAT_CLI_PRINT;
1089 return 1;
1090 }
1091
1092 /* check for unlimited values */
1093 if (v <= 0)
1094 v = global.hardmaxconn;
1095
1096 global.maxconn = v;
1097
1098 /* Dequeues all of the listeners waiting for a resource */
1099 if (!LIST_ISEMPTY(&global_listener_queue))
1100 dequeue_all_listeners(&global_listener_queue);
1101
1102 return 1;
1103 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001104 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001105 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001106 si->applet.st0 = STAT_CLI_PRINT;
1107 return 1;
1108 }
1109 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001110 else if (strcmp(args[1], "rate-limit") == 0) {
1111 if (strcmp(args[2], "connections") == 0) {
1112 if (strcmp(args[3], "global") == 0) {
1113 int v;
1114
1115 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1116 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1117 si->applet.st0 = STAT_CLI_PRINT;
1118 return 1;
1119 }
1120
1121 if (!*args[4]) {
1122 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1123 si->applet.st0 = STAT_CLI_PRINT;
1124 return 1;
1125 }
1126
1127 v = atoi(args[4]);
1128 if (v < 0) {
1129 si->applet.ctx.cli.msg = "Value out of range.\n";
1130 si->applet.st0 = STAT_CLI_PRINT;
1131 return 1;
1132 }
1133
1134 global.cps_lim = v;
1135
1136 /* Dequeues all of the listeners waiting for a resource */
1137 if (!LIST_ISEMPTY(&global_listener_queue))
1138 dequeue_all_listeners(&global_listener_queue);
1139
1140 return 1;
1141 }
1142 else {
1143 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1144 si->applet.st0 = STAT_CLI_PRINT;
1145 return 1;
1146 }
1147 }
1148 else {
1149 si->applet.ctx.cli.msg = "'set rate-limit' only supports 'connections'.\n";
1150 si->applet.st0 = STAT_CLI_PRINT;
1151 return 1;
1152 }
1153 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001154 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001155 return 0;
1156 }
1157 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001158 else if (strcmp(args[0], "enable") == 0) {
1159 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001160 struct server *sv;
1161
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001162 sv = expect_server_admin(s, si, args[2]);
1163 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001164 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001165
Cyril Bontécd19e512010-01-31 22:34:03 +01001166 if (sv->state & SRV_MAINTAIN) {
1167 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001168 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001169 /* If this server tracks the status of another one,
1170 * we must restore the good status.
1171 */
Willy Tarreau44267702011-10-28 15:35:33 +02001172 if (sv->track->state & SRV_RUNNING) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001173 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001174 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001175 } else {
1176 sv->state &= ~SRV_MAINTAIN;
1177 set_server_down(sv);
1178 }
1179 } else {
1180 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001181 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001182 }
1183 }
1184
Willy Tarreau532a4502011-09-07 22:37:44 +02001185 return 1;
1186 }
1187 else if (strcmp(args[1], "frontend") == 0) {
1188 struct proxy *px;
1189
1190 px = expect_frontend_admin(s, si, args[2]);
1191 if (!px)
1192 return 1;
1193
1194 if (px->state == PR_STSTOPPED) {
1195 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1196 si->applet.st0 = STAT_CLI_PRINT;
1197 return 1;
1198 }
1199
1200 if (px->state != PR_STPAUSED) {
1201 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1202 si->applet.st0 = STAT_CLI_PRINT;
1203 return 1;
1204 }
1205
1206 if (!resume_proxy(px)) {
1207 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1208 si->applet.st0 = STAT_CLI_PRINT;
1209 return 1;
1210 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001211 return 1;
1212 }
1213 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001214 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1215 si->applet.st0 = STAT_CLI_PRINT;
1216 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001217 }
1218 }
1219 else if (strcmp(args[0], "disable") == 0) {
1220 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001221 struct server *sv;
1222
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001223 sv = expect_server_admin(s, si, args[2]);
1224 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001225 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001226
Cyril Bontécd19e512010-01-31 22:34:03 +01001227 if (! (sv->state & SRV_MAINTAIN)) {
1228 /* Not already in maintenance, we can change the server state */
1229 sv->state |= SRV_MAINTAIN;
1230 set_server_down(sv);
1231 }
1232
Willy Tarreau532a4502011-09-07 22:37:44 +02001233 return 1;
1234 }
1235 else if (strcmp(args[1], "frontend") == 0) {
1236 struct proxy *px;
1237
1238 px = expect_frontend_admin(s, si, args[2]);
1239 if (!px)
1240 return 1;
1241
1242 if (px->state == PR_STSTOPPED) {
1243 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1244 si->applet.st0 = STAT_CLI_PRINT;
1245 return 1;
1246 }
1247
1248 if (px->state == PR_STPAUSED) {
1249 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1250 si->applet.st0 = STAT_CLI_PRINT;
1251 return 1;
1252 }
1253
1254 if (!pause_proxy(px)) {
1255 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1256 si->applet.st0 = STAT_CLI_PRINT;
1257 return 1;
1258 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001259 return 1;
1260 }
1261 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001262 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1263 si->applet.st0 = STAT_CLI_PRINT;
1264 return 1;
1265 }
1266 }
1267 else if (strcmp(args[0], "shutdown") == 0) {
1268 if (strcmp(args[1], "frontend") == 0) {
1269 struct proxy *px;
1270
1271 px = expect_frontend_admin(s, si, args[2]);
1272 if (!px)
1273 return 1;
1274
1275 if (px->state == PR_STSTOPPED) {
1276 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1277 si->applet.st0 = STAT_CLI_PRINT;
1278 return 1;
1279 }
1280
1281 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1282 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1283 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1284 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1285 stop_proxy(px);
1286 return 1;
1287 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001288 else if (strcmp(args[1], "session") == 0) {
1289 struct session *sess, *ptr;
1290
1291 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1292 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1293 si->applet.st0 = STAT_CLI_PRINT;
1294 return 1;
1295 }
1296
1297 if (!*args[2]) {
1298 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1299 si->applet.st0 = STAT_CLI_PRINT;
1300 return 1;
1301 }
1302
1303 ptr = (void *)strtoul(args[2], NULL, 0);
1304
1305 /* first, look for the requested session in the session table */
1306 list_for_each_entry(sess, &sessions, list) {
1307 if (sess == ptr)
1308 break;
1309 }
1310
1311 /* do we have the session ? */
1312 if (sess != ptr) {
1313 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1314 si->applet.st0 = STAT_CLI_PRINT;
1315 return 1;
1316 }
1317
1318 session_shutdown(sess, SN_ERR_KILLED);
1319 return 1;
1320 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001321 else if (strcmp(args[1], "sessions") == 0) {
1322 if (strcmp(args[2], "server") == 0) {
1323 struct server *sv;
1324 struct session *sess, *sess_bck;
1325
1326 sv = expect_server_admin(s, si, args[3]);
1327 if (!sv)
1328 return 1;
1329
1330 /* kill all the session that are on this server */
1331 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1332 if (sess->srv_conn == sv)
1333 session_shutdown(sess, SN_ERR_KILLED);
1334
1335 return 1;
1336 }
1337 else {
1338 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1339 si->applet.st0 = STAT_CLI_PRINT;
1340 return 1;
1341 }
1342 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001343 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001344 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001345 si->applet.st0 = STAT_CLI_PRINT;
1346 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001347 }
1348 }
1349 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001350 return 0;
1351 }
1352 return 1;
1353}
1354
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001355/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001356 * used to processes I/O from/to the stats unix socket. The system relies on a
1357 * state machine handling requests and various responses. We read a request,
1358 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001359 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1360 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001361 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001362 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001363static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001364{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001365 struct buffer *req = si->ob;
1366 struct buffer *res = si->ib;
1367 int reql;
1368 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001369
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001370 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1371 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001372
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001373 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001374 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001375 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001376 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001377 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001378 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001379 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001380 /* Let's close for real now. We just close the request
1381 * side, the conditions below will complete if needed.
1382 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001383 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001384 break;
1385 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001386 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001387 /* ensure we have some output room left in the event we
1388 * would want to return some info right after parsing.
1389 */
1390 if (buffer_almost_full(si->ib))
1391 break;
1392
David du Colombier7af46052012-05-16 14:16:48 +02001393 reql = bo_getline(si->ob, trash, trashlen);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001394 if (reql <= 0) { /* closed or EOL not found */
1395 if (reql == 0)
1396 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001397 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001398 continue;
1399 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001400
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001401 /* seek for a possible semi-colon. If we find one, we
1402 * replace it with an LF and skip only this part.
1403 */
1404 for (len = 0; len < reql; len++)
1405 if (trash[len] == ';') {
1406 trash[len] = '\n';
1407 reql = len + 1;
1408 break;
1409 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001410
Willy Tarreau816fc222009-10-04 07:36:58 +02001411 /* now it is time to check that we have a full line,
1412 * remove the trailing \n and possibly \r, then cut the
1413 * line.
1414 */
1415 len = reql - 1;
1416 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001417 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001418 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001419 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001420
Willy Tarreau816fc222009-10-04 07:36:58 +02001421 if (len && trash[len-1] == '\r')
1422 len--;
1423
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001424 trash[len] = '\0';
1425
Willy Tarreaubc4af052011-02-13 13:25:14 +01001426 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001427 if (len) {
1428 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001429 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001430 continue;
1431 }
1432 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001433 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001434 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001435 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001436 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001437 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001438 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001439 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001440 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001441 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001442 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001443 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001444 /* if prompt is disabled, print help on empty lines,
1445 * so that the user at least knows how to enable
1446 * prompt and find help.
1447 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001448 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001449 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001450 }
1451
1452 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001453 bo_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001454 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001455 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001456 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001457 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001458 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001459 continue;
1460 }
1461
Willy Tarreaubc4af052011-02-13 13:25:14 +01001462 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001463 case STAT_CLI_PRINT:
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001464 if (bi_putstr(si->ib, si->applet.ctx.cli.msg) != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001465 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001466 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001467 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001468 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001469 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001470 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001471 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001472 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001473 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001474 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001475 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001476 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001477 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001478 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001479 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001480 if (stats_table_request(si, true))
1481 si->applet.st0 = STAT_CLI_PROMPT;
1482 break;
1483 case STAT_CLI_O_CLR:
1484 if (stats_table_request(si, false))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001485 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001486 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001487 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001488 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001489 break;
1490 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001491
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001492 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001493 if (si->applet.st0 == STAT_CLI_PROMPT) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001494 if (bi_putstr(si->ib, si->applet.st1 ? "\n> " : "\n") != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001495 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001496 }
1497
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001498 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001499 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001500 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001501
1502 /* Now we close the output if one of the writers did so,
1503 * or if we're not in interactive mode and the request
1504 * buffer is empty. This still allows pipelined requests
1505 * to be sent in non-interactive mode.
1506 */
Willy Tarreau2e046c62012-03-01 16:08:30 +01001507 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->applet.st1 && !req->o)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001508 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001509 continue;
1510 }
1511
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001512 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001513 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001514 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001515 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001516
Willy Tarreaubc4af052011-02-13 13:25:14 +01001517 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001518 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1519 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001520 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001521 * and nothing more to consume. This is comparable to a broken pipe, so
1522 * we forward the close to the request side so that it flows upstream to
1523 * the client.
1524 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001525 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001526 }
1527
Willy Tarreaubc4af052011-02-13 13:25:14 +01001528 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001529 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1530 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1531 /* We have no more processing to do, and nothing more to send, and
1532 * the client side has closed. So we'll forward this state downstream
1533 * on the response buffer.
1534 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001535 si_shutr(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001536 res->flags |= BF_READ_NULL;
1537 }
1538
1539 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001540 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001541
1542 /* we don't want to expire timeouts while we're processing requests */
1543 si->ib->rex = TICK_ETERNITY;
1544 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001545
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001546 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001547 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 +02001548 __FUNCTION__, __LINE__,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001549 si->state, req->flags, res->flags, req->i, req->o, res->i, res->o);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001550
1551 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1552 /* check that we have released everything then unregister */
1553 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001554 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001555}
1556
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001557/* This function dumps statistics onto the stream interface's read buffer.
1558 * The data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001559 * It returns 0 as long as it does not complete, non-zero upon completion.
1560 * Some states are not used but it makes the code more similar to other
1561 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001562 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001563static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001564{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001565 struct proxy *px;
1566 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001567 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001568
David du Colombier7af46052012-05-16 14:16:48 +02001569 chunk_init(&msg, trash, trashlen);
Willy Tarreau3e76e722007-10-17 18:57:38 +02001570
Willy Tarreau94981132012-05-21 17:09:48 +02001571 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001572 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001573 /* the function had not been called yet */
Willy Tarreau94981132012-05-21 17:09:48 +02001574 si->conn.data_st = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001575 /* fall through */
1576
Willy Tarreau295a8372011-03-10 11:25:07 +01001577 case STAT_ST_HEAD:
1578 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001579 print_csv_header(&msg);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001580 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001581 return 0;
1582 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001583
Willy Tarreau94981132012-05-21 17:09:48 +02001584 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001585 /* fall through */
1586
Willy Tarreau295a8372011-03-10 11:25:07 +01001587 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001588 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001589 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001590 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001591 "Name: " PRODUCT_NAME "\n"
1592 "Version: " HAPROXY_VERSION "\n"
1593 "Release_date: " HAPROXY_DATE "\n"
1594 "Nbproc: %d\n"
1595 "Process_num: %d\n"
1596 "Pid: %d\n"
1597 "Uptime: %dd %dh%02dm%02ds\n"
1598 "Uptime_sec: %d\n"
1599 "Memmax_MB: %d\n"
1600 "Ulimit-n: %d\n"
1601 "Maxsock: %d\n"
1602 "Maxconn: %d\n"
Willy Tarreau91886b62011-09-07 14:38:31 +02001603 "Hard_maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001604 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001605 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001606 "PipesUsed: %d\n"
1607 "PipesFree: %d\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001608 "ConnRate: %d\n"
1609 "ConnRateLimit: %d\n"
1610 "MaxConnRate: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001611 "Tasks: %d\n"
1612 "Run_queue: %d\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02001613 "Idle_pct: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001614 "node: %s\n"
1615 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001616 "",
1617 global.nbproc,
1618 relative_pid,
1619 pid,
1620 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1621 up,
1622 global.rlimit_memmax,
1623 global.rlimit_nofile,
Willy Tarreau91886b62011-09-07 14:38:31 +02001624 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001625 actconn, pipes_used, pipes_free,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001626 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau45a12512011-09-10 16:56:42 +02001627 nb_tasks_cur, run_queue_cur, idle_pct,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001628 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001629 );
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001630 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001631 return 0;
1632 }
1633
Willy Tarreau295a8372011-03-10 11:25:07 +01001634 si->applet.ctx.stats.px = proxy;
1635 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1636 si->applet.ctx.stats.sv = NULL;
Willy Tarreau94981132012-05-21 17:09:48 +02001637 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001638 /* fall through */
1639
Willy Tarreau295a8372011-03-10 11:25:07 +01001640 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001641 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001642 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1643 while (si->applet.ctx.stats.px) {
1644 px = si->applet.ctx.stats.px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001645 /* skip the disabled proxies and non-networked ones */
1646 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001647 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001648 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001649 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001650 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001651
Willy Tarreau295a8372011-03-10 11:25:07 +01001652 si->applet.ctx.stats.px = px->next;
1653 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001654 }
1655 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001656 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001657
Willy Tarreau94981132012-05-21 17:09:48 +02001658 si->conn.data_st = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001659 /* fall through */
1660
Willy Tarreau295a8372011-03-10 11:25:07 +01001661 case STAT_ST_END:
Willy Tarreau94981132012-05-21 17:09:48 +02001662 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001663 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001664
Willy Tarreau295a8372011-03-10 11:25:07 +01001665 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001666 return 1;
1667
1668 default:
1669 /* unknown state ! */
Willy Tarreau94981132012-05-21 17:09:48 +02001670 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001671 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001672 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001673}
1674
1675
Cyril Bonté70be45d2010-10-12 00:14:35 +02001676/* We don't want to land on the posted stats page because a refresh will
1677 * repost the data. We don't want this to happen on accident so we redirect
1678 * the browse to the stats page with a GET.
1679 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001680static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001681{
Willy Tarreau94981132012-05-21 17:09:48 +02001682 struct session *s = si->conn.data_ctx;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001683 struct chunk msg;
1684
David du Colombier7af46052012-05-16 14:16:48 +02001685 chunk_init(&msg, trash, trashlen);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001686
Willy Tarreau94981132012-05-21 17:09:48 +02001687 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001688 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001689 chunk_printf(&msg,
1690 "HTTP/1.0 303 See Other\r\n"
1691 "Cache-Control: no-cache\r\n"
1692 "Content-Type: text/plain\r\n"
1693 "Connection: close\r\n"
1694 "Location: %s;st=%s",
Cyril Bonté19979e12012-04-04 12:57:21 +02001695 uri->uri_prefix,
1696 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
1697 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
1698 stat_status_codes[si->applet.ctx.stats.st_code]) ?
1699 stat_status_codes[si->applet.ctx.stats.st_code] :
1700 stat_status_codes[STAT_STATUS_UNKN]);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001701 chunk_printf(&msg, "\r\n\r\n");
1702
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001703 if (bi_putchk(si->ib, &msg) == -1)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001704 return 0;
1705
1706 s->txn.status = 303;
1707
1708 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1709 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1710 if (!(s->flags & SN_FINST_MASK))
1711 s->flags |= SN_FINST_R;
1712
Willy Tarreau94981132012-05-21 17:09:48 +02001713 si->conn.data_st = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001714 return 1;
1715 }
1716 return 1;
1717}
1718
1719
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001720/* This I/O handler runs as an applet embedded in a stream interface. It is
1721 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001722 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001723 * automatically unregisters itself once transfer is complete.
1724 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001725static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001726{
Willy Tarreau94981132012-05-21 17:09:48 +02001727 struct session *s = si->conn.data_ctx;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001728 struct buffer *req = si->ob;
1729 struct buffer *res = si->ib;
1730
1731 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1732 goto out;
1733
1734 /* check that the output is not closed */
1735 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001736 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001737
Willy Tarreaubc4af052011-02-13 13:25:14 +01001738 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001739 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001740 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001741 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001742 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001743 }
1744 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001745 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001746 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001747 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001748 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001749 }
1750 }
1751
1752 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
Willy Tarreau73b013b2012-05-21 16:31:45 +02001753 si_shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001754
Willy Tarreaubc4af052011-02-13 13:25:14 +01001755 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001756 si_shutr(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001757 res->flags |= BF_READ_NULL;
1758 }
1759
1760 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001761 si_update(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001762
1763 /* we don't want to expire timeouts while we're processing requests */
1764 si->ib->rex = TICK_ETERNITY;
1765 si->ob->wex = TICK_ETERNITY;
1766
1767 out:
1768 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1769 /* check that we have released everything then unregister */
1770 stream_int_unregister_handler(si);
1771 }
1772}
1773
1774
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001775/* This function dumps statistics in HTTP format onto the stream interface's
1776 * read buffer. The data_ctx must have been zeroed first, and the flags
1777 * properly set. It returns 0 if it had to stop writing data and an I/O is
1778 * needed, 1 if the dump is finished and the session must be closed, or -1
1779 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001780 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001781static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001782{
Willy Tarreau94981132012-05-21 17:09:48 +02001783 struct session *s = si->conn.data_ctx;
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001784 struct buffer *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001785 struct proxy *px;
1786 struct chunk msg;
1787 unsigned int up;
1788
David du Colombier7af46052012-05-16 14:16:48 +02001789 chunk_init(&msg, trash, trashlen);
Willy Tarreau91861262007-10-17 17:06:05 +02001790
Willy Tarreau94981132012-05-21 17:09:48 +02001791 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001792 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001793 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001794 "HTTP/1.0 200 OK\r\n"
1795 "Cache-Control: no-cache\r\n"
1796 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001797 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001798 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001799
Willy Tarreau295a8372011-03-10 11:25:07 +01001800 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001801 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001802 uri->refresh);
1803
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001804 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001805
1806 s->txn.status = 200;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001807 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001808 return 0;
1809
Willy Tarreau91861262007-10-17 17:06:05 +02001810 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1811 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1812 if (!(s->flags & SN_FINST_MASK))
1813 s->flags |= SN_FINST_R;
1814
1815 if (s->txn.meth == HTTP_METH_HEAD) {
1816 /* that's all we return in case of HEAD request */
Willy Tarreau94981132012-05-21 17:09:48 +02001817 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001818 return 1;
1819 }
1820
Willy Tarreau94981132012-05-21 17:09:48 +02001821 si->conn.data_st = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001822 /* fall through */
1823
Willy Tarreau295a8372011-03-10 11:25:07 +01001824 case STAT_ST_HEAD:
1825 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001826 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001827 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001828 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1829 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001830 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001831 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1832 "<style type=\"text/css\"><!--\n"
1833 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001834 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001835 " font-size: 12px;"
1836 " font-weight: normal;"
1837 " color: black;"
1838 " background: white;"
1839 "}\n"
1840 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001841 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001842 "}\n"
1843 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001844 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001845 " margin-bottom: 0.5em;"
1846 "}\n"
1847 "h2 {"
1848 " font-family: helvetica, arial;"
1849 " font-size: x-large;"
1850 " font-weight: bold;"
1851 " font-style: italic;"
1852 " color: #6020a0;"
1853 " margin-top: 0em;"
1854 " margin-bottom: 0em;"
1855 "}\n"
1856 "h3 {"
1857 " font-family: helvetica, arial;"
1858 " font-size: 16px;"
1859 " font-weight: bold;"
1860 " color: #b00040;"
1861 " background: #e8e8d0;"
1862 " margin-top: 0em;"
1863 " margin-bottom: 0em;"
1864 "}\n"
1865 "li {"
1866 " margin-top: 0.25em;"
1867 " margin-right: 2em;"
1868 "}\n"
1869 ".hr {margin-top: 0.25em;"
1870 " border-color: black;"
1871 " border-bottom-style: solid;"
1872 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001873 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001874 ".total {background: #20D0D0;color: #ffff80;}\n"
1875 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001876 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001877 ".backend {background: #e8e8d0;}\n"
1878 ".active0 {background: #ff9090;}\n"
1879 ".active1 {background: #ffd020;}\n"
1880 ".active2 {background: #ffffa0;}\n"
1881 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001882 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1883 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1884 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001885 ".backup0 {background: #ff9090;}\n"
1886 ".backup1 {background: #ff80ff;}\n"
1887 ".backup2 {background: #c060ff;}\n"
1888 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001889 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1890 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1891 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001892 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001893 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001894 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001895 "a.px:link {color: #ffff40; text-decoration: none;}"
1896 "a.px:visited {color: #ffff40; text-decoration: none;}"
1897 "a.px:hover {color: #ffffff; text-decoration: none;}"
1898 "a.lfsb:link {color: #000000; text-decoration: none;}"
1899 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1900 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1901 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001902 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001903 "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"
1904 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001905 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001906 "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 +02001907 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001908 "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 +02001909 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001910 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1911 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1912 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001913 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001914 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001915 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001916 (uri->flags&ST_SHNODE) ? " on " : "",
1917 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001918 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001919 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001920 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001921 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001922 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02001923 return 0;
1924
Willy Tarreau94981132012-05-21 17:09:48 +02001925 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02001926 /* fall through */
1927
Willy Tarreau295a8372011-03-10 11:25:07 +01001928 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02001929 up = (now.tv_sec - start_date.tv_sec);
1930
1931 /* WARNING! this has to fit the first packet too.
1932 * We are around 3.5 kB, add adding entries will
1933 * become tricky if we want to support 4kB buffers !
1934 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001935 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001936 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001937 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1938 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001939 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001940 "<hr width=\"100%%\" class=\"hr\">\n"
1941 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001942 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001943 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001944 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001945 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1946 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001947 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02001948 "Running tasks: %d/%d; idle = %d %%<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001949 "</td><td align=\"center\" nowrap>\n"
1950 "<table class=\"lgd\"><tr>\n"
1951 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1952 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1953 "</tr><tr>\n"
1954 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1955 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1956 "</tr><tr>\n"
1957 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1958 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1959 "</tr><tr>\n"
1960 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001961 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001962 "</tr><tr>\n"
1963 "<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 +02001964 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001965 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001966 "</td>"
1967 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1968 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1969 "",
1970 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001971 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1972 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1973 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001974 up / 86400, (up % 86400) / 3600,
1975 (up % 3600) / 60, (up % 60),
1976 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1977 global.rlimit_memmax ? " MB" : "",
1978 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001979 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001980 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau45a12512011-09-10 16:56:42 +02001981 run_queue_cur, nb_tasks_cur, idle_pct
Willy Tarreau91861262007-10-17 17:06:05 +02001982 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001983
Willy Tarreau295a8372011-03-10 11:25:07 +01001984 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001985 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001986 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1987 uri->uri_prefix,
1988 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01001989 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001990 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001991 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001992 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1993 uri->uri_prefix,
1994 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01001995 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001996
Willy Tarreau55bb8452007-10-17 18:44:57 +02001997 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001998 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001999 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002000 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
2001 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002002 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002003 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002004 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002005 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002006 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
2007 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002008 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002009 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002010 }
Willy Tarreau91861262007-10-17 17:06:05 +02002011
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002012 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002013 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
2014 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002015 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2016 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002017
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002018 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002019 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
2020 uri->uri_prefix,
2021 (uri->refresh > 0) ? ";norefresh" : "");
2022
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002023 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002024 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002025 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
Adrian Bridgettafdb6e52012-03-19 23:36:42 +00002026 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002027 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2028 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2029 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2030 "</ul>"
2031 "</td>"
2032 "</tr></table>\n"
2033 ""
2034 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002035
Willy Tarreau295a8372011-03-10 11:25:07 +01002036 if (si->applet.ctx.stats.st_code) {
Cyril Bonté19979e12012-04-04 12:57:21 +02002037 switch (si->applet.ctx.stats.st_code) {
2038 case STAT_STATUS_DONE:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002039 chunk_printf(&msg,
2040 "<p><div class=active3>"
2041 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2042 "Action processed successfully."
2043 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002044 break;
2045 case STAT_STATUS_NONE:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002046 chunk_printf(&msg,
2047 "<p><div class=active2>"
2048 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2049 "Nothing has changed."
2050 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002051 break;
2052 case STAT_STATUS_PART:
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002053 chunk_printf(&msg,
2054 "<p><div class=active2>"
2055 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2056 "Action partially processed.<br>"
2057 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2058 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002059 break;
2060 case STAT_STATUS_ERRP:
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002061 chunk_printf(&msg,
2062 "<p><div class=active0>"
2063 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2064 "Action not processed because of invalid parameters."
2065 "<ul>"
2066 "<li>The action is maybe unknown.</li>"
2067 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2068 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2069 "</ul>"
2070 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002071 break;
2072 case STAT_STATUS_EXCD:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002073 chunk_printf(&msg,
2074 "<p><div class=active0>"
2075 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2076 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2077 "You should retry with less servers at a time.</b>"
2078 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002079 break;
2080 case STAT_STATUS_DENY:
Cyril Bonté474be412010-10-12 00:14:36 +02002081 chunk_printf(&msg,
2082 "<p><div class=active0>"
2083 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2084 "<b>Action denied.</b>"
2085 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002086 break;
2087 default:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002088 chunk_printf(&msg,
2089 "<p><div class=active6>"
2090 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2091 "Unexpected result."
2092 "</div>\n", uri->uri_prefix);
2093 }
2094 chunk_printf(&msg,"<p>\n");
2095 }
2096
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002097 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002098 return 0;
2099 }
Willy Tarreau91861262007-10-17 17:06:05 +02002100
Willy Tarreau295a8372011-03-10 11:25:07 +01002101 si->applet.ctx.stats.px = proxy;
2102 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau94981132012-05-21 17:09:48 +02002103 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02002104 /* fall through */
2105
Willy Tarreau295a8372011-03-10 11:25:07 +01002106 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02002107 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01002108 while (si->applet.ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002109 if (buffer_almost_full(rep))
2110 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002111 px = si->applet.ctx.stats.px;
Willy Tarreau91861262007-10-17 17:06:05 +02002112 /* skip the disabled proxies and non-networked ones */
2113 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002114 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002115 return 0;
2116
Willy Tarreau295a8372011-03-10 11:25:07 +01002117 si->applet.ctx.stats.px = px->next;
2118 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02002119 }
2120 /* here, we just have reached the last proxy */
2121
Willy Tarreau94981132012-05-21 17:09:48 +02002122 si->conn.data_st = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002123 /* fall through */
2124
Willy Tarreau295a8372011-03-10 11:25:07 +01002125 case STAT_ST_END:
2126 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002127 chunk_printf(&msg, "</body></html>\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002128 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002129 return 0;
2130 }
Willy Tarreau91861262007-10-17 17:06:05 +02002131
Willy Tarreau94981132012-05-21 17:09:48 +02002132 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002133 /* fall through */
2134
Willy Tarreau295a8372011-03-10 11:25:07 +01002135 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002136 return 1;
2137
2138 default:
2139 /* unknown state ! */
Willy Tarreau94981132012-05-21 17:09:48 +02002140 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002141 return -1;
2142 }
2143}
2144
2145
2146/*
2147 * Dumps statistics for a proxy.
2148 * Returns 0 if it had to stop dumping data because of lack of buffer space,
2149 * ot non-zero if everything completed.
2150 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002151static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002152{
Willy Tarreau94981132012-05-21 17:09:48 +02002153 struct session *s = si->conn.data_ctx;
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002154 struct buffer *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002155 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002156 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002157 struct chunk msg;
2158
David du Colombier7af46052012-05-16 14:16:48 +02002159 chunk_init(&msg, trash, trashlen);
Willy Tarreau91861262007-10-17 17:06:05 +02002160
Willy Tarreau295a8372011-03-10 11:25:07 +01002161 switch (si->applet.ctx.stats.px_st) {
2162 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002163 /* we are on a new proxy */
2164
2165 if (uri && uri->scope) {
2166 /* we have a limited scope, we have to check the proxy name */
2167 struct stat_scope *scope;
2168 int len;
2169
2170 len = strlen(px->id);
2171 scope = uri->scope;
2172
2173 while (scope) {
2174 /* match exact proxy name */
2175 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2176 break;
2177
2178 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002179 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002180 break;
2181 scope = scope->next;
2182 }
2183
2184 /* proxy name not found : don't dump anything */
2185 if (scope == NULL)
2186 return 1;
2187 }
2188
Willy Tarreau295a8372011-03-10 11:25:07 +01002189 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
2190 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002191 return 1;
2192
Willy Tarreau295a8372011-03-10 11:25:07 +01002193 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002194 /* fall through */
2195
Willy Tarreau295a8372011-03-10 11:25:07 +01002196 case STAT_PX_ST_TH:
2197 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
2198 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002199 /* A form to enable/disable this proxy servers */
2200 chunk_printf(&msg,
2201 "<form action=\"%s\" method=\"post\">",
2202 uri->uri_prefix);
2203 }
2204
Willy Tarreau55bb8452007-10-17 18:44:57 +02002205 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002206 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002207 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002208 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002209 "<th class=\"pxname\" width=\"10%%\"");
2210
2211 if (uri->flags&ST_SHLGNDS) {
2212 /* cap, mode, id */
2213 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
2214 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2215 px->uuid);
2216
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002217 chunk_printf(&msg, "\"");
2218 }
2219
2220 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002221 ">%s<a name=\"%s\"></a>"
2222 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002223 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002224 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002225 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002226 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002227 "<tr class=\"titre\">",
2228 (uri->flags & ST_SHLGNDS)?"<u>":"",
2229 px->id, px->id, px->id,
2230 (uri->flags & ST_SHLGNDS)?"</u>":"",
2231 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2232
Willy Tarreau295a8372011-03-10 11:25:07 +01002233 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002234 /* Column heading for Enable or Disable server */
2235 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
2236 }
2237
2238 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002239 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002240 "<th colspan=3>Queue</th>"
2241 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002242 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002243 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002244 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002245 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002246 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002247 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002248 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002249 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002250 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002251 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002252 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002253 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2254 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002255 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002256
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002257 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002258 return 0;
2259 }
Willy Tarreau91861262007-10-17 17:06:05 +02002260
Willy Tarreau295a8372011-03-10 11:25:07 +01002261 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002262 /* fall through */
2263
Willy Tarreau295a8372011-03-10 11:25:07 +01002264 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002265 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002266 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002267 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
2268 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002269 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002270 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002271 "<tr class=\"frontend\">");
2272
Willy Tarreau295a8372011-03-10 11:25:07 +01002273 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002274 /* Column sub-heading for Enable or Disable server */
2275 chunk_printf(&msg, "<td></td>");
2276 }
2277
2278 chunk_printf(&msg,
2279 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002280 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002281 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2282 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002283 "",
2284 px->id, px->id);
2285
2286 if (px->mode == PR_MODE_HTTP) {
2287 chunk_printf(&msg,
2288 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01002289 "<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 +01002290 "",
2291 read_freq_ctr(&px->fe_req_per_sec),
2292 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002293 px->fe_counters.p.http.rps_max,
2294 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01002295 LIM2A2(px->fe_sps_lim, "-"));
2296 } else {
2297 chunk_printf(&msg,
2298 /* sessions rate : current, max, limit */
2299 "<td>%s</td><td>%s</td><td>%s</td>"
2300 "",
2301 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002302 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01002303 }
2304
2305 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002306 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002307 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002308 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002309 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002310 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002311
2312 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2313 if (px->mode == PR_MODE_HTTP) {
2314 int i;
2315
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002316 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002317
2318 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002319 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002320
Willy Tarreaueabea072011-09-10 23:29:44 +02002321 chunk_printf(&msg, " other=%lld,", px->fe_counters.p.http.rsp[0]);
2322 chunk_printf(&msg, " intercepted=%lld\"", px->fe_counters.intercepted_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002323 }
2324
2325 chunk_printf(&msg,
2326 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002327 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002328 /* bytes : in, out */
2329 "<td>%s</td><td>%s</td>"
2330 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002331 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002332 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002333 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002334 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002335
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002336 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002337 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002338 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002339 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002340 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002341 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002342 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002343 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002344 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002345 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002346 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002347 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002348 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2349 U2H2(px->fe_counters.failed_req),
Willy Tarreau562515c2011-07-25 08:11:52 +02002350 px->state == PR_STREADY ? "OPEN" :
2351 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002352 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002353 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002354 /* pxid, name, queue cur, queue max, */
2355 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002356 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002357 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002358 /* bytes : in, out */
2359 "%lld,%lld,"
2360 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002361 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002362 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002363 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002364 /* warnings: retries, redispatches */
2365 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002366 /* server status : reflect frontend status */
2367 "%s,"
2368 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002369 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002370 /* pid, iid, sid, throttle, lbtot, tracked, type */
2371 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002372 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002373 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002374 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002375 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002376 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002377 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2378 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2379 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2380 px->fe_counters.failed_req,
Willy Tarreau562515c2011-07-25 08:11:52 +02002381 px->state == PR_STREADY ? "OPEN" :
2382 px->state == PR_STFULL ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002383 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002384 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002385 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002386
2387 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2388 if (px->mode == PR_MODE_HTTP) {
2389 int i;
2390
2391 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002392 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002393
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002394 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002395 } else {
2396 chunk_printf(&msg, ",,,,,,");
2397 }
2398
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002399 /* failed health analyses */
2400 chunk_printf(&msg, ",");
2401
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002402 /* requests : req_rate, req_rate_max, req_tot, */
2403 chunk_printf(&msg, "%u,%u,%lld,",
2404 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002405 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002406
Willy Tarreauae526782010-03-04 20:34:23 +01002407 /* errors: cli_aborts, srv_aborts */
2408 chunk_printf(&msg, ",,");
2409
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002410 /* finish with EOL */
2411 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002412 }
Willy Tarreau91861262007-10-17 17:06:05 +02002413
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002414 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002415 return 0;
2416 }
2417
Willy Tarreau295a8372011-03-10 11:25:07 +01002418 si->applet.ctx.stats.l = px->listen; /* may be NULL */
2419 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002420 /* fall through */
2421
Willy Tarreau295a8372011-03-10 11:25:07 +01002422 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002423 /* stats.l has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002424 for (; si->applet.ctx.stats.l != NULL; si->applet.ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002425 if (buffer_almost_full(rep))
2426 return 0;
2427
Willy Tarreau295a8372011-03-10 11:25:07 +01002428 l = si->applet.ctx.stats.l;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002429 if (!l->counters)
2430 continue;
2431
Willy Tarreau295a8372011-03-10 11:25:07 +01002432 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2433 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002434 break;
2435
Willy Tarreau295a8372011-03-10 11:25:07 +01002436 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002437 continue;
2438 }
2439
Willy Tarreau295a8372011-03-10 11:25:07 +01002440 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002441 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002442 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002443 /* Column sub-heading for Enable or Disable server */
2444 chunk_printf(&msg, "<td></td>");
2445 }
2446 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002447
2448 if (uri->flags&ST_SHLGNDS) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02002449 char str[INET6_ADDRSTRLEN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002450 int port;
2451
Willy Tarreau631f01c2011-09-05 00:36:48 +02002452 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002453
Willy Tarreau631f01c2011-09-05 00:36:48 +02002454 port = get_host_port(&l->addr);
2455 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2456 case AF_INET:
2457 chunk_printf(&msg, "IPv4: %s:%d, ", str, port);
2458 break;
2459 case AF_INET6:
2460 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, port);
2461 break;
2462 case AF_UNIX:
2463 chunk_printf(&msg, "unix, ");
2464 break;
2465 case -1:
2466 chunk_printf(&msg, "(%s), ", strerror(errno));
2467 break;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002468 }
2469
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002470 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002471 chunk_printf(&msg, "id: %d\"", l->luid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002472 }
2473
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002474 chunk_printf(&msg,
2475 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002476 ">%s<a name=\"%s/+%s\"></a>"
2477 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002478 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002479 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002480 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002481 "<td>%s</td><td>%s</td><td>%s</td>"
2482 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002483 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002484 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002485 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002486 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002487 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002488 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002489 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2490 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2491
2492 chunk_printf(&msg,
2493 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002494 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002495 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002496 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002497 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002498 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002499 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002500 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002501 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002502 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002503 "",
2504 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2505 U2H2(l->counters->failed_req),
Willy Tarreaua17c2d92011-07-25 08:16:20 +02002506 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002507 } else {
2508 chunk_printf(&msg,
2509 /* pxid, name, queue cur, queue max, */
2510 "%s,%s,,,"
2511 /* sessions: current, max, limit, total */
2512 "%d,%d,%d,%lld,"
2513 /* bytes: in, out */
2514 "%lld,%lld,"
2515 /* denied: req, resp */
2516 "%lld,%lld,"
2517 /* errors: request, connect, response */
2518 "%lld,,,"
2519 /* warnings: retries, redispatches */
2520 ",,"
2521 /* server status: reflect listener status */
2522 "%s,"
2523 /* rest of server: nothing */
2524 ",,,,,,,,"
2525 /* pid, iid, sid, throttle, lbtot, tracked, type */
2526 "%d,%d,%d,,,,%d,"
2527 /* rate, rate_lim, rate_max */
2528 ",,,"
2529 /* check_status, check_code, check_duration */
2530 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002531 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2532 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002533 /* failed health analyses */
2534 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002535 /* requests : req_rate, req_rate_max, req_tot, */
2536 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002537 /* errors: cli_aborts, srv_aborts */
2538 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002539 "\n",
2540 px->id, l->name,
2541 l->nbconn, l->counters->conn_max,
2542 l->maxconn, l->counters->cum_conn,
2543 l->counters->bytes_in, l->counters->bytes_out,
2544 l->counters->denied_req, l->counters->denied_resp,
2545 l->counters->failed_req,
2546 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2547 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2548 }
2549
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002550 if (bi_putchk(rep, &msg) == -1)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002551 return 0;
2552 }
2553
Willy Tarreau295a8372011-03-10 11:25:07 +01002554 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2555 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002556 /* fall through */
2557
Willy Tarreau295a8372011-03-10 11:25:07 +01002558 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002559 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002560 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002561 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 +02002562
Willy Tarreau4e33d862009-10-11 23:35:10 +02002563 if (buffer_almost_full(rep))
2564 return 0;
2565
Willy Tarreau295a8372011-03-10 11:25:07 +01002566 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002567
Willy Tarreau295a8372011-03-10 11:25:07 +01002568 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2569 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002570 break;
2571
Willy Tarreau295a8372011-03-10 11:25:07 +01002572 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002573 continue;
2574 }
2575
Willy Tarreau44267702011-10-28 15:35:33 +02002576 if (sv->track)
2577 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002578 else
2579 svs = sv;
2580
Willy Tarreau91861262007-10-17 17:06:05 +02002581 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002582 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002583 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002584 else if (svs->state & SRV_RUNNING) {
2585 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002586 sv_state = 3; /* UP */
2587 else
2588 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002589
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002590 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002591 sv_state += 2;
2592 }
Willy Tarreau91861262007-10-17 17:06:05 +02002593 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002594 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002595 sv_state = 1; /* going up */
2596 else
2597 sv_state = 0; /* DOWN */
2598
Willy Tarreau295a8372011-03-10 11:25:07 +01002599 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002600 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002601 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002602 continue;
2603 }
2604
Willy Tarreau295a8372011-03-10 11:25:07 +01002605 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002606 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2607 "UP %d/%d &darr;", "UP",
2608 "NOLB %d/%d &darr;", "NOLB",
2609 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002610 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002611 chunk_printf(&msg,
2612 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002613 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002614 );
2615 }
2616 else {
2617 chunk_printf(&msg,
2618 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002619 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002620 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2621 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002622
Willy Tarreau295a8372011-03-10 11:25:07 +01002623 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002624 chunk_printf(&msg,
2625 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2626 sv->id);
2627 }
2628
2629 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002630
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002631 if (uri->flags&ST_SHLGNDS) {
2632 char str[INET6_ADDRSTRLEN];
2633
Willy Tarreau631f01c2011-09-05 00:36:48 +02002634 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002635
Willy Tarreau631f01c2011-09-05 00:36:48 +02002636 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01002637 case AF_INET:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002638 chunk_printf(&msg, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
David du Colombier6f5ccb12011-03-10 22:26:24 +01002639 break;
2640 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002641 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2642 break;
2643 case AF_UNIX:
2644 chunk_printf(&msg, "unix, ");
2645 break;
2646 case -1:
2647 chunk_printf(&msg, "(%s), ", strerror(errno));
2648 break;
2649 default: /* address family not supported */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002650 break;
2651 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002652
2653 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002654 chunk_printf(&msg, "id: %d", sv->puid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002655
2656 /* cookie */
2657 if (sv->cookie) {
2658 struct chunk src;
2659
2660 chunk_printf(&msg, ", cookie: '");
2661
2662 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2663 chunk_htmlencode(&msg, &src);
2664
2665 chunk_printf(&msg, "'");
2666 }
2667
2668 chunk_printf(&msg, "\"");
2669 }
2670
2671 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002672 ">%s<a name=\"%s/%s\"></a>"
2673 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002674 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002675 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002676 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002677 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002678 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002679 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002680 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002681 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002682 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002683 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002684 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002685 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2686 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002687 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2688
2689 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2690 if (px->mode == PR_MODE_HTTP) {
2691 int i;
2692
2693 chunk_printf(&msg, " title=\"rsp codes:");
2694
2695 for (i = 1; i < 6; i++)
2696 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2697
2698 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2699 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002700
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002701 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002702 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002703 ">%s%s%s</td><td>%s</td>",
2704 (px->mode == PR_MODE_HTTP)?"<u>":"",
2705 U2H0(sv->counters.cum_sess),
2706 (px->mode == PR_MODE_HTTP)?"</u>":"",
2707 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002708
2709 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002710 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002711 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002712 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002713 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002714 /* errors : request, connect */
2715 "<td></td><td>%s</td>"
2716 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002717 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002718 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002719 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002720 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002721 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2722 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002723 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002724 sv->counters.cli_aborts,
2725 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002726 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002727 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002728
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002729 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002730 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002731
Cyril Bontécd19e512010-01-31 22:34:03 +01002732 if (sv->state & SRV_MAINTAIN) {
2733 chunk_printf(&msg, "%s ",
2734 human_time(now.tv_sec - sv->last_change, 1));
2735 chunk_printf(&msg, "MAINT");
2736 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002737 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2738 chunk_printf(&msg, "%s ",
2739 human_time(now.tv_sec - svs->last_change, 1));
2740 chunk_printf(&msg, "MAINT(via)");
2741 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002742 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002743 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002744 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002745
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002746 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002747 srv_hlt_st[sv_state],
2748 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2749 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002750 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002751
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002752 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002753 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002754 get_check_status_description(sv->check_status));
2755
2756 if (*sv->check_desc) {
2757 struct chunk src;
2758
2759 chunk_printf(&msg, ": ");
2760
2761 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2762 chunk_htmlencode(&msg, &src);
2763 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002764
Willy Tarreaue0454092010-02-26 12:29:07 +01002765 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002766 tv_iszero(&sv->check_start)?"":"* ",
2767 get_check_status_info(sv->check_status));
2768
2769 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002770 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002771
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002772 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002773 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002774 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002775 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002776
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002777 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002778 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002779 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002780 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002781 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002782 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002783 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002784 (sv->state & SRV_BACKUP) ? "-" : "Y",
2785 (sv->state & SRV_BACKUP) ? "Y" : "-");
2786
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002787 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002788 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002789 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002790 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2791
2792 if (svs->observe)
2793 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2794
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002795 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002796 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002797 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002798 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002799 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002800 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002801 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002802 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2803 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002804 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002805 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002806 "<td colspan=3></td>");
2807
2808 /* throttle */
2809 if ((sv->state & SRV_WARMINGUP) &&
2810 now.tv_sec < sv->last_change + sv->slowstart &&
2811 now.tv_sec >= sv->last_change) {
2812 unsigned int ratio;
2813 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002814 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002815 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002816 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002817 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002818 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002819 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002820 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002821 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2822 "UP %d/%d,", "UP,",
2823 "NOLB %d/%d,", "NOLB,",
2824 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002825 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002826 /* pxid, name */
2827 "%s,%s,"
2828 /* queue : current, max */
2829 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002830 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002831 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002832 /* bytes : in, out */
2833 "%lld,%lld,"
2834 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002835 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002836 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002837 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002838 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002839 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002840 "",
2841 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002842 sv->nbpend, sv->counters.nbpend_max,
2843 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002844 sv->counters.bytes_in, sv->counters.bytes_out,
2845 sv->counters.failed_secu,
2846 sv->counters.failed_conns, sv->counters.failed_resp,
2847 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002848
Willy Tarreau55bb8452007-10-17 18:44:57 +02002849 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002850 if (sv->state & SRV_MAINTAIN) {
2851 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002852 }
2853 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2854 chunk_printf(&msg, "MAINT(via),");
2855 }
2856 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002857 chunk_printf(&msg,
2858 srv_hlt_st[sv_state],
2859 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2860 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2861 }
Willy Tarreau91861262007-10-17 17:06:05 +02002862
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002863 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002864 /* weight, active, backup */
2865 "%d,%d,%d,"
2866 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002867 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002868 (sv->state & SRV_BACKUP) ? 0 : 1,
2869 (sv->state & SRV_BACKUP) ? 1 : 0);
2870
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002871 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002872 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002873 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002874 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002875 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002876 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002877 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002878 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002879 ",,,,");
2880
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002881 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002882 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002883 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002884 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002885 LIM2A0(sv->maxqueue, ""),
2886 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002887
2888 /* throttle */
2889 if ((sv->state & SRV_WARMINGUP) &&
2890 now.tv_sec < sv->last_change + sv->slowstart &&
2891 now.tv_sec >= sv->last_change) {
2892 unsigned int ratio;
2893 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002894 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002895 }
2896
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002897 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002898 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002899
2900 /* tracked */
Willy Tarreau44267702011-10-28 15:35:33 +02002901 if (sv->track)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002902 chunk_printf(&msg, "%s/%s,",
Willy Tarreau44267702011-10-28 15:35:33 +02002903 sv->track->proxy->id, sv->track->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002904 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002905 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002906
Willy Tarreau7f062c42009-03-05 18:43:00 +01002907 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002908 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002909
2910 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002911 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002912 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002913 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002914
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002915 if (sv->state & SRV_CHECKED) {
2916 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002917 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002918
2919 /* check_code */
2920 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002921 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002922 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002923 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002924
2925 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002926 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002927 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002928 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002929 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002930
2931 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002932 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002933 }
2934
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002935 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2936 if (px->mode == PR_MODE_HTTP) {
2937 int i;
2938
2939 for (i=1; i<6; i++)
2940 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2941
2942 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2943 } else {
2944 chunk_printf(&msg, ",,,,,,");
2945 }
2946
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002947 /* failed health analyses */
2948 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2949
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002950 /* requests : req_rate, req_rate_max, req_tot, */
2951 chunk_printf(&msg, ",,,");
2952
Willy Tarreauae526782010-03-04 20:34:23 +01002953 /* errors: cli_aborts, srv_aborts */
2954 chunk_printf(&msg, "%lld,%lld,",
2955 sv->counters.cli_aborts, sv->counters.srv_aborts);
2956
Willy Tarreau7f062c42009-03-05 18:43:00 +01002957 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002958 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002959 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002960 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002961 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002962 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002963
Willy Tarreau295a8372011-03-10 11:25:07 +01002964 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02002965 /* fall through */
2966
Willy Tarreau295a8372011-03-10 11:25:07 +01002967 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02002968 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002969 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002970 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2971 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002972 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01002973 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002974 /* Column sub-heading for Enable or Disable server */
2975 chunk_printf(&msg, "<td></td>");
2976 }
2977 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002978
2979 if (uri->flags&ST_SHLGNDS) {
2980 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002981 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002982 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002983
2984 /* cookie */
2985 if (px->cookie_name) {
2986 struct chunk src;
2987
2988 chunk_printf(&msg, ", cookie: '");
2989
2990 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2991 chunk_htmlencode(&msg, &src);
2992
2993 chunk_printf(&msg, "'");
2994 }
2995
2996 chunk_printf(&msg, "\"");
2997
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002998 }
2999
3000 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02003001 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01003002 ">%s<a name=\"%s/Backend\"></a>"
3003 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02003004 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003005 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003006 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003007 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003008 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003009 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02003010 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01003011 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003012 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
3013 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02003014
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003015 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003016 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003017 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003018 "<td"
3019 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003020 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003021
3022 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3023 if (px->mode == PR_MODE_HTTP) {
3024 int i;
3025
3026 chunk_printf(&msg, " title=\"rsp codes:");
3027
3028 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003029 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003030
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003031 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003032 }
3033
3034 chunk_printf(&msg,
3035 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01003036 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003037 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003038 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003039 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003040 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003041 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01003042 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003043 U2H7(px->be_counters.cum_lbconn),
3044 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003045
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003046 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02003047 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003048 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01003049 /* errors : request, connect */
3050 "<td></td><td>%s</td>"
3051 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01003052 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003053 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003054 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003055 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02003056 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003057 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02003058 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003059 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3060 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003061 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003062 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
3063 U2H2(px->be_counters.failed_conns),
3064 px->be_counters.cli_aborts,
3065 px->be_counters.srv_aborts,
3066 U2H5(px->be_counters.failed_resp),
3067 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003068 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01003069 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3070 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01003071 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003072 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003073
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003074 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003075 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003076 "<td class=ac>&nbsp;</td><td>%d</td>"
3077 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003078 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003079 "</tr>",
3080 px->down_trans,
3081 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003082 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003083 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02003084 /* pxid, name */
3085 "%s,BACKEND,"
3086 /* queue : current, max */
3087 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003088 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003089 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003090 /* bytes : in, out */
3091 "%lld,%lld,"
3092 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003093 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003094 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003095 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003096 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003097 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003098 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02003099 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003100 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02003101 * active and backups. */
3102 "%s,"
3103 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003104 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003105 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003106 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003107 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003108 /* rate, rate_lim, rate_max, */
3109 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003110 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003111 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02003112 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003113 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3114 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3115 px->be_counters.bytes_in, px->be_counters.bytes_out,
3116 px->be_counters.denied_req, px->be_counters.denied_resp,
3117 px->be_counters.failed_conns, px->be_counters.failed_resp,
3118 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01003119 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01003120 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003121 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003122 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01003123 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003124 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003125 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003126 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003127 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003128
3129 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3130 if (px->mode == PR_MODE_HTTP) {
3131 int i;
3132
3133 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003134 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003135
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003136 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003137 } else {
3138 chunk_printf(&msg, ",,,,,,");
3139 }
3140
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003141 /* failed health analyses */
3142 chunk_printf(&msg, ",");
3143
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003144 /* requests : req_rate, req_rate_max, req_tot, */
3145 chunk_printf(&msg, ",,,");
3146
Willy Tarreauae526782010-03-04 20:34:23 +01003147 /* errors: cli_aborts, srv_aborts */
3148 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003149 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01003150
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003151 /* finish with EOL */
3152 chunk_printf(&msg, "\n");
3153
Willy Tarreau55bb8452007-10-17 18:44:57 +02003154 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003155 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003156 return 0;
3157 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003158
Willy Tarreau295a8372011-03-10 11:25:07 +01003159 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02003160 /* fall through */
3161
Willy Tarreau295a8372011-03-10 11:25:07 +01003162 case STAT_PX_ST_END:
3163 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003164 chunk_printf(&msg, "</table>");
3165
Willy Tarreau295a8372011-03-10 11:25:07 +01003166 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003167 /* close the form used to enable/disable this proxy servers */
3168 chunk_printf(&msg,
3169 "Choose the action to perform on the checked servers : "
3170 "<select name=action>"
3171 "<option value=\"\"></option>"
3172 "<option value=\"disable\">Disable</option>"
3173 "<option value=\"enable\">Enable</option>"
3174 "</select>"
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003175 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003176 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3177 "</form>",
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003178 px->uuid);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003179 }
3180
3181 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02003182
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003183 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003184 return 0;
3185 }
Willy Tarreau91861262007-10-17 17:06:05 +02003186
Willy Tarreau295a8372011-03-10 11:25:07 +01003187 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02003188 /* fall through */
3189
Willy Tarreau295a8372011-03-10 11:25:07 +01003190 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02003191 return 1;
3192
3193 default:
3194 /* unknown state, we should put an abort() here ! */
3195 return 1;
3196 }
3197}
3198
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003199/* This function dumps a complete session state onto the stream intreface's
3200 * read buffer. The data_ctx must have been zeroed first, and the flags
3201 * properly set. The session has to be set in data_ctx.sess.target. It returns
3202 * 0 if the output buffer is full and it needs to be called again, otherwise
3203 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003204 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003205static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003206{
3207 struct tm tm;
3208 struct chunk msg;
3209 struct session *sess;
3210 extern const char *monthname[12];
3211 char pn[INET6_ADDRSTRLEN];
3212
David du Colombier7af46052012-05-16 14:16:48 +02003213 chunk_init(&msg, trash, trashlen);
Willy Tarreau295a8372011-03-10 11:25:07 +01003214 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003215
Willy Tarreau295a8372011-03-10 11:25:07 +01003216 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003217 /* session changed, no need to go any further */
3218 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003219 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003220 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003221 si->applet.ctx.sess.target = NULL;
3222 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003223 return 1;
3224 }
3225
Willy Tarreau295a8372011-03-10 11:25:07 +01003226 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003227 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003228 si->applet.ctx.sess.uid = sess->uniq_id;
3229 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003230 /* fall through */
3231
3232 case 1:
3233 chunk_printf(&msg,
3234 "%p: id=%u, proto=%s",
3235 sess,
3236 sess->uniq_id,
3237 sess->listener->proto->name);
3238
Willy Tarreau6471afb2011-09-23 10:54:59 +02003239 switch (addr_to_str(&sess->si[0].addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003240 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003241 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003242 chunk_printf(&msg, " source=%s:%d\n",
Willy Tarreau6471afb2011-09-23 10:54:59 +02003243 pn, get_host_port(&sess->si[0].addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003244 break;
3245 case AF_UNIX:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003246 chunk_printf(&msg, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003247 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003248 default:
3249 /* no more information to print right now */
3250 chunk_printf(&msg, "\n");
3251 break;
3252 }
3253
3254 chunk_printf(&msg,
3255 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003256 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003257
3258 chunk_printf(&msg,
Daniel Schultze90690c72012-03-23 10:53:36 -07003259 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003260 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3261 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3262 sess->listener ? sess->listener->luid : 0);
3263
Willy Tarreau59b94792012-05-11 16:16:40 +02003264 si_get_to_addr(&sess->si[0]);
Daniel Schultze90690c72012-03-23 10:53:36 -07003265 switch (addr_to_str(&sess->si[0].addr.to, pn, sizeof(pn))) {
3266 case AF_INET:
3267 case AF_INET6:
3268 chunk_printf(&msg, " addr=%s:%d\n",
3269 pn, get_host_port(&sess->si[0].addr.to));
3270 break;
3271 case AF_UNIX:
3272 chunk_printf(&msg, " addr=unix:%d\n", sess->listener->luid);
3273 break;
3274 default:
3275 /* no more information to print right now */
3276 chunk_printf(&msg, "\n");
3277 break;
3278 }
3279
Willy Tarreau50943332011-09-02 17:33:05 +02003280 if (sess->be->cap & PR_CAP_BE)
3281 chunk_printf(&msg,
Daniel Schultze90690c72012-03-23 10:53:36 -07003282 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003283 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003284 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3285 else
3286 chunk_printf(&msg, " backend=<NONE> (id=-1 mode=-)");
3287
Willy Tarreau59b94792012-05-11 16:16:40 +02003288 si_get_from_addr(&sess->si[1]);
Daniel Schultze90690c72012-03-23 10:53:36 -07003289 switch (addr_to_str(&sess->si[1].addr.from, pn, sizeof(pn))) {
3290 case AF_INET:
3291 case AF_INET6:
3292 chunk_printf(&msg, " addr=%s:%d\n",
3293 pn, get_host_port(&sess->si[1].addr.from));
3294 break;
3295 case AF_UNIX:
3296 chunk_printf(&msg, " addr=unix\n");
3297 break;
3298 default:
3299 /* no more information to print right now */
3300 chunk_printf(&msg, "\n");
3301 break;
3302 }
3303
3304 if (sess->be->cap & PR_CAP_BE)
3305 chunk_printf(&msg,
3306 " server=%s (id=%u)",
Willy Tarreau50943332011-09-02 17:33:05 +02003307 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
3308 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
3309 else
Daniel Schultze90690c72012-03-23 10:53:36 -07003310 chunk_printf(&msg, " server=<NONE> (id=-1)");
3311
Willy Tarreau59b94792012-05-11 16:16:40 +02003312 si_get_to_addr(&sess->si[1]);
Daniel Schultze90690c72012-03-23 10:53:36 -07003313 switch (addr_to_str(&sess->si[1].addr.to, pn, sizeof(pn))) {
3314 case AF_INET:
3315 case AF_INET6:
3316 chunk_printf(&msg, " addr=%s:%d\n",
3317 pn, get_host_port(&sess->si[1].addr.to));
3318 break;
3319 case AF_UNIX:
3320 chunk_printf(&msg, " addr=unix\n");
3321 break;
3322 default:
3323 /* no more information to print right now */
3324 chunk_printf(&msg, "\n");
3325 break;
3326 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003327
3328 chunk_printf(&msg,
3329 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
3330 sess->task,
3331 sess->task->state,
3332 sess->task->nice, sess->task->calls,
3333 sess->task->expire ?
3334 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3335 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3336 TICKS_TO_MS(1000)) : "<NEVER>",
3337 task_in_rq(sess->task) ? ", running" : "");
3338
3339 get_localtime(sess->logs.accept_date.tv_sec, &tm);
3340 chunk_printf(&msg,
3341 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
3342 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3343 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3344 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3345
3346 chunk_printf(&msg,
3347 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3348 &sess->si[0],
3349 sess->si[0].state,
3350 sess->si[0].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003351 si_fd(&sess->si[0]),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003352 sess->si[0].exp ?
3353 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3354 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3355 TICKS_TO_MS(1000)) : "<NEVER>",
3356 sess->si[0].err_type);
3357
3358 chunk_printf(&msg,
3359 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3360 &sess->si[1],
3361 sess->si[1].state,
3362 sess->si[1].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003363 si_fd(&sess->si[1]),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003364 sess->si[1].exp ?
3365 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3366 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3367 TICKS_TO_MS(1000)) : "<NEVER>",
3368 sess->si[1].err_type);
3369
3370 chunk_printf(&msg,
3371 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3372 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3373 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3374
3375
3376 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003377 " req=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003378 " an_exp=%s",
3379 sess->req,
3380 sess->req->flags, sess->req->analysers,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003381 sess->req->i, sess->req->o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003382 sess->req->pipe ? sess->req->pipe->data : 0,
3383 sess->req->to_forward,
3384 sess->req->analyse_exp ?
3385 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3386 TICKS_TO_MS(1000)) : "<NEVER>");
3387
3388 chunk_printf(&msg,
3389 " rex=%s",
3390 sess->req->rex ?
3391 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3392 TICKS_TO_MS(1000)) : "<NEVER>");
3393
3394 chunk_printf(&msg,
3395 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003396 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003397 sess->req->wex ?
3398 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3399 TICKS_TO_MS(1000)) : "<NEVER>",
3400 sess->req->data,
Willy Tarreau89fa7062012-03-02 16:13:16 +01003401 (int)(sess->req->p - sess->req->data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003402 sess->txn.req.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003403 sess->req->total);
3404
3405 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003406 " res=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003407 " an_exp=%s",
3408 sess->rep,
3409 sess->rep->flags, sess->rep->analysers,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003410 sess->rep->i, sess->rep->o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003411 sess->rep->pipe ? sess->rep->pipe->data : 0,
3412 sess->rep->to_forward,
3413 sess->rep->analyse_exp ?
3414 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3415 TICKS_TO_MS(1000)) : "<NEVER>");
3416
3417 chunk_printf(&msg,
3418 " rex=%s",
3419 sess->rep->rex ?
3420 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3421 TICKS_TO_MS(1000)) : "<NEVER>");
3422
3423 chunk_printf(&msg,
3424 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003425 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003426 sess->rep->wex ?
3427 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3428 TICKS_TO_MS(1000)) : "<NEVER>",
3429 sess->rep->data,
Willy Tarreau89fa7062012-03-02 16:13:16 +01003430 (int)(sess->rep->p - sess->rep->data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003431 sess->txn.rsp.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003432 sess->rep->total);
3433
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003434 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003435 return 0;
3436
3437 /* use other states to dump the contents */
3438 }
3439 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003440 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003441 return 1;
3442}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003443
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003444/* This function dumps all sessions' states onto the stream intreface's
3445 * read buffer. The data_ctx must have been zeroed first, and the flags
3446 * properly set. It returns 0 if the output buffer is full and it needs
3447 * to be called again, otherwise non-zero. It is designed to be called
3448 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003449 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003450static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003451{
3452 struct chunk msg;
3453
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003454 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003455 /* If we're forced to shut down, we might have to remove our
3456 * reference to the last session being dumped.
3457 */
Willy Tarreau94981132012-05-21 17:09:48 +02003458 if (si->conn.data_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003459 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3460 LIST_DEL(&si->applet.ctx.sess.bref.users);
3461 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003462 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003463 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003464 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003465 }
3466
David du Colombier7af46052012-05-16 14:16:48 +02003467 chunk_init(&msg, trash, trashlen);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003468
Willy Tarreau94981132012-05-21 17:09:48 +02003469 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003470 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003471 /* the function had not been called yet, let's prepare the
3472 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003473 * pointer to the first in the global list. When a target
3474 * session is being destroyed, it is responsible for updating
3475 * this pointer. We know we have reached the end when this
3476 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003477 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003478 LIST_INIT(&si->applet.ctx.sess.bref.users);
3479 si->applet.ctx.sess.bref.ref = sessions.n;
Willy Tarreau94981132012-05-21 17:09:48 +02003480 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003481 /* fall through */
3482
Willy Tarreau295a8372011-03-10 11:25:07 +01003483 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003484 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003485 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3486 LIST_DEL(&si->applet.ctx.sess.bref.users);
3487 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003488 }
3489
3490 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003491 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003492 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003493 struct session *curr_sess;
3494
Willy Tarreau295a8372011-03-10 11:25:07 +01003495 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003496
Willy Tarreau295a8372011-03-10 11:25:07 +01003497 if (si->applet.ctx.sess.target) {
3498 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003499 goto next_sess;
3500
Willy Tarreau295a8372011-03-10 11:25:07 +01003501 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003502 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003503 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003504 return 0;
3505
3506 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003507 LIST_DEL(&si->applet.ctx.sess.bref.users);
3508 LIST_INIT(&si->applet.ctx.sess.bref.users);
3509 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003510 break;
3511 }
3512
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003513 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003514 "%p: proto=%s",
3515 curr_sess,
3516 curr_sess->listener->proto->name);
3517
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003518
Willy Tarreau6471afb2011-09-23 10:54:59 +02003519 switch (addr_to_str(&curr_sess->si[0].addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003520 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003521 case AF_INET6:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003522 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003523 " src=%s:%d fe=%s be=%s srv=%s",
3524 pn,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003525 get_host_port(&curr_sess->si[0].addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003526 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003527 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003528 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003529 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003530 break;
3531 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02003532 chunk_printf(&msg,
3533 " src=unix:%d fe=%s be=%s srv=%s",
3534 curr_sess->listener->luid,
3535 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003536 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003537 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003538 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003539 break;
3540 }
3541
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003542 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003543 " ts=%02x age=%s calls=%d",
3544 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003545 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3546 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003547
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003548 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003549 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003550 curr_sess->req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003551 curr_sess->req->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003552 curr_sess->req->analysers,
3553 curr_sess->req->rex ?
3554 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3555 TICKS_TO_MS(1000)) : "");
3556
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003557 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003558 ",wx=%s",
3559 curr_sess->req->wex ?
3560 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3561 TICKS_TO_MS(1000)) : "");
3562
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003563 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003564 ",ax=%s]",
3565 curr_sess->req->analyse_exp ?
3566 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3567 TICKS_TO_MS(1000)) : "");
3568
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003569 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003570 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003571 curr_sess->rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003572 curr_sess->rep->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003573 curr_sess->rep->analysers,
3574 curr_sess->rep->rex ?
3575 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3576 TICKS_TO_MS(1000)) : "");
3577
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003578 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003579 ",wx=%s",
3580 curr_sess->rep->wex ?
3581 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3582 TICKS_TO_MS(1000)) : "");
3583
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003584 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003585 ",ax=%s]",
3586 curr_sess->rep->analyse_exp ?
3587 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3588 TICKS_TO_MS(1000)) : "");
3589
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003590 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003591 " s0=[%d,%1xh,fd=%d,ex=%s]",
3592 curr_sess->si[0].state,
3593 curr_sess->si[0].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003594 si_fd(&curr_sess->si[0]),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003595 curr_sess->si[0].exp ?
3596 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3597 TICKS_TO_MS(1000)) : "");
3598
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003599 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003600 " s1=[%d,%1xh,fd=%d,ex=%s]",
3601 curr_sess->si[1].state,
3602 curr_sess->si[1].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003603 si_fd(&curr_sess->si[1]),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003604 curr_sess->si[1].exp ?
3605 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3606 TICKS_TO_MS(1000)) : "");
3607
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003608 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003609 " exp=%s",
3610 curr_sess->task->expire ?
3611 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3612 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003613 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003614 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003615
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003616 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003617
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003618 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003619 /* let's try again later from this session. We add ourselves into
3620 * this session's users so that it can remove us upon termination.
3621 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003622 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003623 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003624 }
3625
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003626 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003627 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003628 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003629
Willy Tarreau295a8372011-03-10 11:25:07 +01003630 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003631 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003632 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003633 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3634 else
3635 chunk_printf(&msg, "Session not found.\n");
3636
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003637 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003638 return 0;
3639
Willy Tarreau295a8372011-03-10 11:25:07 +01003640 si->applet.ctx.sess.target = NULL;
3641 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003642 return 1;
3643 }
3644
Willy Tarreau94981132012-05-21 17:09:48 +02003645 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003646 /* fall through */
3647
3648 default:
Willy Tarreau94981132012-05-21 17:09:48 +02003649 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003650 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003651 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003652}
3653
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003654/* This function dumps all tables' states onto the stream intreface's
3655 * read buffer. The data_ctx must have been zeroed first, and the flags
3656 * properly set. It returns 0 if the output buffer is full and it needs
3657 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003658 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003659static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003660{
Willy Tarreau94981132012-05-21 17:09:48 +02003661 struct session *s = si->conn.data_ctx;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003662 struct chunk msg;
3663 struct ebmb_node *eb;
3664 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003665 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003666
3667 /*
Willy Tarreau94981132012-05-21 17:09:48 +02003668 * We have 3 possible states in si->conn.data_st :
Willy Tarreau295a8372011-03-10 11:25:07 +01003669 * - STAT_ST_INIT : the first call
3670 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003671 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003672 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003673 * and the entry pointer points to the next entry to be dumped,
3674 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003675 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003676 * data though.
3677 */
3678
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003679 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003680 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau94981132012-05-21 17:09:48 +02003681 if (si->conn.data_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003682 si->applet.ctx.table.entry->ref_cnt--;
3683 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003684 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003685 return 1;
3686 }
3687
David du Colombier7af46052012-05-16 14:16:48 +02003688 chunk_init(&msg, trash, trashlen);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003689
Willy Tarreau94981132012-05-21 17:09:48 +02003690 while (si->conn.data_st != STAT_ST_FIN) {
3691 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003692 case STAT_ST_INIT:
3693 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3694 if (!si->applet.ctx.table.proxy)
3695 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003696
Willy Tarreau295a8372011-03-10 11:25:07 +01003697 si->applet.ctx.table.entry = NULL;
Willy Tarreau94981132012-05-21 17:09:48 +02003698 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003699 break;
3700
Willy Tarreau295a8372011-03-10 11:25:07 +01003701 case STAT_ST_INFO:
3702 if (!si->applet.ctx.table.proxy ||
3703 (si->applet.ctx.table.target &&
3704 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
Willy Tarreau94981132012-05-21 17:09:48 +02003705 si->conn.data_st = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003706 break;
3707 }
3708
Willy Tarreau295a8372011-03-10 11:25:07 +01003709 if (si->applet.ctx.table.proxy->table.size) {
Simon Hormanc88b8872011-06-15 15:18:49 +09003710 if (show && !stats_dump_table_head_to_buffer(&msg, si, si->applet.ctx.table.proxy,
3711 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003712 return 0;
3713
Willy Tarreau295a8372011-03-10 11:25:07 +01003714 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003715 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
3716 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003717 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003718 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003719 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3720 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau94981132012-05-21 17:09:48 +02003721 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003722 break;
3723 }
3724 }
3725 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003726 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003727 break;
3728
Willy Tarreau295a8372011-03-10 11:25:07 +01003729 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003730 skip_entry = false;
3731
Willy Tarreau295a8372011-03-10 11:25:07 +01003732 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003733 /* we're filtering on some data contents */
3734 void *ptr;
3735 long long data;
3736
Willy Tarreau295a8372011-03-10 11:25:07 +01003737 dt = si->applet.ctx.table.data_type;
3738 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3739 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003740 dt);
3741
3742 data = 0;
3743 switch (stktable_data_types[dt].std_type) {
3744 case STD_T_SINT:
3745 data = stktable_data_cast(ptr, std_t_sint);
3746 break;
3747 case STD_T_UINT:
3748 data = stktable_data_cast(ptr, std_t_uint);
3749 break;
3750 case STD_T_ULL:
3751 data = stktable_data_cast(ptr, std_t_ull);
3752 break;
3753 case STD_T_FRQP:
3754 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003755 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003756 break;
3757 }
3758
3759 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003760 if ((data < si->applet.ctx.table.value &&
3761 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3762 si->applet.ctx.table.data_op == STD_OP_GT ||
3763 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3764 (data == si->applet.ctx.table.value &&
3765 (si->applet.ctx.table.data_op == STD_OP_NE ||
3766 si->applet.ctx.table.data_op == STD_OP_GT ||
3767 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3768 (data > si->applet.ctx.table.value &&
3769 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3770 si->applet.ctx.table.data_op == STD_OP_LT ||
3771 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003772 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003773 }
3774
Simon Hormanc88b8872011-06-15 15:18:49 +09003775 if (show && !skip_entry &&
3776 !stats_dump_table_entry_to_buffer(&msg, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003777 si->applet.ctx.table.entry))
3778 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003779
Willy Tarreau295a8372011-03-10 11:25:07 +01003780 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003781
Willy Tarreau295a8372011-03-10 11:25:07 +01003782 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003783 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003784 struct stksess *old = si->applet.ctx.table.entry;
3785 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01003786 if (show)
3787 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3788 else
3789 stksess_kill(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau295a8372011-03-10 11:25:07 +01003790 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003791 break;
3792 }
3793
Simon Hormanc88b8872011-06-15 15:18:49 +09003794
3795 if (show)
3796 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3797 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3798 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3799
Willy Tarreau295a8372011-03-10 11:25:07 +01003800 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau94981132012-05-21 17:09:48 +02003801 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003802 break;
3803
Willy Tarreau295a8372011-03-10 11:25:07 +01003804 case STAT_ST_END:
Willy Tarreau94981132012-05-21 17:09:48 +02003805 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003806 break;
3807 }
3808 }
3809 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003810}
3811
Willy Tarreaud426a182010-03-05 14:58:26 +01003812/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003813 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3814 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3815 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3816 * lines are respected within the limit of 70 output chars. Lines that are
3817 * continuation of a previous truncated line begin with "+" instead of " "
3818 * after the offset. The new pointer is returned.
3819 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003820static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3821 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003822{
3823 int end;
3824 unsigned char c;
3825
3826 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003827 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003828 return ptr;
3829
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003830 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003831
Willy Tarreaud426a182010-03-05 14:58:26 +01003832 while (ptr < len && ptr < bsize) {
3833 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003834 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003835 if (out->len > end - 2)
3836 break;
3837 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003838 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003839 if (out->len > end - 3)
3840 break;
3841 out->str[out->len++] = '\\';
3842 switch (c) {
3843 case '\t': c = 't'; break;
3844 case '\n': c = 'n'; break;
3845 case '\r': c = 'r'; break;
3846 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003847 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003848 }
3849 out->str[out->len++] = c;
3850 } else {
3851 if (out->len > end - 5)
3852 break;
3853 out->str[out->len++] = '\\';
3854 out->str[out->len++] = 'x';
3855 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3856 out->str[out->len++] = hextab[c & 0xF];
3857 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003858 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003859 /* we had a line break, let's return now */
3860 out->str[out->len++] = '\n';
3861 *line = ptr;
3862 return ptr;
3863 }
3864 }
3865 /* we have an incomplete line, we return it as-is */
3866 out->str[out->len++] = '\n';
3867 return ptr;
3868}
3869
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003870/* This function dumps all captured errors onto the stream intreface's
3871 * read buffer. The data_ctx must have been zeroed first, and the flags
3872 * properly set. It returns 0 if the output buffer is full and it needs
3873 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003874 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003875static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003876{
3877 extern const char *monthname[12];
3878 struct chunk msg;
3879
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003880 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003881 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003882
David du Colombier7af46052012-05-16 14:16:48 +02003883 chunk_init(&msg, trash, trashlen);
Willy Tarreau74808cb2009-03-04 15:53:18 +01003884
Willy Tarreau295a8372011-03-10 11:25:07 +01003885 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003886 /* the function had not been called yet, let's prepare the
3887 * buffer for a response.
3888 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003889 struct tm tm;
3890
3891 get_localtime(date.tv_sec, &tm);
3892 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3893 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3894 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3895 error_snapshot_id);
3896
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003897 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003898 /* Socket buffer full. Let's try again later from the same point */
3899 return 0;
3900 }
3901
Willy Tarreau295a8372011-03-10 11:25:07 +01003902 si->applet.ctx.errors.px = proxy;
3903 si->applet.ctx.errors.buf = 0;
3904 si->applet.ctx.errors.bol = 0;
3905 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003906 }
3907
3908 /* we have two inner loops here, one for the proxy, the other one for
3909 * the buffer.
3910 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003911 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003912 struct error_snapshot *es;
3913
Willy Tarreau295a8372011-03-10 11:25:07 +01003914 if (si->applet.ctx.errors.buf == 0)
3915 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003916 else
Willy Tarreau295a8372011-03-10 11:25:07 +01003917 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003918
3919 if (!es->when.tv_sec)
3920 goto next;
3921
Willy Tarreau295a8372011-03-10 11:25:07 +01003922 if (si->applet.ctx.errors.iid >= 0 &&
3923 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
3924 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003925 goto next;
3926
Willy Tarreau295a8372011-03-10 11:25:07 +01003927 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003928 /* just print headers now */
3929
3930 char pn[INET6_ADDRSTRLEN];
3931 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003932 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003933
3934 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003935 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003936 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003937 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003938
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003939 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
3940 case AF_INET:
3941 case AF_INET6:
3942 port = get_host_port(&es->src);
3943 break;
3944 default:
3945 port = 0;
3946 }
3947
Willy Tarreau295a8372011-03-10 11:25:07 +01003948 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003949 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003950 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003951 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003952 " backend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01003953 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003954 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3955 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01003956 break;
3957 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003958 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003959 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003960 " frontend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01003961 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003962 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01003963 break;
3964 }
3965
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02003966 chunk_printf(&msg,
3967 ", server %s (#%d), event #%u\n"
3968 " src %s:%d, session #%d, session flags 0x%08x\n"
3969 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
3970 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
3971 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
3972 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
3973 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
3974 es->ev_id,
3975 pn, port, es->sid, es->s_flags,
3976 es->state, es->m_flags, es->t_flags,
3977 es->m_clen, es->m_blen,
3978 es->b_flags, es->b_out, es->b_tot,
3979 es->len, es->b_wrap, es->pos);
3980
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003981 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003982 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003983 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003984 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003985 si->applet.ctx.errors.ptr = 0;
3986 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003987 }
3988
Willy Tarreau295a8372011-03-10 11:25:07 +01003989 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003990 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003991 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003992 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003993 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02003994 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003995 goto next;
3996 }
3997
3998 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01003999 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004000 int newptr;
4001 int newline;
4002
Willy Tarreau295a8372011-03-10 11:25:07 +01004003 newline = si->applet.ctx.errors.bol;
4004 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
4005 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004006 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004007
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004008 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004009 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004010 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004011 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004012 si->applet.ctx.errors.ptr = newptr;
4013 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004014 };
4015 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01004016 si->applet.ctx.errors.bol = 0;
4017 si->applet.ctx.errors.ptr = -1;
4018 si->applet.ctx.errors.buf++;
4019 if (si->applet.ctx.errors.buf > 1) {
4020 si->applet.ctx.errors.buf = 0;
4021 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004022 }
4023 }
4024
4025 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004026 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004027}
4028
Willy Tarreaub24281b2011-02-13 13:16:36 +01004029struct si_applet http_stats_applet = {
4030 .name = "<STATS>", /* used for logging */
4031 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004032 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004033};
4034
Simon Horman9bd2c732011-06-15 15:18:44 +09004035static struct si_applet cli_applet = {
Willy Tarreaub24281b2011-02-13 13:16:36 +01004036 .name = "<CLI>", /* used for logging */
4037 .fct = cli_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004038 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004039};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004040
Willy Tarreau10522fd2008-07-09 20:12:41 +02004041static struct cfg_kw_list cfg_kws = {{ },{
4042 { CFG_GLOBAL, "stats", stats_parse_global },
4043 { 0, NULL, NULL },
4044}};
4045
4046__attribute__((constructor))
4047static void __dumpstats_module_init(void)
4048{
4049 cfg_register_keywords(&cfg_kws);
4050}
4051
Willy Tarreau91861262007-10-17 17:06:05 +02004052/*
4053 * Local variables:
4054 * c-indent-level: 8
4055 * c-basic-offset: 8
4056 * End:
4057 */