blob: a125242c1852ae8bb6d1e2aa1fb240a1b0ed2e59 [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 Tarreau654694e2012-06-07 01:03:16 +020083 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +010084 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +020085 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +020086 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +020087 " disable : put a server or frontend in maintenance mode\n"
88 " enable : re-enable a server or frontend which is in maintenance mode\n"
89 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020090 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020091
Simon Horman9bd2c732011-06-15 15:18:44 +090092static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +020093 "Permission denied\n"
94 "";
95
Willy Tarreau295a8372011-03-10 11:25:07 +010096/* data transmission states for the stats responses */
97enum {
98 STAT_ST_INIT = 0,
99 STAT_ST_HEAD,
100 STAT_ST_INFO,
101 STAT_ST_LIST,
102 STAT_ST_END,
103 STAT_ST_FIN,
104};
105
106/* data transmission states for the stats responses inside a proxy */
107enum {
108 STAT_PX_ST_INIT = 0,
109 STAT_PX_ST_TH,
110 STAT_PX_ST_FE,
111 STAT_PX_ST_LI,
112 STAT_PX_ST_SV,
113 STAT_PX_ST_BE,
114 STAT_PX_ST_END,
115 STAT_PX_ST_FIN,
116};
117
Cyril Bonté19979e12012-04-04 12:57:21 +0200118extern const char *stat_status_codes[];
119
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200120/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +0200121 * a new stats socket. It returns a positive value upon success, 0 if the connection
122 * needs to be closed and ignored, or a negative value upon critical failure.
123 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900124static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200125{
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200126 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100127 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200128 copy_target(&s->target, &s->si[1].target); // for logging only
Willy Tarreau94981132012-05-21 17:09:48 +0200129 s->si[1].conn.data_ctx = s;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100130 s->si[1].applet.st1 = 0;
131 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200132
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200133 tv_zero(&s->logs.tv_request);
134 s->logs.t_queue = 0;
135 s->logs.t_connect = 0;
136 s->logs.t_data = 0;
137 s->logs.t_close = 0;
138 s->logs.bytes_in = s->logs.bytes_out = 0;
139 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
140 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200141
Willy Tarreaueb472682010-05-28 18:46:57 +0200142 s->req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
143
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200144 if (s->listener->timeout) {
145 s->req->rto = *s->listener->timeout;
146 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200147 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200148 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200149}
150
Willy Tarreau07e9e642010-08-17 21:48:17 +0200151/* allocate a new stats frontend named <name>, and return it
152 * (or NULL in case of lack of memory).
153 */
154static struct proxy *alloc_stats_fe(const char *name)
155{
156 struct proxy *fe;
157
158 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
159 if (!fe)
160 return NULL;
161
Willy Tarreau237250c2011-07-29 01:49:03 +0200162 init_new_proxy(fe);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200163
164 fe->last_change = now.tv_sec;
165 fe->id = strdup("GLOBAL");
166 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200167 fe->maxconn = 10; /* default to 10 concurrent connections */
168 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
169
Willy Tarreau07e9e642010-08-17 21:48:17 +0200170 return fe;
171}
172
Willy Tarreaufbee7132007-10-18 13:53:22 +0200173/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200174 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
175 * error message into the <err> buffer which will be preallocated. The trailing
176 * '\n' must not be written. The function must be called with <args> pointing to
177 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200178 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200179static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200180 struct proxy *defpx, char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200181{
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200182 if (!strcmp(args[1], "socket")) {
Andreas Kohn16171e22011-01-19 20:29:32 +0100183 struct sockaddr_un *su;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200184 int cur_arg;
185
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200186 if (*args[2] == 0) {
187 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 +0200188 return -1;
189 }
190
191 if (global.stats_sock.state != LI_NEW) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200192 memprintf(err, "'%s %s' already specified in global section", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200193 return -1;
194 }
195
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200196 su = str2sun(args[2]);
Andreas Kohn16171e22011-01-19 20:29:32 +0100197 if (!su) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200198 memprintf(err, "'%s %s' : path would require truncation", args[0], args[1]);
Andreas Kohn16171e22011-01-19 20:29:32 +0100199 return -1;
200 }
201 memcpy(&global.stats_sock.addr, su, sizeof(struct sockaddr_un)); // guaranteed to fit
Willy Tarreaufbee7132007-10-18 13:53:22 +0200202
Willy Tarreau89a63132009-08-16 17:41:45 +0200203 if (!global.stats_fe) {
Willy Tarreau07e9e642010-08-17 21:48:17 +0200204 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200205 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200206 return -1;
207 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200208 }
209
Willy Tarreaufbee7132007-10-18 13:53:22 +0200210 global.stats_sock.state = LI_INIT;
Willy Tarreau3c63fd82011-09-07 18:00:47 +0200211 global.stats_sock.options = LI_O_UNLIMITED;
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200212 global.stats_sock.accept = session_accept;
213 global.stats_fe->accept = stats_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200214 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200215 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200216 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreaueb472682010-05-28 18:46:57 +0200217 global.stats_sock.frontend = global.stats_fe;
Willy Tarreau6162db22009-10-10 17:13:00 +0200218 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200219 global.stats_sock.maxconn = global.stats_fe->maxconn;
Willy Tarreau89a63132009-08-16 17:41:45 +0200220 global.stats_sock.timeout = &global.stats_fe->timeout.client;
221
222 global.stats_sock.next = global.stats_fe->listen;
223 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200224
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200225 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200226 while (*args[cur_arg]) {
227 if (!strcmp(args[cur_arg], "uid")) {
228 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
229 cur_arg += 2;
230 }
231 else if (!strcmp(args[cur_arg], "gid")) {
232 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
233 cur_arg += 2;
234 }
235 else if (!strcmp(args[cur_arg], "mode")) {
236 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
237 cur_arg += 2;
238 }
239 else if (!strcmp(args[cur_arg], "user")) {
240 struct passwd *user;
241 user = getpwnam(args[cur_arg + 1]);
242 if (!user) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200243 memprintf(err, "'%s %s' : unknown user '%s'", args[0], args[1], args[cur_arg + 1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200244 return -1;
245 }
246 global.stats_sock.perm.ux.uid = user->pw_uid;
247 cur_arg += 2;
248 }
249 else if (!strcmp(args[cur_arg], "group")) {
250 struct group *group;
251 group = getgrnam(args[cur_arg + 1]);
252 if (!group) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200253 memprintf(err, "'%s %s' : unknown group '%s'", args[0], args[1], args[cur_arg + 1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200254 return -1;
255 }
256 global.stats_sock.perm.ux.gid = group->gr_gid;
257 cur_arg += 2;
258 }
Willy Tarreau6162db22009-10-10 17:13:00 +0200259 else if (!strcmp(args[cur_arg], "level")) {
260 if (!strcmp(args[cur_arg+1], "user"))
261 global.stats_sock.perm.ux.level = ACCESS_LVL_USER;
262 else if (!strcmp(args[cur_arg+1], "operator"))
263 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER;
264 else if (!strcmp(args[cur_arg+1], "admin"))
265 global.stats_sock.perm.ux.level = ACCESS_LVL_ADMIN;
266 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200267 memprintf(err, "'%s %s' : '%s' only supports 'user', 'operator', and 'admin' (got '%s')",
268 args[0], args[1], args[cur_arg], args[cur_arg+1]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200269 return -1;
270 }
271 cur_arg += 2;
272 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200273 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200274 memprintf(err, "'%s %s' only supports 'user', 'uid', 'group', 'gid', 'level', and 'mode' (got '%s')",
275 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200276 return -1;
277 }
278 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100279
Emeric Brund88fd822012-05-18 18:30:29 +0200280 global.stats_sock.sock = &sock_raw;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200281 uxst_add_listener(&global.stats_sock);
282 global.maxsock++;
283 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200284 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100285 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200286 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100287
288 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200289 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100290 return -1;
291 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200292
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100293 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200294 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200295 return -1;
296 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200297 if (!global.stats_fe) {
298 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200299 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200300 return -1;
301 }
302 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200303 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200304 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200305 else if (!strcmp(args[1], "maxconn")) {
306 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200307
308 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200309 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200310 return -1;
311 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200312
313 if (!global.stats_fe) {
314 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200315 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200316 return -1;
317 }
318 }
319 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200320 }
321 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200322 memprintf(err, "'%s' only supports 'socket', 'maxconn' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200323 return -1;
324 }
325 return 0;
326}
327
Simon Horman9bd2c732011-06-15 15:18:44 +0900328static int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100329{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200330 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100331 "# pxname,svname,"
332 "qcur,qmax,"
333 "scur,smax,slim,stot,"
334 "bin,bout,"
335 "dreq,dresp,"
336 "ereq,econ,eresp,"
337 "wretr,wredis,"
338 "status,weight,act,bck,"
339 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200340 "pid,iid,sid,throttle,lbtot,tracked,type,"
341 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200342 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100343 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100344 "req_rate,req_rate_max,req_tot,"
345 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100346 "\n");
347}
348
Simon Hormand9366582011-06-15 15:18:45 +0900349/* print a string of text buffer to <out>. The format is :
350 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
351 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
352 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
353 */
354static int dump_text(struct chunk *out, const char *buf, int bsize)
355{
356 unsigned char c;
357 int ptr = 0;
358
359 while (buf[ptr] && ptr < bsize) {
360 c = buf[ptr];
361 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
362 if (out->len > out->size - 1)
363 break;
364 out->str[out->len++] = c;
365 }
366 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
367 if (out->len > out->size - 2)
368 break;
369 out->str[out->len++] = '\\';
370 switch (c) {
371 case ' ': c = ' '; break;
372 case '\t': c = 't'; break;
373 case '\n': c = 'n'; break;
374 case '\r': c = 'r'; break;
375 case '\e': c = 'e'; break;
376 case '\\': c = '\\'; break;
377 }
378 out->str[out->len++] = c;
379 }
380 else {
381 if (out->len > out->size - 4)
382 break;
383 out->str[out->len++] = '\\';
384 out->str[out->len++] = 'x';
385 out->str[out->len++] = hextab[(c >> 4) & 0xF];
386 out->str[out->len++] = hextab[c & 0xF];
387 }
388 ptr++;
389 }
390
391 return ptr;
392}
393
394/* print a buffer in hexa.
395 * Print stopped if <bsize> is reached, or if no more place in the chunk.
396 */
397static int dump_binary(struct chunk *out, const char *buf, int bsize)
398{
399 unsigned char c;
400 int ptr = 0;
401
402 while (ptr < bsize) {
403 c = buf[ptr];
404
405 if (out->len > out->size - 2)
406 break;
407 out->str[out->len++] = hextab[(c >> 4) & 0xF];
408 out->str[out->len++] = hextab[c & 0xF];
409
410 ptr++;
411 }
412 return ptr;
413}
414
415/* Dump the status of a table to a stream interface's
416 * read buffer. It returns 0 if the output buffer is full
417 * and needs to be called again, otherwise non-zero.
418 */
419static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
420 struct proxy *proxy, struct proxy *target)
421{
Willy Tarreau94981132012-05-21 17:09:48 +0200422 struct session *s = si->conn.data_ctx;
Simon Hormand9366582011-06-15 15:18:45 +0900423
424 chunk_printf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
425 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
426
427 /* any other information should be dumped here */
428
429 if (target && s->listener->perm.ux.level < ACCESS_LVL_OPER)
430 chunk_printf(msg, "# contents not dumped due to insufficient privileges\n");
431
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200432 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900433 return 0;
434
435 return 1;
436}
437
438/* Dump the a table entry to a stream interface's
439 * read buffer. It returns 0 if the output buffer is full
440 * and needs to be called again, otherwise non-zero.
441 */
442static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
443 struct proxy *proxy, struct stksess *entry)
444{
445 int dt;
446
447 chunk_printf(msg, "%p:", entry);
448
449 if (proxy->table.type == STKTABLE_TYPE_IP) {
450 char addr[INET_ADDRSTRLEN];
451 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
452 chunk_printf(msg, " key=%s", addr);
453 }
454 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
455 char addr[INET6_ADDRSTRLEN];
456 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
457 chunk_printf(msg, " key=%s", addr);
458 }
459 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
460 chunk_printf(msg, " key=%u", *(unsigned int *)entry->key.key);
461 }
462 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
463 chunk_printf(msg, " key=");
464 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
465 }
466 else {
467 chunk_printf(msg, " key=");
468 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
469 }
470
471 chunk_printf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
472
473 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
474 void *ptr;
475
476 if (proxy->table.data_ofs[dt] == 0)
477 continue;
478 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
479 chunk_printf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
480 else
481 chunk_printf(msg, " %s=", stktable_data_types[dt].name);
482
483 ptr = stktable_data_ptr(&proxy->table, entry, dt);
484 switch (stktable_data_types[dt].std_type) {
485 case STD_T_SINT:
486 chunk_printf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
487 break;
488 case STD_T_UINT:
489 chunk_printf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
490 break;
491 case STD_T_ULL:
492 chunk_printf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
493 break;
494 case STD_T_FRQP:
495 chunk_printf(msg, "%d",
496 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
497 proxy->table.data_arg[dt].u));
498 break;
499 }
500 }
501 chunk_printf(msg, "\n");
502
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200503 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900504 return 0;
505
506 return 1;
507}
508
Willy Tarreaudec98142012-06-06 23:37:08 +0200509static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900510{
Willy Tarreau94981132012-05-21 17:09:48 +0200511 struct session *s = si->conn.data_ctx;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900512 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900513 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900514 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900515 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreaudec98142012-06-06 23:37:08 +0200516 struct chunk msg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200517 long long value;
518 int data_type;
519 void *ptr;
520 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900521
Simon Hormand5b9fd92011-06-15 15:18:48 +0900522 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900523
524 if (!*args[4]) {
525 si->applet.ctx.cli.msg = "Key value expected\n";
526 si->applet.st0 = STAT_CLI_PRINT;
527 return;
528 }
529
Simon Hormanc5b89f62011-06-15 15:18:50 +0900530 switch (px->table.type) {
531 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900532 uint32_key = htonl(inetaddr_host(args[4]));
533 static_table_key.key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900534 break;
535 case STKTABLE_TYPE_IPV6:
536 inet_pton(AF_INET6, args[4], ip6_key);
537 static_table_key.key = &ip6_key;
538 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900539 case STKTABLE_TYPE_INTEGER:
540 {
541 char *endptr;
542 unsigned long val;
543 errno = 0;
544 val = strtoul(args[4], &endptr, 10);
545 if ((errno == ERANGE && val == ULONG_MAX) ||
546 (errno != 0 && val == 0) || endptr == args[4] ||
547 val > 0xffffffff) {
548 si->applet.ctx.cli.msg = "Invalid key\n";
549 si->applet.st0 = STAT_CLI_PRINT;
550 return;
551 }
552 uint32_key = (uint32_t) val;
553 static_table_key.key = &uint32_key;
554 break;
555 }
556 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900557 case STKTABLE_TYPE_STRING:
558 static_table_key.key = args[4];
559 static_table_key.key_len = strlen(args[4]);
560 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900561 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200562 switch (action) {
563 case STAT_CLI_O_TAB:
564 si->applet.ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
565 break;
566 case STAT_CLI_O_CLR:
567 si->applet.ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
568 break;
569 default:
570 si->applet.ctx.cli.msg = "Unknown action\n";
571 break;
572 }
Simon Horman121f3052011-06-15 15:18:46 +0900573 si->applet.st0 = STAT_CLI_PRINT;
574 return;
575 }
576
577 /* check permissions */
578 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
579 si->applet.ctx.cli.msg = stats_permission_denied_msg;
580 si->applet.st0 = STAT_CLI_PRINT;
581 return;
582 }
583
584 ts = stktable_lookup_key(&px->table, &static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900585
Willy Tarreaudec98142012-06-06 23:37:08 +0200586 switch (action) {
587 case STAT_CLI_O_TAB:
588 if (!ts)
589 return;
David du Colombier7af46052012-05-16 14:16:48 +0200590 chunk_init(&msg, trash, trashlen);
Simon Horman17bce342011-06-15 15:18:47 +0900591 if (!stats_dump_table_head_to_buffer(&msg, si, px, px))
592 return;
593 stats_dump_table_entry_to_buffer(&msg, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900594 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200595
596 case STAT_CLI_O_CLR:
597 if (!ts)
598 return;
599 if (ts->ref_cnt) {
600 /* don't delete an entry which is currently referenced */
601 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
602 si->applet.st0 = STAT_CLI_PRINT;
603 return;
604 }
605 stksess_kill(&px->table, ts);
606 break;
Simon Horman17bce342011-06-15 15:18:47 +0900607
Willy Tarreau654694e2012-06-07 01:03:16 +0200608 case STAT_CLI_O_SET:
609 if (strncmp(args[5], "data.", 5) != 0) {
610 si->applet.ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
611 si->applet.st0 = STAT_CLI_PRINT;
612 return;
613 }
614
615 data_type = stktable_get_data_type(args[5] + 5);
616 if (data_type < 0) {
617 si->applet.ctx.cli.msg = "Unknown data type\n";
618 si->applet.st0 = STAT_CLI_PRINT;
619 return;
620 }
621
622 if (!px->table.data_ofs[data_type]) {
623 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
624 si->applet.st0 = STAT_CLI_PRINT;
625 return;
626 }
627
628 if (!*args[6] || strl2llrc(args[6], strlen(args[6]), &value) != 0) {
629 si->applet.ctx.cli.msg = "Require a valid integer value to store\n";
630 si->applet.st0 = STAT_CLI_PRINT;
631 return;
632 }
633
634 if (ts)
635 stktable_touch(&px->table, ts, 1);
636 else {
637 ts = stksess_new(&px->table, &static_table_key);
638 if (!ts) {
639 /* don't delete an entry which is currently referenced */
640 si->applet.ctx.cli.msg = "Unable to allocate a new entry\n";
641 si->applet.st0 = STAT_CLI_PRINT;
642 return;
643 }
644 stktable_store(&px->table, ts, 1);
645 }
646
647 ptr = stktable_data_ptr(&px->table, ts, data_type);
648 switch (stktable_data_types[data_type].std_type) {
649 case STD_T_SINT:
650 stktable_data_cast(ptr, std_t_sint) = value;
651 break;
652 case STD_T_UINT:
653 stktable_data_cast(ptr, std_t_uint) = value;
654 break;
655 case STD_T_ULL:
656 stktable_data_cast(ptr, std_t_ull) = value;
657 break;
658 case STD_T_FRQP:
659 /* We only reset the previous value so that it slowly fades out */
660 frqp = &stktable_data_cast(ptr, std_t_frqp);
661 frqp->curr_tick = now_ms;
662 frqp->prev_ctr = value;
663 frqp->curr_ctr = 0;
664 break;
665 }
666 break;
667
Willy Tarreaudec98142012-06-06 23:37:08 +0200668 default:
669 si->applet.ctx.cli.msg = "Unknown action\n";
Simon Horman121f3052011-06-15 15:18:46 +0900670 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200671 break;
Simon Horman121f3052011-06-15 15:18:46 +0900672 }
Simon Horman121f3052011-06-15 15:18:46 +0900673}
674
Willy Tarreau654694e2012-06-07 01:03:16 +0200675static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900676{
Willy Tarreau654694e2012-06-07 01:03:16 +0200677 if (action != STAT_CLI_O_TAB) {
678 si->applet.ctx.cli.msg = "content-based lookup is only supported with the \"show\" action";
679 si->applet.st0 = STAT_CLI_PRINT;
680 return;
681 }
682
Simon Hormand5b9fd92011-06-15 15:18:48 +0900683 /* condition on stored data value */
684 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
685 if (si->applet.ctx.table.data_type < 0) {
686 si->applet.ctx.cli.msg = "Unknown data type\n";
687 si->applet.st0 = STAT_CLI_PRINT;
688 return;
689 }
690
691 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
692 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
693 si->applet.st0 = STAT_CLI_PRINT;
694 return;
695 }
696
697 si->applet.ctx.table.data_op = get_std_op(args[4]);
698 if (si->applet.ctx.table.data_op < 0) {
699 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
700 si->applet.st0 = STAT_CLI_PRINT;
701 return;
702 }
703
704 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
705 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
706 si->applet.st0 = STAT_CLI_PRINT;
707 return;
708 }
709}
710
Willy Tarreaudec98142012-06-06 23:37:08 +0200711static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900712{
713 si->applet.ctx.table.data_type = -1;
Willy Tarreau94981132012-05-21 17:09:48 +0200714 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200715 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900716 si->applet.ctx.table.proxy = NULL;
717 si->applet.ctx.table.entry = NULL;
Willy Tarreaudec98142012-06-06 23:37:08 +0200718 si->applet.st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900719
720 if (*args[2]) {
721 si->applet.ctx.table.target = find_stktable(args[2]);
722 if (!si->applet.ctx.table.target) {
723 si->applet.ctx.cli.msg = "No such table\n";
724 si->applet.st0 = STAT_CLI_PRINT;
725 return;
726 }
727 }
728 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200729 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900730 goto err_args;
731 return;
732 }
733
734 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200735 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900736 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200737 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900738 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900739 goto err_args;
740
741 return;
742
743err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200744 switch (action) {
745 case STAT_CLI_O_TAB:
Simon Hormand5b9fd92011-06-15 15:18:48 +0900746 si->applet.ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200747 break;
748 case STAT_CLI_O_CLR:
Simon Hormanc88b8872011-06-15 15:18:49 +0900749 si->applet.ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200750 break;
751 default:
752 si->applet.ctx.cli.msg = "Unknown action\n";
753 break;
754 }
Simon Hormand5b9fd92011-06-15 15:18:48 +0900755 si->applet.st0 = STAT_CLI_PRINT;
756}
757
Willy Tarreau532a4502011-09-07 22:37:44 +0200758/* Expects to find a frontend named <arg> and returns it, otherwise displays various
759 * adequate error messages and returns NULL. This function also expects the session
760 * level to be admin.
761 */
762static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
763{
764 struct proxy *px;
765
766 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
767 si->applet.ctx.cli.msg = stats_permission_denied_msg;
768 si->applet.st0 = STAT_CLI_PRINT;
769 return NULL;
770 }
771
772 if (!*arg) {
773 si->applet.ctx.cli.msg = "A frontend name is expected.\n";
774 si->applet.st0 = STAT_CLI_PRINT;
775 return NULL;
776 }
777
778 px = findproxy(arg, PR_CAP_FE);
779 if (!px) {
780 si->applet.ctx.cli.msg = "No such frontend.\n";
781 si->applet.st0 = STAT_CLI_PRINT;
782 return NULL;
783 }
784 return px;
785}
786
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200787/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
788 * and returns the pointer to the server. Otherwise, display adequate error messages
789 * and returns NULL. This function also expects the session level to be admin. Note:
790 * the <arg> is modified to remove the '/'.
791 */
792static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
793{
794 struct proxy *px;
795 struct server *sv;
796 char *line;
797
798 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
799 si->applet.ctx.cli.msg = stats_permission_denied_msg;
800 si->applet.st0 = STAT_CLI_PRINT;
801 return NULL;
802 }
803
804 /* split "backend/server" and make <line> point to server */
805 for (line = arg; *line; line++)
806 if (*line == '/') {
807 *line++ = '\0';
808 break;
809 }
810
811 if (!*line || !*arg) {
812 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
813 si->applet.st0 = STAT_CLI_PRINT;
814 return NULL;
815 }
816
817 if (!get_backend_server(arg, line, &px, &sv)) {
818 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
819 si->applet.st0 = STAT_CLI_PRINT;
820 return NULL;
821 }
822
823 if (px->state == PR_STSTOPPED) {
824 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
825 si->applet.st0 = STAT_CLI_PRINT;
826 return NULL;
827 }
828
829 return sv;
830}
831
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200832/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200833 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100834 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200835 * designating the function which will have to process the request, which can
836 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200837 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900838static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200839{
Willy Tarreau94981132012-05-21 17:09:48 +0200840 struct session *s = si->conn.data_ctx;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200841 char *args[MAX_STATS_ARGS + 1];
842 int arg;
843
844 while (isspace((unsigned char)*line))
845 line++;
846
847 arg = 0;
848 args[arg] = line;
849
850 while (*line && arg < MAX_STATS_ARGS) {
851 if (isspace((unsigned char)*line)) {
852 *line++ = '\0';
853
854 while (isspace((unsigned char)*line))
855 line++;
856
857 args[++arg] = line;
858 continue;
859 }
860
861 line++;
862 }
863
864 while (++arg <= MAX_STATS_ARGS)
865 args[arg] = line;
866
Willy Tarreau295a8372011-03-10 11:25:07 +0100867 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200868 if (strcmp(args[0], "show") == 0) {
869 if (strcmp(args[1], "stat") == 0) {
870 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100871 si->applet.ctx.stats.flags |= STAT_BOUND;
872 si->applet.ctx.stats.iid = atoi(args[2]);
873 si->applet.ctx.stats.type = atoi(args[3]);
874 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200875 }
876
Willy Tarreau295a8372011-03-10 11:25:07 +0100877 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
878 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau94981132012-05-21 17:09:48 +0200879 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100880 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200881 }
882 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100883 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
884 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau94981132012-05-21 17:09:48 +0200885 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100886 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200887 }
888 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau94981132012-05-21 17:09:48 +0200889 si->conn.data_st = STAT_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200890 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100891 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100892 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200893 return 1;
894 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100895 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100896 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100897 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100898 si->applet.ctx.sess.target = NULL;
899 si->applet.ctx.sess.section = 0; /* start with session status */
900 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100901 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200902 }
903 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200904 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100905 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100906 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200907 return 1;
908 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200909 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100910 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200911 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100912 si->applet.ctx.errors.iid = -1;
913 si->applet.ctx.errors.px = NULL;
Willy Tarreau94981132012-05-21 17:09:48 +0200914 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100915 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200916 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200917 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200918 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200919 }
Aman Guptaceafb4a2012-04-02 18:57:54 -0700920 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200921 return 0;
922 }
923 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200924 else if (strcmp(args[0], "clear") == 0) {
925 if (strcmp(args[1], "counters") == 0) {
926 struct proxy *px;
927 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200928 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200929 int clrall = 0;
930
931 if (strcmp(args[2], "all") == 0)
932 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200933
Willy Tarreau6162db22009-10-10 17:13:00 +0200934 /* check permissions */
935 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
936 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100937 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100938 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200939 return 1;
940 }
941
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200942 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100943 if (clrall) {
944 memset(&px->be_counters, 0, sizeof(px->be_counters));
945 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
946 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200947 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100948 px->be_counters.conn_max = 0;
949 px->be_counters.p.http.rps_max = 0;
950 px->be_counters.sps_max = 0;
951 px->be_counters.cps_max = 0;
952 px->be_counters.nbpend_max = 0;
953
954 px->fe_counters.conn_max = 0;
955 px->fe_counters.p.http.rps_max = 0;
956 px->fe_counters.sps_max = 0;
957 px->fe_counters.cps_max = 0;
958 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200959 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200960
961 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200962 if (clrall)
963 memset(&sv->counters, 0, sizeof(sv->counters));
964 else {
965 sv->counters.cur_sess_max = 0;
966 sv->counters.nbpend_max = 0;
967 sv->counters.sps_max = 0;
968 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200969
970 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200971 if (li->counters) {
972 if (clrall)
973 memset(li->counters, 0, sizeof(*li->counters));
974 else
975 li->counters->conn_max = 0;
976 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200977 }
978
Willy Tarreau81c25d02011-09-07 15:17:21 +0200979 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200980 return 1;
981 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200982 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200983 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200984 /* end of processing */
985 return 1;
986 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200987 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200988 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200989 return 0;
990 }
991 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200992 else if (strcmp(args[0], "get") == 0) {
993 if (strcmp(args[1], "weight") == 0) {
994 struct proxy *px;
995 struct server *sv;
996
997 /* split "backend/server" and make <line> point to server */
998 for (line = args[2]; *line; line++)
999 if (*line == '/') {
1000 *line++ = '\0';
1001 break;
1002 }
1003
1004 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001005 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001006 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001007 return 1;
1008 }
1009
1010 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001011 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001012 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001013 return 1;
1014 }
1015
1016 /* return server's effective weight at the moment */
David du Colombier7af46052012-05-16 14:16:48 +02001017 snprintf(trash, trashlen, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001018 bi_putstr(si->ib, trash);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001019 return 1;
1020 }
1021 else { /* not "get weight" */
1022 return 0;
1023 }
1024 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001025 else if (strcmp(args[0], "set") == 0) {
1026 if (strcmp(args[1], "weight") == 0) {
1027 struct proxy *px;
1028 struct server *sv;
1029 int w;
1030
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001031 sv = expect_server_admin(s, si, args[2]);
1032 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001033 return 1;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001034 px = sv->proxy;
Willy Tarreau4483d432009-10-10 19:30:08 +02001035
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001036 /* if the weight is terminated with '%', it is set relative to
1037 * the initial weight, otherwise it is absolute.
1038 */
1039 if (!*args[3]) {
1040 si->applet.ctx.cli.msg = "Require <weight> or <weight%>.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001041 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001042 return 1;
1043 }
1044
Willy Tarreau4483d432009-10-10 19:30:08 +02001045 w = atoi(args[3]);
1046 if (strchr(args[3], '%') != NULL) {
1047 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001048 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001049 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001050 return 1;
1051 }
1052 w = sv->iweight * w / 100;
1053 }
1054 else {
1055 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001056 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001057 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001058 return 1;
1059 }
1060 }
1061
1062 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001063 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 +01001064 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001065 return 1;
1066 }
1067
1068 sv->uweight = w;
1069
1070 if (px->lbprm.algo & BE_LB_PROP_DYN) {
1071 /* we must take care of not pushing the server to full throttle during slow starts */
1072 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
1073 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
1074 else
1075 sv->eweight = BE_WEIGHT_SCALE;
1076 sv->eweight *= sv->uweight;
1077 } else {
1078 sv->eweight = sv->uweight;
1079 }
1080
1081 /* static LB algorithms are a bit harder to update */
1082 if (px->lbprm.update_server_eweight)
1083 px->lbprm.update_server_eweight(sv);
Willy Tarreau9580d162012-05-19 19:07:40 +02001084 else if (sv->eweight) {
1085 if (px->lbprm.set_server_status_up)
1086 px->lbprm.set_server_status_up(sv);
1087 }
1088 else {
1089 if (px->lbprm.set_server_status_down)
1090 px->lbprm.set_server_status_down(sv);
1091 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001092
1093 return 1;
1094 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001095 else if (strcmp(args[1], "timeout") == 0) {
1096 if (strcmp(args[2], "cli") == 0) {
1097 unsigned timeout;
1098 const char *res;
1099
1100 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001101 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001102 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001103 return 1;
1104 }
1105
1106 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1107 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001108 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001109 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001110 return 1;
1111 }
1112
1113 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1114 return 1;
1115 }
1116 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001117 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001118 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001119 return 1;
1120 }
1121 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001122 else if (strcmp(args[1], "maxconn") == 0) {
1123 if (strcmp(args[2], "frontend") == 0) {
1124 struct proxy *px;
1125 struct listener *l;
1126 int v;
1127
Willy Tarreau532a4502011-09-07 22:37:44 +02001128 px = expect_frontend_admin(s, si, args[3]);
1129 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001130 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001131
1132 if (!*args[4]) {
1133 si->applet.ctx.cli.msg = "Integer value expected.\n";
1134 si->applet.st0 = STAT_CLI_PRINT;
1135 return 1;
1136 }
1137
1138 v = atoi(args[4]);
1139 /* check for unlimited values, we restore default setting (cfg_maxpconn) */
1140 if (v < 1) {
1141 si->applet.ctx.cli.msg = "Value out of range.\n";
1142 si->applet.st0 = STAT_CLI_PRINT;
1143 return 1;
1144 }
1145
1146 /* OK, the value is fine, so we assign it to the proxy and to all of
1147 * its listeners. The blocked ones will be dequeued.
1148 */
1149 px->maxconn = v;
1150 for (l = px->listen; l != NULL; l = l->next) {
1151 l->maxconn = v;
1152 if (l->state == LI_FULL)
1153 resume_listener(l);
1154 }
1155
1156 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1157 dequeue_all_listeners(&s->fe->listener_queue);
1158
1159 return 1;
1160 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001161 else if (strcmp(args[2], "global") == 0) {
1162 int v;
1163
1164 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1165 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1166 si->applet.st0 = STAT_CLI_PRINT;
1167 return 1;
1168 }
1169
1170 if (!*args[3]) {
1171 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1172 si->applet.st0 = STAT_CLI_PRINT;
1173 return 1;
1174 }
1175
1176 v = atoi(args[3]);
1177 if (v > global.hardmaxconn) {
1178 si->applet.ctx.cli.msg = "Value out of range.\n";
1179 si->applet.st0 = STAT_CLI_PRINT;
1180 return 1;
1181 }
1182
1183 /* check for unlimited values */
1184 if (v <= 0)
1185 v = global.hardmaxconn;
1186
1187 global.maxconn = v;
1188
1189 /* Dequeues all of the listeners waiting for a resource */
1190 if (!LIST_ISEMPTY(&global_listener_queue))
1191 dequeue_all_listeners(&global_listener_queue);
1192
1193 return 1;
1194 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001195 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001196 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001197 si->applet.st0 = STAT_CLI_PRINT;
1198 return 1;
1199 }
1200 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001201 else if (strcmp(args[1], "rate-limit") == 0) {
1202 if (strcmp(args[2], "connections") == 0) {
1203 if (strcmp(args[3], "global") == 0) {
1204 int v;
1205
1206 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1207 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1208 si->applet.st0 = STAT_CLI_PRINT;
1209 return 1;
1210 }
1211
1212 if (!*args[4]) {
1213 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1214 si->applet.st0 = STAT_CLI_PRINT;
1215 return 1;
1216 }
1217
1218 v = atoi(args[4]);
1219 if (v < 0) {
1220 si->applet.ctx.cli.msg = "Value out of range.\n";
1221 si->applet.st0 = STAT_CLI_PRINT;
1222 return 1;
1223 }
1224
1225 global.cps_lim = v;
1226
1227 /* Dequeues all of the listeners waiting for a resource */
1228 if (!LIST_ISEMPTY(&global_listener_queue))
1229 dequeue_all_listeners(&global_listener_queue);
1230
1231 return 1;
1232 }
1233 else {
1234 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1235 si->applet.st0 = STAT_CLI_PRINT;
1236 return 1;
1237 }
1238 }
1239 else {
1240 si->applet.ctx.cli.msg = "'set rate-limit' only supports 'connections'.\n";
1241 si->applet.st0 = STAT_CLI_PRINT;
1242 return 1;
1243 }
1244 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001245 else if (strcmp(args[1], "table") == 0) {
1246 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1247 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001248 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001249 return 0;
1250 }
1251 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001252 else if (strcmp(args[0], "enable") == 0) {
1253 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001254 struct server *sv;
1255
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001256 sv = expect_server_admin(s, si, args[2]);
1257 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001258 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001259
Cyril Bontécd19e512010-01-31 22:34:03 +01001260 if (sv->state & SRV_MAINTAIN) {
1261 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001262 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001263 /* If this server tracks the status of another one,
1264 * we must restore the good status.
1265 */
Willy Tarreau44267702011-10-28 15:35:33 +02001266 if (sv->track->state & SRV_RUNNING) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001267 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001268 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001269 } else {
1270 sv->state &= ~SRV_MAINTAIN;
1271 set_server_down(sv);
1272 }
1273 } else {
1274 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001275 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001276 }
1277 }
1278
Willy Tarreau532a4502011-09-07 22:37:44 +02001279 return 1;
1280 }
1281 else if (strcmp(args[1], "frontend") == 0) {
1282 struct proxy *px;
1283
1284 px = expect_frontend_admin(s, si, args[2]);
1285 if (!px)
1286 return 1;
1287
1288 if (px->state == PR_STSTOPPED) {
1289 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1290 si->applet.st0 = STAT_CLI_PRINT;
1291 return 1;
1292 }
1293
1294 if (px->state != PR_STPAUSED) {
1295 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1296 si->applet.st0 = STAT_CLI_PRINT;
1297 return 1;
1298 }
1299
1300 if (!resume_proxy(px)) {
1301 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1302 si->applet.st0 = STAT_CLI_PRINT;
1303 return 1;
1304 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001305 return 1;
1306 }
1307 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001308 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1309 si->applet.st0 = STAT_CLI_PRINT;
1310 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001311 }
1312 }
1313 else if (strcmp(args[0], "disable") == 0) {
1314 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001315 struct server *sv;
1316
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001317 sv = expect_server_admin(s, si, args[2]);
1318 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001319 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001320
Cyril Bontécd19e512010-01-31 22:34:03 +01001321 if (! (sv->state & SRV_MAINTAIN)) {
1322 /* Not already in maintenance, we can change the server state */
1323 sv->state |= SRV_MAINTAIN;
1324 set_server_down(sv);
1325 }
1326
Willy Tarreau532a4502011-09-07 22:37:44 +02001327 return 1;
1328 }
1329 else if (strcmp(args[1], "frontend") == 0) {
1330 struct proxy *px;
1331
1332 px = expect_frontend_admin(s, si, args[2]);
1333 if (!px)
1334 return 1;
1335
1336 if (px->state == PR_STSTOPPED) {
1337 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1338 si->applet.st0 = STAT_CLI_PRINT;
1339 return 1;
1340 }
1341
1342 if (px->state == PR_STPAUSED) {
1343 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1344 si->applet.st0 = STAT_CLI_PRINT;
1345 return 1;
1346 }
1347
1348 if (!pause_proxy(px)) {
1349 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1350 si->applet.st0 = STAT_CLI_PRINT;
1351 return 1;
1352 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001353 return 1;
1354 }
1355 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001356 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1357 si->applet.st0 = STAT_CLI_PRINT;
1358 return 1;
1359 }
1360 }
1361 else if (strcmp(args[0], "shutdown") == 0) {
1362 if (strcmp(args[1], "frontend") == 0) {
1363 struct proxy *px;
1364
1365 px = expect_frontend_admin(s, si, args[2]);
1366 if (!px)
1367 return 1;
1368
1369 if (px->state == PR_STSTOPPED) {
1370 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1371 si->applet.st0 = STAT_CLI_PRINT;
1372 return 1;
1373 }
1374
1375 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1376 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1377 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1378 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1379 stop_proxy(px);
1380 return 1;
1381 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001382 else if (strcmp(args[1], "session") == 0) {
1383 struct session *sess, *ptr;
1384
1385 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1386 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1387 si->applet.st0 = STAT_CLI_PRINT;
1388 return 1;
1389 }
1390
1391 if (!*args[2]) {
1392 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1393 si->applet.st0 = STAT_CLI_PRINT;
1394 return 1;
1395 }
1396
1397 ptr = (void *)strtoul(args[2], NULL, 0);
1398
1399 /* first, look for the requested session in the session table */
1400 list_for_each_entry(sess, &sessions, list) {
1401 if (sess == ptr)
1402 break;
1403 }
1404
1405 /* do we have the session ? */
1406 if (sess != ptr) {
1407 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1408 si->applet.st0 = STAT_CLI_PRINT;
1409 return 1;
1410 }
1411
1412 session_shutdown(sess, SN_ERR_KILLED);
1413 return 1;
1414 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001415 else if (strcmp(args[1], "sessions") == 0) {
1416 if (strcmp(args[2], "server") == 0) {
1417 struct server *sv;
1418 struct session *sess, *sess_bck;
1419
1420 sv = expect_server_admin(s, si, args[3]);
1421 if (!sv)
1422 return 1;
1423
1424 /* kill all the session that are on this server */
1425 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1426 if (sess->srv_conn == sv)
1427 session_shutdown(sess, SN_ERR_KILLED);
1428
1429 return 1;
1430 }
1431 else {
1432 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1433 si->applet.st0 = STAT_CLI_PRINT;
1434 return 1;
1435 }
1436 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001437 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001438 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001439 si->applet.st0 = STAT_CLI_PRINT;
1440 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001441 }
1442 }
1443 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001444 return 0;
1445 }
1446 return 1;
1447}
1448
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001449/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001450 * used to processes I/O from/to the stats unix socket. The system relies on a
1451 * state machine handling requests and various responses. We read a request,
1452 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001453 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1454 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001455 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001456 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001457static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001458{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001459 struct buffer *req = si->ob;
1460 struct buffer *res = si->ib;
1461 int reql;
1462 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001463
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001464 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1465 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001466
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001467 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001468 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001469 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001470 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001471 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001472 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001473 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001474 /* Let's close for real now. We just close the request
1475 * side, the conditions below will complete if needed.
1476 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001477 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001478 break;
1479 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001480 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001481 /* ensure we have some output room left in the event we
1482 * would want to return some info right after parsing.
1483 */
1484 if (buffer_almost_full(si->ib))
1485 break;
1486
David du Colombier7af46052012-05-16 14:16:48 +02001487 reql = bo_getline(si->ob, trash, trashlen);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001488 if (reql <= 0) { /* closed or EOL not found */
1489 if (reql == 0)
1490 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001491 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001492 continue;
1493 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001494
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001495 /* seek for a possible semi-colon. If we find one, we
1496 * replace it with an LF and skip only this part.
1497 */
1498 for (len = 0; len < reql; len++)
1499 if (trash[len] == ';') {
1500 trash[len] = '\n';
1501 reql = len + 1;
1502 break;
1503 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001504
Willy Tarreau816fc222009-10-04 07:36:58 +02001505 /* now it is time to check that we have a full line,
1506 * remove the trailing \n and possibly \r, then cut the
1507 * line.
1508 */
1509 len = reql - 1;
1510 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001511 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001512 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001513 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001514
Willy Tarreau816fc222009-10-04 07:36:58 +02001515 if (len && trash[len-1] == '\r')
1516 len--;
1517
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001518 trash[len] = '\0';
1519
Willy Tarreaubc4af052011-02-13 13:25:14 +01001520 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001521 if (len) {
1522 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001523 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001524 continue;
1525 }
1526 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001527 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001528 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001529 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001530 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001531 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001532 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001533 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001534 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001535 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001536 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001537 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001538 /* if prompt is disabled, print help on empty lines,
1539 * so that the user at least knows how to enable
1540 * prompt and find help.
1541 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001542 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001543 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001544 }
1545
1546 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001547 bo_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001548 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001549 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001550 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001551 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001552 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001553 continue;
1554 }
1555
Willy Tarreaubc4af052011-02-13 13:25:14 +01001556 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001557 case STAT_CLI_PRINT:
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001558 if (bi_putstr(si->ib, si->applet.ctx.cli.msg) != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001559 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001560 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001561 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001562 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001563 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001564 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001565 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001566 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001567 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001568 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001569 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001570 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001571 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001572 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001573 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001574 case STAT_CLI_O_CLR:
Willy Tarreaudec98142012-06-06 23:37:08 +02001575 if (stats_table_request(si, si->applet.st0))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001576 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001577 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001578 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001579 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001580 break;
1581 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001582
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001583 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001584 if (si->applet.st0 == STAT_CLI_PROMPT) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001585 if (bi_putstr(si->ib, si->applet.st1 ? "\n> " : "\n") != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001586 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001587 }
1588
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001589 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001590 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001591 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001592
1593 /* Now we close the output if one of the writers did so,
1594 * or if we're not in interactive mode and the request
1595 * buffer is empty. This still allows pipelined requests
1596 * to be sent in non-interactive mode.
1597 */
Willy Tarreau2e046c62012-03-01 16:08:30 +01001598 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->applet.st1 && !req->o)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001599 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001600 continue;
1601 }
1602
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001603 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001604 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001605 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001606 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001607
Willy Tarreaubc4af052011-02-13 13:25:14 +01001608 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001609 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1610 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001611 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001612 * and nothing more to consume. This is comparable to a broken pipe, so
1613 * we forward the close to the request side so that it flows upstream to
1614 * the client.
1615 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001616 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001617 }
1618
Willy Tarreaubc4af052011-02-13 13:25:14 +01001619 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001620 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1621 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1622 /* We have no more processing to do, and nothing more to send, and
1623 * the client side has closed. So we'll forward this state downstream
1624 * on the response buffer.
1625 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001626 si_shutr(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001627 res->flags |= BF_READ_NULL;
1628 }
1629
1630 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001631 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001632
1633 /* we don't want to expire timeouts while we're processing requests */
1634 si->ib->rex = TICK_ETERNITY;
1635 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001636
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001637 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001638 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 +02001639 __FUNCTION__, __LINE__,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001640 si->state, req->flags, res->flags, req->i, req->o, res->i, res->o);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001641
1642 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1643 /* check that we have released everything then unregister */
1644 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001645 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001646}
1647
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001648/* This function dumps statistics onto the stream interface's read buffer.
1649 * The data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001650 * It returns 0 as long as it does not complete, non-zero upon completion.
1651 * Some states are not used but it makes the code more similar to other
1652 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001653 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001654static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001655{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001656 struct proxy *px;
1657 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001658 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001659
David du Colombier7af46052012-05-16 14:16:48 +02001660 chunk_init(&msg, trash, trashlen);
Willy Tarreau3e76e722007-10-17 18:57:38 +02001661
Willy Tarreau94981132012-05-21 17:09:48 +02001662 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001663 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001664 /* the function had not been called yet */
Willy Tarreau94981132012-05-21 17:09:48 +02001665 si->conn.data_st = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001666 /* fall through */
1667
Willy Tarreau295a8372011-03-10 11:25:07 +01001668 case STAT_ST_HEAD:
1669 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001670 print_csv_header(&msg);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001671 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001672 return 0;
1673 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001674
Willy Tarreau94981132012-05-21 17:09:48 +02001675 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001676 /* fall through */
1677
Willy Tarreau295a8372011-03-10 11:25:07 +01001678 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001679 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001680 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001681 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001682 "Name: " PRODUCT_NAME "\n"
1683 "Version: " HAPROXY_VERSION "\n"
1684 "Release_date: " HAPROXY_DATE "\n"
1685 "Nbproc: %d\n"
1686 "Process_num: %d\n"
1687 "Pid: %d\n"
1688 "Uptime: %dd %dh%02dm%02ds\n"
1689 "Uptime_sec: %d\n"
1690 "Memmax_MB: %d\n"
1691 "Ulimit-n: %d\n"
1692 "Maxsock: %d\n"
1693 "Maxconn: %d\n"
Willy Tarreau91886b62011-09-07 14:38:31 +02001694 "Hard_maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001695 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001696 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001697 "PipesUsed: %d\n"
1698 "PipesFree: %d\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001699 "ConnRate: %d\n"
1700 "ConnRateLimit: %d\n"
1701 "MaxConnRate: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001702 "Tasks: %d\n"
1703 "Run_queue: %d\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02001704 "Idle_pct: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001705 "node: %s\n"
1706 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001707 "",
1708 global.nbproc,
1709 relative_pid,
1710 pid,
1711 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1712 up,
1713 global.rlimit_memmax,
1714 global.rlimit_nofile,
Willy Tarreau91886b62011-09-07 14:38:31 +02001715 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001716 actconn, pipes_used, pipes_free,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001717 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau45a12512011-09-10 16:56:42 +02001718 nb_tasks_cur, run_queue_cur, idle_pct,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001719 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001720 );
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001721 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001722 return 0;
1723 }
1724
Willy Tarreau295a8372011-03-10 11:25:07 +01001725 si->applet.ctx.stats.px = proxy;
1726 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1727 si->applet.ctx.stats.sv = NULL;
Willy Tarreau94981132012-05-21 17:09:48 +02001728 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001729 /* fall through */
1730
Willy Tarreau295a8372011-03-10 11:25:07 +01001731 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001732 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001733 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1734 while (si->applet.ctx.stats.px) {
1735 px = si->applet.ctx.stats.px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001736 /* skip the disabled proxies and non-networked ones */
1737 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001738 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001739 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001740 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001741 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001742
Willy Tarreau295a8372011-03-10 11:25:07 +01001743 si->applet.ctx.stats.px = px->next;
1744 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001745 }
1746 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001747 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001748
Willy Tarreau94981132012-05-21 17:09:48 +02001749 si->conn.data_st = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001750 /* fall through */
1751
Willy Tarreau295a8372011-03-10 11:25:07 +01001752 case STAT_ST_END:
Willy Tarreau94981132012-05-21 17:09:48 +02001753 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001754 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001755
Willy Tarreau295a8372011-03-10 11:25:07 +01001756 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001757 return 1;
1758
1759 default:
1760 /* unknown state ! */
Willy Tarreau94981132012-05-21 17:09:48 +02001761 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001762 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001763 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001764}
1765
1766
Cyril Bonté70be45d2010-10-12 00:14:35 +02001767/* We don't want to land on the posted stats page because a refresh will
1768 * repost the data. We don't want this to happen on accident so we redirect
1769 * the browse to the stats page with a GET.
1770 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001771static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001772{
Willy Tarreau94981132012-05-21 17:09:48 +02001773 struct session *s = si->conn.data_ctx;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001774 struct chunk msg;
1775
David du Colombier7af46052012-05-16 14:16:48 +02001776 chunk_init(&msg, trash, trashlen);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001777
Willy Tarreau94981132012-05-21 17:09:48 +02001778 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001779 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001780 chunk_printf(&msg,
1781 "HTTP/1.0 303 See Other\r\n"
1782 "Cache-Control: no-cache\r\n"
1783 "Content-Type: text/plain\r\n"
1784 "Connection: close\r\n"
1785 "Location: %s;st=%s",
Cyril Bonté19979e12012-04-04 12:57:21 +02001786 uri->uri_prefix,
1787 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
1788 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
1789 stat_status_codes[si->applet.ctx.stats.st_code]) ?
1790 stat_status_codes[si->applet.ctx.stats.st_code] :
1791 stat_status_codes[STAT_STATUS_UNKN]);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001792 chunk_printf(&msg, "\r\n\r\n");
1793
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001794 if (bi_putchk(si->ib, &msg) == -1)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001795 return 0;
1796
1797 s->txn.status = 303;
1798
1799 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1800 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1801 if (!(s->flags & SN_FINST_MASK))
1802 s->flags |= SN_FINST_R;
1803
Willy Tarreau94981132012-05-21 17:09:48 +02001804 si->conn.data_st = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001805 return 1;
1806 }
1807 return 1;
1808}
1809
1810
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001811/* This I/O handler runs as an applet embedded in a stream interface. It is
1812 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001813 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001814 * automatically unregisters itself once transfer is complete.
1815 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001816static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001817{
Willy Tarreau94981132012-05-21 17:09:48 +02001818 struct session *s = si->conn.data_ctx;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001819 struct buffer *req = si->ob;
1820 struct buffer *res = si->ib;
1821
1822 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1823 goto out;
1824
1825 /* check that the output is not closed */
1826 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001827 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001828
Willy Tarreaubc4af052011-02-13 13:25:14 +01001829 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001830 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001831 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001832 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001833 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001834 }
1835 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001836 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001837 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001838 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001839 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001840 }
1841 }
1842
1843 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
Willy Tarreau73b013b2012-05-21 16:31:45 +02001844 si_shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001845
Willy Tarreaubc4af052011-02-13 13:25:14 +01001846 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001847 si_shutr(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001848 res->flags |= BF_READ_NULL;
1849 }
1850
1851 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001852 si_update(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001853
1854 /* we don't want to expire timeouts while we're processing requests */
1855 si->ib->rex = TICK_ETERNITY;
1856 si->ob->wex = TICK_ETERNITY;
1857
1858 out:
1859 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1860 /* check that we have released everything then unregister */
1861 stream_int_unregister_handler(si);
1862 }
1863}
1864
1865
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001866/* This function dumps statistics in HTTP format onto the stream interface's
1867 * read buffer. The data_ctx must have been zeroed first, and the flags
1868 * properly set. It returns 0 if it had to stop writing data and an I/O is
1869 * needed, 1 if the dump is finished and the session must be closed, or -1
1870 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001871 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001872static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001873{
Willy Tarreau94981132012-05-21 17:09:48 +02001874 struct session *s = si->conn.data_ctx;
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001875 struct buffer *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001876 struct proxy *px;
1877 struct chunk msg;
1878 unsigned int up;
1879
David du Colombier7af46052012-05-16 14:16:48 +02001880 chunk_init(&msg, trash, trashlen);
Willy Tarreau91861262007-10-17 17:06:05 +02001881
Willy Tarreau94981132012-05-21 17:09:48 +02001882 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001883 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001884 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001885 "HTTP/1.0 200 OK\r\n"
1886 "Cache-Control: no-cache\r\n"
1887 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001888 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001889 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001890
Willy Tarreau295a8372011-03-10 11:25:07 +01001891 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001892 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001893 uri->refresh);
1894
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001895 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001896
1897 s->txn.status = 200;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001898 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001899 return 0;
1900
Willy Tarreau91861262007-10-17 17:06:05 +02001901 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1902 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1903 if (!(s->flags & SN_FINST_MASK))
1904 s->flags |= SN_FINST_R;
1905
1906 if (s->txn.meth == HTTP_METH_HEAD) {
1907 /* that's all we return in case of HEAD request */
Willy Tarreau94981132012-05-21 17:09:48 +02001908 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001909 return 1;
1910 }
1911
Willy Tarreau94981132012-05-21 17:09:48 +02001912 si->conn.data_st = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001913 /* fall through */
1914
Willy Tarreau295a8372011-03-10 11:25:07 +01001915 case STAT_ST_HEAD:
1916 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001917 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001918 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001919 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1920 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001921 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001922 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1923 "<style type=\"text/css\"><!--\n"
1924 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001925 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001926 " font-size: 12px;"
1927 " font-weight: normal;"
1928 " color: black;"
1929 " background: white;"
1930 "}\n"
1931 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001932 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001933 "}\n"
1934 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001935 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001936 " margin-bottom: 0.5em;"
1937 "}\n"
1938 "h2 {"
1939 " font-family: helvetica, arial;"
1940 " font-size: x-large;"
1941 " font-weight: bold;"
1942 " font-style: italic;"
1943 " color: #6020a0;"
1944 " margin-top: 0em;"
1945 " margin-bottom: 0em;"
1946 "}\n"
1947 "h3 {"
1948 " font-family: helvetica, arial;"
1949 " font-size: 16px;"
1950 " font-weight: bold;"
1951 " color: #b00040;"
1952 " background: #e8e8d0;"
1953 " margin-top: 0em;"
1954 " margin-bottom: 0em;"
1955 "}\n"
1956 "li {"
1957 " margin-top: 0.25em;"
1958 " margin-right: 2em;"
1959 "}\n"
1960 ".hr {margin-top: 0.25em;"
1961 " border-color: black;"
1962 " border-bottom-style: solid;"
1963 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001964 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001965 ".total {background: #20D0D0;color: #ffff80;}\n"
1966 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001967 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001968 ".backend {background: #e8e8d0;}\n"
1969 ".active0 {background: #ff9090;}\n"
1970 ".active1 {background: #ffd020;}\n"
1971 ".active2 {background: #ffffa0;}\n"
1972 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001973 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1974 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1975 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001976 ".backup0 {background: #ff9090;}\n"
1977 ".backup1 {background: #ff80ff;}\n"
1978 ".backup2 {background: #c060ff;}\n"
1979 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001980 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1981 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1982 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001983 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001984 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001985 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001986 "a.px:link {color: #ffff40; text-decoration: none;}"
1987 "a.px:visited {color: #ffff40; text-decoration: none;}"
1988 "a.px:hover {color: #ffffff; text-decoration: none;}"
1989 "a.lfsb:link {color: #000000; text-decoration: none;}"
1990 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1991 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1992 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001993 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001994 "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"
1995 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001996 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001997 "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 +02001998 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001999 "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 +02002000 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002001 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2002 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2003 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01002004 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002005 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02002006 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002007 (uri->flags&ST_SHNODE) ? " on " : "",
2008 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02002009 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02002010 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002011 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002012 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002013 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002014 return 0;
2015
Willy Tarreau94981132012-05-21 17:09:48 +02002016 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02002017 /* fall through */
2018
Willy Tarreau295a8372011-03-10 11:25:07 +01002019 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02002020 up = (now.tv_sec - start_date.tv_sec);
2021
2022 /* WARNING! this has to fit the first packet too.
2023 * We are around 3.5 kB, add adding entries will
2024 * become tricky if we want to support 4kB buffers !
2025 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002026 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002027 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002028 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2029 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002030 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002031 "<hr width=\"100%%\" class=\"hr\">\n"
2032 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002033 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01002034 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002035 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002036 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2037 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02002038 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02002039 "Running tasks: %d/%d; idle = %d %%<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002040 "</td><td align=\"center\" nowrap>\n"
2041 "<table class=\"lgd\"><tr>\n"
2042 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2043 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2044 "</tr><tr>\n"
2045 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
2046 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
2047 "</tr><tr>\n"
2048 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
2049 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2050 "</tr><tr>\n"
2051 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01002052 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01002053 "</tr><tr>\n"
2054 "<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 +02002055 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01002056 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02002057 "</td>"
2058 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2059 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2060 "",
2061 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002062 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2063 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
2064 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02002065 up / 86400, (up % 86400) / 3600,
2066 (up % 3600) / 60, (up % 60),
2067 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2068 global.rlimit_memmax ? " MB" : "",
2069 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01002070 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02002071 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau45a12512011-09-10 16:56:42 +02002072 run_queue_cur, nb_tasks_cur, idle_pct
Willy Tarreau91861262007-10-17 17:06:05 +02002073 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002074
Willy Tarreau295a8372011-03-10 11:25:07 +01002075 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002076 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002077 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
2078 uri->uri_prefix,
2079 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01002080 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002081 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002082 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002083 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2084 uri->uri_prefix,
2085 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01002086 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002087
Willy Tarreau55bb8452007-10-17 18:44:57 +02002088 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002089 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002090 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002091 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
2092 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002093 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002094 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002095 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002096 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002097 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
2098 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002099 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002100 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002101 }
Willy Tarreau91861262007-10-17 17:06:05 +02002102
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002103 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002104 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
2105 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002106 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2107 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002108
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002109 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002110 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
2111 uri->uri_prefix,
2112 (uri->refresh > 0) ? ";norefresh" : "");
2113
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002114 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002115 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002116 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
Adrian Bridgettafdb6e52012-03-19 23:36:42 +00002117 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002118 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2119 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2120 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2121 "</ul>"
2122 "</td>"
2123 "</tr></table>\n"
2124 ""
2125 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002126
Willy Tarreau295a8372011-03-10 11:25:07 +01002127 if (si->applet.ctx.stats.st_code) {
Cyril Bonté19979e12012-04-04 12:57:21 +02002128 switch (si->applet.ctx.stats.st_code) {
2129 case STAT_STATUS_DONE:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002130 chunk_printf(&msg,
2131 "<p><div class=active3>"
2132 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2133 "Action processed successfully."
2134 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002135 break;
2136 case STAT_STATUS_NONE:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002137 chunk_printf(&msg,
2138 "<p><div class=active2>"
2139 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2140 "Nothing has changed."
2141 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002142 break;
2143 case STAT_STATUS_PART:
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002144 chunk_printf(&msg,
2145 "<p><div class=active2>"
2146 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2147 "Action partially processed.<br>"
2148 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2149 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002150 break;
2151 case STAT_STATUS_ERRP:
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002152 chunk_printf(&msg,
2153 "<p><div class=active0>"
2154 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2155 "Action not processed because of invalid parameters."
2156 "<ul>"
2157 "<li>The action is maybe unknown.</li>"
2158 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2159 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2160 "</ul>"
2161 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002162 break;
2163 case STAT_STATUS_EXCD:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002164 chunk_printf(&msg,
2165 "<p><div class=active0>"
2166 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2167 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2168 "You should retry with less servers at a time.</b>"
2169 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002170 break;
2171 case STAT_STATUS_DENY:
Cyril Bonté474be412010-10-12 00:14:36 +02002172 chunk_printf(&msg,
2173 "<p><div class=active0>"
2174 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2175 "<b>Action denied.</b>"
2176 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002177 break;
2178 default:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002179 chunk_printf(&msg,
2180 "<p><div class=active6>"
2181 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2182 "Unexpected result."
2183 "</div>\n", uri->uri_prefix);
2184 }
2185 chunk_printf(&msg,"<p>\n");
2186 }
2187
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002188 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002189 return 0;
2190 }
Willy Tarreau91861262007-10-17 17:06:05 +02002191
Willy Tarreau295a8372011-03-10 11:25:07 +01002192 si->applet.ctx.stats.px = proxy;
2193 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau94981132012-05-21 17:09:48 +02002194 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02002195 /* fall through */
2196
Willy Tarreau295a8372011-03-10 11:25:07 +01002197 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02002198 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01002199 while (si->applet.ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002200 if (buffer_almost_full(rep))
2201 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002202 px = si->applet.ctx.stats.px;
Willy Tarreau91861262007-10-17 17:06:05 +02002203 /* skip the disabled proxies and non-networked ones */
2204 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002205 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002206 return 0;
2207
Willy Tarreau295a8372011-03-10 11:25:07 +01002208 si->applet.ctx.stats.px = px->next;
2209 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02002210 }
2211 /* here, we just have reached the last proxy */
2212
Willy Tarreau94981132012-05-21 17:09:48 +02002213 si->conn.data_st = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002214 /* fall through */
2215
Willy Tarreau295a8372011-03-10 11:25:07 +01002216 case STAT_ST_END:
2217 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002218 chunk_printf(&msg, "</body></html>\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002219 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002220 return 0;
2221 }
Willy Tarreau91861262007-10-17 17:06:05 +02002222
Willy Tarreau94981132012-05-21 17:09:48 +02002223 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002224 /* fall through */
2225
Willy Tarreau295a8372011-03-10 11:25:07 +01002226 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002227 return 1;
2228
2229 default:
2230 /* unknown state ! */
Willy Tarreau94981132012-05-21 17:09:48 +02002231 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002232 return -1;
2233 }
2234}
2235
2236
2237/*
2238 * Dumps statistics for a proxy.
2239 * Returns 0 if it had to stop dumping data because of lack of buffer space,
2240 * ot non-zero if everything completed.
2241 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002242static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002243{
Willy Tarreau94981132012-05-21 17:09:48 +02002244 struct session *s = si->conn.data_ctx;
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002245 struct buffer *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002246 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002247 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002248 struct chunk msg;
2249
David du Colombier7af46052012-05-16 14:16:48 +02002250 chunk_init(&msg, trash, trashlen);
Willy Tarreau91861262007-10-17 17:06:05 +02002251
Willy Tarreau295a8372011-03-10 11:25:07 +01002252 switch (si->applet.ctx.stats.px_st) {
2253 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002254 /* we are on a new proxy */
2255
2256 if (uri && uri->scope) {
2257 /* we have a limited scope, we have to check the proxy name */
2258 struct stat_scope *scope;
2259 int len;
2260
2261 len = strlen(px->id);
2262 scope = uri->scope;
2263
2264 while (scope) {
2265 /* match exact proxy name */
2266 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2267 break;
2268
2269 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002270 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002271 break;
2272 scope = scope->next;
2273 }
2274
2275 /* proxy name not found : don't dump anything */
2276 if (scope == NULL)
2277 return 1;
2278 }
2279
Willy Tarreau295a8372011-03-10 11:25:07 +01002280 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
2281 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002282 return 1;
2283
Willy Tarreau295a8372011-03-10 11:25:07 +01002284 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002285 /* fall through */
2286
Willy Tarreau295a8372011-03-10 11:25:07 +01002287 case STAT_PX_ST_TH:
2288 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
2289 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002290 /* A form to enable/disable this proxy servers */
2291 chunk_printf(&msg,
2292 "<form action=\"%s\" method=\"post\">",
2293 uri->uri_prefix);
2294 }
2295
Willy Tarreau55bb8452007-10-17 18:44:57 +02002296 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002297 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002298 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002299 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002300 "<th class=\"pxname\" width=\"10%%\"");
2301
2302 if (uri->flags&ST_SHLGNDS) {
2303 /* cap, mode, id */
2304 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
2305 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2306 px->uuid);
2307
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002308 chunk_printf(&msg, "\"");
2309 }
2310
2311 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002312 ">%s<a name=\"%s\"></a>"
2313 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002314 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002315 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002316 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002317 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002318 "<tr class=\"titre\">",
2319 (uri->flags & ST_SHLGNDS)?"<u>":"",
2320 px->id, px->id, px->id,
2321 (uri->flags & ST_SHLGNDS)?"</u>":"",
2322 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2323
Willy Tarreau295a8372011-03-10 11:25:07 +01002324 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002325 /* Column heading for Enable or Disable server */
2326 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
2327 }
2328
2329 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002330 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002331 "<th colspan=3>Queue</th>"
2332 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002333 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002334 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002335 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002336 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002337 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002338 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002339 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002340 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002341 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002342 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002343 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002344 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2345 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002346 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002347
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002348 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002349 return 0;
2350 }
Willy Tarreau91861262007-10-17 17:06:05 +02002351
Willy Tarreau295a8372011-03-10 11:25:07 +01002352 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002353 /* fall through */
2354
Willy Tarreau295a8372011-03-10 11:25:07 +01002355 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002356 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002357 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002358 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
2359 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002360 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002361 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002362 "<tr class=\"frontend\">");
2363
Willy Tarreau295a8372011-03-10 11:25:07 +01002364 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002365 /* Column sub-heading for Enable or Disable server */
2366 chunk_printf(&msg, "<td></td>");
2367 }
2368
2369 chunk_printf(&msg,
2370 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002371 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002372 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2373 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002374 "",
2375 px->id, px->id);
2376
2377 if (px->mode == PR_MODE_HTTP) {
2378 chunk_printf(&msg,
2379 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01002380 "<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 +01002381 "",
2382 read_freq_ctr(&px->fe_req_per_sec),
2383 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002384 px->fe_counters.p.http.rps_max,
2385 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01002386 LIM2A2(px->fe_sps_lim, "-"));
2387 } else {
2388 chunk_printf(&msg,
2389 /* sessions rate : current, max, limit */
2390 "<td>%s</td><td>%s</td><td>%s</td>"
2391 "",
2392 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002393 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01002394 }
2395
2396 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002397 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002398 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002399 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002400 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002401 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002402
2403 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2404 if (px->mode == PR_MODE_HTTP) {
2405 int i;
2406
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002407 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002408
2409 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002410 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002411
Willy Tarreaueabea072011-09-10 23:29:44 +02002412 chunk_printf(&msg, " other=%lld,", px->fe_counters.p.http.rsp[0]);
2413 chunk_printf(&msg, " intercepted=%lld\"", px->fe_counters.intercepted_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002414 }
2415
2416 chunk_printf(&msg,
2417 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002418 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002419 /* bytes : in, out */
2420 "<td>%s</td><td>%s</td>"
2421 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002422 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002423 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002424 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002425 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002426
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002427 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002428 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002429 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002430 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002431 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002432 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002433 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002434 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002435 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002436 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002437 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002438 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002439 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2440 U2H2(px->fe_counters.failed_req),
Willy Tarreau562515c2011-07-25 08:11:52 +02002441 px->state == PR_STREADY ? "OPEN" :
2442 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002443 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002444 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002445 /* pxid, name, queue cur, queue max, */
2446 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002447 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002448 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002449 /* bytes : in, out */
2450 "%lld,%lld,"
2451 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002452 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002453 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002454 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002455 /* warnings: retries, redispatches */
2456 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002457 /* server status : reflect frontend status */
2458 "%s,"
2459 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002460 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002461 /* pid, iid, sid, throttle, lbtot, tracked, type */
2462 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002463 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002464 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002465 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002466 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002467 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002468 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2469 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2470 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2471 px->fe_counters.failed_req,
Willy Tarreau562515c2011-07-25 08:11:52 +02002472 px->state == PR_STREADY ? "OPEN" :
2473 px->state == PR_STFULL ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002474 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002475 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002476 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002477
2478 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2479 if (px->mode == PR_MODE_HTTP) {
2480 int i;
2481
2482 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002483 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002484
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002485 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002486 } else {
2487 chunk_printf(&msg, ",,,,,,");
2488 }
2489
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002490 /* failed health analyses */
2491 chunk_printf(&msg, ",");
2492
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002493 /* requests : req_rate, req_rate_max, req_tot, */
2494 chunk_printf(&msg, "%u,%u,%lld,",
2495 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002496 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002497
Willy Tarreauae526782010-03-04 20:34:23 +01002498 /* errors: cli_aborts, srv_aborts */
2499 chunk_printf(&msg, ",,");
2500
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002501 /* finish with EOL */
2502 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002503 }
Willy Tarreau91861262007-10-17 17:06:05 +02002504
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002505 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002506 return 0;
2507 }
2508
Willy Tarreau295a8372011-03-10 11:25:07 +01002509 si->applet.ctx.stats.l = px->listen; /* may be NULL */
2510 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002511 /* fall through */
2512
Willy Tarreau295a8372011-03-10 11:25:07 +01002513 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002514 /* stats.l has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002515 for (; si->applet.ctx.stats.l != NULL; si->applet.ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002516 if (buffer_almost_full(rep))
2517 return 0;
2518
Willy Tarreau295a8372011-03-10 11:25:07 +01002519 l = si->applet.ctx.stats.l;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002520 if (!l->counters)
2521 continue;
2522
Willy Tarreau295a8372011-03-10 11:25:07 +01002523 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2524 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002525 break;
2526
Willy Tarreau295a8372011-03-10 11:25:07 +01002527 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002528 continue;
2529 }
2530
Willy Tarreau295a8372011-03-10 11:25:07 +01002531 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002532 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002533 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002534 /* Column sub-heading for Enable or Disable server */
2535 chunk_printf(&msg, "<td></td>");
2536 }
2537 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002538
2539 if (uri->flags&ST_SHLGNDS) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02002540 char str[INET6_ADDRSTRLEN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002541 int port;
2542
Willy Tarreau631f01c2011-09-05 00:36:48 +02002543 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002544
Willy Tarreau631f01c2011-09-05 00:36:48 +02002545 port = get_host_port(&l->addr);
2546 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2547 case AF_INET:
2548 chunk_printf(&msg, "IPv4: %s:%d, ", str, port);
2549 break;
2550 case AF_INET6:
2551 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, port);
2552 break;
2553 case AF_UNIX:
2554 chunk_printf(&msg, "unix, ");
2555 break;
2556 case -1:
2557 chunk_printf(&msg, "(%s), ", strerror(errno));
2558 break;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002559 }
2560
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002561 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002562 chunk_printf(&msg, "id: %d\"", l->luid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002563 }
2564
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002565 chunk_printf(&msg,
2566 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002567 ">%s<a name=\"%s/+%s\"></a>"
2568 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002569 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002570 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002571 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002572 "<td>%s</td><td>%s</td><td>%s</td>"
2573 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002574 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002575 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002576 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002577 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002578 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002579 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002580 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2581 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2582
2583 chunk_printf(&msg,
2584 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002585 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002586 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002587 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002588 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002589 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002590 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002591 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002592 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002593 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002594 "",
2595 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2596 U2H2(l->counters->failed_req),
Willy Tarreaua17c2d92011-07-25 08:16:20 +02002597 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002598 } else {
2599 chunk_printf(&msg,
2600 /* pxid, name, queue cur, queue max, */
2601 "%s,%s,,,"
2602 /* sessions: current, max, limit, total */
2603 "%d,%d,%d,%lld,"
2604 /* bytes: in, out */
2605 "%lld,%lld,"
2606 /* denied: req, resp */
2607 "%lld,%lld,"
2608 /* errors: request, connect, response */
2609 "%lld,,,"
2610 /* warnings: retries, redispatches */
2611 ",,"
2612 /* server status: reflect listener status */
2613 "%s,"
2614 /* rest of server: nothing */
2615 ",,,,,,,,"
2616 /* pid, iid, sid, throttle, lbtot, tracked, type */
2617 "%d,%d,%d,,,,%d,"
2618 /* rate, rate_lim, rate_max */
2619 ",,,"
2620 /* check_status, check_code, check_duration */
2621 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002622 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2623 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002624 /* failed health analyses */
2625 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002626 /* requests : req_rate, req_rate_max, req_tot, */
2627 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002628 /* errors: cli_aborts, srv_aborts */
2629 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002630 "\n",
2631 px->id, l->name,
2632 l->nbconn, l->counters->conn_max,
2633 l->maxconn, l->counters->cum_conn,
2634 l->counters->bytes_in, l->counters->bytes_out,
2635 l->counters->denied_req, l->counters->denied_resp,
2636 l->counters->failed_req,
2637 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2638 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2639 }
2640
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002641 if (bi_putchk(rep, &msg) == -1)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002642 return 0;
2643 }
2644
Willy Tarreau295a8372011-03-10 11:25:07 +01002645 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2646 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002647 /* fall through */
2648
Willy Tarreau295a8372011-03-10 11:25:07 +01002649 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002650 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002651 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002652 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 +02002653
Willy Tarreau4e33d862009-10-11 23:35:10 +02002654 if (buffer_almost_full(rep))
2655 return 0;
2656
Willy Tarreau295a8372011-03-10 11:25:07 +01002657 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002658
Willy Tarreau295a8372011-03-10 11:25:07 +01002659 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2660 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002661 break;
2662
Willy Tarreau295a8372011-03-10 11:25:07 +01002663 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002664 continue;
2665 }
2666
Willy Tarreau44267702011-10-28 15:35:33 +02002667 if (sv->track)
2668 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002669 else
2670 svs = sv;
2671
Willy Tarreau91861262007-10-17 17:06:05 +02002672 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002673 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002674 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002675 else if (svs->state & SRV_RUNNING) {
2676 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002677 sv_state = 3; /* UP */
2678 else
2679 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002680
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002681 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002682 sv_state += 2;
2683 }
Willy Tarreau91861262007-10-17 17:06:05 +02002684 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002685 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002686 sv_state = 1; /* going up */
2687 else
2688 sv_state = 0; /* DOWN */
2689
Willy Tarreau295a8372011-03-10 11:25:07 +01002690 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002691 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002692 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002693 continue;
2694 }
2695
Willy Tarreau295a8372011-03-10 11:25:07 +01002696 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002697 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2698 "UP %d/%d &darr;", "UP",
2699 "NOLB %d/%d &darr;", "NOLB",
2700 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002701 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002702 chunk_printf(&msg,
2703 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002704 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002705 );
2706 }
2707 else {
2708 chunk_printf(&msg,
2709 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002710 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002711 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2712 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002713
Willy Tarreau295a8372011-03-10 11:25:07 +01002714 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002715 chunk_printf(&msg,
2716 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2717 sv->id);
2718 }
2719
2720 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002721
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002722 if (uri->flags&ST_SHLGNDS) {
2723 char str[INET6_ADDRSTRLEN];
2724
Willy Tarreau631f01c2011-09-05 00:36:48 +02002725 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002726
Willy Tarreau631f01c2011-09-05 00:36:48 +02002727 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01002728 case AF_INET:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002729 chunk_printf(&msg, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
David du Colombier6f5ccb12011-03-10 22:26:24 +01002730 break;
2731 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002732 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2733 break;
2734 case AF_UNIX:
2735 chunk_printf(&msg, "unix, ");
2736 break;
2737 case -1:
2738 chunk_printf(&msg, "(%s), ", strerror(errno));
2739 break;
2740 default: /* address family not supported */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002741 break;
2742 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002743
2744 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002745 chunk_printf(&msg, "id: %d", sv->puid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002746
2747 /* cookie */
2748 if (sv->cookie) {
2749 struct chunk src;
2750
2751 chunk_printf(&msg, ", cookie: '");
2752
2753 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2754 chunk_htmlencode(&msg, &src);
2755
2756 chunk_printf(&msg, "'");
2757 }
2758
2759 chunk_printf(&msg, "\"");
2760 }
2761
2762 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002763 ">%s<a name=\"%s/%s\"></a>"
2764 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002765 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002766 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002767 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002768 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002769 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002770 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002771 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002772 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002773 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002774 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002775 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002776 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2777 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002778 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2779
2780 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2781 if (px->mode == PR_MODE_HTTP) {
2782 int i;
2783
2784 chunk_printf(&msg, " title=\"rsp codes:");
2785
2786 for (i = 1; i < 6; i++)
2787 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2788
2789 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2790 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002791
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002792 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002793 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002794 ">%s%s%s</td><td>%s</td>",
2795 (px->mode == PR_MODE_HTTP)?"<u>":"",
2796 U2H0(sv->counters.cum_sess),
2797 (px->mode == PR_MODE_HTTP)?"</u>":"",
2798 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002799
2800 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002801 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002802 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002803 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002804 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002805 /* errors : request, connect */
2806 "<td></td><td>%s</td>"
2807 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002808 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002809 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002810 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002811 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002812 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2813 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002814 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002815 sv->counters.cli_aborts,
2816 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002817 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002818 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002819
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002820 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002821 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002822
Cyril Bontécd19e512010-01-31 22:34:03 +01002823 if (sv->state & SRV_MAINTAIN) {
2824 chunk_printf(&msg, "%s ",
2825 human_time(now.tv_sec - sv->last_change, 1));
2826 chunk_printf(&msg, "MAINT");
2827 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002828 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2829 chunk_printf(&msg, "%s ",
2830 human_time(now.tv_sec - svs->last_change, 1));
2831 chunk_printf(&msg, "MAINT(via)");
2832 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002833 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002834 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002835 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002836
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002837 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002838 srv_hlt_st[sv_state],
2839 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2840 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002841 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002842
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002843 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002844 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002845 get_check_status_description(sv->check_status));
2846
2847 if (*sv->check_desc) {
2848 struct chunk src;
2849
2850 chunk_printf(&msg, ": ");
2851
2852 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2853 chunk_htmlencode(&msg, &src);
2854 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002855
Willy Tarreaue0454092010-02-26 12:29:07 +01002856 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002857 tv_iszero(&sv->check_start)?"":"* ",
2858 get_check_status_info(sv->check_status));
2859
2860 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002861 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002862
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002863 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002864 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002865 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002866 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002867
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002868 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002869 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002870 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002871 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002872 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002873 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002874 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002875 (sv->state & SRV_BACKUP) ? "-" : "Y",
2876 (sv->state & SRV_BACKUP) ? "Y" : "-");
2877
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002878 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002879 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002880 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002881 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2882
2883 if (svs->observe)
2884 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2885
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002886 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002887 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002888 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002889 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002890 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002891 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002892 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002893 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2894 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002895 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002896 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002897 "<td colspan=3></td>");
2898
2899 /* throttle */
2900 if ((sv->state & SRV_WARMINGUP) &&
2901 now.tv_sec < sv->last_change + sv->slowstart &&
2902 now.tv_sec >= sv->last_change) {
2903 unsigned int ratio;
2904 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002905 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002906 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002907 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002908 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002909 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002910 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002911 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002912 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2913 "UP %d/%d,", "UP,",
2914 "NOLB %d/%d,", "NOLB,",
2915 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002916 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002917 /* pxid, name */
2918 "%s,%s,"
2919 /* queue : current, max */
2920 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002921 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002922 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002923 /* bytes : in, out */
2924 "%lld,%lld,"
2925 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002926 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002927 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002928 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002929 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002930 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002931 "",
2932 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002933 sv->nbpend, sv->counters.nbpend_max,
2934 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002935 sv->counters.bytes_in, sv->counters.bytes_out,
2936 sv->counters.failed_secu,
2937 sv->counters.failed_conns, sv->counters.failed_resp,
2938 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002939
Willy Tarreau55bb8452007-10-17 18:44:57 +02002940 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002941 if (sv->state & SRV_MAINTAIN) {
2942 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002943 }
2944 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2945 chunk_printf(&msg, "MAINT(via),");
2946 }
2947 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002948 chunk_printf(&msg,
2949 srv_hlt_st[sv_state],
2950 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2951 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2952 }
Willy Tarreau91861262007-10-17 17:06:05 +02002953
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002954 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002955 /* weight, active, backup */
2956 "%d,%d,%d,"
2957 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002958 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002959 (sv->state & SRV_BACKUP) ? 0 : 1,
2960 (sv->state & SRV_BACKUP) ? 1 : 0);
2961
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002962 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002963 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002964 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002965 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002966 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002967 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002968 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002969 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002970 ",,,,");
2971
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002972 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002973 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002974 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002975 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002976 LIM2A0(sv->maxqueue, ""),
2977 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002978
2979 /* throttle */
2980 if ((sv->state & SRV_WARMINGUP) &&
2981 now.tv_sec < sv->last_change + sv->slowstart &&
2982 now.tv_sec >= sv->last_change) {
2983 unsigned int ratio;
2984 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002985 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002986 }
2987
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002988 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002989 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002990
2991 /* tracked */
Willy Tarreau44267702011-10-28 15:35:33 +02002992 if (sv->track)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002993 chunk_printf(&msg, "%s/%s,",
Willy Tarreau44267702011-10-28 15:35:33 +02002994 sv->track->proxy->id, sv->track->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002995 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002996 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002997
Willy Tarreau7f062c42009-03-05 18:43:00 +01002998 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002999 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01003000
3001 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003002 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003003 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02003004 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01003005
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003006 if (sv->state & SRV_CHECKED) {
3007 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003008 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003009
3010 /* check_code */
3011 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003012 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003013 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003014 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003015
3016 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01003017 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003018 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003019 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003020 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003021
3022 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003023 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003024 }
3025
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003026 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3027 if (px->mode == PR_MODE_HTTP) {
3028 int i;
3029
3030 for (i=1; i<6; i++)
3031 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
3032
3033 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
3034 } else {
3035 chunk_printf(&msg, ",,,,,,");
3036 }
3037
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003038 /* failed health analyses */
3039 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
3040
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003041 /* requests : req_rate, req_rate_max, req_tot, */
3042 chunk_printf(&msg, ",,,");
3043
Willy Tarreauae526782010-03-04 20:34:23 +01003044 /* errors: cli_aborts, srv_aborts */
3045 chunk_printf(&msg, "%lld,%lld,",
3046 sv->counters.cli_aborts, sv->counters.srv_aborts);
3047
Willy Tarreau7f062c42009-03-05 18:43:00 +01003048 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003049 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003050 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003051 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003052 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003053 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02003054
Willy Tarreau295a8372011-03-10 11:25:07 +01003055 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02003056 /* fall through */
3057
Willy Tarreau295a8372011-03-10 11:25:07 +01003058 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02003059 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003060 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01003061 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
3062 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003063 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01003064 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003065 /* Column sub-heading for Enable or Disable server */
3066 chunk_printf(&msg, "<td></td>");
3067 }
3068 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003069
3070 if (uri->flags&ST_SHLGNDS) {
3071 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003072 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003073 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003074
3075 /* cookie */
3076 if (px->cookie_name) {
3077 struct chunk src;
3078
3079 chunk_printf(&msg, ", cookie: '");
3080
3081 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3082 chunk_htmlencode(&msg, &src);
3083
3084 chunk_printf(&msg, "'");
3085 }
3086
3087 chunk_printf(&msg, "\"");
3088
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003089 }
3090
3091 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02003092 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01003093 ">%s<a name=\"%s/Backend\"></a>"
3094 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02003095 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003096 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003097 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003098 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003099 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003100 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02003101 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01003102 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003103 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
3104 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02003105
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003106 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003107 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003108 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003109 "<td"
3110 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003111 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003112
3113 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3114 if (px->mode == PR_MODE_HTTP) {
3115 int i;
3116
3117 chunk_printf(&msg, " title=\"rsp codes:");
3118
3119 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003120 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003121
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003122 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003123 }
3124
3125 chunk_printf(&msg,
3126 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01003127 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003128 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003129 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003130 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003131 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003132 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01003133 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003134 U2H7(px->be_counters.cum_lbconn),
3135 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003136
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003137 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02003138 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003139 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01003140 /* errors : request, connect */
3141 "<td></td><td>%s</td>"
3142 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01003143 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003144 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003145 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003146 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02003147 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003148 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02003149 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003150 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3151 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003152 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003153 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
3154 U2H2(px->be_counters.failed_conns),
3155 px->be_counters.cli_aborts,
3156 px->be_counters.srv_aborts,
3157 U2H5(px->be_counters.failed_resp),
3158 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003159 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01003160 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3161 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01003162 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003163 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003164
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003165 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003166 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003167 "<td class=ac>&nbsp;</td><td>%d</td>"
3168 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003169 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003170 "</tr>",
3171 px->down_trans,
3172 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003173 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003174 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02003175 /* pxid, name */
3176 "%s,BACKEND,"
3177 /* queue : current, max */
3178 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003179 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003180 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003181 /* bytes : in, out */
3182 "%lld,%lld,"
3183 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003184 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003185 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003186 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003187 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003188 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003189 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02003190 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003191 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02003192 * active and backups. */
3193 "%s,"
3194 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003195 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003196 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003197 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003198 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003199 /* rate, rate_lim, rate_max, */
3200 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003201 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003202 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02003203 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003204 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3205 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3206 px->be_counters.bytes_in, px->be_counters.bytes_out,
3207 px->be_counters.denied_req, px->be_counters.denied_resp,
3208 px->be_counters.failed_conns, px->be_counters.failed_resp,
3209 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01003210 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01003211 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003212 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003213 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01003214 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003215 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003216 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003217 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003218 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003219
3220 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3221 if (px->mode == PR_MODE_HTTP) {
3222 int i;
3223
3224 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003225 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003226
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003227 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003228 } else {
3229 chunk_printf(&msg, ",,,,,,");
3230 }
3231
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003232 /* failed health analyses */
3233 chunk_printf(&msg, ",");
3234
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003235 /* requests : req_rate, req_rate_max, req_tot, */
3236 chunk_printf(&msg, ",,,");
3237
Willy Tarreauae526782010-03-04 20:34:23 +01003238 /* errors: cli_aborts, srv_aborts */
3239 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003240 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01003241
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003242 /* finish with EOL */
3243 chunk_printf(&msg, "\n");
3244
Willy Tarreau55bb8452007-10-17 18:44:57 +02003245 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003246 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003247 return 0;
3248 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003249
Willy Tarreau295a8372011-03-10 11:25:07 +01003250 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02003251 /* fall through */
3252
Willy Tarreau295a8372011-03-10 11:25:07 +01003253 case STAT_PX_ST_END:
3254 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003255 chunk_printf(&msg, "</table>");
3256
Willy Tarreau295a8372011-03-10 11:25:07 +01003257 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003258 /* close the form used to enable/disable this proxy servers */
3259 chunk_printf(&msg,
3260 "Choose the action to perform on the checked servers : "
3261 "<select name=action>"
3262 "<option value=\"\"></option>"
3263 "<option value=\"disable\">Disable</option>"
3264 "<option value=\"enable\">Enable</option>"
Willy Tarreaud7282242012-06-04 00:22:44 +02003265 "<option value=\"stop\">Soft Stop</option>"
3266 "<option value=\"start\">Soft Start</option>"
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003267 "<option value=\"shutdown\">Kill Sessions</option>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003268 "</select>"
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003269 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003270 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3271 "</form>",
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003272 px->uuid);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003273 }
3274
3275 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02003276
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003277 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003278 return 0;
3279 }
Willy Tarreau91861262007-10-17 17:06:05 +02003280
Willy Tarreau295a8372011-03-10 11:25:07 +01003281 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02003282 /* fall through */
3283
Willy Tarreau295a8372011-03-10 11:25:07 +01003284 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02003285 return 1;
3286
3287 default:
3288 /* unknown state, we should put an abort() here ! */
3289 return 1;
3290 }
3291}
3292
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003293/* This function dumps a complete session state onto the stream intreface's
3294 * read buffer. The data_ctx must have been zeroed first, and the flags
3295 * properly set. The session has to be set in data_ctx.sess.target. It returns
3296 * 0 if the output buffer is full and it needs to be called again, otherwise
3297 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003298 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003299static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003300{
3301 struct tm tm;
3302 struct chunk msg;
3303 struct session *sess;
3304 extern const char *monthname[12];
3305 char pn[INET6_ADDRSTRLEN];
3306
David du Colombier7af46052012-05-16 14:16:48 +02003307 chunk_init(&msg, trash, trashlen);
Willy Tarreau295a8372011-03-10 11:25:07 +01003308 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003309
Willy Tarreau295a8372011-03-10 11:25:07 +01003310 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003311 /* session changed, no need to go any further */
3312 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003313 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003314 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003315 si->applet.ctx.sess.target = NULL;
3316 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003317 return 1;
3318 }
3319
Willy Tarreau295a8372011-03-10 11:25:07 +01003320 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003321 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003322 si->applet.ctx.sess.uid = sess->uniq_id;
3323 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003324 /* fall through */
3325
3326 case 1:
3327 chunk_printf(&msg,
3328 "%p: id=%u, proto=%s",
3329 sess,
3330 sess->uniq_id,
3331 sess->listener->proto->name);
3332
Willy Tarreau6471afb2011-09-23 10:54:59 +02003333 switch (addr_to_str(&sess->si[0].addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003334 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003335 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003336 chunk_printf(&msg, " source=%s:%d\n",
Willy Tarreau6471afb2011-09-23 10:54:59 +02003337 pn, get_host_port(&sess->si[0].addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003338 break;
3339 case AF_UNIX:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003340 chunk_printf(&msg, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003341 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003342 default:
3343 /* no more information to print right now */
3344 chunk_printf(&msg, "\n");
3345 break;
3346 }
3347
3348 chunk_printf(&msg,
3349 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003350 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003351
3352 chunk_printf(&msg,
Daniel Schultze90690c72012-03-23 10:53:36 -07003353 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003354 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3355 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3356 sess->listener ? sess->listener->luid : 0);
3357
Willy Tarreau59b94792012-05-11 16:16:40 +02003358 si_get_to_addr(&sess->si[0]);
Daniel Schultze90690c72012-03-23 10:53:36 -07003359 switch (addr_to_str(&sess->si[0].addr.to, pn, sizeof(pn))) {
3360 case AF_INET:
3361 case AF_INET6:
3362 chunk_printf(&msg, " addr=%s:%d\n",
3363 pn, get_host_port(&sess->si[0].addr.to));
3364 break;
3365 case AF_UNIX:
3366 chunk_printf(&msg, " addr=unix:%d\n", sess->listener->luid);
3367 break;
3368 default:
3369 /* no more information to print right now */
3370 chunk_printf(&msg, "\n");
3371 break;
3372 }
3373
Willy Tarreau50943332011-09-02 17:33:05 +02003374 if (sess->be->cap & PR_CAP_BE)
3375 chunk_printf(&msg,
Daniel Schultze90690c72012-03-23 10:53:36 -07003376 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003377 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003378 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3379 else
3380 chunk_printf(&msg, " backend=<NONE> (id=-1 mode=-)");
3381
Willy Tarreau59b94792012-05-11 16:16:40 +02003382 si_get_from_addr(&sess->si[1]);
Daniel Schultze90690c72012-03-23 10:53:36 -07003383 switch (addr_to_str(&sess->si[1].addr.from, pn, sizeof(pn))) {
3384 case AF_INET:
3385 case AF_INET6:
3386 chunk_printf(&msg, " addr=%s:%d\n",
3387 pn, get_host_port(&sess->si[1].addr.from));
3388 break;
3389 case AF_UNIX:
3390 chunk_printf(&msg, " addr=unix\n");
3391 break;
3392 default:
3393 /* no more information to print right now */
3394 chunk_printf(&msg, "\n");
3395 break;
3396 }
3397
3398 if (sess->be->cap & PR_CAP_BE)
3399 chunk_printf(&msg,
3400 " server=%s (id=%u)",
Willy Tarreau50943332011-09-02 17:33:05 +02003401 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
3402 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
3403 else
Daniel Schultze90690c72012-03-23 10:53:36 -07003404 chunk_printf(&msg, " server=<NONE> (id=-1)");
3405
Willy Tarreau59b94792012-05-11 16:16:40 +02003406 si_get_to_addr(&sess->si[1]);
Daniel Schultze90690c72012-03-23 10:53:36 -07003407 switch (addr_to_str(&sess->si[1].addr.to, pn, sizeof(pn))) {
3408 case AF_INET:
3409 case AF_INET6:
3410 chunk_printf(&msg, " addr=%s:%d\n",
3411 pn, get_host_port(&sess->si[1].addr.to));
3412 break;
3413 case AF_UNIX:
3414 chunk_printf(&msg, " addr=unix\n");
3415 break;
3416 default:
3417 /* no more information to print right now */
3418 chunk_printf(&msg, "\n");
3419 break;
3420 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003421
3422 chunk_printf(&msg,
3423 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
3424 sess->task,
3425 sess->task->state,
3426 sess->task->nice, sess->task->calls,
3427 sess->task->expire ?
3428 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3429 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3430 TICKS_TO_MS(1000)) : "<NEVER>",
3431 task_in_rq(sess->task) ? ", running" : "");
3432
3433 get_localtime(sess->logs.accept_date.tv_sec, &tm);
3434 chunk_printf(&msg,
3435 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
3436 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3437 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3438 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3439
3440 chunk_printf(&msg,
3441 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3442 &sess->si[0],
3443 sess->si[0].state,
3444 sess->si[0].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003445 si_fd(&sess->si[0]),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003446 sess->si[0].exp ?
3447 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3448 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3449 TICKS_TO_MS(1000)) : "<NEVER>",
3450 sess->si[0].err_type);
3451
3452 chunk_printf(&msg,
3453 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3454 &sess->si[1],
3455 sess->si[1].state,
3456 sess->si[1].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003457 si_fd(&sess->si[1]),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003458 sess->si[1].exp ?
3459 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3460 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3461 TICKS_TO_MS(1000)) : "<NEVER>",
3462 sess->si[1].err_type);
3463
3464 chunk_printf(&msg,
3465 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3466 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3467 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3468
3469
3470 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003471 " req=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003472 " an_exp=%s",
3473 sess->req,
3474 sess->req->flags, sess->req->analysers,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003475 sess->req->i, sess->req->o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003476 sess->req->pipe ? sess->req->pipe->data : 0,
3477 sess->req->to_forward,
3478 sess->req->analyse_exp ?
3479 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3480 TICKS_TO_MS(1000)) : "<NEVER>");
3481
3482 chunk_printf(&msg,
3483 " rex=%s",
3484 sess->req->rex ?
3485 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3486 TICKS_TO_MS(1000)) : "<NEVER>");
3487
3488 chunk_printf(&msg,
3489 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003490 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003491 sess->req->wex ?
3492 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3493 TICKS_TO_MS(1000)) : "<NEVER>",
3494 sess->req->data,
Willy Tarreau89fa7062012-03-02 16:13:16 +01003495 (int)(sess->req->p - sess->req->data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003496 sess->txn.req.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003497 sess->req->total);
3498
3499 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003500 " res=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003501 " an_exp=%s",
3502 sess->rep,
3503 sess->rep->flags, sess->rep->analysers,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003504 sess->rep->i, sess->rep->o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003505 sess->rep->pipe ? sess->rep->pipe->data : 0,
3506 sess->rep->to_forward,
3507 sess->rep->analyse_exp ?
3508 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3509 TICKS_TO_MS(1000)) : "<NEVER>");
3510
3511 chunk_printf(&msg,
3512 " rex=%s",
3513 sess->rep->rex ?
3514 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3515 TICKS_TO_MS(1000)) : "<NEVER>");
3516
3517 chunk_printf(&msg,
3518 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003519 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003520 sess->rep->wex ?
3521 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3522 TICKS_TO_MS(1000)) : "<NEVER>",
3523 sess->rep->data,
Willy Tarreau89fa7062012-03-02 16:13:16 +01003524 (int)(sess->rep->p - sess->rep->data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003525 sess->txn.rsp.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003526 sess->rep->total);
3527
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003528 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003529 return 0;
3530
3531 /* use other states to dump the contents */
3532 }
3533 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003534 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003535 return 1;
3536}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003537
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003538/* This function dumps all sessions' states onto the stream intreface's
3539 * read buffer. The data_ctx must have been zeroed first, and the flags
3540 * properly set. It returns 0 if the output buffer is full and it needs
3541 * to be called again, otherwise non-zero. It is designed to be called
3542 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003543 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003544static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003545{
3546 struct chunk msg;
3547
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003548 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003549 /* If we're forced to shut down, we might have to remove our
3550 * reference to the last session being dumped.
3551 */
Willy Tarreau94981132012-05-21 17:09:48 +02003552 if (si->conn.data_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003553 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3554 LIST_DEL(&si->applet.ctx.sess.bref.users);
3555 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003556 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003557 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003558 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003559 }
3560
David du Colombier7af46052012-05-16 14:16:48 +02003561 chunk_init(&msg, trash, trashlen);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003562
Willy Tarreau94981132012-05-21 17:09:48 +02003563 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003564 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003565 /* the function had not been called yet, let's prepare the
3566 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003567 * pointer to the first in the global list. When a target
3568 * session is being destroyed, it is responsible for updating
3569 * this pointer. We know we have reached the end when this
3570 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003571 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003572 LIST_INIT(&si->applet.ctx.sess.bref.users);
3573 si->applet.ctx.sess.bref.ref = sessions.n;
Willy Tarreau94981132012-05-21 17:09:48 +02003574 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003575 /* fall through */
3576
Willy Tarreau295a8372011-03-10 11:25:07 +01003577 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003578 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003579 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3580 LIST_DEL(&si->applet.ctx.sess.bref.users);
3581 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003582 }
3583
3584 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003585 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003586 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003587 struct session *curr_sess;
3588
Willy Tarreau295a8372011-03-10 11:25:07 +01003589 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003590
Willy Tarreau295a8372011-03-10 11:25:07 +01003591 if (si->applet.ctx.sess.target) {
3592 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003593 goto next_sess;
3594
Willy Tarreau295a8372011-03-10 11:25:07 +01003595 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003596 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003597 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003598 return 0;
3599
3600 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003601 LIST_DEL(&si->applet.ctx.sess.bref.users);
3602 LIST_INIT(&si->applet.ctx.sess.bref.users);
3603 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003604 break;
3605 }
3606
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003607 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003608 "%p: proto=%s",
3609 curr_sess,
3610 curr_sess->listener->proto->name);
3611
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003612
Willy Tarreau6471afb2011-09-23 10:54:59 +02003613 switch (addr_to_str(&curr_sess->si[0].addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003614 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003615 case AF_INET6:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003616 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003617 " src=%s:%d fe=%s be=%s srv=%s",
3618 pn,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003619 get_host_port(&curr_sess->si[0].addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003620 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003621 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003622 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003623 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003624 break;
3625 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02003626 chunk_printf(&msg,
3627 " src=unix:%d fe=%s be=%s srv=%s",
3628 curr_sess->listener->luid,
3629 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003630 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003631 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003632 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003633 break;
3634 }
3635
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003636 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003637 " ts=%02x age=%s calls=%d",
3638 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003639 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3640 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003641
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003642 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003643 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003644 curr_sess->req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003645 curr_sess->req->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003646 curr_sess->req->analysers,
3647 curr_sess->req->rex ?
3648 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3649 TICKS_TO_MS(1000)) : "");
3650
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003651 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003652 ",wx=%s",
3653 curr_sess->req->wex ?
3654 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3655 TICKS_TO_MS(1000)) : "");
3656
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003657 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003658 ",ax=%s]",
3659 curr_sess->req->analyse_exp ?
3660 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3661 TICKS_TO_MS(1000)) : "");
3662
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003663 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003664 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003665 curr_sess->rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003666 curr_sess->rep->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003667 curr_sess->rep->analysers,
3668 curr_sess->rep->rex ?
3669 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3670 TICKS_TO_MS(1000)) : "");
3671
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003672 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003673 ",wx=%s",
3674 curr_sess->rep->wex ?
3675 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3676 TICKS_TO_MS(1000)) : "");
3677
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003678 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003679 ",ax=%s]",
3680 curr_sess->rep->analyse_exp ?
3681 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3682 TICKS_TO_MS(1000)) : "");
3683
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003684 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003685 " s0=[%d,%1xh,fd=%d,ex=%s]",
3686 curr_sess->si[0].state,
3687 curr_sess->si[0].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003688 si_fd(&curr_sess->si[0]),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003689 curr_sess->si[0].exp ?
3690 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3691 TICKS_TO_MS(1000)) : "");
3692
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003693 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003694 " s1=[%d,%1xh,fd=%d,ex=%s]",
3695 curr_sess->si[1].state,
3696 curr_sess->si[1].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003697 si_fd(&curr_sess->si[1]),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003698 curr_sess->si[1].exp ?
3699 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3700 TICKS_TO_MS(1000)) : "");
3701
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003702 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003703 " exp=%s",
3704 curr_sess->task->expire ?
3705 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3706 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003707 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003708 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003709
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003710 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003711
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003712 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003713 /* let's try again later from this session. We add ourselves into
3714 * this session's users so that it can remove us upon termination.
3715 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003716 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003717 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003718 }
3719
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003720 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003721 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003722 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003723
Willy Tarreau295a8372011-03-10 11:25:07 +01003724 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003725 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003726 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003727 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3728 else
3729 chunk_printf(&msg, "Session not found.\n");
3730
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003731 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003732 return 0;
3733
Willy Tarreau295a8372011-03-10 11:25:07 +01003734 si->applet.ctx.sess.target = NULL;
3735 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003736 return 1;
3737 }
3738
Willy Tarreau94981132012-05-21 17:09:48 +02003739 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003740 /* fall through */
3741
3742 default:
Willy Tarreau94981132012-05-21 17:09:48 +02003743 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003744 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003745 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003746}
3747
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003748/* This function dumps all tables' states onto the stream intreface's
3749 * read buffer. The data_ctx must have been zeroed first, and the flags
3750 * properly set. It returns 0 if the output buffer is full and it needs
3751 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003752 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003753static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003754{
Willy Tarreau94981132012-05-21 17:09:48 +02003755 struct session *s = si->conn.data_ctx;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003756 struct chunk msg;
3757 struct ebmb_node *eb;
3758 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003759 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003760
3761 /*
Willy Tarreau94981132012-05-21 17:09:48 +02003762 * We have 3 possible states in si->conn.data_st :
Willy Tarreau295a8372011-03-10 11:25:07 +01003763 * - STAT_ST_INIT : the first call
3764 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003765 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003766 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003767 * and the entry pointer points to the next entry to be dumped,
3768 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003769 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003770 * data though.
3771 */
3772
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003773 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003774 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau94981132012-05-21 17:09:48 +02003775 if (si->conn.data_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003776 si->applet.ctx.table.entry->ref_cnt--;
3777 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003778 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003779 return 1;
3780 }
3781
David du Colombier7af46052012-05-16 14:16:48 +02003782 chunk_init(&msg, trash, trashlen);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003783
Willy Tarreau94981132012-05-21 17:09:48 +02003784 while (si->conn.data_st != STAT_ST_FIN) {
3785 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003786 case STAT_ST_INIT:
3787 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3788 if (!si->applet.ctx.table.proxy)
3789 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003790
Willy Tarreau295a8372011-03-10 11:25:07 +01003791 si->applet.ctx.table.entry = NULL;
Willy Tarreau94981132012-05-21 17:09:48 +02003792 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003793 break;
3794
Willy Tarreau295a8372011-03-10 11:25:07 +01003795 case STAT_ST_INFO:
3796 if (!si->applet.ctx.table.proxy ||
3797 (si->applet.ctx.table.target &&
3798 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
Willy Tarreau94981132012-05-21 17:09:48 +02003799 si->conn.data_st = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003800 break;
3801 }
3802
Willy Tarreau295a8372011-03-10 11:25:07 +01003803 if (si->applet.ctx.table.proxy->table.size) {
Simon Hormanc88b8872011-06-15 15:18:49 +09003804 if (show && !stats_dump_table_head_to_buffer(&msg, si, si->applet.ctx.table.proxy,
3805 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003806 return 0;
3807
Willy Tarreau295a8372011-03-10 11:25:07 +01003808 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003809 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
3810 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003811 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003812 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003813 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3814 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau94981132012-05-21 17:09:48 +02003815 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003816 break;
3817 }
3818 }
3819 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003820 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003821 break;
3822
Willy Tarreau295a8372011-03-10 11:25:07 +01003823 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003824 skip_entry = false;
3825
Willy Tarreau295a8372011-03-10 11:25:07 +01003826 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003827 /* we're filtering on some data contents */
3828 void *ptr;
3829 long long data;
3830
Willy Tarreau295a8372011-03-10 11:25:07 +01003831 dt = si->applet.ctx.table.data_type;
3832 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3833 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003834 dt);
3835
3836 data = 0;
3837 switch (stktable_data_types[dt].std_type) {
3838 case STD_T_SINT:
3839 data = stktable_data_cast(ptr, std_t_sint);
3840 break;
3841 case STD_T_UINT:
3842 data = stktable_data_cast(ptr, std_t_uint);
3843 break;
3844 case STD_T_ULL:
3845 data = stktable_data_cast(ptr, std_t_ull);
3846 break;
3847 case STD_T_FRQP:
3848 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003849 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003850 break;
3851 }
3852
3853 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003854 if ((data < si->applet.ctx.table.value &&
3855 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3856 si->applet.ctx.table.data_op == STD_OP_GT ||
3857 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3858 (data == si->applet.ctx.table.value &&
3859 (si->applet.ctx.table.data_op == STD_OP_NE ||
3860 si->applet.ctx.table.data_op == STD_OP_GT ||
3861 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3862 (data > si->applet.ctx.table.value &&
3863 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3864 si->applet.ctx.table.data_op == STD_OP_LT ||
3865 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003866 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003867 }
3868
Simon Hormanc88b8872011-06-15 15:18:49 +09003869 if (show && !skip_entry &&
3870 !stats_dump_table_entry_to_buffer(&msg, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003871 si->applet.ctx.table.entry))
3872 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003873
Willy Tarreau295a8372011-03-10 11:25:07 +01003874 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003875
Willy Tarreau295a8372011-03-10 11:25:07 +01003876 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003877 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003878 struct stksess *old = si->applet.ctx.table.entry;
3879 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01003880 if (show)
3881 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3882 else
3883 stksess_kill(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau295a8372011-03-10 11:25:07 +01003884 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003885 break;
3886 }
3887
Simon Hormanc88b8872011-06-15 15:18:49 +09003888
3889 if (show)
3890 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3891 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3892 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3893
Willy Tarreau295a8372011-03-10 11:25:07 +01003894 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau94981132012-05-21 17:09:48 +02003895 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003896 break;
3897
Willy Tarreau295a8372011-03-10 11:25:07 +01003898 case STAT_ST_END:
Willy Tarreau94981132012-05-21 17:09:48 +02003899 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003900 break;
3901 }
3902 }
3903 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003904}
3905
Willy Tarreaud426a182010-03-05 14:58:26 +01003906/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003907 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3908 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3909 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3910 * lines are respected within the limit of 70 output chars. Lines that are
3911 * continuation of a previous truncated line begin with "+" instead of " "
3912 * after the offset. The new pointer is returned.
3913 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003914static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3915 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003916{
3917 int end;
3918 unsigned char c;
3919
3920 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003921 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003922 return ptr;
3923
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003924 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003925
Willy Tarreaud426a182010-03-05 14:58:26 +01003926 while (ptr < len && ptr < bsize) {
3927 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003928 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003929 if (out->len > end - 2)
3930 break;
3931 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003932 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003933 if (out->len > end - 3)
3934 break;
3935 out->str[out->len++] = '\\';
3936 switch (c) {
3937 case '\t': c = 't'; break;
3938 case '\n': c = 'n'; break;
3939 case '\r': c = 'r'; break;
3940 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003941 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003942 }
3943 out->str[out->len++] = c;
3944 } else {
3945 if (out->len > end - 5)
3946 break;
3947 out->str[out->len++] = '\\';
3948 out->str[out->len++] = 'x';
3949 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3950 out->str[out->len++] = hextab[c & 0xF];
3951 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003952 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003953 /* we had a line break, let's return now */
3954 out->str[out->len++] = '\n';
3955 *line = ptr;
3956 return ptr;
3957 }
3958 }
3959 /* we have an incomplete line, we return it as-is */
3960 out->str[out->len++] = '\n';
3961 return ptr;
3962}
3963
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003964/* This function dumps all captured errors onto the stream intreface's
3965 * read buffer. The data_ctx must have been zeroed first, and the flags
3966 * properly set. It returns 0 if the output buffer is full and it needs
3967 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003968 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003969static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003970{
3971 extern const char *monthname[12];
3972 struct chunk msg;
3973
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003974 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003975 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003976
David du Colombier7af46052012-05-16 14:16:48 +02003977 chunk_init(&msg, trash, trashlen);
Willy Tarreau74808cb2009-03-04 15:53:18 +01003978
Willy Tarreau295a8372011-03-10 11:25:07 +01003979 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003980 /* the function had not been called yet, let's prepare the
3981 * buffer for a response.
3982 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003983 struct tm tm;
3984
3985 get_localtime(date.tv_sec, &tm);
3986 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3987 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3988 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3989 error_snapshot_id);
3990
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003991 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003992 /* Socket buffer full. Let's try again later from the same point */
3993 return 0;
3994 }
3995
Willy Tarreau295a8372011-03-10 11:25:07 +01003996 si->applet.ctx.errors.px = proxy;
3997 si->applet.ctx.errors.buf = 0;
3998 si->applet.ctx.errors.bol = 0;
3999 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004000 }
4001
4002 /* we have two inner loops here, one for the proxy, the other one for
4003 * the buffer.
4004 */
Willy Tarreau295a8372011-03-10 11:25:07 +01004005 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004006 struct error_snapshot *es;
4007
Willy Tarreau295a8372011-03-10 11:25:07 +01004008 if (si->applet.ctx.errors.buf == 0)
4009 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004010 else
Willy Tarreau295a8372011-03-10 11:25:07 +01004011 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004012
4013 if (!es->when.tv_sec)
4014 goto next;
4015
Willy Tarreau295a8372011-03-10 11:25:07 +01004016 if (si->applet.ctx.errors.iid >= 0 &&
4017 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
4018 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004019 goto next;
4020
Willy Tarreau295a8372011-03-10 11:25:07 +01004021 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004022 /* just print headers now */
4023
4024 char pn[INET6_ADDRSTRLEN];
4025 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004026 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004027
4028 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01004029 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01004030 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02004031 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01004032
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004033 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
4034 case AF_INET:
4035 case AF_INET6:
4036 port = get_host_port(&es->src);
4037 break;
4038 default:
4039 port = 0;
4040 }
4041
Willy Tarreau295a8372011-03-10 11:25:07 +01004042 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004043 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004044 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004045 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004046 " backend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004047 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004048 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
4049 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004050 break;
4051 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004052 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004053 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004054 " frontend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004055 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004056 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004057 break;
4058 }
4059
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004060 chunk_printf(&msg,
4061 ", server %s (#%d), event #%u\n"
4062 " src %s:%d, session #%d, session flags 0x%08x\n"
4063 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
4064 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
4065 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
4066 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
4067 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
4068 es->ev_id,
4069 pn, port, es->sid, es->s_flags,
4070 es->state, es->m_flags, es->t_flags,
4071 es->m_clen, es->m_blen,
4072 es->b_flags, es->b_out, es->b_tot,
4073 es->len, es->b_wrap, es->pos);
4074
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004075 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004076 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004077 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004078 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004079 si->applet.ctx.errors.ptr = 0;
4080 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004081 }
4082
Willy Tarreau295a8372011-03-10 11:25:07 +01004083 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004084 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004085 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004086 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004087 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004088 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004089 goto next;
4090 }
4091
4092 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01004093 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004094 int newptr;
4095 int newline;
4096
Willy Tarreau295a8372011-03-10 11:25:07 +01004097 newline = si->applet.ctx.errors.bol;
4098 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
4099 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004100 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004101
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004102 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004103 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004104 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004105 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004106 si->applet.ctx.errors.ptr = newptr;
4107 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004108 };
4109 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01004110 si->applet.ctx.errors.bol = 0;
4111 si->applet.ctx.errors.ptr = -1;
4112 si->applet.ctx.errors.buf++;
4113 if (si->applet.ctx.errors.buf > 1) {
4114 si->applet.ctx.errors.buf = 0;
4115 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004116 }
4117 }
4118
4119 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004120 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004121}
4122
Willy Tarreaub24281b2011-02-13 13:16:36 +01004123struct si_applet http_stats_applet = {
4124 .name = "<STATS>", /* used for logging */
4125 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004126 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004127};
4128
Simon Horman9bd2c732011-06-15 15:18:44 +09004129static struct si_applet cli_applet = {
Willy Tarreaub24281b2011-02-13 13:16:36 +01004130 .name = "<CLI>", /* used for logging */
4131 .fct = cli_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004132 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004133};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004134
Willy Tarreau10522fd2008-07-09 20:12:41 +02004135static struct cfg_kw_list cfg_kws = {{ },{
4136 { CFG_GLOBAL, "stats", stats_parse_global },
4137 { 0, NULL, NULL },
4138}};
4139
4140__attribute__((constructor))
4141static void __dumpstats_module_init(void)
4142{
4143 cfg_register_keywords(&cfg_kws);
4144}
4145
Willy Tarreau91861262007-10-17 17:06:05 +02004146/*
4147 * Local variables:
4148 * c-indent-level: 8
4149 * c-basic-offset: 8
4150 * End:
4151 */