blob: ed577ad670a0b723e43bec672ea47a972eb5e1c8 [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>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020020#include <pwd.h>
21#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020022
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreau10522fd2008-07-09 20:12:41 +020027#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020028#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
38
Willy Tarreau91861262007-10-17 17:06:05 +020039#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020040
41#include <proto/backend.h>
42#include <proto/buffers.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020043#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020044#include <proto/dumpstats.h>
45#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010046#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020047#include <proto/log.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010048#include <proto/pipe.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020049#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020050#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020051#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020052#include <proto/server.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010053#include <proto/stream_interface.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020054#include <proto/stream_sock.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010055#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020056
Willy Tarreau5ca791d2009-08-16 19:06:42 +020057const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020058 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +020059 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +020060 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020061 " help : this message\n"
62 " prompt : toggle interactive mode with prompt\n"
63 " quit : disconnect\n"
64 " show info : report information about the running process\n"
65 " show stat : report counters for each proxy and server\n"
66 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +010067 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +020068 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +020069 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +020070 " set weight : change a server's weight\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +010071 " set timeout : change a timeout setting\n"
Cyril Bontécd19e512010-01-31 22:34:03 +010072 " disable server : set a server in maintenance mode\n"
73 " enable server : re-enable a server that was previously in maintenance mode\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020074 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020075
Willy Tarreau6162db22009-10-10 17:13:00 +020076const char stats_permission_denied_msg[] =
77 "Permission denied\n"
78 "";
79
Willy Tarreau295a8372011-03-10 11:25:07 +010080/* data transmission states for the stats responses */
81enum {
82 STAT_ST_INIT = 0,
83 STAT_ST_HEAD,
84 STAT_ST_INFO,
85 STAT_ST_LIST,
86 STAT_ST_END,
87 STAT_ST_FIN,
88};
89
90/* data transmission states for the stats responses inside a proxy */
91enum {
92 STAT_PX_ST_INIT = 0,
93 STAT_PX_ST_TH,
94 STAT_PX_ST_FE,
95 STAT_PX_ST_LI,
96 STAT_PX_ST_SV,
97 STAT_PX_ST_BE,
98 STAT_PX_ST_END,
99 STAT_PX_ST_FIN,
100};
101
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200102/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +0200103 * a new stats socket. It returns a positive value upon success, 0 if the connection
104 * needs to be closed and ignored, or a negative value upon critical failure.
105 */
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200106int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200107{
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200108 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100109 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreaubc4af052011-02-13 13:25:14 +0100110 s->si[1].applet.private = s;
111 s->si[1].applet.st1 = 0;
112 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200113
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200114 tv_zero(&s->logs.tv_request);
115 s->logs.t_queue = 0;
116 s->logs.t_connect = 0;
117 s->logs.t_data = 0;
118 s->logs.t_close = 0;
119 s->logs.bytes_in = s->logs.bytes_out = 0;
120 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
121 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200122
Willy Tarreaueb472682010-05-28 18:46:57 +0200123 s->req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
124
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200125 if (s->listener->timeout) {
126 s->req->rto = *s->listener->timeout;
127 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200128 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200129 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200130}
131
Willy Tarreau07e9e642010-08-17 21:48:17 +0200132/* allocate a new stats frontend named <name>, and return it
133 * (or NULL in case of lack of memory).
134 */
135static struct proxy *alloc_stats_fe(const char *name)
136{
137 struct proxy *fe;
138
139 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
140 if (!fe)
141 return NULL;
142
143 LIST_INIT(&fe->pendconns);
144 LIST_INIT(&fe->acl);
145 LIST_INIT(&fe->block_cond);
146 LIST_INIT(&fe->redirect_rules);
147 LIST_INIT(&fe->mon_fail_cond);
148 LIST_INIT(&fe->switching_rules);
149 LIST_INIT(&fe->tcp_req.inspect_rules);
150
151 /* Timeouts are defined as -1, so we cannot use the zeroed area
152 * as a default value.
153 */
154 proxy_reset_timeouts(fe);
155
156 fe->last_change = now.tv_sec;
157 fe->id = strdup("GLOBAL");
158 fe->cap = PR_CAP_FE;
159 return fe;
160}
161
Willy Tarreaufbee7132007-10-18 13:53:22 +0200162/* This function parses a "stats" statement in the "global" section. It returns
163 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
164 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
165 * zero included. The trailing '\n' must not be written. The function must be
166 * called with <args> pointing to the first word after "stats".
167 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200168static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
169 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200170{
Willy Tarreau10522fd2008-07-09 20:12:41 +0200171 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200172 if (!strcmp(args[0], "socket")) {
Andreas Kohn16171e22011-01-19 20:29:32 +0100173 struct sockaddr_un *su;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200174 int cur_arg;
175
176 if (*args[1] == 0) {
177 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
178 return -1;
179 }
180
181 if (global.stats_sock.state != LI_NEW) {
182 snprintf(err, errlen, "'stats socket' already specified in global section");
183 return -1;
184 }
185
Andreas Kohn16171e22011-01-19 20:29:32 +0100186 su = str2sun(args[1]);
187 if (!su) {
188 snprintf(err, errlen, "'stats socket' path would require truncation");
189 return -1;
190 }
191 memcpy(&global.stats_sock.addr, su, sizeof(struct sockaddr_un)); // guaranteed to fit
Willy Tarreaufbee7132007-10-18 13:53:22 +0200192
Willy Tarreau89a63132009-08-16 17:41:45 +0200193 if (!global.stats_fe) {
Willy Tarreau07e9e642010-08-17 21:48:17 +0200194 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau89a63132009-08-16 17:41:45 +0200195 snprintf(err, errlen, "out of memory");
196 return -1;
197 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200198 global.stats_fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreau89a63132009-08-16 17:41:45 +0200199 }
200
Willy Tarreaufbee7132007-10-18 13:53:22 +0200201 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100202 global.stats_sock.options = LI_O_NONE;
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200203 global.stats_sock.accept = session_accept;
204 global.stats_fe->accept = stats_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200205 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200206 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200207 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreaueb472682010-05-28 18:46:57 +0200208 global.stats_sock.frontend = global.stats_fe;
Willy Tarreau6162db22009-10-10 17:13:00 +0200209 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau07e9e642010-08-17 21:48:17 +0200210 global.stats_fe->maxconn = global.stats_sock.maxconn;
Willy Tarreau89a63132009-08-16 17:41:45 +0200211 global.stats_sock.timeout = &global.stats_fe->timeout.client;
212
213 global.stats_sock.next = global.stats_fe->listen;
214 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200215
216 cur_arg = 2;
217 while (*args[cur_arg]) {
218 if (!strcmp(args[cur_arg], "uid")) {
219 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
220 cur_arg += 2;
221 }
222 else if (!strcmp(args[cur_arg], "gid")) {
223 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
224 cur_arg += 2;
225 }
226 else if (!strcmp(args[cur_arg], "mode")) {
227 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
228 cur_arg += 2;
229 }
230 else if (!strcmp(args[cur_arg], "user")) {
231 struct passwd *user;
232 user = getpwnam(args[cur_arg + 1]);
233 if (!user) {
234 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
235 args[cur_arg + 1]);
236 return -1;
237 }
238 global.stats_sock.perm.ux.uid = user->pw_uid;
239 cur_arg += 2;
240 }
241 else if (!strcmp(args[cur_arg], "group")) {
242 struct group *group;
243 group = getgrnam(args[cur_arg + 1]);
244 if (!group) {
245 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
246 args[cur_arg + 1]);
247 return -1;
248 }
249 global.stats_sock.perm.ux.gid = group->gr_gid;
250 cur_arg += 2;
251 }
Willy Tarreau6162db22009-10-10 17:13:00 +0200252 else if (!strcmp(args[cur_arg], "level")) {
253 if (!strcmp(args[cur_arg+1], "user"))
254 global.stats_sock.perm.ux.level = ACCESS_LVL_USER;
255 else if (!strcmp(args[cur_arg+1], "operator"))
256 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER;
257 else if (!strcmp(args[cur_arg+1], "admin"))
258 global.stats_sock.perm.ux.level = ACCESS_LVL_ADMIN;
259 else {
260 snprintf(err, errlen, "'stats socket level' only supports 'user', 'operator', and 'admin'");
261 return -1;
262 }
263 cur_arg += 2;
264 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200265 else {
Willy Tarreau6162db22009-10-10 17:13:00 +0200266 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', 'level', and 'mode'");
Willy Tarreaufbee7132007-10-18 13:53:22 +0200267 return -1;
268 }
269 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100270
Willy Tarreaufbee7132007-10-18 13:53:22 +0200271 uxst_add_listener(&global.stats_sock);
272 global.maxsock++;
273 }
274 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100275 unsigned timeout;
276 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
277
278 if (res) {
279 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
280 return -1;
281 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200282
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100283 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200284 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
285 return -1;
286 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200287 if (!global.stats_fe) {
288 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
289 snprintf(err, errlen, "out of memory");
290 return -1;
291 }
292 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200293 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200294 }
295 else if (!strcmp(args[0], "maxconn")) {
296 int maxconn = atol(args[1]);
297
298 if (maxconn <= 0) {
299 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
300 return -1;
301 }
302 global.maxsock -= global.stats_sock.maxconn;
303 global.stats_sock.maxconn = maxconn;
304 global.maxsock += global.stats_sock.maxconn;
Willy Tarreaueb472682010-05-28 18:46:57 +0200305 if (global.stats_fe)
306 global.stats_fe->maxconn = global.stats_sock.maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200307 }
308 else {
309 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
310 return -1;
311 }
312 return 0;
313}
314
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200315int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100316{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200317 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100318 "# pxname,svname,"
319 "qcur,qmax,"
320 "scur,smax,slim,stot,"
321 "bin,bout,"
322 "dreq,dresp,"
323 "ereq,econ,eresp,"
324 "wretr,wredis,"
325 "status,weight,act,bck,"
326 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200327 "pid,iid,sid,throttle,lbtot,tracked,type,"
328 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200329 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100330 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100331 "req_rate,req_rate_max,req_tot,"
332 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100333 "\n");
334}
335
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200336/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200337 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100338 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200339 * designating the function which will have to process the request, which can
340 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200341 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200342int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200343{
Willy Tarreaubc4af052011-02-13 13:25:14 +0100344 struct session *s = si->applet.private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200345 char *args[MAX_STATS_ARGS + 1];
346 int arg;
347
348 while (isspace((unsigned char)*line))
349 line++;
350
351 arg = 0;
352 args[arg] = line;
353
354 while (*line && arg < MAX_STATS_ARGS) {
355 if (isspace((unsigned char)*line)) {
356 *line++ = '\0';
357
358 while (isspace((unsigned char)*line))
359 line++;
360
361 args[++arg] = line;
362 continue;
363 }
364
365 line++;
366 }
367
368 while (++arg <= MAX_STATS_ARGS)
369 args[arg] = line;
370
Willy Tarreau295a8372011-03-10 11:25:07 +0100371 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200372 if (strcmp(args[0], "show") == 0) {
373 if (strcmp(args[1], "stat") == 0) {
374 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100375 si->applet.ctx.stats.flags |= STAT_BOUND;
376 si->applet.ctx.stats.iid = atoi(args[2]);
377 si->applet.ctx.stats.type = atoi(args[3]);
378 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200379 }
380
Willy Tarreau295a8372011-03-10 11:25:07 +0100381 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
382 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
383 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100384 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200385 }
386 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100387 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
388 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
389 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100390 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200391 }
392 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100393 si->applet.state = STAT_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200394 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100395 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100396 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200397 return 1;
398 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100399 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100400 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100401 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100402 si->applet.ctx.sess.target = NULL;
403 si->applet.ctx.sess.section = 0; /* start with session status */
404 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100405 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200406 }
407 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200408 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100409 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100410 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200411 return 1;
412 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200413 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100414 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200415 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100416 si->applet.ctx.errors.iid = -1;
417 si->applet.ctx.errors.px = NULL;
418 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100419 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200420 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200421 else if (strcmp(args[1], "table") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100422 si->applet.state = STAT_ST_INIT;
Willy Tarreau603861e2010-07-18 08:31:59 +0200423 if (*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100424 si->applet.ctx.table.target = find_stktable(args[2]);
425 if (!si->applet.ctx.table.target) {
426 si->applet.ctx.cli.msg = "No such table\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100427 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau603861e2010-07-18 08:31:59 +0200428 return 1;
429 }
430 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200431 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100432 si->applet.ctx.table.target = NULL;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200433
Willy Tarreau295a8372011-03-10 11:25:07 +0100434 si->applet.ctx.table.data_type = -1;
435 if (si->applet.ctx.table.target && strncmp(args[3], "data.", 5) == 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200436 /* condition on stored data value */
Willy Tarreau295a8372011-03-10 11:25:07 +0100437 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
438 if (si->applet.ctx.table.data_type < 0) {
439 si->applet.ctx.cli.msg = "Unknown data type\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100440 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200441 return 1;
442 }
443
Willy Tarreau295a8372011-03-10 11:25:07 +0100444 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
445 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100446 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200447 return 1;
448 }
449
Willy Tarreau295a8372011-03-10 11:25:07 +0100450 si->applet.ctx.table.data_op = get_std_op(args[4]);
451 if (si->applet.ctx.table.data_op < 0) {
452 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100453 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200454 return 1;
455 }
456
Willy Tarreau295a8372011-03-10 11:25:07 +0100457 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
458 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100459 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200460 return 1;
461 }
462 }
463 else if (*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100464 si->applet.ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value>\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100465 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200466 return 1;
467 }
468
Willy Tarreau295a8372011-03-10 11:25:07 +0100469 si->applet.ctx.table.proxy = NULL;
470 si->applet.ctx.table.entry = NULL;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100471 si->applet.st0 = STAT_CLI_O_TAB; // stats_dump_table_to_buffer
Willy Tarreau69f58c82010-07-12 17:55:33 +0200472 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200473 else { /* neither "stat" nor "info" nor "sess" nor "errors" no "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200474 return 0;
475 }
476 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200477 else if (strcmp(args[0], "clear") == 0) {
478 if (strcmp(args[1], "counters") == 0) {
479 struct proxy *px;
480 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200481 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200482 int clrall = 0;
483
484 if (strcmp(args[2], "all") == 0)
485 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200486
Willy Tarreau6162db22009-10-10 17:13:00 +0200487 /* check permissions */
488 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
489 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100490 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100491 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200492 return 1;
493 }
494
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200495 for (px = proxy; px; px = px->next) {
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200496 if (clrall)
497 memset(&px->counters, 0, sizeof(px->counters));
498 else {
499 px->counters.feconn_max = 0;
500 px->counters.beconn_max = 0;
Willy Tarreaud9b587f2010-02-26 10:05:55 +0100501 px->counters.fe_rps_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200502 px->counters.fe_sps_max = 0;
Willy Tarreaub36b4242010-06-04 20:59:39 +0200503 px->counters.fe_cps_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200504 px->counters.be_sps_max = 0;
505 px->counters.nbpend_max = 0;
506 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200507
508 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200509 if (clrall)
510 memset(&sv->counters, 0, sizeof(sv->counters));
511 else {
512 sv->counters.cur_sess_max = 0;
513 sv->counters.nbpend_max = 0;
514 sv->counters.sps_max = 0;
515 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200516
517 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200518 if (li->counters) {
519 if (clrall)
520 memset(li->counters, 0, sizeof(*li->counters));
521 else
522 li->counters->conn_max = 0;
523 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200524 }
525
526 return 1;
527 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200528 else if (strcmp(args[1], "table") == 0) {
529 struct proxy *px;
530 struct stksess *ts;
531 unsigned int ip_key;
532
533 if (!*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100534 si->applet.ctx.cli.msg = "\"table\" argument expected\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100535 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200536 return 1;
537 }
538
539 px = find_stktable(args[2]);
540
541 if (!px) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100542 si->applet.ctx.cli.msg = "No such table\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100543 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200544 return 1;
545 }
546
547 if (strcmp(args[3], "key") != 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100548 si->applet.ctx.cli.msg = "\"key\" argument expected\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100549 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200550 return 1;
551 }
552
553 if (!*args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100554 si->applet.ctx.cli.msg = "Key value expected\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100555 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200556 return 1;
557 }
558
559 if (px->table.type == STKTABLE_TYPE_IP) {
560 ip_key = htonl(inetaddr_host(args[4]));
561 static_table_key.key = (void *)&ip_key;
562 }
563 else {
Willy Tarreau295a8372011-03-10 11:25:07 +0100564 si->applet.ctx.cli.msg = "Removing keys from non-ip tables is not supported\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100565 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200566 return 1;
567 }
568
569 /* check permissions */
570 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100571 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100572 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200573 return 1;
574 }
575
576 ts = stktable_lookup_key(&px->table, &static_table_key);
577 if (!ts) {
578 /* silent return, entry was already removed */
579 return 1;
580 }
581 else if (ts->ref_cnt) {
582 /* don't delete an entry which is currently referenced */
Willy Tarreau295a8372011-03-10 11:25:07 +0100583 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100584 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200585 return 1;
586 }
587
Willy Tarreaudf39e952010-08-10 11:11:40 +0200588 stksess_kill(&px->table, ts);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200589 /* end of processing */
590 return 1;
591 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200592 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200593 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200594 return 0;
595 }
596 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200597 else if (strcmp(args[0], "get") == 0) {
598 if (strcmp(args[1], "weight") == 0) {
599 struct proxy *px;
600 struct server *sv;
601
602 /* split "backend/server" and make <line> point to server */
603 for (line = args[2]; *line; line++)
604 if (*line == '/') {
605 *line++ = '\0';
606 break;
607 }
608
609 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100610 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100611 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200612 return 1;
613 }
614
615 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100616 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100617 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200618 return 1;
619 }
620
621 /* return server's effective weight at the moment */
622 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
623 buffer_feed(si->ib, trash);
624 return 1;
625 }
626 else { /* not "get weight" */
627 return 0;
628 }
629 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200630 else if (strcmp(args[0], "set") == 0) {
631 if (strcmp(args[1], "weight") == 0) {
632 struct proxy *px;
633 struct server *sv;
634 int w;
635
636 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100637 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100638 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200639 return 1;
640 }
641
642 /* split "backend/server" and make <line> point to server */
643 for (line = args[2]; *line; line++)
644 if (*line == '/') {
645 *line++ = '\0';
646 break;
647 }
648
649 if (!*line || !*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100650 si->applet.ctx.cli.msg = "Require 'backend/server' and 'weight' or 'weight%'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100651 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200652 return 1;
653 }
654
655 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100656 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100657 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200658 return 1;
659 }
660
Cyril Bonté613f0df2011-03-03 20:49:04 +0100661 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100662 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100663 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100664 return 1;
665 }
666
Willy Tarreau4483d432009-10-10 19:30:08 +0200667 /* if the weight is terminated with '%', it is set relative to
668 * the initial weight, otherwise it is absolute.
669 */
670 w = atoi(args[3]);
671 if (strchr(args[3], '%') != NULL) {
672 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100673 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100674 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200675 return 1;
676 }
677 w = sv->iweight * w / 100;
678 }
679 else {
680 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100681 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100682 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200683 return 1;
684 }
685 }
686
687 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100688 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 +0100689 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200690 return 1;
691 }
692
693 sv->uweight = w;
694
695 if (px->lbprm.algo & BE_LB_PROP_DYN) {
696 /* we must take care of not pushing the server to full throttle during slow starts */
697 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
698 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
699 else
700 sv->eweight = BE_WEIGHT_SCALE;
701 sv->eweight *= sv->uweight;
702 } else {
703 sv->eweight = sv->uweight;
704 }
705
706 /* static LB algorithms are a bit harder to update */
707 if (px->lbprm.update_server_eweight)
708 px->lbprm.update_server_eweight(sv);
709 else if (sv->eweight)
710 px->lbprm.set_server_status_up(sv);
711 else
712 px->lbprm.set_server_status_down(sv);
713
714 return 1;
715 }
Willy Tarreau7aabd112010-01-26 10:59:06 +0100716 else if (strcmp(args[1], "timeout") == 0) {
717 if (strcmp(args[2], "cli") == 0) {
718 unsigned timeout;
719 const char *res;
720
721 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100722 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100723 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100724 return 1;
725 }
726
727 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
728 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100729 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100730 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100731 return 1;
732 }
733
734 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
735 return 1;
736 }
737 else {
Willy Tarreau295a8372011-03-10 11:25:07 +0100738 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100739 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100740 return 1;
741 }
742 }
743 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +0200744 return 0;
745 }
746 }
Cyril Bontécd19e512010-01-31 22:34:03 +0100747 else if (strcmp(args[0], "enable") == 0) {
748 if (strcmp(args[1], "server") == 0) {
749 struct proxy *px;
750 struct server *sv;
751
752 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100753 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100754 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100755 return 1;
756 }
757
758 /* split "backend/server" and make <line> point to server */
759 for (line = args[2]; *line; line++)
760 if (*line == '/') {
761 *line++ = '\0';
762 break;
763 }
764
765 if (!*line || !*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100766 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100767 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100768 return 1;
769 }
770
771 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100772 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100773 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100774 return 1;
775 }
776
Cyril Bonté613f0df2011-03-03 20:49:04 +0100777 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100778 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100779 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100780 return 1;
781 }
782
Cyril Bontécd19e512010-01-31 22:34:03 +0100783 if (sv->state & SRV_MAINTAIN) {
784 /* The server is really in maintenance, we can change the server state */
785 if (sv->tracked) {
786 /* If this server tracks the status of another one,
787 * we must restore the good status.
788 */
789 if (sv->tracked->state & SRV_RUNNING) {
790 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +0200791 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +0100792 } else {
793 sv->state &= ~SRV_MAINTAIN;
794 set_server_down(sv);
795 }
796 } else {
797 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +0200798 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +0100799 }
800 }
801
802 return 1;
803 }
804 else { /* unknown "enable" parameter */
805 return 0;
806 }
807 }
808 else if (strcmp(args[0], "disable") == 0) {
809 if (strcmp(args[1], "server") == 0) {
810 struct proxy *px;
811 struct server *sv;
812
813 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100814 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100815 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100816 return 1;
817 }
818
819 /* split "backend/server" and make <line> point to server */
820 for (line = args[2]; *line; line++)
821 if (*line == '/') {
822 *line++ = '\0';
823 break;
824 }
825
826 if (!*line || !*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100827 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100828 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100829 return 1;
830 }
831
832 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100833 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100834 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100835 return 1;
836 }
837
Cyril Bonté613f0df2011-03-03 20:49:04 +0100838 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100839 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100840 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100841 return 1;
842 }
843
Cyril Bontécd19e512010-01-31 22:34:03 +0100844 if (! (sv->state & SRV_MAINTAIN)) {
845 /* Not already in maintenance, we can change the server state */
846 sv->state |= SRV_MAINTAIN;
847 set_server_down(sv);
848 }
849
850 return 1;
851 }
852 else { /* unknown "disable" parameter */
853 return 0;
854 }
855 }
856 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200857 return 0;
858 }
859 return 1;
860}
861
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200862/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200863 * used to processes I/O from/to the stats unix socket. The system relies on a
864 * state machine handling requests and various responses. We read a request,
865 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +0100866 * Then we can read again. The state is stored in si->applet.st0 and is one of the
867 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200868 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200869 */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100870static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200871{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200872 struct buffer *req = si->ob;
873 struct buffer *res = si->ib;
874 int reql;
875 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200876
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200877 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
878 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200879
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200880 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +0100881 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200882 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +0100883 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +0100884 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200885 }
Willy Tarreaubc4af052011-02-13 13:25:14 +0100886 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200887 /* Let's close for real now. We just close the request
888 * side, the conditions below will complete if needed.
889 */
890 si->shutw(si);
891 break;
892 }
Willy Tarreaubc4af052011-02-13 13:25:14 +0100893 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +0200894 /* ensure we have some output room left in the event we
895 * would want to return some info right after parsing.
896 */
897 if (buffer_almost_full(si->ib))
898 break;
899
Willy Tarreau74b08c92010-09-08 17:04:31 +0200900 reql = buffer_get_line(si->ob, trash, sizeof(trash));
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200901 if (reql <= 0) { /* closed or EOL not found */
902 if (reql == 0)
903 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100904 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200905 continue;
906 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200907
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200908 /* seek for a possible semi-colon. If we find one, we
909 * replace it with an LF and skip only this part.
910 */
911 for (len = 0; len < reql; len++)
912 if (trash[len] == ';') {
913 trash[len] = '\n';
914 reql = len + 1;
915 break;
916 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200917
Willy Tarreau816fc222009-10-04 07:36:58 +0200918 /* now it is time to check that we have a full line,
919 * remove the trailing \n and possibly \r, then cut the
920 * line.
921 */
922 len = reql - 1;
923 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +0100924 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200925 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200926 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200927
Willy Tarreau816fc222009-10-04 07:36:58 +0200928 if (len && trash[len-1] == '\r')
929 len--;
930
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200931 trash[len] = '\0';
932
Willy Tarreaubc4af052011-02-13 13:25:14 +0100933 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200934 if (len) {
935 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +0100936 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200937 continue;
938 }
939 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +0100940 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200941 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200942 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100943 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100944 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200945 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200946 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +0100947 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200948 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200949 }
Willy Tarreaubc4af052011-02-13 13:25:14 +0100950 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200951 /* if prompt is disabled, print help on empty lines,
952 * so that the user at least knows how to enable
953 * prompt and find help.
954 */
Willy Tarreau295a8372011-03-10 11:25:07 +0100955 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100956 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200957 }
958
959 /* re-adjust req buffer */
960 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200961 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200962 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200963 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200964 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +0100965 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200966 continue;
967 }
968
Willy Tarreaubc4af052011-02-13 13:25:14 +0100969 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200970 case STAT_CLI_PRINT:
Willy Tarreau295a8372011-03-10 11:25:07 +0100971 if (buffer_feed(si->ib, si->applet.ctx.cli.msg) < 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +0100972 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200973 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200974 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +0100975 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +0100976 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200977 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200978 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +0100979 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +0100980 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200981 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200982 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +0100983 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +0100984 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200985 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +0200986 case STAT_CLI_O_TAB:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +0100987 if (stats_dump_table_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +0100988 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +0200989 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200990 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +0100991 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200992 break;
993 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200994
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200995 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +0100996 if (si->applet.st0 == STAT_CLI_PROMPT) {
997 if (buffer_feed(si->ib, si->applet.st1 ? "\n> " : "\n") < 0)
998 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200999 }
1000
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001001 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001002 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001003 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001004
1005 /* Now we close the output if one of the writers did so,
1006 * or if we're not in interactive mode and the request
1007 * buffer is empty. This still allows pipelined requests
1008 * to be sent in non-interactive mode.
1009 */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001010 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->applet.st1 && !req->send_max)) {
1011 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001012 continue;
1013 }
1014
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001015 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001016 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001017 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001018 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001019
Willy Tarreaubc4af052011-02-13 13:25:14 +01001020 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001021 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1022 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1023 /* Other size has closed, let's abort if we have no more processing to do
1024 * and nothing more to consume. This is comparable to a broken pipe, so
1025 * we forward the close to the request side so that it flows upstream to
1026 * the client.
1027 */
1028 si->shutw(si);
1029 }
1030
Willy Tarreaubc4af052011-02-13 13:25:14 +01001031 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001032 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1033 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1034 /* We have no more processing to do, and nothing more to send, and
1035 * the client side has closed. So we'll forward this state downstream
1036 * on the response buffer.
1037 */
1038 si->shutr(si);
1039 res->flags |= BF_READ_NULL;
1040 }
1041
1042 /* update all other flags and resync with the other side */
1043 si->update(si);
1044
1045 /* we don't want to expire timeouts while we're processing requests */
1046 si->ib->rex = TICK_ETERNITY;
1047 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001048
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001049 out:
1050 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
1051 __FUNCTION__, __LINE__,
1052 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
1053
1054 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1055 /* check that we have released everything then unregister */
1056 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001057 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001058}
1059
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001060/* This function dumps statistics onto the stream interface's read buffer.
1061 * The data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001062 * It returns 0 as long as it does not complete, non-zero upon completion.
1063 * Some states are not used but it makes the code more similar to other
1064 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001065 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001066int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001067{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001068 struct proxy *px;
1069 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001070 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001071
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001072 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +02001073
Willy Tarreau295a8372011-03-10 11:25:07 +01001074 switch (si->applet.state) {
1075 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001076 /* the function had not been called yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001077 si->applet.state = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001078 /* fall through */
1079
Willy Tarreau295a8372011-03-10 11:25:07 +01001080 case STAT_ST_HEAD:
1081 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001082 print_csv_header(&msg);
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001083 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001084 return 0;
1085 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001086
Willy Tarreau295a8372011-03-10 11:25:07 +01001087 si->applet.state = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001088 /* fall through */
1089
Willy Tarreau295a8372011-03-10 11:25:07 +01001090 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001091 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001092 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001093 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001094 "Name: " PRODUCT_NAME "\n"
1095 "Version: " HAPROXY_VERSION "\n"
1096 "Release_date: " HAPROXY_DATE "\n"
1097 "Nbproc: %d\n"
1098 "Process_num: %d\n"
1099 "Pid: %d\n"
1100 "Uptime: %dd %dh%02dm%02ds\n"
1101 "Uptime_sec: %d\n"
1102 "Memmax_MB: %d\n"
1103 "Ulimit-n: %d\n"
1104 "Maxsock: %d\n"
1105 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001106 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001107 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001108 "PipesUsed: %d\n"
1109 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001110 "Tasks: %d\n"
1111 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001112 "node: %s\n"
1113 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001114 "",
1115 global.nbproc,
1116 relative_pid,
1117 pid,
1118 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1119 up,
1120 global.rlimit_memmax,
1121 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001122 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001123 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001124 nb_tasks_cur, run_queue_cur,
1125 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001126 );
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001127 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001128 return 0;
1129 }
1130
Willy Tarreau295a8372011-03-10 11:25:07 +01001131 si->applet.ctx.stats.px = proxy;
1132 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1133 si->applet.ctx.stats.sv = NULL;
1134 si->applet.state = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001135 /* fall through */
1136
Willy Tarreau295a8372011-03-10 11:25:07 +01001137 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001138 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001139 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1140 while (si->applet.ctx.stats.px) {
1141 px = si->applet.ctx.stats.px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001142 /* skip the disabled proxies and non-networked ones */
1143 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001144 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001145 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001146 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001147 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001148
Willy Tarreau295a8372011-03-10 11:25:07 +01001149 si->applet.ctx.stats.px = px->next;
1150 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001151 }
1152 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001153 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001154
Willy Tarreau295a8372011-03-10 11:25:07 +01001155 si->applet.state = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001156 /* fall through */
1157
Willy Tarreau295a8372011-03-10 11:25:07 +01001158 case STAT_ST_END:
1159 si->applet.state = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001160 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001161
Willy Tarreau295a8372011-03-10 11:25:07 +01001162 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001163 return 1;
1164
1165 default:
1166 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001167 si->applet.state = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001168 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001169 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001170}
1171
1172
Cyril Bonté70be45d2010-10-12 00:14:35 +02001173/* We don't want to land on the posted stats page because a refresh will
1174 * repost the data. We don't want this to happen on accident so we redirect
1175 * the browse to the stats page with a GET.
1176 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001177int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001178{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001179 struct session *s = si->applet.private;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001180 struct chunk msg;
1181
1182 chunk_init(&msg, trash, sizeof(trash));
1183
Willy Tarreau295a8372011-03-10 11:25:07 +01001184 switch (si->applet.state) {
1185 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001186 chunk_printf(&msg,
1187 "HTTP/1.0 303 See Other\r\n"
1188 "Cache-Control: no-cache\r\n"
1189 "Content-Type: text/plain\r\n"
1190 "Connection: close\r\n"
1191 "Location: %s;st=%s",
Willy Tarreau295a8372011-03-10 11:25:07 +01001192 uri->uri_prefix, si->applet.ctx.stats.st_code);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001193 chunk_printf(&msg, "\r\n\r\n");
1194
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001195 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001196 return 0;
1197
1198 s->txn.status = 303;
1199
1200 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1201 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1202 if (!(s->flags & SN_FINST_MASK))
1203 s->flags |= SN_FINST_R;
1204
Willy Tarreau295a8372011-03-10 11:25:07 +01001205 si->applet.state = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001206 return 1;
1207 }
1208 return 1;
1209}
1210
1211
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001212/* This I/O handler runs as an applet embedded in a stream interface. It is
1213 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001214 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001215 * automatically unregisters itself once transfer is complete.
1216 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001217static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001218{
Willy Tarreaubc4af052011-02-13 13:25:14 +01001219 struct session *s = si->applet.private;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001220 struct buffer *req = si->ob;
1221 struct buffer *res = si->ib;
1222
1223 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1224 goto out;
1225
1226 /* check that the output is not closed */
1227 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001228 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001229
Willy Tarreaubc4af052011-02-13 13:25:14 +01001230 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001231 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001232 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001233 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001234 si->shutw(si);
1235 }
1236 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001237 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001238 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001239 si->shutw(si);
1240 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001241 }
1242 }
1243
1244 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
1245 si->shutw(si);
1246
Willy Tarreaubc4af052011-02-13 13:25:14 +01001247 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001248 si->shutr(si);
1249 res->flags |= BF_READ_NULL;
1250 }
1251
1252 /* update all other flags and resync with the other side */
1253 si->update(si);
1254
1255 /* we don't want to expire timeouts while we're processing requests */
1256 si->ib->rex = TICK_ETERNITY;
1257 si->ob->wex = TICK_ETERNITY;
1258
1259 out:
1260 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1261 /* check that we have released everything then unregister */
1262 stream_int_unregister_handler(si);
1263 }
1264}
1265
1266
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001267/* This function dumps statistics in HTTP format onto the stream interface's
1268 * read buffer. The data_ctx must have been zeroed first, and the flags
1269 * properly set. It returns 0 if it had to stop writing data and an I/O is
1270 * needed, 1 if the dump is finished and the session must be closed, or -1
1271 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001272 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001273int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001274{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001275 struct session *s = si->applet.private;
1276 struct buffer *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001277 struct proxy *px;
1278 struct chunk msg;
1279 unsigned int up;
1280
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001281 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001282
Willy Tarreau295a8372011-03-10 11:25:07 +01001283 switch (si->applet.state) {
1284 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001285 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001286 "HTTP/1.0 200 OK\r\n"
1287 "Cache-Control: no-cache\r\n"
1288 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001289 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001290 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001291
Willy Tarreau295a8372011-03-10 11:25:07 +01001292 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001293 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001294 uri->refresh);
1295
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001296 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001297
1298 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001299 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001300 return 0;
1301
Willy Tarreau91861262007-10-17 17:06:05 +02001302 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1303 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1304 if (!(s->flags & SN_FINST_MASK))
1305 s->flags |= SN_FINST_R;
1306
1307 if (s->txn.meth == HTTP_METH_HEAD) {
1308 /* that's all we return in case of HEAD request */
Willy Tarreau295a8372011-03-10 11:25:07 +01001309 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001310 return 1;
1311 }
1312
Willy Tarreau295a8372011-03-10 11:25:07 +01001313 si->applet.state = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001314 /* fall through */
1315
Willy Tarreau295a8372011-03-10 11:25:07 +01001316 case STAT_ST_HEAD:
1317 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001318 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001319 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001320 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1321 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001322 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001323 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1324 "<style type=\"text/css\"><!--\n"
1325 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001326 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001327 " font-size: 12px;"
1328 " font-weight: normal;"
1329 " color: black;"
1330 " background: white;"
1331 "}\n"
1332 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001333 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001334 "}\n"
1335 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001336 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001337 " margin-bottom: 0.5em;"
1338 "}\n"
1339 "h2 {"
1340 " font-family: helvetica, arial;"
1341 " font-size: x-large;"
1342 " font-weight: bold;"
1343 " font-style: italic;"
1344 " color: #6020a0;"
1345 " margin-top: 0em;"
1346 " margin-bottom: 0em;"
1347 "}\n"
1348 "h3 {"
1349 " font-family: helvetica, arial;"
1350 " font-size: 16px;"
1351 " font-weight: bold;"
1352 " color: #b00040;"
1353 " background: #e8e8d0;"
1354 " margin-top: 0em;"
1355 " margin-bottom: 0em;"
1356 "}\n"
1357 "li {"
1358 " margin-top: 0.25em;"
1359 " margin-right: 2em;"
1360 "}\n"
1361 ".hr {margin-top: 0.25em;"
1362 " border-color: black;"
1363 " border-bottom-style: solid;"
1364 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001365 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001366 ".total {background: #20D0D0;color: #ffff80;}\n"
1367 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001368 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001369 ".backend {background: #e8e8d0;}\n"
1370 ".active0 {background: #ff9090;}\n"
1371 ".active1 {background: #ffd020;}\n"
1372 ".active2 {background: #ffffa0;}\n"
1373 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001374 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1375 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1376 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001377 ".backup0 {background: #ff9090;}\n"
1378 ".backup1 {background: #ff80ff;}\n"
1379 ".backup2 {background: #c060ff;}\n"
1380 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001381 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1382 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1383 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001384 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001385 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001386 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001387 "a.px:link {color: #ffff40; text-decoration: none;}"
1388 "a.px:visited {color: #ffff40; text-decoration: none;}"
1389 "a.px:hover {color: #ffffff; text-decoration: none;}"
1390 "a.lfsb:link {color: #000000; text-decoration: none;}"
1391 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1392 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1393 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001394 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001395 "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"
1396 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001397 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001398 "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 +02001399 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001400 "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 +02001401 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001402 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1403 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1404 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001405 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001406 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001407 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001408 (uri->flags&ST_SHNODE) ? " on " : "",
1409 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001410 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001411 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001412 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001413 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001414 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001415 return 0;
1416
Willy Tarreau295a8372011-03-10 11:25:07 +01001417 si->applet.state = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02001418 /* fall through */
1419
Willy Tarreau295a8372011-03-10 11:25:07 +01001420 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02001421 up = (now.tv_sec - start_date.tv_sec);
1422
1423 /* WARNING! this has to fit the first packet too.
1424 * We are around 3.5 kB, add adding entries will
1425 * become tricky if we want to support 4kB buffers !
1426 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001427 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001428 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001429 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1430 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001431 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001432 "<hr width=\"100%%\" class=\"hr\">\n"
1433 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001434 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001435 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001436 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001437 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1438 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
1439 "current conns = %d; current pipes = %d/%d<br>\n"
1440 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001441 "</td><td align=\"center\" nowrap>\n"
1442 "<table class=\"lgd\"><tr>\n"
1443 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1444 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1445 "</tr><tr>\n"
1446 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1447 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1448 "</tr><tr>\n"
1449 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1450 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1451 "</tr><tr>\n"
1452 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001453 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001454 "</tr><tr>\n"
1455 "<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 +02001456 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001457 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001458 "</td>"
1459 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1460 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1461 "",
1462 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001463 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1464 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1465 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001466 up / 86400, (up % 86400) / 3600,
1467 (up % 3600) / 60, (up % 60),
1468 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1469 global.rlimit_memmax ? " MB" : "",
1470 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001471 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001472 actconn, pipes_used, pipes_used+pipes_free,
1473 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +02001474 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001475
Willy Tarreau295a8372011-03-10 11:25:07 +01001476 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001477 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001478 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1479 uri->uri_prefix,
1480 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01001481 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001482 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001483 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001484 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1485 uri->uri_prefix,
1486 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01001487 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001488
Willy Tarreau55bb8452007-10-17 18:44:57 +02001489 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001490 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001491 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001492 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1493 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001494 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001495 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001496 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001497 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001498 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1499 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001500 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001501 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001502 }
Willy Tarreau91861262007-10-17 17:06:05 +02001503
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001504 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001505 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
1506 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001507 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
1508 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001509
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001510 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02001511 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
1512 uri->uri_prefix,
1513 (uri->refresh > 0) ? ";norefresh" : "");
1514
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001515 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001516 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001517 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1518 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
1519 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
1520 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
1521 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
1522 "</ul>"
1523 "</td>"
1524 "</tr></table>\n"
1525 ""
1526 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001527
Willy Tarreau295a8372011-03-10 11:25:07 +01001528 if (si->applet.ctx.stats.st_code) {
1529 if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001530 chunk_printf(&msg,
1531 "<p><div class=active3>"
1532 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1533 "Action processed successfully."
1534 "</div>\n", uri->uri_prefix);
1535 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001536 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_NONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001537 chunk_printf(&msg,
1538 "<p><div class=active2>"
1539 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1540 "Nothing has changed."
1541 "</div>\n", uri->uri_prefix);
1542 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001543 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_EXCD) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001544 chunk_printf(&msg,
1545 "<p><div class=active0>"
1546 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1547 "<b>Action not processed : the buffer couldn't store all the data.<br>"
1548 "You should retry with less servers at a time.</b>"
1549 "</div>\n", uri->uri_prefix);
1550 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001551 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DENY) == 0) {
Cyril Bonté474be412010-10-12 00:14:36 +02001552 chunk_printf(&msg,
1553 "<p><div class=active0>"
1554 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1555 "<b>Action denied.</b>"
1556 "</div>\n", uri->uri_prefix);
1557 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001558 else {
1559 chunk_printf(&msg,
1560 "<p><div class=active6>"
1561 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1562 "Unexpected result."
1563 "</div>\n", uri->uri_prefix);
1564 }
1565 chunk_printf(&msg,"<p>\n");
1566 }
1567
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001568 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001569 return 0;
1570 }
Willy Tarreau91861262007-10-17 17:06:05 +02001571
Willy Tarreau295a8372011-03-10 11:25:07 +01001572 si->applet.ctx.stats.px = proxy;
1573 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1574 si->applet.state = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02001575 /* fall through */
1576
Willy Tarreau295a8372011-03-10 11:25:07 +01001577 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02001578 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001579 while (si->applet.ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001580 if (buffer_almost_full(rep))
1581 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01001582 px = si->applet.ctx.stats.px;
Willy Tarreau91861262007-10-17 17:06:05 +02001583 /* skip the disabled proxies and non-networked ones */
1584 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001585 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001586 return 0;
1587
Willy Tarreau295a8372011-03-10 11:25:07 +01001588 si->applet.ctx.stats.px = px->next;
1589 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02001590 }
1591 /* here, we just have reached the last proxy */
1592
Willy Tarreau295a8372011-03-10 11:25:07 +01001593 si->applet.state = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02001594 /* fall through */
1595
Willy Tarreau295a8372011-03-10 11:25:07 +01001596 case STAT_ST_END:
1597 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001598 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001599 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001600 return 0;
1601 }
Willy Tarreau91861262007-10-17 17:06:05 +02001602
Willy Tarreau295a8372011-03-10 11:25:07 +01001603 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001604 /* fall through */
1605
Willy Tarreau295a8372011-03-10 11:25:07 +01001606 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02001607 return 1;
1608
1609 default:
1610 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001611 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001612 return -1;
1613 }
1614}
1615
1616
1617/*
1618 * Dumps statistics for a proxy.
1619 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1620 * ot non-zero if everything completed.
1621 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001622int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001623{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001624 struct session *s = si->applet.private;
1625 struct buffer *rep = si->ib;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001626 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001627 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02001628 struct chunk msg;
1629
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001630 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001631
Willy Tarreau295a8372011-03-10 11:25:07 +01001632 switch (si->applet.ctx.stats.px_st) {
1633 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02001634 /* we are on a new proxy */
1635
1636 if (uri && uri->scope) {
1637 /* we have a limited scope, we have to check the proxy name */
1638 struct stat_scope *scope;
1639 int len;
1640
1641 len = strlen(px->id);
1642 scope = uri->scope;
1643
1644 while (scope) {
1645 /* match exact proxy name */
1646 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1647 break;
1648
1649 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001650 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001651 break;
1652 scope = scope->next;
1653 }
1654
1655 /* proxy name not found : don't dump anything */
1656 if (scope == NULL)
1657 return 1;
1658 }
1659
Willy Tarreau295a8372011-03-10 11:25:07 +01001660 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
1661 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001662 return 1;
1663
Willy Tarreau295a8372011-03-10 11:25:07 +01001664 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02001665 /* fall through */
1666
Willy Tarreau295a8372011-03-10 11:25:07 +01001667 case STAT_PX_ST_TH:
1668 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
1669 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001670 /* A form to enable/disable this proxy servers */
1671 chunk_printf(&msg,
1672 "<form action=\"%s\" method=\"post\">",
1673 uri->uri_prefix);
1674 }
1675
Willy Tarreau55bb8452007-10-17 18:44:57 +02001676 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001677 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001678 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001679 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001680 "<th class=\"pxname\" width=\"10%%\"");
1681
1682 if (uri->flags&ST_SHLGNDS) {
1683 /* cap, mode, id */
1684 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
1685 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1686 px->uuid);
1687
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001688 chunk_printf(&msg, "\"");
1689 }
1690
1691 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001692 ">%s<a name=\"%s\"></a>"
1693 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001694 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001695 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001696 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001697 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001698 "<tr class=\"titre\">",
1699 (uri->flags & ST_SHLGNDS)?"<u>":"",
1700 px->id, px->id, px->id,
1701 (uri->flags & ST_SHLGNDS)?"</u>":"",
1702 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1703
Willy Tarreau295a8372011-03-10 11:25:07 +01001704 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001705 /* Column heading for Enable or Disable server */
1706 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
1707 }
1708
1709 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001710 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001711 "<th colspan=3>Queue</th>"
1712 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001713 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001714 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001715 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001716 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001717 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001718 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001719 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001720 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001721 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001722 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001723 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001724 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1725 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001726 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001727
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001728 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001729 return 0;
1730 }
Willy Tarreau91861262007-10-17 17:06:05 +02001731
Willy Tarreau295a8372011-03-10 11:25:07 +01001732 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02001733 /* fall through */
1734
Willy Tarreau295a8372011-03-10 11:25:07 +01001735 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02001736 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001737 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01001738 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
1739 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001740 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001741 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02001742 "<tr class=\"frontend\">");
1743
Willy Tarreau295a8372011-03-10 11:25:07 +01001744 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001745 /* Column sub-heading for Enable or Disable server */
1746 chunk_printf(&msg, "<td></td>");
1747 }
1748
1749 chunk_printf(&msg,
1750 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001751 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001752 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
1753 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01001754 "",
1755 px->id, px->id);
1756
1757 if (px->mode == PR_MODE_HTTP) {
1758 chunk_printf(&msg,
1759 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01001760 "<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 +01001761 "",
1762 read_freq_ctr(&px->fe_req_per_sec),
1763 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
1764 px->counters.fe_rps_max,
Cyril Bonté4d179eb2010-07-23 18:59:11 +02001765 U2H1(px->counters.fe_sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01001766 LIM2A2(px->fe_sps_lim, "-"));
1767 } else {
1768 chunk_printf(&msg,
1769 /* sessions rate : current, max, limit */
1770 "<td>%s</td><td>%s</td><td>%s</td>"
1771 "",
1772 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
1773 U2H1(px->counters.fe_sps_max), LIM2A2(px->fe_sps_lim, "-"));
1774 }
1775
1776 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001777 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001778 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001779 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001780 "",
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001781 U2H3(px->feconn), U2H4(px->counters.feconn_max), U2H5(px->maxconn));
1782
1783 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1784 if (px->mode == PR_MODE_HTTP) {
1785 int i;
1786
Willy Tarreaub44939a2010-02-26 11:35:39 +01001787 chunk_printf(&msg, " title=\"%lld requests:", px->counters.cum_fe_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001788
1789 for (i = 1; i < 6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01001790 chunk_printf(&msg, " %dxx=%lld,", i, px->counters.fe.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001791
Willy Tarreau24657792010-02-26 10:30:28 +01001792 chunk_printf(&msg, " other=%lld\"", px->counters.fe.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001793 }
1794
1795 chunk_printf(&msg,
1796 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01001797 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001798 /* bytes : in, out */
1799 "<td>%s</td><td>%s</td>"
1800 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001801 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreaub36b4242010-06-04 20:59:39 +02001802 U2H6(px->counters.cum_fesess),
Willy Tarreaue0454092010-02-26 12:29:07 +01001803 (px->mode == PR_MODE_HTTP)?"</u>":"",
1804 U2H7(px->counters.bytes_in), U2H8(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001805
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001806 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001807 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001808 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001809 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001810 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001811 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001812 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001813 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001814 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001815 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001816 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02001817 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001818 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
1819 U2H2(px->counters.failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +02001820 px->state == PR_STRUN ? "OPEN" :
1821 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001822 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001823 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001824 /* pxid, name, queue cur, queue max, */
1825 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001826 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001827 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001828 /* bytes : in, out */
1829 "%lld,%lld,"
1830 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001831 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001832 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001833 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001834 /* warnings: retries, redispatches */
1835 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001836 /* server status : reflect frontend status */
1837 "%s,"
1838 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001839 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001840 /* pid, iid, sid, throttle, lbtot, tracked, type */
1841 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001842 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001843 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001844 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001845 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02001846 px->id,
Willy Tarreaub36b4242010-06-04 20:59:39 +02001847 px->feconn, px->counters.feconn_max, px->maxconn, px->counters.cum_fesess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001848 px->counters.bytes_in, px->counters.bytes_out,
1849 px->counters.denied_req, px->counters.denied_resp,
1850 px->counters.failed_req,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001851 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +01001852 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01001853 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001854 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001855 px->fe_sps_lim, px->counters.fe_sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001856
1857 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1858 if (px->mode == PR_MODE_HTTP) {
1859 int i;
1860
1861 for (i=1; i<6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01001862 chunk_printf(&msg, "%lld,", px->counters.fe.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001863
Willy Tarreau24657792010-02-26 10:30:28 +01001864 chunk_printf(&msg, "%lld,", px->counters.fe.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001865 } else {
1866 chunk_printf(&msg, ",,,,,,");
1867 }
1868
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001869 /* failed health analyses */
1870 chunk_printf(&msg, ",");
1871
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001872 /* requests : req_rate, req_rate_max, req_tot, */
1873 chunk_printf(&msg, "%u,%u,%lld,",
1874 read_freq_ctr(&px->fe_req_per_sec),
1875 px->counters.fe_rps_max, px->counters.cum_fe_req);
1876
Willy Tarreauae526782010-03-04 20:34:23 +01001877 /* errors: cli_aborts, srv_aborts */
1878 chunk_printf(&msg, ",,");
1879
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001880 /* finish with EOL */
1881 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001882 }
Willy Tarreau91861262007-10-17 17:06:05 +02001883
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001884 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001885 return 0;
1886 }
1887
Willy Tarreau295a8372011-03-10 11:25:07 +01001888 si->applet.ctx.stats.l = px->listen; /* may be NULL */
1889 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001890 /* fall through */
1891
Willy Tarreau295a8372011-03-10 11:25:07 +01001892 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001893 /* stats.l has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01001894 for (; si->applet.ctx.stats.l != NULL; si->applet.ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001895 if (buffer_almost_full(rep))
1896 return 0;
1897
Willy Tarreau295a8372011-03-10 11:25:07 +01001898 l = si->applet.ctx.stats.l;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001899 if (!l->counters)
1900 continue;
1901
Willy Tarreau295a8372011-03-10 11:25:07 +01001902 if (si->applet.ctx.stats.flags & STAT_BOUND) {
1903 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001904 break;
1905
Willy Tarreau295a8372011-03-10 11:25:07 +01001906 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001907 continue;
1908 }
1909
Willy Tarreau295a8372011-03-10 11:25:07 +01001910 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001911 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01001912 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001913 /* Column sub-heading for Enable or Disable server */
1914 chunk_printf(&msg, "<td></td>");
1915 }
1916 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001917
1918 if (uri->flags&ST_SHLGNDS) {
1919 char str[INET6_ADDRSTRLEN], *fmt = NULL;
1920 int port;
1921
1922 chunk_printf(&msg, " title=\"IP: ");
1923
1924 port = (l->addr.ss_family == AF_INET6)
1925 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
1926 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
1927
1928 if (l->addr.ss_family == AF_INET) {
1929 if (inet_ntop(AF_INET,
1930 (const void *)&((struct sockaddr_in *)&l->addr)->sin_addr,
1931 str, sizeof(str)))
1932 fmt = "%s:%d";
1933 } else {
1934 if (inet_ntop(AF_INET6,
1935 (const void *)&((struct sockaddr_in6 *)(&l->addr))->sin6_addr,
1936 str, sizeof(str)))
1937 fmt = "[%s]:%d";
1938 }
1939
1940 if (fmt)
1941 chunk_printf(&msg, fmt, str, port);
1942 else
1943 chunk_printf(&msg, "(%s)", strerror(errno));
1944
1945 /* id */
1946 chunk_printf(&msg, ", id: %d", l->luid);
1947
1948 chunk_printf(&msg, "\"");
1949 }
1950
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001951 chunk_printf(&msg,
1952 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01001953 ">%s<a name=\"%s/+%s\"></a>"
1954 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001955 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001956 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001957 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001958 "<td>%s</td><td>%s</td><td>%s</td>"
1959 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001960 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001961 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001962 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001963 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01001964 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01001965 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001966 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
1967 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
1968
1969 chunk_printf(&msg,
1970 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001971 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001972 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001973 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001974 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001975 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001976 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001977 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001978 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001979 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001980 "",
1981 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
1982 U2H2(l->counters->failed_req),
1983 (l->nbconn < l->maxconn) ? "OPEN" : "FULL");
1984 } else {
1985 chunk_printf(&msg,
1986 /* pxid, name, queue cur, queue max, */
1987 "%s,%s,,,"
1988 /* sessions: current, max, limit, total */
1989 "%d,%d,%d,%lld,"
1990 /* bytes: in, out */
1991 "%lld,%lld,"
1992 /* denied: req, resp */
1993 "%lld,%lld,"
1994 /* errors: request, connect, response */
1995 "%lld,,,"
1996 /* warnings: retries, redispatches */
1997 ",,"
1998 /* server status: reflect listener status */
1999 "%s,"
2000 /* rest of server: nothing */
2001 ",,,,,,,,"
2002 /* pid, iid, sid, throttle, lbtot, tracked, type */
2003 "%d,%d,%d,,,,%d,"
2004 /* rate, rate_lim, rate_max */
2005 ",,,"
2006 /* check_status, check_code, check_duration */
2007 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002008 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2009 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002010 /* failed health analyses */
2011 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002012 /* requests : req_rate, req_rate_max, req_tot, */
2013 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002014 /* errors: cli_aborts, srv_aborts */
2015 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002016 "\n",
2017 px->id, l->name,
2018 l->nbconn, l->counters->conn_max,
2019 l->maxconn, l->counters->cum_conn,
2020 l->counters->bytes_in, l->counters->bytes_out,
2021 l->counters->denied_req, l->counters->denied_resp,
2022 l->counters->failed_req,
2023 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2024 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2025 }
2026
2027 if (buffer_feed_chunk(rep, &msg) >= 0)
2028 return 0;
2029 }
2030
Willy Tarreau295a8372011-03-10 11:25:07 +01002031 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2032 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002033 /* fall through */
2034
Willy Tarreau295a8372011-03-10 11:25:07 +01002035 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002036 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002037 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002038 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 +02002039
Willy Tarreau4e33d862009-10-11 23:35:10 +02002040 if (buffer_almost_full(rep))
2041 return 0;
2042
Willy Tarreau295a8372011-03-10 11:25:07 +01002043 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002044
Willy Tarreau295a8372011-03-10 11:25:07 +01002045 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2046 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002047 break;
2048
Willy Tarreau295a8372011-03-10 11:25:07 +01002049 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002050 continue;
2051 }
2052
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002053 if (sv->tracked)
2054 svs = sv->tracked;
2055 else
2056 svs = sv;
2057
Willy Tarreau91861262007-10-17 17:06:05 +02002058 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002059 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002060 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002061 else if (svs->state & SRV_RUNNING) {
2062 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002063 sv_state = 3; /* UP */
2064 else
2065 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002066
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002067 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002068 sv_state += 2;
2069 }
Willy Tarreau91861262007-10-17 17:06:05 +02002070 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002071 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002072 sv_state = 1; /* going up */
2073 else
2074 sv_state = 0; /* DOWN */
2075
Willy Tarreau295a8372011-03-10 11:25:07 +01002076 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002077 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002078 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002079 continue;
2080 }
2081
Willy Tarreau295a8372011-03-10 11:25:07 +01002082 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002083 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2084 "UP %d/%d &darr;", "UP",
2085 "NOLB %d/%d &darr;", "NOLB",
2086 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002087 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002088 chunk_printf(&msg,
2089 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002090 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002091 );
2092 }
2093 else {
2094 chunk_printf(&msg,
2095 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002096 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002097 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2098 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002099
Willy Tarreau295a8372011-03-10 11:25:07 +01002100 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002101 chunk_printf(&msg,
2102 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2103 sv->id);
2104 }
2105
2106 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002107
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002108 if (uri->flags&ST_SHLGNDS) {
2109 char str[INET6_ADDRSTRLEN];
2110
2111 chunk_printf(&msg, " title=\"IP: ");
2112
2113 /* IP */
2114 if (inet_ntop(sv->addr.sin_family, &sv->addr.sin_addr, str, sizeof(str)))
2115 chunk_printf(&msg, "%s:%d", str, htons(sv->addr.sin_port));
2116 else
2117 chunk_printf(&msg, "(%s)", strerror(errno));
2118
2119 /* id */
2120 chunk_printf(&msg, ", id: %d", sv->puid);
2121
2122 /* cookie */
2123 if (sv->cookie) {
2124 struct chunk src;
2125
2126 chunk_printf(&msg, ", cookie: '");
2127
2128 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2129 chunk_htmlencode(&msg, &src);
2130
2131 chunk_printf(&msg, "'");
2132 }
2133
2134 chunk_printf(&msg, "\"");
2135 }
2136
2137 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002138 ">%s<a name=\"%s/%s\"></a>"
2139 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002140 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002141 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002142 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002143 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002144 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002145 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002146 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002147 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002148 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002149 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002150 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002151 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2152 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002153 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2154
2155 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2156 if (px->mode == PR_MODE_HTTP) {
2157 int i;
2158
2159 chunk_printf(&msg, " title=\"rsp codes:");
2160
2161 for (i = 1; i < 6; i++)
2162 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2163
2164 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2165 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002166
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002167 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002168 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002169 ">%s%s%s</td><td>%s</td>",
2170 (px->mode == PR_MODE_HTTP)?"<u>":"",
2171 U2H0(sv->counters.cum_sess),
2172 (px->mode == PR_MODE_HTTP)?"</u>":"",
2173 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002174
2175 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002176 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002177 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002178 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002179 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002180 /* errors : request, connect */
2181 "<td></td><td>%s</td>"
2182 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002183 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002184 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002185 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002186 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002187 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2188 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002189 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002190 sv->counters.cli_aborts,
2191 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002192 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002193 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002194
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002195 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002196 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002197
Cyril Bontécd19e512010-01-31 22:34:03 +01002198 if (sv->state & SRV_MAINTAIN) {
2199 chunk_printf(&msg, "%s ",
2200 human_time(now.tv_sec - sv->last_change, 1));
2201 chunk_printf(&msg, "MAINT");
2202 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002203 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2204 chunk_printf(&msg, "%s ",
2205 human_time(now.tv_sec - svs->last_change, 1));
2206 chunk_printf(&msg, "MAINT(via)");
2207 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002208 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002209 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002210 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002211
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002212 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002213 srv_hlt_st[sv_state],
2214 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2215 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002216 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002217
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002218 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002219 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002220 get_check_status_description(sv->check_status));
2221
2222 if (*sv->check_desc) {
2223 struct chunk src;
2224
2225 chunk_printf(&msg, ": ");
2226
2227 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2228 chunk_htmlencode(&msg, &src);
2229 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002230
Willy Tarreaue0454092010-02-26 12:29:07 +01002231 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002232 tv_iszero(&sv->check_start)?"":"* ",
2233 get_check_status_info(sv->check_status));
2234
2235 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002236 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002237
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002238 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002239 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002240 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002241 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002242
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002243 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002244 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002245 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002246 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002247 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002248 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002249 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002250 (sv->state & SRV_BACKUP) ? "-" : "Y",
2251 (sv->state & SRV_BACKUP) ? "Y" : "-");
2252
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002253 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002254 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002255 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002256 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2257
2258 if (svs->observe)
2259 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2260
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002261 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002262 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002263 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002264 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002265 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002266 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002267 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002268 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2269 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002270 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002271 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002272 "<td colspan=3></td>");
2273
2274 /* throttle */
2275 if ((sv->state & SRV_WARMINGUP) &&
2276 now.tv_sec < sv->last_change + sv->slowstart &&
2277 now.tv_sec >= sv->last_change) {
2278 unsigned int ratio;
2279 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002280 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002281 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002282 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002283 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002284 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002285 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002286 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002287 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2288 "UP %d/%d,", "UP,",
2289 "NOLB %d/%d,", "NOLB,",
2290 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002291 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002292 /* pxid, name */
2293 "%s,%s,"
2294 /* queue : current, max */
2295 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002296 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002297 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002298 /* bytes : in, out */
2299 "%lld,%lld,"
2300 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002301 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002302 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002303 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002304 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002305 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002306 "",
2307 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002308 sv->nbpend, sv->counters.nbpend_max,
2309 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002310 sv->counters.bytes_in, sv->counters.bytes_out,
2311 sv->counters.failed_secu,
2312 sv->counters.failed_conns, sv->counters.failed_resp,
2313 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002314
Willy Tarreau55bb8452007-10-17 18:44:57 +02002315 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002316 if (sv->state & SRV_MAINTAIN) {
2317 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002318 }
2319 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2320 chunk_printf(&msg, "MAINT(via),");
2321 }
2322 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002323 chunk_printf(&msg,
2324 srv_hlt_st[sv_state],
2325 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2326 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2327 }
Willy Tarreau91861262007-10-17 17:06:05 +02002328
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002329 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002330 /* weight, active, backup */
2331 "%d,%d,%d,"
2332 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002333 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002334 (sv->state & SRV_BACKUP) ? 0 : 1,
2335 (sv->state & SRV_BACKUP) ? 1 : 0);
2336
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002337 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002338 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002339 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002340 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002341 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002342 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002343 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002344 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002345 ",,,,");
2346
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002347 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002348 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002349 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002350 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002351 LIM2A0(sv->maxqueue, ""),
2352 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002353
2354 /* throttle */
2355 if ((sv->state & SRV_WARMINGUP) &&
2356 now.tv_sec < sv->last_change + sv->slowstart &&
2357 now.tv_sec >= sv->last_change) {
2358 unsigned int ratio;
2359 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002360 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002361 }
2362
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002363 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002364 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002365
2366 /* tracked */
2367 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002368 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002369 sv->tracked->proxy->id, sv->tracked->id);
2370 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002371 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002372
Willy Tarreau7f062c42009-03-05 18:43:00 +01002373 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002374 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002375
2376 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002377 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002378 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002379 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002380
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002381 if (sv->state & SRV_CHECKED) {
2382 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002383 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002384
2385 /* check_code */
2386 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002387 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002388 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002389 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002390
2391 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002392 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002393 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002394 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002395 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002396
2397 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002398 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002399 }
2400
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002401 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2402 if (px->mode == PR_MODE_HTTP) {
2403 int i;
2404
2405 for (i=1; i<6; i++)
2406 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2407
2408 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2409 } else {
2410 chunk_printf(&msg, ",,,,,,");
2411 }
2412
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002413 /* failed health analyses */
2414 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2415
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002416 /* requests : req_rate, req_rate_max, req_tot, */
2417 chunk_printf(&msg, ",,,");
2418
Willy Tarreauae526782010-03-04 20:34:23 +01002419 /* errors: cli_aborts, srv_aborts */
2420 chunk_printf(&msg, "%lld,%lld,",
2421 sv->counters.cli_aborts, sv->counters.srv_aborts);
2422
Willy Tarreau7f062c42009-03-05 18:43:00 +01002423 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002424 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002425 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002426 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002427 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002428 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002429
Willy Tarreau295a8372011-03-10 11:25:07 +01002430 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02002431 /* fall through */
2432
Willy Tarreau295a8372011-03-10 11:25:07 +01002433 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02002434 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002435 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002436 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2437 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002438 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01002439 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002440 /* Column sub-heading for Enable or Disable server */
2441 chunk_printf(&msg, "<td></td>");
2442 }
2443 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002444
2445 if (uri->flags&ST_SHLGNDS) {
2446 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002447 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002448 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002449
2450 /* cookie */
2451 if (px->cookie_name) {
2452 struct chunk src;
2453
2454 chunk_printf(&msg, ", cookie: '");
2455
2456 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2457 chunk_htmlencode(&msg, &src);
2458
2459 chunk_printf(&msg, "'");
2460 }
2461
2462 chunk_printf(&msg, "\"");
2463
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002464 }
2465
2466 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002467 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01002468 ">%s<a name=\"%s/Backend\"></a>"
2469 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002470 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002471 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002472 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002473 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002474 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002475 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002476 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002477 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002478 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->counters.nbpend_max),
2479 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->counters.be_sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02002480
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002481 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002482 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002483 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002484 "<td"
2485 "",
2486 U2H2(px->beconn), U2H3(px->counters.beconn_max), U2H4(px->fullconn));
2487
2488 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2489 if (px->mode == PR_MODE_HTTP) {
2490 int i;
2491
2492 chunk_printf(&msg, " title=\"rsp codes:");
2493
2494 for (i = 1; i < 6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01002495 chunk_printf(&msg, " %dxx=%lld", i, px->counters.be.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002496
Willy Tarreau24657792010-02-26 10:30:28 +01002497 chunk_printf(&msg, " other=%lld\"", px->counters.be.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002498 }
2499
2500 chunk_printf(&msg,
2501 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002502 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002503 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002504 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002505 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002506 (px->mode == PR_MODE_HTTP)?"<u>":"",
2507 U2H6(px->counters.cum_beconn),
2508 (px->mode == PR_MODE_HTTP)?"</u>":"",
2509 U2H7(px->counters.cum_lbconn),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002510 U2H8(px->counters.bytes_in), U2H9(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002511
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002512 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002513 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002514 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002515 /* errors : request, connect */
2516 "<td></td><td>%s</td>"
2517 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002518 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002519 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002520 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002521 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02002522 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002523 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02002524 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002525 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2526 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002527 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002528 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
Willy Tarreauae526782010-03-04 20:34:23 +01002529 U2H2(px->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002530 px->counters.cli_aborts,
2531 px->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002532 U2H5(px->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002533 px->counters.retries, px->counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002534 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01002535 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2536 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01002537 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002538 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002539
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002540 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002541 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002542 "<td class=ac>&nbsp;</td><td>%d</td>"
2543 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002544 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002545 "</tr>",
2546 px->down_trans,
2547 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002548 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002549 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002550 /* pxid, name */
2551 "%s,BACKEND,"
2552 /* queue : current, max */
2553 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002554 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002555 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002556 /* bytes : in, out */
2557 "%lld,%lld,"
2558 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002559 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002560 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002561 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002562 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002563 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002564 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02002565 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002566 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02002567 * active and backups. */
2568 "%s,"
2569 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002570 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002571 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002572 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002573 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002574 /* rate, rate_lim, rate_max, */
2575 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002576 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002577 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002578 px->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002579 px->nbpend /* or px->totpend ? */, px->counters.nbpend_max,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002580 px->beconn, px->counters.beconn_max, px->fullconn, px->counters.cum_beconn,
2581 px->counters.bytes_in, px->counters.bytes_out,
2582 px->counters.denied_req, px->counters.denied_resp,
2583 px->counters.failed_conns, px->counters.failed_resp,
2584 px->counters.retries, px->counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01002585 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01002586 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002587 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002588 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01002589 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002590 relative_pid, px->uuid,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002591 px->counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002592 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002593 px->counters.be_sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002594
2595 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2596 if (px->mode == PR_MODE_HTTP) {
2597 int i;
2598
2599 for (i=1; i<6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01002600 chunk_printf(&msg, "%lld,", px->counters.be.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002601
Willy Tarreau24657792010-02-26 10:30:28 +01002602 chunk_printf(&msg, "%lld,", px->counters.be.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002603 } else {
2604 chunk_printf(&msg, ",,,,,,");
2605 }
2606
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002607 /* failed health analyses */
2608 chunk_printf(&msg, ",");
2609
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002610 /* requests : req_rate, req_rate_max, req_tot, */
2611 chunk_printf(&msg, ",,,");
2612
Willy Tarreauae526782010-03-04 20:34:23 +01002613 /* errors: cli_aborts, srv_aborts */
2614 chunk_printf(&msg, "%lld,%lld,",
2615 px->counters.cli_aborts, px->counters.srv_aborts);
2616
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002617 /* finish with EOL */
2618 chunk_printf(&msg, "\n");
2619
Willy Tarreau55bb8452007-10-17 18:44:57 +02002620 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002621 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002622 return 0;
2623 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002624
Willy Tarreau295a8372011-03-10 11:25:07 +01002625 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002626 /* fall through */
2627
Willy Tarreau295a8372011-03-10 11:25:07 +01002628 case STAT_PX_ST_END:
2629 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002630 chunk_printf(&msg, "</table>");
2631
Willy Tarreau295a8372011-03-10 11:25:07 +01002632 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002633 /* close the form used to enable/disable this proxy servers */
2634 chunk_printf(&msg,
2635 "Choose the action to perform on the checked servers : "
2636 "<select name=action>"
2637 "<option value=\"\"></option>"
2638 "<option value=\"disable\">Disable</option>"
2639 "<option value=\"enable\">Enable</option>"
2640 "</select>"
2641 "<input type=\"hidden\" name=\"b\" value=\"%s\">"
2642 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2643 "</form>",
2644 px->id);
2645 }
2646
2647 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02002648
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002649 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002650 return 0;
2651 }
Willy Tarreau91861262007-10-17 17:06:05 +02002652
Willy Tarreau295a8372011-03-10 11:25:07 +01002653 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002654 /* fall through */
2655
Willy Tarreau295a8372011-03-10 11:25:07 +01002656 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002657 return 1;
2658
2659 default:
2660 /* unknown state, we should put an abort() here ! */
2661 return 1;
2662 }
2663}
2664
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002665/* This function dumps a complete session state onto the stream intreface's
2666 * read buffer. The data_ctx must have been zeroed first, and the flags
2667 * properly set. The session has to be set in data_ctx.sess.target. It returns
2668 * 0 if the output buffer is full and it needs to be called again, otherwise
2669 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002670 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002671int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002672{
2673 struct tm tm;
2674 struct chunk msg;
2675 struct session *sess;
2676 extern const char *monthname[12];
2677 char pn[INET6_ADDRSTRLEN];
2678
2679 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau295a8372011-03-10 11:25:07 +01002680 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002681
Willy Tarreau295a8372011-03-10 11:25:07 +01002682 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002683 /* session changed, no need to go any further */
2684 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002685 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002686 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002687 si->applet.ctx.sess.target = NULL;
2688 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002689 return 1;
2690 }
2691
Willy Tarreau295a8372011-03-10 11:25:07 +01002692 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002693 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01002694 si->applet.ctx.sess.uid = sess->uniq_id;
2695 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002696 /* fall through */
2697
2698 case 1:
2699 chunk_printf(&msg,
2700 "%p: id=%u, proto=%s",
2701 sess,
2702 sess->uniq_id,
2703 sess->listener->proto->name);
2704
2705 switch (sess->listener->proto->sock_family) {
2706 case AF_INET:
2707 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002708 (const void *)&((struct sockaddr_in *)&sess->si[0].addr.c.from)->sin_addr,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002709 pn, sizeof(pn));
2710
2711 chunk_printf(&msg,
2712 " source=%s:%d\n",
2713 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002714 ntohs(((struct sockaddr_in *)&sess->si[0].addr.c.from)->sin_port));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002715 break;
2716 case AF_INET6:
2717 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002718 (const void *)&((struct sockaddr_in6 *)(&sess->si[0].addr.c.from))->sin6_addr,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002719 pn, sizeof(pn));
2720
2721 chunk_printf(&msg,
2722 " source=%s:%d\n",
2723 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002724 ntohs(((struct sockaddr_in6 *)&sess->si[0].addr.c.from)->sin6_port));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002725 break;
2726 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02002727 chunk_printf(&msg,
2728 " source=unix:%d\n", sess->listener->luid);
2729 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002730 default:
2731 /* no more information to print right now */
2732 chunk_printf(&msg, "\n");
2733 break;
2734 }
2735
2736 chunk_printf(&msg,
2737 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02002738 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002739
2740 chunk_printf(&msg,
2741 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)\n",
2742 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
2743 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
2744 sess->listener ? sess->listener->luid : 0);
2745
2746 chunk_printf(&msg,
2747 " backend=%s (id=%u mode=%s) server=%s (id=%u)\n",
2748 sess->be->id, sess->be->uuid, sess->be->mode ? "http" : "tcp",
Willy Tarreau827aee92011-03-10 16:55:02 +01002749 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
2750 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002751
2752 chunk_printf(&msg,
2753 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
2754 sess->task,
2755 sess->task->state,
2756 sess->task->nice, sess->task->calls,
2757 sess->task->expire ?
2758 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
2759 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
2760 TICKS_TO_MS(1000)) : "<NEVER>",
2761 task_in_rq(sess->task) ? ", running" : "");
2762
2763 get_localtime(sess->logs.accept_date.tv_sec, &tm);
2764 chunk_printf(&msg,
2765 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
2766 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2767 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
2768 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
2769
2770 chunk_printf(&msg,
2771 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
2772 &sess->si[0],
2773 sess->si[0].state,
2774 sess->si[0].flags,
2775 sess->si[0].fd,
2776 sess->si[0].exp ?
2777 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
2778 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
2779 TICKS_TO_MS(1000)) : "<NEVER>",
2780 sess->si[0].err_type);
2781
2782 chunk_printf(&msg,
2783 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
2784 &sess->si[1],
2785 sess->si[1].state,
2786 sess->si[1].flags,
2787 sess->si[1].fd,
2788 sess->si[1].exp ?
2789 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
2790 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
2791 TICKS_TO_MS(1000)) : "<NEVER>",
2792 sess->si[1].err_type);
2793
2794 chunk_printf(&msg,
2795 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
2796 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
2797 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
2798
2799
2800 chunk_printf(&msg,
2801 " req=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%ld)\n"
2802 " an_exp=%s",
2803 sess->req,
2804 sess->req->flags, sess->req->analysers,
2805 sess->req->l, sess->req->send_max,
2806 sess->req->pipe ? sess->req->pipe->data : 0,
2807 sess->req->to_forward,
2808 sess->req->analyse_exp ?
2809 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
2810 TICKS_TO_MS(1000)) : "<NEVER>");
2811
2812 chunk_printf(&msg,
2813 " rex=%s",
2814 sess->req->rex ?
2815 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
2816 TICKS_TO_MS(1000)) : "<NEVER>");
2817
2818 chunk_printf(&msg,
2819 " wex=%s\n"
2820 " data=%p r=%d w=%d lr=%d total=%lld\n",
2821 sess->req->wex ?
2822 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
2823 TICKS_TO_MS(1000)) : "<NEVER>",
2824 sess->req->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01002825 (int)(sess->req->r - sess->req->data),
2826 (int)(sess->req->w - sess->req->data),
2827 (int)(sess->req->lr - sess->req->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002828 sess->req->total);
2829
2830 chunk_printf(&msg,
2831 " res=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%ld)\n"
2832 " an_exp=%s",
2833 sess->rep,
2834 sess->rep->flags, sess->rep->analysers,
2835 sess->rep->l, sess->rep->send_max,
2836 sess->rep->pipe ? sess->rep->pipe->data : 0,
2837 sess->rep->to_forward,
2838 sess->rep->analyse_exp ?
2839 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
2840 TICKS_TO_MS(1000)) : "<NEVER>");
2841
2842 chunk_printf(&msg,
2843 " rex=%s",
2844 sess->rep->rex ?
2845 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
2846 TICKS_TO_MS(1000)) : "<NEVER>");
2847
2848 chunk_printf(&msg,
2849 " wex=%s\n"
2850 " data=%p r=%d w=%d lr=%d total=%lld\n",
2851 sess->rep->wex ?
2852 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
2853 TICKS_TO_MS(1000)) : "<NEVER>",
2854 sess->rep->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01002855 (int)(sess->rep->r - sess->rep->data),
2856 (int)(sess->rep->w - sess->rep->data),
2857 (int)(sess->rep->lr - sess->rep->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002858 sess->rep->total);
2859
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002860 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002861 return 0;
2862
2863 /* use other states to dump the contents */
2864 }
2865 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01002866 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002867 return 1;
2868}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002869
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002870/* This function dumps all sessions' states onto the stream intreface's
2871 * read buffer. The data_ctx must have been zeroed first, and the flags
2872 * properly set. It returns 0 if the output buffer is full and it needs
2873 * to be called again, otherwise non-zero. It is designed to be called
2874 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002875 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002876int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002877{
2878 struct chunk msg;
2879
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002880 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002881 /* If we're forced to shut down, we might have to remove our
2882 * reference to the last session being dumped.
2883 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002884 if (si->applet.state == STAT_ST_LIST) {
2885 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
2886 LIST_DEL(&si->applet.ctx.sess.bref.users);
2887 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002888 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002889 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002890 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002891 }
2892
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002893 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002894
Willy Tarreau295a8372011-03-10 11:25:07 +01002895 switch (si->applet.state) {
2896 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002897 /* the function had not been called yet, let's prepare the
2898 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002899 * pointer to the first in the global list. When a target
2900 * session is being destroyed, it is responsible for updating
2901 * this pointer. We know we have reached the end when this
2902 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002903 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002904 LIST_INIT(&si->applet.ctx.sess.bref.users);
2905 si->applet.ctx.sess.bref.ref = sessions.n;
2906 si->applet.state = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002907 /* fall through */
2908
Willy Tarreau295a8372011-03-10 11:25:07 +01002909 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002910 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01002911 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
2912 LIST_DEL(&si->applet.ctx.sess.bref.users);
2913 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002914 }
2915
2916 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01002917 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01002918 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002919 struct session *curr_sess;
2920
Willy Tarreau295a8372011-03-10 11:25:07 +01002921 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002922
Willy Tarreau295a8372011-03-10 11:25:07 +01002923 if (si->applet.ctx.sess.target) {
2924 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002925 goto next_sess;
2926
Willy Tarreau295a8372011-03-10 11:25:07 +01002927 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002928 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002929 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002930 return 0;
2931
2932 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01002933 LIST_DEL(&si->applet.ctx.sess.bref.users);
2934 LIST_INIT(&si->applet.ctx.sess.bref.users);
2935 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002936 break;
2937 }
2938
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002939 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002940 "%p: proto=%s",
2941 curr_sess,
2942 curr_sess->listener->proto->name);
2943
2944 switch (curr_sess->listener->proto->sock_family) {
2945 case AF_INET:
2946 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002947 (const void *)&((struct sockaddr_in *)&curr_sess->si[0].addr.c.from)->sin_addr,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002948 pn, sizeof(pn));
2949
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002950 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002951 " src=%s:%d fe=%s be=%s srv=%s",
2952 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002953 ntohs(((struct sockaddr_in *)&curr_sess->si[0].addr.c.from)->sin_port),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002954 curr_sess->fe->id,
2955 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01002956 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002957 );
2958 break;
2959 case AF_INET6:
2960 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002961 (const void *)&((struct sockaddr_in6 *)(&curr_sess->si[0].addr.c.from))->sin6_addr,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002962 pn, sizeof(pn));
2963
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002964 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002965 " src=%s:%d fe=%s be=%s srv=%s",
2966 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002967 ntohs(((struct sockaddr_in6 *)&curr_sess->si[0].addr.c.from)->sin6_port),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002968 curr_sess->fe->id,
2969 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01002970 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002971 );
2972
2973 break;
2974 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02002975 chunk_printf(&msg,
2976 " src=unix:%d fe=%s be=%s srv=%s",
2977 curr_sess->listener->luid,
2978 curr_sess->fe->id,
2979 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01002980 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02002981 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002982 break;
2983 }
2984
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002985 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02002986 " ts=%02x age=%s calls=%d",
2987 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01002988 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
2989 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002990
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002991 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002992 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
2993 curr_sess->req->flags,
2994 curr_sess->req->l,
2995 curr_sess->req->analysers,
2996 curr_sess->req->rex ?
2997 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
2998 TICKS_TO_MS(1000)) : "");
2999
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003000 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003001 ",wx=%s",
3002 curr_sess->req->wex ?
3003 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3004 TICKS_TO_MS(1000)) : "");
3005
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003006 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003007 ",ax=%s]",
3008 curr_sess->req->analyse_exp ?
3009 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3010 TICKS_TO_MS(1000)) : "");
3011
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003012 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003013 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
3014 curr_sess->rep->flags,
3015 curr_sess->rep->l,
3016 curr_sess->rep->analysers,
3017 curr_sess->rep->rex ?
3018 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3019 TICKS_TO_MS(1000)) : "");
3020
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003021 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003022 ",wx=%s",
3023 curr_sess->rep->wex ?
3024 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3025 TICKS_TO_MS(1000)) : "");
3026
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003027 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003028 ",ax=%s]",
3029 curr_sess->rep->analyse_exp ?
3030 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3031 TICKS_TO_MS(1000)) : "");
3032
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003033 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003034 " s0=[%d,%1xh,fd=%d,ex=%s]",
3035 curr_sess->si[0].state,
3036 curr_sess->si[0].flags,
3037 curr_sess->si[0].fd,
3038 curr_sess->si[0].exp ?
3039 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3040 TICKS_TO_MS(1000)) : "");
3041
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003042 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003043 " s1=[%d,%1xh,fd=%d,ex=%s]",
3044 curr_sess->si[1].state,
3045 curr_sess->si[1].flags,
3046 curr_sess->si[1].fd,
3047 curr_sess->si[1].exp ?
3048 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3049 TICKS_TO_MS(1000)) : "");
3050
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003051 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003052 " exp=%s",
3053 curr_sess->task->expire ?
3054 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3055 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003056 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003057 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003058
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003059 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003060
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003061 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003062 /* let's try again later from this session. We add ourselves into
3063 * this session's users so that it can remove us upon termination.
3064 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003065 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003066 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003067 }
3068
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003069 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003070 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003071 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003072
Willy Tarreau295a8372011-03-10 11:25:07 +01003073 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003074 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003075 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003076 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3077 else
3078 chunk_printf(&msg, "Session not found.\n");
3079
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003080 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003081 return 0;
3082
Willy Tarreau295a8372011-03-10 11:25:07 +01003083 si->applet.ctx.sess.target = NULL;
3084 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003085 return 1;
3086 }
3087
Willy Tarreau295a8372011-03-10 11:25:07 +01003088 si->applet.state = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003089 /* fall through */
3090
3091 default:
Willy Tarreau295a8372011-03-10 11:25:07 +01003092 si->applet.state = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003093 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003094 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003095}
3096
Emeric Brun1e029aa2010-09-23 18:12:53 +02003097
3098/* print a string of text buffer to <out>. The format is :
3099 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
3100 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
3101 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
3102 */
3103
3104static int dump_text(struct chunk *out, const char *buf, int bsize)
3105{
3106 unsigned char c;
3107 int ptr = 0;
3108
3109 while (buf[ptr] && ptr < bsize) {
3110 c = buf[ptr];
3111 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
3112 if (out->len > out->size - 1)
3113 break;
3114 out->str[out->len++] = c;
3115 }
3116 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
3117 if (out->len > out->size - 2)
3118 break;
3119 out->str[out->len++] = '\\';
3120 switch (c) {
3121 case ' ': c = ' '; break;
3122 case '\t': c = 't'; break;
3123 case '\n': c = 'n'; break;
3124 case '\r': c = 'r'; break;
3125 case '\e': c = 'e'; break;
3126 case '\\': c = '\\'; break;
3127 }
3128 out->str[out->len++] = c;
3129 }
3130 else {
3131 if (out->len > out->size - 4)
3132 break;
3133 out->str[out->len++] = '\\';
3134 out->str[out->len++] = 'x';
3135 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3136 out->str[out->len++] = hextab[c & 0xF];
3137 }
3138 ptr++;
3139 }
3140
3141 return ptr;
3142}
3143
3144/* print a buffer in hexa.
3145 * Print stopped if <bsize> is reached, or if no more place in the chunk.
3146 */
3147
3148static int dump_binary(struct chunk *out, const char *buf, int bsize)
3149{
3150 unsigned char c;
3151 int ptr = 0;
3152
3153 while (ptr < bsize) {
3154 c = buf[ptr];
3155
3156 if (out->len > out->size - 2)
3157 break;
3158 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3159 out->str[out->len++] = hextab[c & 0xF];
3160
3161 ptr++;
3162 }
3163 return ptr;
3164}
3165
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003166/* This function dumps all tables' states onto the stream intreface's
3167 * read buffer. The data_ctx must have been zeroed first, and the flags
3168 * properly set. It returns 0 if the output buffer is full and it needs
3169 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003170 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003171int stats_dump_table_to_buffer(struct stream_interface *si)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003172{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003173 struct session *s = si->applet.private;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003174 struct chunk msg;
3175 struct ebmb_node *eb;
3176 int dt;
3177
3178 /*
Willy Tarreau295a8372011-03-10 11:25:07 +01003179 * We have 3 possible states in si->applet.state :
3180 * - STAT_ST_INIT : the first call
3181 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003182 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003183 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003184 * and the entry pointer points to the next entry to be dumped,
3185 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003186 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003187 * data though.
3188 */
3189
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003190 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003191 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau295a8372011-03-10 11:25:07 +01003192 if (si->applet.state == STAT_ST_LIST) {
3193 si->applet.ctx.table.entry->ref_cnt--;
3194 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003195 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003196 return 1;
3197 }
3198
3199 chunk_init(&msg, trash, sizeof(trash));
3200
Willy Tarreau295a8372011-03-10 11:25:07 +01003201 while (si->applet.state != STAT_ST_FIN) {
3202 switch (si->applet.state) {
3203 case STAT_ST_INIT:
3204 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3205 if (!si->applet.ctx.table.proxy)
3206 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003207
Willy Tarreau295a8372011-03-10 11:25:07 +01003208 si->applet.ctx.table.entry = NULL;
3209 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003210 break;
3211
Willy Tarreau295a8372011-03-10 11:25:07 +01003212 case STAT_ST_INFO:
3213 if (!si->applet.ctx.table.proxy ||
3214 (si->applet.ctx.table.target &&
3215 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
3216 si->applet.state = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003217 break;
3218 }
3219
Willy Tarreau295a8372011-03-10 11:25:07 +01003220 if (si->applet.ctx.table.proxy->table.size) {
Emeric Brun1e029aa2010-09-23 18:12:53 +02003221 chunk_printf(&msg, "# table: %s, type: %s, size:%d, used:%d\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003222 si->applet.ctx.table.proxy->id,
3223 stktable_types[si->applet.ctx.table.proxy->table.type].kw,
3224 si->applet.ctx.table.proxy->table.size,
3225 si->applet.ctx.table.proxy->table.current);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003226
3227 /* any other information should be dumped here */
3228
Willy Tarreau295a8372011-03-10 11:25:07 +01003229 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003230 s->listener->perm.ux.level < ACCESS_LVL_OPER)
3231 chunk_printf(&msg, "# contents not dumped due to insufficient privileges\n");
3232
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003233 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003234 return 0;
3235
Willy Tarreau295a8372011-03-10 11:25:07 +01003236 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003237 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
3238 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003239 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003240 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003241 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3242 si->applet.ctx.table.entry->ref_cnt++;
3243 si->applet.state = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003244 break;
3245 }
3246 }
3247 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003248 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003249 break;
3250
Willy Tarreau295a8372011-03-10 11:25:07 +01003251 case STAT_ST_LIST:
3252 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003253 /* we're filtering on some data contents */
3254 void *ptr;
3255 long long data;
3256
Willy Tarreau295a8372011-03-10 11:25:07 +01003257 dt = si->applet.ctx.table.data_type;
3258 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3259 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003260 dt);
3261
3262 data = 0;
3263 switch (stktable_data_types[dt].std_type) {
3264 case STD_T_SINT:
3265 data = stktable_data_cast(ptr, std_t_sint);
3266 break;
3267 case STD_T_UINT:
3268 data = stktable_data_cast(ptr, std_t_uint);
3269 break;
3270 case STD_T_ULL:
3271 data = stktable_data_cast(ptr, std_t_ull);
3272 break;
3273 case STD_T_FRQP:
3274 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003275 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003276 break;
3277 }
3278
3279 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003280 if ((data < si->applet.ctx.table.value &&
3281 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3282 si->applet.ctx.table.data_op == STD_OP_GT ||
3283 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3284 (data == si->applet.ctx.table.value &&
3285 (si->applet.ctx.table.data_op == STD_OP_NE ||
3286 si->applet.ctx.table.data_op == STD_OP_GT ||
3287 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3288 (data > si->applet.ctx.table.value &&
3289 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3290 si->applet.ctx.table.data_op == STD_OP_LT ||
3291 si->applet.ctx.table.data_op == STD_OP_LE)))
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003292 goto skip_entry;
3293 }
3294
Willy Tarreau295a8372011-03-10 11:25:07 +01003295 chunk_printf(&msg, "%p:", si->applet.ctx.table.entry);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003296
Willy Tarreau295a8372011-03-10 11:25:07 +01003297 if (si->applet.ctx.table.proxy->table.type == STKTABLE_TYPE_IP) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003298 char addr[16];
3299 inet_ntop(AF_INET,
Willy Tarreau295a8372011-03-10 11:25:07 +01003300 (const void *)&si->applet.ctx.table.entry->key.key,
Willy Tarreau69f58c82010-07-12 17:55:33 +02003301 addr, sizeof(addr));
3302 chunk_printf(&msg, " key=%s", addr);
3303 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003304 else if (si->applet.ctx.table.proxy->table.type == STKTABLE_TYPE_INTEGER) {
3305 chunk_printf(&msg, " key=%u", *(unsigned int *)si->applet.ctx.table.entry->key.key);
Emeric Brun1e029aa2010-09-23 18:12:53 +02003306 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003307 else if (si->applet.ctx.table.proxy->table.type == STKTABLE_TYPE_STRING) {
Emeric Brun1e029aa2010-09-23 18:12:53 +02003308 chunk_printf(&msg, " key=");
Willy Tarreau295a8372011-03-10 11:25:07 +01003309 dump_text(&msg, (const char *)si->applet.ctx.table.entry->key.key, si->applet.ctx.table.proxy->table.key_size);
Emeric Brun1e029aa2010-09-23 18:12:53 +02003310 }
3311 else {
3312 chunk_printf(&msg, " key=");
Willy Tarreau295a8372011-03-10 11:25:07 +01003313 dump_binary(&msg, (const char *)si->applet.ctx.table.entry->key.key, si->applet.ctx.table.proxy->table.key_size);
Emeric Brun1e029aa2010-09-23 18:12:53 +02003314 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003315
3316 chunk_printf(&msg, " use=%d exp=%d",
Willy Tarreau295a8372011-03-10 11:25:07 +01003317 si->applet.ctx.table.entry->ref_cnt - 1,
3318 tick_remain(now_ms, si->applet.ctx.table.entry->expire));
Willy Tarreau69f58c82010-07-12 17:55:33 +02003319
3320 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
3321 void *ptr;
3322
Willy Tarreau295a8372011-03-10 11:25:07 +01003323 if (si->applet.ctx.table.proxy->table.data_ofs[dt] == 0)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003324 continue;
3325 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
3326 chunk_printf(&msg, " %s(%d)=",
3327 stktable_data_types[dt].name,
Willy Tarreau295a8372011-03-10 11:25:07 +01003328 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003329 else
3330 chunk_printf(&msg, " %s=", stktable_data_types[dt].name);
3331
Willy Tarreau295a8372011-03-10 11:25:07 +01003332 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3333 si->applet.ctx.table.entry,
Willy Tarreau69f58c82010-07-12 17:55:33 +02003334 dt);
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02003335 switch (stktable_data_types[dt].std_type) {
3336 case STD_T_SINT:
3337 chunk_printf(&msg, "%d", stktable_data_cast(ptr, std_t_sint));
Willy Tarreau69f58c82010-07-12 17:55:33 +02003338 break;
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02003339 case STD_T_UINT:
3340 chunk_printf(&msg, "%u", stktable_data_cast(ptr, std_t_uint));
3341 break;
3342 case STD_T_ULL:
3343 chunk_printf(&msg, "%lld", stktable_data_cast(ptr, std_t_ull));
3344 break;
3345 case STD_T_FRQP:
Willy Tarreau69f58c82010-07-12 17:55:33 +02003346 chunk_printf(&msg, "%d",
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02003347 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003348 si->applet.ctx.table.proxy->table.data_arg[dt].u));
Willy Tarreau69f58c82010-07-12 17:55:33 +02003349 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003350 }
3351 }
3352 chunk_printf(&msg, "\n");
3353
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003354 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003355 return 0;
3356
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003357 skip_entry:
Willy Tarreau295a8372011-03-10 11:25:07 +01003358 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003359
Willy Tarreau295a8372011-03-10 11:25:07 +01003360 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003361 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003362 struct stksess *old = si->applet.ctx.table.entry;
3363 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3364 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3365 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003366 break;
3367 }
3368
Willy Tarreau295a8372011-03-10 11:25:07 +01003369 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3370 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
3371 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003372 break;
3373
Willy Tarreau295a8372011-03-10 11:25:07 +01003374 case STAT_ST_END:
3375 si->applet.state = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003376 break;
3377 }
3378 }
3379 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003380}
3381
Willy Tarreaud426a182010-03-05 14:58:26 +01003382/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003383 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3384 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3385 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3386 * lines are respected within the limit of 70 output chars. Lines that are
3387 * continuation of a previous truncated line begin with "+" instead of " "
3388 * after the offset. The new pointer is returned.
3389 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003390static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3391 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003392{
3393 int end;
3394 unsigned char c;
3395
3396 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003397 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003398 return ptr;
3399
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003400 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003401
Willy Tarreaud426a182010-03-05 14:58:26 +01003402 while (ptr < len && ptr < bsize) {
3403 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003404 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003405 if (out->len > end - 2)
3406 break;
3407 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003408 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003409 if (out->len > end - 3)
3410 break;
3411 out->str[out->len++] = '\\';
3412 switch (c) {
3413 case '\t': c = 't'; break;
3414 case '\n': c = 'n'; break;
3415 case '\r': c = 'r'; break;
3416 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003417 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003418 }
3419 out->str[out->len++] = c;
3420 } else {
3421 if (out->len > end - 5)
3422 break;
3423 out->str[out->len++] = '\\';
3424 out->str[out->len++] = 'x';
3425 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3426 out->str[out->len++] = hextab[c & 0xF];
3427 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003428 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003429 /* we had a line break, let's return now */
3430 out->str[out->len++] = '\n';
3431 *line = ptr;
3432 return ptr;
3433 }
3434 }
3435 /* we have an incomplete line, we return it as-is */
3436 out->str[out->len++] = '\n';
3437 return ptr;
3438}
3439
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003440/* This function dumps all captured errors onto the stream intreface's
3441 * read buffer. The data_ctx must have been zeroed first, and the flags
3442 * properly set. It returns 0 if the output buffer is full and it needs
3443 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003444 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003445int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003446{
3447 extern const char *monthname[12];
3448 struct chunk msg;
3449
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003450 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003451 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003452
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003453 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003454
Willy Tarreau295a8372011-03-10 11:25:07 +01003455 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003456 /* the function had not been called yet, let's prepare the
3457 * buffer for a response.
3458 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003459 struct tm tm;
3460
3461 get_localtime(date.tv_sec, &tm);
3462 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3463 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3464 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3465 error_snapshot_id);
3466
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003467 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003468 /* Socket buffer full. Let's try again later from the same point */
3469 return 0;
3470 }
3471
Willy Tarreau295a8372011-03-10 11:25:07 +01003472 si->applet.ctx.errors.px = proxy;
3473 si->applet.ctx.errors.buf = 0;
3474 si->applet.ctx.errors.bol = 0;
3475 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003476 }
3477
3478 /* we have two inner loops here, one for the proxy, the other one for
3479 * the buffer.
3480 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003481 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003482 struct error_snapshot *es;
3483
Willy Tarreau295a8372011-03-10 11:25:07 +01003484 if (si->applet.ctx.errors.buf == 0)
3485 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003486 else
Willy Tarreau295a8372011-03-10 11:25:07 +01003487 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003488
3489 if (!es->when.tv_sec)
3490 goto next;
3491
Willy Tarreau295a8372011-03-10 11:25:07 +01003492 if (si->applet.ctx.errors.iid >= 0 &&
3493 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
3494 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003495 goto next;
3496
Willy Tarreau295a8372011-03-10 11:25:07 +01003497 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003498 /* just print headers now */
3499
3500 char pn[INET6_ADDRSTRLEN];
3501 struct tm tm;
3502
3503 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003504 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003505 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003506 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003507
3508
3509 if (es->src.ss_family == AF_INET)
3510 inet_ntop(AF_INET,
3511 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
3512 pn, sizeof(pn));
3513 else
3514 inet_ntop(AF_INET6,
3515 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
3516 pn, sizeof(pn));
3517
Willy Tarreau295a8372011-03-10 11:25:07 +01003518 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003519 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003520 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003521 " frontend %s (#%d): invalid request\n"
3522 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003523 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003524 " request length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003525 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau3fd6cec2010-03-25 06:45:07 +01003526 pn, es->sid, (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3527 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003528 es->srv ? es->srv->id : "<NONE>",
3529 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003530 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003531 es->len, es->pos);
3532 break;
3533 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003534 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003535 " backend %s (#%d) : invalid response\n"
3536 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003537 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003538 " response length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003539 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003540 pn, es->sid, es->oe->id, es->oe->uuid,
3541 es->srv ? es->srv->id : "<NONE>",
3542 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003543 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003544 es->len, es->pos);
3545 break;
3546 }
3547
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003548 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003549 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003550 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003551 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003552 si->applet.ctx.errors.ptr = 0;
3553 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003554 }
3555
Willy Tarreau295a8372011-03-10 11:25:07 +01003556 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003557 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003558 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003559 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003560 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau61b34732009-10-03 23:49:35 +02003561 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003562 goto next;
3563 }
3564
3565 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01003566 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003567 int newptr;
3568 int newline;
3569
Willy Tarreau295a8372011-03-10 11:25:07 +01003570 newline = si->applet.ctx.errors.bol;
3571 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
3572 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02003573 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003574
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003575 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003576 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003577 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003578 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003579 si->applet.ctx.errors.ptr = newptr;
3580 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003581 };
3582 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01003583 si->applet.ctx.errors.bol = 0;
3584 si->applet.ctx.errors.ptr = -1;
3585 si->applet.ctx.errors.buf++;
3586 if (si->applet.ctx.errors.buf > 1) {
3587 si->applet.ctx.errors.buf = 0;
3588 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003589 }
3590 }
3591
3592 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02003593 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003594}
3595
Willy Tarreaub24281b2011-02-13 13:16:36 +01003596struct si_applet http_stats_applet = {
3597 .name = "<STATS>", /* used for logging */
3598 .fct = http_stats_io_handler,
3599};
3600
3601struct si_applet cli_applet = {
3602 .name = "<CLI>", /* used for logging */
3603 .fct = cli_io_handler,
3604};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003605
Willy Tarreau10522fd2008-07-09 20:12:41 +02003606static struct cfg_kw_list cfg_kws = {{ },{
3607 { CFG_GLOBAL, "stats", stats_parse_global },
3608 { 0, NULL, NULL },
3609}};
3610
3611__attribute__((constructor))
3612static void __dumpstats_module_init(void)
3613{
3614 cfg_register_keywords(&cfg_kws);
3615}
3616
Willy Tarreau91861262007-10-17 17:06:05 +02003617/*
3618 * Local variables:
3619 * c-indent-level: 8
3620 * c-basic-offset: 8
3621 * End:
3622 */