blob: 34f09014fee6eb77d34fab17c2a15afb415f6df2 [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 Tarreau7d0aaf32011-03-10 23:25:56 +0100496 if (clrall) {
497 memset(&px->be_counters, 0, sizeof(px->be_counters));
498 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
499 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200500 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100501 px->be_counters.conn_max = 0;
502 px->be_counters.p.http.rps_max = 0;
503 px->be_counters.sps_max = 0;
504 px->be_counters.cps_max = 0;
505 px->be_counters.nbpend_max = 0;
506
507 px->fe_counters.conn_max = 0;
508 px->fe_counters.p.http.rps_max = 0;
509 px->fe_counters.sps_max = 0;
510 px->fe_counters.cps_max = 0;
511 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200512 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200513
514 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200515 if (clrall)
516 memset(&sv->counters, 0, sizeof(sv->counters));
517 else {
518 sv->counters.cur_sess_max = 0;
519 sv->counters.nbpend_max = 0;
520 sv->counters.sps_max = 0;
521 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200522
523 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200524 if (li->counters) {
525 if (clrall)
526 memset(li->counters, 0, sizeof(*li->counters));
527 else
528 li->counters->conn_max = 0;
529 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200530 }
531
532 return 1;
533 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200534 else if (strcmp(args[1], "table") == 0) {
535 struct proxy *px;
536 struct stksess *ts;
537 unsigned int ip_key;
538
539 if (!*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100540 si->applet.ctx.cli.msg = "\"table\" argument expected\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100541 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200542 return 1;
543 }
544
545 px = find_stktable(args[2]);
546
547 if (!px) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100548 si->applet.ctx.cli.msg = "No such table\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 (strcmp(args[3], "key") != 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100554 si->applet.ctx.cli.msg = "\"key\" argument 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 (!*args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100560 si->applet.ctx.cli.msg = "Key value expected\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100561 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200562 return 1;
563 }
564
565 if (px->table.type == STKTABLE_TYPE_IP) {
566 ip_key = htonl(inetaddr_host(args[4]));
567 static_table_key.key = (void *)&ip_key;
568 }
569 else {
Willy Tarreau295a8372011-03-10 11:25:07 +0100570 si->applet.ctx.cli.msg = "Removing keys from non-ip tables is not supported\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100571 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200572 return 1;
573 }
574
575 /* check permissions */
576 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100577 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100578 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200579 return 1;
580 }
581
582 ts = stktable_lookup_key(&px->table, &static_table_key);
583 if (!ts) {
584 /* silent return, entry was already removed */
585 return 1;
586 }
587 else if (ts->ref_cnt) {
588 /* don't delete an entry which is currently referenced */
Willy Tarreau295a8372011-03-10 11:25:07 +0100589 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100590 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200591 return 1;
592 }
593
Willy Tarreaudf39e952010-08-10 11:11:40 +0200594 stksess_kill(&px->table, ts);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200595 /* end of processing */
596 return 1;
597 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200598 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200599 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200600 return 0;
601 }
602 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200603 else if (strcmp(args[0], "get") == 0) {
604 if (strcmp(args[1], "weight") == 0) {
605 struct proxy *px;
606 struct server *sv;
607
608 /* split "backend/server" and make <line> point to server */
609 for (line = args[2]; *line; line++)
610 if (*line == '/') {
611 *line++ = '\0';
612 break;
613 }
614
615 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100616 si->applet.ctx.cli.msg = "Require 'backend/server'.\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 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100622 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100623 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200624 return 1;
625 }
626
627 /* return server's effective weight at the moment */
628 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
629 buffer_feed(si->ib, trash);
630 return 1;
631 }
632 else { /* not "get weight" */
633 return 0;
634 }
635 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200636 else if (strcmp(args[0], "set") == 0) {
637 if (strcmp(args[1], "weight") == 0) {
638 struct proxy *px;
639 struct server *sv;
640 int w;
641
642 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100643 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100644 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200645 return 1;
646 }
647
648 /* split "backend/server" and make <line> point to server */
649 for (line = args[2]; *line; line++)
650 if (*line == '/') {
651 *line++ = '\0';
652 break;
653 }
654
655 if (!*line || !*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100656 si->applet.ctx.cli.msg = "Require 'backend/server' and 'weight' or 'weight%'.\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
661 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100662 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100663 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200664 return 1;
665 }
666
Cyril Bonté613f0df2011-03-03 20:49:04 +0100667 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100668 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100669 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100670 return 1;
671 }
672
Willy Tarreau4483d432009-10-10 19:30:08 +0200673 /* if the weight is terminated with '%', it is set relative to
674 * the initial weight, otherwise it is absolute.
675 */
676 w = atoi(args[3]);
677 if (strchr(args[3], '%') != NULL) {
678 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100679 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100680 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200681 return 1;
682 }
683 w = sv->iweight * w / 100;
684 }
685 else {
686 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100687 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100688 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200689 return 1;
690 }
691 }
692
693 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100694 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 +0100695 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200696 return 1;
697 }
698
699 sv->uweight = w;
700
701 if (px->lbprm.algo & BE_LB_PROP_DYN) {
702 /* we must take care of not pushing the server to full throttle during slow starts */
703 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
704 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
705 else
706 sv->eweight = BE_WEIGHT_SCALE;
707 sv->eweight *= sv->uweight;
708 } else {
709 sv->eweight = sv->uweight;
710 }
711
712 /* static LB algorithms are a bit harder to update */
713 if (px->lbprm.update_server_eweight)
714 px->lbprm.update_server_eweight(sv);
715 else if (sv->eweight)
716 px->lbprm.set_server_status_up(sv);
717 else
718 px->lbprm.set_server_status_down(sv);
719
720 return 1;
721 }
Willy Tarreau7aabd112010-01-26 10:59:06 +0100722 else if (strcmp(args[1], "timeout") == 0) {
723 if (strcmp(args[2], "cli") == 0) {
724 unsigned timeout;
725 const char *res;
726
727 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100728 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100729 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100730 return 1;
731 }
732
733 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
734 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100735 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100736 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100737 return 1;
738 }
739
740 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
741 return 1;
742 }
743 else {
Willy Tarreau295a8372011-03-10 11:25:07 +0100744 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100745 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100746 return 1;
747 }
748 }
749 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +0200750 return 0;
751 }
752 }
Cyril Bontécd19e512010-01-31 22:34:03 +0100753 else if (strcmp(args[0], "enable") == 0) {
754 if (strcmp(args[1], "server") == 0) {
755 struct proxy *px;
756 struct server *sv;
757
758 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100759 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100760 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100761 return 1;
762 }
763
764 /* split "backend/server" and make <line> point to server */
765 for (line = args[2]; *line; line++)
766 if (*line == '/') {
767 *line++ = '\0';
768 break;
769 }
770
771 if (!*line || !*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100772 si->applet.ctx.cli.msg = "Require 'backend/server'.\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
777 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100778 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100779 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100780 return 1;
781 }
782
Cyril Bonté613f0df2011-03-03 20:49:04 +0100783 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100784 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100785 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100786 return 1;
787 }
788
Cyril Bontécd19e512010-01-31 22:34:03 +0100789 if (sv->state & SRV_MAINTAIN) {
790 /* The server is really in maintenance, we can change the server state */
791 if (sv->tracked) {
792 /* If this server tracks the status of another one,
793 * we must restore the good status.
794 */
795 if (sv->tracked->state & SRV_RUNNING) {
796 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +0200797 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +0100798 } else {
799 sv->state &= ~SRV_MAINTAIN;
800 set_server_down(sv);
801 }
802 } else {
803 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +0200804 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +0100805 }
806 }
807
808 return 1;
809 }
810 else { /* unknown "enable" parameter */
811 return 0;
812 }
813 }
814 else if (strcmp(args[0], "disable") == 0) {
815 if (strcmp(args[1], "server") == 0) {
816 struct proxy *px;
817 struct server *sv;
818
819 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100820 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100821 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100822 return 1;
823 }
824
825 /* split "backend/server" and make <line> point to server */
826 for (line = args[2]; *line; line++)
827 if (*line == '/') {
828 *line++ = '\0';
829 break;
830 }
831
832 if (!*line || !*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100833 si->applet.ctx.cli.msg = "Require 'backend/server'.\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
838 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100839 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100840 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100841 return 1;
842 }
843
Cyril Bonté613f0df2011-03-03 20:49:04 +0100844 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100845 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100846 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100847 return 1;
848 }
849
Cyril Bontécd19e512010-01-31 22:34:03 +0100850 if (! (sv->state & SRV_MAINTAIN)) {
851 /* Not already in maintenance, we can change the server state */
852 sv->state |= SRV_MAINTAIN;
853 set_server_down(sv);
854 }
855
856 return 1;
857 }
858 else { /* unknown "disable" parameter */
859 return 0;
860 }
861 }
862 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200863 return 0;
864 }
865 return 1;
866}
867
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200868/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200869 * used to processes I/O from/to the stats unix socket. The system relies on a
870 * state machine handling requests and various responses. We read a request,
871 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +0100872 * Then we can read again. The state is stored in si->applet.st0 and is one of the
873 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200874 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200875 */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100876static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200877{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200878 struct buffer *req = si->ob;
879 struct buffer *res = si->ib;
880 int reql;
881 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200882
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200883 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
884 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200885
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200886 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +0100887 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200888 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +0100889 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +0100890 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200891 }
Willy Tarreaubc4af052011-02-13 13:25:14 +0100892 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200893 /* Let's close for real now. We just close the request
894 * side, the conditions below will complete if needed.
895 */
896 si->shutw(si);
897 break;
898 }
Willy Tarreaubc4af052011-02-13 13:25:14 +0100899 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +0200900 /* ensure we have some output room left in the event we
901 * would want to return some info right after parsing.
902 */
903 if (buffer_almost_full(si->ib))
904 break;
905
Willy Tarreau74b08c92010-09-08 17:04:31 +0200906 reql = buffer_get_line(si->ob, trash, sizeof(trash));
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200907 if (reql <= 0) { /* closed or EOL not found */
908 if (reql == 0)
909 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100910 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200911 continue;
912 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200913
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200914 /* seek for a possible semi-colon. If we find one, we
915 * replace it with an LF and skip only this part.
916 */
917 for (len = 0; len < reql; len++)
918 if (trash[len] == ';') {
919 trash[len] = '\n';
920 reql = len + 1;
921 break;
922 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200923
Willy Tarreau816fc222009-10-04 07:36:58 +0200924 /* now it is time to check that we have a full line,
925 * remove the trailing \n and possibly \r, then cut the
926 * line.
927 */
928 len = reql - 1;
929 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +0100930 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200931 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200932 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200933
Willy Tarreau816fc222009-10-04 07:36:58 +0200934 if (len && trash[len-1] == '\r')
935 len--;
936
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200937 trash[len] = '\0';
938
Willy Tarreaubc4af052011-02-13 13:25:14 +0100939 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200940 if (len) {
941 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +0100942 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200943 continue;
944 }
945 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +0100946 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200947 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200948 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100949 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100950 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200951 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200952 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +0100953 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200954 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200955 }
Willy Tarreaubc4af052011-02-13 13:25:14 +0100956 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200957 /* if prompt is disabled, print help on empty lines,
958 * so that the user at least knows how to enable
959 * prompt and find help.
960 */
Willy Tarreau295a8372011-03-10 11:25:07 +0100961 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100962 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200963 }
964
965 /* re-adjust req buffer */
966 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200967 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200968 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200969 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200970 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +0100971 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200972 continue;
973 }
974
Willy Tarreaubc4af052011-02-13 13:25:14 +0100975 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200976 case STAT_CLI_PRINT:
Willy Tarreau295a8372011-03-10 11:25:07 +0100977 if (buffer_feed(si->ib, si->applet.ctx.cli.msg) < 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +0100978 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200979 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200980 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +0100981 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +0100982 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200983 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200984 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +0100985 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +0100986 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200987 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200988 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +0100989 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +0100990 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200991 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +0200992 case STAT_CLI_O_TAB:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +0100993 if (stats_dump_table_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +0100994 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +0200995 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200996 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +0100997 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200998 break;
999 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001000
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001001 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001002 if (si->applet.st0 == STAT_CLI_PROMPT) {
1003 if (buffer_feed(si->ib, si->applet.st1 ? "\n> " : "\n") < 0)
1004 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001005 }
1006
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001007 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001008 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001009 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001010
1011 /* Now we close the output if one of the writers did so,
1012 * or if we're not in interactive mode and the request
1013 * buffer is empty. This still allows pipelined requests
1014 * to be sent in non-interactive mode.
1015 */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001016 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->applet.st1 && !req->send_max)) {
1017 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001018 continue;
1019 }
1020
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001021 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001022 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001023 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001024 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001025
Willy Tarreaubc4af052011-02-13 13:25:14 +01001026 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001027 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1028 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1029 /* Other size has closed, let's abort if we have no more processing to do
1030 * and nothing more to consume. This is comparable to a broken pipe, so
1031 * we forward the close to the request side so that it flows upstream to
1032 * the client.
1033 */
1034 si->shutw(si);
1035 }
1036
Willy Tarreaubc4af052011-02-13 13:25:14 +01001037 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001038 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1039 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1040 /* We have no more processing to do, and nothing more to send, and
1041 * the client side has closed. So we'll forward this state downstream
1042 * on the response buffer.
1043 */
1044 si->shutr(si);
1045 res->flags |= BF_READ_NULL;
1046 }
1047
1048 /* update all other flags and resync with the other side */
1049 si->update(si);
1050
1051 /* we don't want to expire timeouts while we're processing requests */
1052 si->ib->rex = TICK_ETERNITY;
1053 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001054
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001055 out:
1056 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
1057 __FUNCTION__, __LINE__,
1058 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
1059
1060 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1061 /* check that we have released everything then unregister */
1062 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001063 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001064}
1065
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001066/* This function dumps statistics onto the stream interface's read buffer.
1067 * The data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001068 * It returns 0 as long as it does not complete, non-zero upon completion.
1069 * Some states are not used but it makes the code more similar to other
1070 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001071 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001072int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001073{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001074 struct proxy *px;
1075 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001076 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001077
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001078 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +02001079
Willy Tarreau295a8372011-03-10 11:25:07 +01001080 switch (si->applet.state) {
1081 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001082 /* the function had not been called yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001083 si->applet.state = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001084 /* fall through */
1085
Willy Tarreau295a8372011-03-10 11:25:07 +01001086 case STAT_ST_HEAD:
1087 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001088 print_csv_header(&msg);
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001089 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001090 return 0;
1091 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001092
Willy Tarreau295a8372011-03-10 11:25:07 +01001093 si->applet.state = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001094 /* fall through */
1095
Willy Tarreau295a8372011-03-10 11:25:07 +01001096 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001097 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001098 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001099 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001100 "Name: " PRODUCT_NAME "\n"
1101 "Version: " HAPROXY_VERSION "\n"
1102 "Release_date: " HAPROXY_DATE "\n"
1103 "Nbproc: %d\n"
1104 "Process_num: %d\n"
1105 "Pid: %d\n"
1106 "Uptime: %dd %dh%02dm%02ds\n"
1107 "Uptime_sec: %d\n"
1108 "Memmax_MB: %d\n"
1109 "Ulimit-n: %d\n"
1110 "Maxsock: %d\n"
1111 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001112 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001113 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001114 "PipesUsed: %d\n"
1115 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001116 "Tasks: %d\n"
1117 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001118 "node: %s\n"
1119 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001120 "",
1121 global.nbproc,
1122 relative_pid,
1123 pid,
1124 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1125 up,
1126 global.rlimit_memmax,
1127 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001128 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001129 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001130 nb_tasks_cur, run_queue_cur,
1131 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001132 );
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001133 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001134 return 0;
1135 }
1136
Willy Tarreau295a8372011-03-10 11:25:07 +01001137 si->applet.ctx.stats.px = proxy;
1138 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1139 si->applet.ctx.stats.sv = NULL;
1140 si->applet.state = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001141 /* fall through */
1142
Willy Tarreau295a8372011-03-10 11:25:07 +01001143 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001144 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001145 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1146 while (si->applet.ctx.stats.px) {
1147 px = si->applet.ctx.stats.px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001148 /* skip the disabled proxies and non-networked ones */
1149 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001150 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001151 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001152 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001153 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001154
Willy Tarreau295a8372011-03-10 11:25:07 +01001155 si->applet.ctx.stats.px = px->next;
1156 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001157 }
1158 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001159 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001160
Willy Tarreau295a8372011-03-10 11:25:07 +01001161 si->applet.state = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001162 /* fall through */
1163
Willy Tarreau295a8372011-03-10 11:25:07 +01001164 case STAT_ST_END:
1165 si->applet.state = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001166 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001167
Willy Tarreau295a8372011-03-10 11:25:07 +01001168 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001169 return 1;
1170
1171 default:
1172 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001173 si->applet.state = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001174 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001175 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001176}
1177
1178
Cyril Bonté70be45d2010-10-12 00:14:35 +02001179/* We don't want to land on the posted stats page because a refresh will
1180 * repost the data. We don't want this to happen on accident so we redirect
1181 * the browse to the stats page with a GET.
1182 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001183int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001184{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001185 struct session *s = si->applet.private;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001186 struct chunk msg;
1187
1188 chunk_init(&msg, trash, sizeof(trash));
1189
Willy Tarreau295a8372011-03-10 11:25:07 +01001190 switch (si->applet.state) {
1191 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001192 chunk_printf(&msg,
1193 "HTTP/1.0 303 See Other\r\n"
1194 "Cache-Control: no-cache\r\n"
1195 "Content-Type: text/plain\r\n"
1196 "Connection: close\r\n"
1197 "Location: %s;st=%s",
Willy Tarreau295a8372011-03-10 11:25:07 +01001198 uri->uri_prefix, si->applet.ctx.stats.st_code);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001199 chunk_printf(&msg, "\r\n\r\n");
1200
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001201 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001202 return 0;
1203
1204 s->txn.status = 303;
1205
1206 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1207 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1208 if (!(s->flags & SN_FINST_MASK))
1209 s->flags |= SN_FINST_R;
1210
Willy Tarreau295a8372011-03-10 11:25:07 +01001211 si->applet.state = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001212 return 1;
1213 }
1214 return 1;
1215}
1216
1217
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001218/* This I/O handler runs as an applet embedded in a stream interface. It is
1219 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001220 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001221 * automatically unregisters itself once transfer is complete.
1222 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001223static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001224{
Willy Tarreaubc4af052011-02-13 13:25:14 +01001225 struct session *s = si->applet.private;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001226 struct buffer *req = si->ob;
1227 struct buffer *res = si->ib;
1228
1229 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1230 goto out;
1231
1232 /* check that the output is not closed */
1233 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001234 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001235
Willy Tarreaubc4af052011-02-13 13:25:14 +01001236 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001237 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001238 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001239 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001240 si->shutw(si);
1241 }
1242 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001243 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001244 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001245 si->shutw(si);
1246 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001247 }
1248 }
1249
1250 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
1251 si->shutw(si);
1252
Willy Tarreaubc4af052011-02-13 13:25:14 +01001253 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001254 si->shutr(si);
1255 res->flags |= BF_READ_NULL;
1256 }
1257
1258 /* update all other flags and resync with the other side */
1259 si->update(si);
1260
1261 /* we don't want to expire timeouts while we're processing requests */
1262 si->ib->rex = TICK_ETERNITY;
1263 si->ob->wex = TICK_ETERNITY;
1264
1265 out:
1266 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1267 /* check that we have released everything then unregister */
1268 stream_int_unregister_handler(si);
1269 }
1270}
1271
1272
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001273/* This function dumps statistics in HTTP format onto the stream interface's
1274 * read buffer. The data_ctx must have been zeroed first, and the flags
1275 * properly set. It returns 0 if it had to stop writing data and an I/O is
1276 * needed, 1 if the dump is finished and the session must be closed, or -1
1277 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001278 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001279int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001280{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001281 struct session *s = si->applet.private;
1282 struct buffer *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001283 struct proxy *px;
1284 struct chunk msg;
1285 unsigned int up;
1286
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001287 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001288
Willy Tarreau295a8372011-03-10 11:25:07 +01001289 switch (si->applet.state) {
1290 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001291 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001292 "HTTP/1.0 200 OK\r\n"
1293 "Cache-Control: no-cache\r\n"
1294 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001295 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001296 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001297
Willy Tarreau295a8372011-03-10 11:25:07 +01001298 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001299 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001300 uri->refresh);
1301
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001302 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001303
1304 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001305 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001306 return 0;
1307
Willy Tarreau91861262007-10-17 17:06:05 +02001308 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1309 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1310 if (!(s->flags & SN_FINST_MASK))
1311 s->flags |= SN_FINST_R;
1312
1313 if (s->txn.meth == HTTP_METH_HEAD) {
1314 /* that's all we return in case of HEAD request */
Willy Tarreau295a8372011-03-10 11:25:07 +01001315 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001316 return 1;
1317 }
1318
Willy Tarreau295a8372011-03-10 11:25:07 +01001319 si->applet.state = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001320 /* fall through */
1321
Willy Tarreau295a8372011-03-10 11:25:07 +01001322 case STAT_ST_HEAD:
1323 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001324 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001325 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001326 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1327 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001328 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001329 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1330 "<style type=\"text/css\"><!--\n"
1331 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001332 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001333 " font-size: 12px;"
1334 " font-weight: normal;"
1335 " color: black;"
1336 " background: white;"
1337 "}\n"
1338 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001339 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001340 "}\n"
1341 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001342 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001343 " margin-bottom: 0.5em;"
1344 "}\n"
1345 "h2 {"
1346 " font-family: helvetica, arial;"
1347 " font-size: x-large;"
1348 " font-weight: bold;"
1349 " font-style: italic;"
1350 " color: #6020a0;"
1351 " margin-top: 0em;"
1352 " margin-bottom: 0em;"
1353 "}\n"
1354 "h3 {"
1355 " font-family: helvetica, arial;"
1356 " font-size: 16px;"
1357 " font-weight: bold;"
1358 " color: #b00040;"
1359 " background: #e8e8d0;"
1360 " margin-top: 0em;"
1361 " margin-bottom: 0em;"
1362 "}\n"
1363 "li {"
1364 " margin-top: 0.25em;"
1365 " margin-right: 2em;"
1366 "}\n"
1367 ".hr {margin-top: 0.25em;"
1368 " border-color: black;"
1369 " border-bottom-style: solid;"
1370 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001371 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001372 ".total {background: #20D0D0;color: #ffff80;}\n"
1373 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001374 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001375 ".backend {background: #e8e8d0;}\n"
1376 ".active0 {background: #ff9090;}\n"
1377 ".active1 {background: #ffd020;}\n"
1378 ".active2 {background: #ffffa0;}\n"
1379 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001380 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1381 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1382 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001383 ".backup0 {background: #ff9090;}\n"
1384 ".backup1 {background: #ff80ff;}\n"
1385 ".backup2 {background: #c060ff;}\n"
1386 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001387 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1388 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1389 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001390 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001391 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001392 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001393 "a.px:link {color: #ffff40; text-decoration: none;}"
1394 "a.px:visited {color: #ffff40; text-decoration: none;}"
1395 "a.px:hover {color: #ffffff; text-decoration: none;}"
1396 "a.lfsb:link {color: #000000; text-decoration: none;}"
1397 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1398 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1399 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001400 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001401 "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"
1402 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001403 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001404 "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 +02001405 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001406 "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 +02001407 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001408 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1409 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1410 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001411 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001412 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001413 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001414 (uri->flags&ST_SHNODE) ? " on " : "",
1415 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001416 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001417 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001418 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001419 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001420 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001421 return 0;
1422
Willy Tarreau295a8372011-03-10 11:25:07 +01001423 si->applet.state = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02001424 /* fall through */
1425
Willy Tarreau295a8372011-03-10 11:25:07 +01001426 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02001427 up = (now.tv_sec - start_date.tv_sec);
1428
1429 /* WARNING! this has to fit the first packet too.
1430 * We are around 3.5 kB, add adding entries will
1431 * become tricky if we want to support 4kB buffers !
1432 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001433 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001434 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001435 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1436 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001437 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001438 "<hr width=\"100%%\" class=\"hr\">\n"
1439 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001440 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001441 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001442 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001443 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1444 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
1445 "current conns = %d; current pipes = %d/%d<br>\n"
1446 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001447 "</td><td align=\"center\" nowrap>\n"
1448 "<table class=\"lgd\"><tr>\n"
1449 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1450 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1451 "</tr><tr>\n"
1452 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1453 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1454 "</tr><tr>\n"
1455 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1456 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1457 "</tr><tr>\n"
1458 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001459 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001460 "</tr><tr>\n"
1461 "<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 +02001462 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001463 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001464 "</td>"
1465 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1466 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1467 "",
1468 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001469 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1470 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1471 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001472 up / 86400, (up % 86400) / 3600,
1473 (up % 3600) / 60, (up % 60),
1474 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1475 global.rlimit_memmax ? " MB" : "",
1476 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001477 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001478 actconn, pipes_used, pipes_used+pipes_free,
1479 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +02001480 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001481
Willy Tarreau295a8372011-03-10 11:25:07 +01001482 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
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\">Show all servers</a><br>\n",
1485 uri->uri_prefix,
1486 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01001487 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001488 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001489 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001490 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1491 uri->uri_prefix,
1492 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01001493 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001494
Willy Tarreau55bb8452007-10-17 18:44:57 +02001495 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001496 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
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\">Enable 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 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001502 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001503 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001504 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1505 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001506 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001507 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001508 }
Willy Tarreau91861262007-10-17 17:06:05 +02001509
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001510 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001511 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
1512 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001513 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
1514 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001515
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001516 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02001517 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
1518 uri->uri_prefix,
1519 (uri->refresh > 0) ? ";norefresh" : "");
1520
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001521 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001522 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001523 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1524 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
1525 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
1526 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
1527 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
1528 "</ul>"
1529 "</td>"
1530 "</tr></table>\n"
1531 ""
1532 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001533
Willy Tarreau295a8372011-03-10 11:25:07 +01001534 if (si->applet.ctx.stats.st_code) {
1535 if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001536 chunk_printf(&msg,
1537 "<p><div class=active3>"
1538 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1539 "Action processed successfully."
1540 "</div>\n", uri->uri_prefix);
1541 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001542 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_NONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001543 chunk_printf(&msg,
1544 "<p><div class=active2>"
1545 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1546 "Nothing has changed."
1547 "</div>\n", uri->uri_prefix);
1548 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001549 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_EXCD) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001550 chunk_printf(&msg,
1551 "<p><div class=active0>"
1552 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1553 "<b>Action not processed : the buffer couldn't store all the data.<br>"
1554 "You should retry with less servers at a time.</b>"
1555 "</div>\n", uri->uri_prefix);
1556 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001557 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DENY) == 0) {
Cyril Bonté474be412010-10-12 00:14:36 +02001558 chunk_printf(&msg,
1559 "<p><div class=active0>"
1560 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1561 "<b>Action denied.</b>"
1562 "</div>\n", uri->uri_prefix);
1563 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001564 else {
1565 chunk_printf(&msg,
1566 "<p><div class=active6>"
1567 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1568 "Unexpected result."
1569 "</div>\n", uri->uri_prefix);
1570 }
1571 chunk_printf(&msg,"<p>\n");
1572 }
1573
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001574 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001575 return 0;
1576 }
Willy Tarreau91861262007-10-17 17:06:05 +02001577
Willy Tarreau295a8372011-03-10 11:25:07 +01001578 si->applet.ctx.stats.px = proxy;
1579 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1580 si->applet.state = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02001581 /* fall through */
1582
Willy Tarreau295a8372011-03-10 11:25:07 +01001583 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02001584 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001585 while (si->applet.ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001586 if (buffer_almost_full(rep))
1587 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01001588 px = si->applet.ctx.stats.px;
Willy Tarreau91861262007-10-17 17:06:05 +02001589 /* skip the disabled proxies and non-networked ones */
1590 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001591 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001592 return 0;
1593
Willy Tarreau295a8372011-03-10 11:25:07 +01001594 si->applet.ctx.stats.px = px->next;
1595 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02001596 }
1597 /* here, we just have reached the last proxy */
1598
Willy Tarreau295a8372011-03-10 11:25:07 +01001599 si->applet.state = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02001600 /* fall through */
1601
Willy Tarreau295a8372011-03-10 11:25:07 +01001602 case STAT_ST_END:
1603 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001604 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001605 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001606 return 0;
1607 }
Willy Tarreau91861262007-10-17 17:06:05 +02001608
Willy Tarreau295a8372011-03-10 11:25:07 +01001609 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001610 /* fall through */
1611
Willy Tarreau295a8372011-03-10 11:25:07 +01001612 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02001613 return 1;
1614
1615 default:
1616 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001617 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001618 return -1;
1619 }
1620}
1621
1622
1623/*
1624 * Dumps statistics for a proxy.
1625 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1626 * ot non-zero if everything completed.
1627 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001628int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001629{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001630 struct session *s = si->applet.private;
1631 struct buffer *rep = si->ib;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001632 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001633 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02001634 struct chunk msg;
1635
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001636 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001637
Willy Tarreau295a8372011-03-10 11:25:07 +01001638 switch (si->applet.ctx.stats.px_st) {
1639 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02001640 /* we are on a new proxy */
1641
1642 if (uri && uri->scope) {
1643 /* we have a limited scope, we have to check the proxy name */
1644 struct stat_scope *scope;
1645 int len;
1646
1647 len = strlen(px->id);
1648 scope = uri->scope;
1649
1650 while (scope) {
1651 /* match exact proxy name */
1652 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1653 break;
1654
1655 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001656 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001657 break;
1658 scope = scope->next;
1659 }
1660
1661 /* proxy name not found : don't dump anything */
1662 if (scope == NULL)
1663 return 1;
1664 }
1665
Willy Tarreau295a8372011-03-10 11:25:07 +01001666 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
1667 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001668 return 1;
1669
Willy Tarreau295a8372011-03-10 11:25:07 +01001670 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02001671 /* fall through */
1672
Willy Tarreau295a8372011-03-10 11:25:07 +01001673 case STAT_PX_ST_TH:
1674 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
1675 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001676 /* A form to enable/disable this proxy servers */
1677 chunk_printf(&msg,
1678 "<form action=\"%s\" method=\"post\">",
1679 uri->uri_prefix);
1680 }
1681
Willy Tarreau55bb8452007-10-17 18:44:57 +02001682 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001683 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001684 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001685 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001686 "<th class=\"pxname\" width=\"10%%\"");
1687
1688 if (uri->flags&ST_SHLGNDS) {
1689 /* cap, mode, id */
1690 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
1691 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1692 px->uuid);
1693
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001694 chunk_printf(&msg, "\"");
1695 }
1696
1697 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001698 ">%s<a name=\"%s\"></a>"
1699 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001700 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001701 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001702 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001703 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001704 "<tr class=\"titre\">",
1705 (uri->flags & ST_SHLGNDS)?"<u>":"",
1706 px->id, px->id, px->id,
1707 (uri->flags & ST_SHLGNDS)?"</u>":"",
1708 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1709
Willy Tarreau295a8372011-03-10 11:25:07 +01001710 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001711 /* Column heading for Enable or Disable server */
1712 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
1713 }
1714
1715 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001716 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001717 "<th colspan=3>Queue</th>"
1718 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001719 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001720 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001721 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001722 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001723 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001724 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001725 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001726 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001727 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001728 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001729 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001730 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1731 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001732 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001733
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001734 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001735 return 0;
1736 }
Willy Tarreau91861262007-10-17 17:06:05 +02001737
Willy Tarreau295a8372011-03-10 11:25:07 +01001738 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02001739 /* fall through */
1740
Willy Tarreau295a8372011-03-10 11:25:07 +01001741 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02001742 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001743 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01001744 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
1745 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001746 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001747 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02001748 "<tr class=\"frontend\">");
1749
Willy Tarreau295a8372011-03-10 11:25:07 +01001750 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001751 /* Column sub-heading for Enable or Disable server */
1752 chunk_printf(&msg, "<td></td>");
1753 }
1754
1755 chunk_printf(&msg,
1756 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001757 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001758 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
1759 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01001760 "",
1761 px->id, px->id);
1762
1763 if (px->mode == PR_MODE_HTTP) {
1764 chunk_printf(&msg,
1765 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01001766 "<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 +01001767 "",
1768 read_freq_ctr(&px->fe_req_per_sec),
1769 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001770 px->fe_counters.p.http.rps_max,
1771 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01001772 LIM2A2(px->fe_sps_lim, "-"));
1773 } else {
1774 chunk_printf(&msg,
1775 /* sessions rate : current, max, limit */
1776 "<td>%s</td><td>%s</td><td>%s</td>"
1777 "",
1778 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001779 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01001780 }
1781
1782 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001783 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001784 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001785 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001786 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001787 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001788
1789 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1790 if (px->mode == PR_MODE_HTTP) {
1791 int i;
1792
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001793 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001794
1795 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001796 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001797
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001798 chunk_printf(&msg, " other=%lld\"", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001799 }
1800
1801 chunk_printf(&msg,
1802 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01001803 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001804 /* bytes : in, out */
1805 "<td>%s</td><td>%s</td>"
1806 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001807 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001808 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01001809 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001810 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001811
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001812 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001813 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001814 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001815 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001816 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001817 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001818 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001819 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001820 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001821 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001822 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02001823 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001824 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
1825 U2H2(px->fe_counters.failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +02001826 px->state == PR_STRUN ? "OPEN" :
1827 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001828 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001829 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001830 /* pxid, name, queue cur, queue max, */
1831 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001832 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001833 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001834 /* bytes : in, out */
1835 "%lld,%lld,"
1836 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001837 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001838 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001839 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001840 /* warnings: retries, redispatches */
1841 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001842 /* server status : reflect frontend status */
1843 "%s,"
1844 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001845 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001846 /* pid, iid, sid, throttle, lbtot, tracked, type */
1847 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001848 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001849 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001850 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001851 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02001852 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001853 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
1854 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
1855 px->fe_counters.denied_req, px->fe_counters.denied_resp,
1856 px->fe_counters.failed_req,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001857 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +01001858 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01001859 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001860 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001861 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001862
1863 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1864 if (px->mode == PR_MODE_HTTP) {
1865 int i;
1866
1867 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001868 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001869
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001870 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001871 } else {
1872 chunk_printf(&msg, ",,,,,,");
1873 }
1874
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001875 /* failed health analyses */
1876 chunk_printf(&msg, ",");
1877
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001878 /* requests : req_rate, req_rate_max, req_tot, */
1879 chunk_printf(&msg, "%u,%u,%lld,",
1880 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001881 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001882
Willy Tarreauae526782010-03-04 20:34:23 +01001883 /* errors: cli_aborts, srv_aborts */
1884 chunk_printf(&msg, ",,");
1885
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001886 /* finish with EOL */
1887 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001888 }
Willy Tarreau91861262007-10-17 17:06:05 +02001889
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001890 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001891 return 0;
1892 }
1893
Willy Tarreau295a8372011-03-10 11:25:07 +01001894 si->applet.ctx.stats.l = px->listen; /* may be NULL */
1895 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001896 /* fall through */
1897
Willy Tarreau295a8372011-03-10 11:25:07 +01001898 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001899 /* stats.l has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01001900 for (; si->applet.ctx.stats.l != NULL; si->applet.ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001901 if (buffer_almost_full(rep))
1902 return 0;
1903
Willy Tarreau295a8372011-03-10 11:25:07 +01001904 l = si->applet.ctx.stats.l;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001905 if (!l->counters)
1906 continue;
1907
Willy Tarreau295a8372011-03-10 11:25:07 +01001908 if (si->applet.ctx.stats.flags & STAT_BOUND) {
1909 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001910 break;
1911
Willy Tarreau295a8372011-03-10 11:25:07 +01001912 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001913 continue;
1914 }
1915
Willy Tarreau295a8372011-03-10 11:25:07 +01001916 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001917 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01001918 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001919 /* Column sub-heading for Enable or Disable server */
1920 chunk_printf(&msg, "<td></td>");
1921 }
1922 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001923
1924 if (uri->flags&ST_SHLGNDS) {
1925 char str[INET6_ADDRSTRLEN], *fmt = NULL;
1926 int port;
1927
1928 chunk_printf(&msg, " title=\"IP: ");
1929
1930 port = (l->addr.ss_family == AF_INET6)
1931 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
1932 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
1933
1934 if (l->addr.ss_family == AF_INET) {
1935 if (inet_ntop(AF_INET,
1936 (const void *)&((struct sockaddr_in *)&l->addr)->sin_addr,
1937 str, sizeof(str)))
1938 fmt = "%s:%d";
1939 } else {
1940 if (inet_ntop(AF_INET6,
1941 (const void *)&((struct sockaddr_in6 *)(&l->addr))->sin6_addr,
1942 str, sizeof(str)))
1943 fmt = "[%s]:%d";
1944 }
1945
1946 if (fmt)
1947 chunk_printf(&msg, fmt, str, port);
1948 else
1949 chunk_printf(&msg, "(%s)", strerror(errno));
1950
1951 /* id */
1952 chunk_printf(&msg, ", id: %d", l->luid);
1953
1954 chunk_printf(&msg, "\"");
1955 }
1956
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001957 chunk_printf(&msg,
1958 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01001959 ">%s<a name=\"%s/+%s\"></a>"
1960 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001961 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001962 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001963 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001964 "<td>%s</td><td>%s</td><td>%s</td>"
1965 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001966 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001967 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001968 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001969 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01001970 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01001971 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001972 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
1973 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
1974
1975 chunk_printf(&msg,
1976 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001977 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001978 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001979 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001980 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001981 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001982 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001983 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001984 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001985 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001986 "",
1987 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
1988 U2H2(l->counters->failed_req),
1989 (l->nbconn < l->maxconn) ? "OPEN" : "FULL");
1990 } else {
1991 chunk_printf(&msg,
1992 /* pxid, name, queue cur, queue max, */
1993 "%s,%s,,,"
1994 /* sessions: current, max, limit, total */
1995 "%d,%d,%d,%lld,"
1996 /* bytes: in, out */
1997 "%lld,%lld,"
1998 /* denied: req, resp */
1999 "%lld,%lld,"
2000 /* errors: request, connect, response */
2001 "%lld,,,"
2002 /* warnings: retries, redispatches */
2003 ",,"
2004 /* server status: reflect listener status */
2005 "%s,"
2006 /* rest of server: nothing */
2007 ",,,,,,,,"
2008 /* pid, iid, sid, throttle, lbtot, tracked, type */
2009 "%d,%d,%d,,,,%d,"
2010 /* rate, rate_lim, rate_max */
2011 ",,,"
2012 /* check_status, check_code, check_duration */
2013 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002014 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2015 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002016 /* failed health analyses */
2017 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002018 /* requests : req_rate, req_rate_max, req_tot, */
2019 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002020 /* errors: cli_aborts, srv_aborts */
2021 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002022 "\n",
2023 px->id, l->name,
2024 l->nbconn, l->counters->conn_max,
2025 l->maxconn, l->counters->cum_conn,
2026 l->counters->bytes_in, l->counters->bytes_out,
2027 l->counters->denied_req, l->counters->denied_resp,
2028 l->counters->failed_req,
2029 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2030 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2031 }
2032
2033 if (buffer_feed_chunk(rep, &msg) >= 0)
2034 return 0;
2035 }
2036
Willy Tarreau295a8372011-03-10 11:25:07 +01002037 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2038 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002039 /* fall through */
2040
Willy Tarreau295a8372011-03-10 11:25:07 +01002041 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002042 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002043 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002044 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 +02002045
Willy Tarreau4e33d862009-10-11 23:35:10 +02002046 if (buffer_almost_full(rep))
2047 return 0;
2048
Willy Tarreau295a8372011-03-10 11:25:07 +01002049 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002050
Willy Tarreau295a8372011-03-10 11:25:07 +01002051 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2052 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002053 break;
2054
Willy Tarreau295a8372011-03-10 11:25:07 +01002055 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002056 continue;
2057 }
2058
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002059 if (sv->tracked)
2060 svs = sv->tracked;
2061 else
2062 svs = sv;
2063
Willy Tarreau91861262007-10-17 17:06:05 +02002064 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002065 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002066 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002067 else if (svs->state & SRV_RUNNING) {
2068 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002069 sv_state = 3; /* UP */
2070 else
2071 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002072
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002073 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002074 sv_state += 2;
2075 }
Willy Tarreau91861262007-10-17 17:06:05 +02002076 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002077 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002078 sv_state = 1; /* going up */
2079 else
2080 sv_state = 0; /* DOWN */
2081
Willy Tarreau295a8372011-03-10 11:25:07 +01002082 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002083 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002084 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002085 continue;
2086 }
2087
Willy Tarreau295a8372011-03-10 11:25:07 +01002088 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002089 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2090 "UP %d/%d &darr;", "UP",
2091 "NOLB %d/%d &darr;", "NOLB",
2092 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002093 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002094 chunk_printf(&msg,
2095 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002096 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002097 );
2098 }
2099 else {
2100 chunk_printf(&msg,
2101 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002102 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002103 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2104 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002105
Willy Tarreau295a8372011-03-10 11:25:07 +01002106 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002107 chunk_printf(&msg,
2108 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2109 sv->id);
2110 }
2111
2112 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002113
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002114 if (uri->flags&ST_SHLGNDS) {
2115 char str[INET6_ADDRSTRLEN];
2116
2117 chunk_printf(&msg, " title=\"IP: ");
2118
2119 /* IP */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002120 switch (sv->addr.ss_family) {
2121 case AF_INET:
2122 if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&sv->addr)->sin_addr, str, sizeof(str)))
2123 chunk_printf(&msg, "%s:%d", str, htons(((struct sockaddr_in *)&sv->addr)->sin_port));
2124 else
2125 chunk_printf(&msg, "(%s)", strerror(errno));
2126 break;
2127 case AF_INET6:
2128 if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&sv->addr)->sin6_addr, str, sizeof(str)))
2129 chunk_printf(&msg, "%s:%d", str, htons(((struct sockaddr_in6 *)&sv->addr)->sin6_port));
2130 else
2131 chunk_printf(&msg, "(%s)", strerror(errno));
2132 break;
2133 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002134
2135 /* id */
2136 chunk_printf(&msg, ", id: %d", sv->puid);
2137
2138 /* cookie */
2139 if (sv->cookie) {
2140 struct chunk src;
2141
2142 chunk_printf(&msg, ", cookie: '");
2143
2144 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2145 chunk_htmlencode(&msg, &src);
2146
2147 chunk_printf(&msg, "'");
2148 }
2149
2150 chunk_printf(&msg, "\"");
2151 }
2152
2153 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002154 ">%s<a name=\"%s/%s\"></a>"
2155 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002156 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002157 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002158 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002159 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002160 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002161 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002162 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002163 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002164 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002165 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002166 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002167 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2168 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002169 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2170
2171 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2172 if (px->mode == PR_MODE_HTTP) {
2173 int i;
2174
2175 chunk_printf(&msg, " title=\"rsp codes:");
2176
2177 for (i = 1; i < 6; i++)
2178 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2179
2180 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2181 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002182
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002183 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002184 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002185 ">%s%s%s</td><td>%s</td>",
2186 (px->mode == PR_MODE_HTTP)?"<u>":"",
2187 U2H0(sv->counters.cum_sess),
2188 (px->mode == PR_MODE_HTTP)?"</u>":"",
2189 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002190
2191 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002192 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002193 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002194 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002195 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002196 /* errors : request, connect */
2197 "<td></td><td>%s</td>"
2198 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002199 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002200 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002201 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002202 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002203 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2204 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002205 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002206 sv->counters.cli_aborts,
2207 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002208 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002209 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002210
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002211 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002212 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002213
Cyril Bontécd19e512010-01-31 22:34:03 +01002214 if (sv->state & SRV_MAINTAIN) {
2215 chunk_printf(&msg, "%s ",
2216 human_time(now.tv_sec - sv->last_change, 1));
2217 chunk_printf(&msg, "MAINT");
2218 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002219 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2220 chunk_printf(&msg, "%s ",
2221 human_time(now.tv_sec - svs->last_change, 1));
2222 chunk_printf(&msg, "MAINT(via)");
2223 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002224 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002225 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002226 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002227
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002228 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002229 srv_hlt_st[sv_state],
2230 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2231 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002232 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002233
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002234 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002235 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002236 get_check_status_description(sv->check_status));
2237
2238 if (*sv->check_desc) {
2239 struct chunk src;
2240
2241 chunk_printf(&msg, ": ");
2242
2243 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2244 chunk_htmlencode(&msg, &src);
2245 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002246
Willy Tarreaue0454092010-02-26 12:29:07 +01002247 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002248 tv_iszero(&sv->check_start)?"":"* ",
2249 get_check_status_info(sv->check_status));
2250
2251 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002252 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002253
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002254 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002255 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002256 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002257 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002258
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002259 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002260 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002261 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002262 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002263 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002264 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002265 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002266 (sv->state & SRV_BACKUP) ? "-" : "Y",
2267 (sv->state & SRV_BACKUP) ? "Y" : "-");
2268
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002269 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002270 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002271 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002272 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2273
2274 if (svs->observe)
2275 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2276
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002277 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002278 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002279 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002280 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002281 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002282 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002283 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002284 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2285 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002286 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002287 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002288 "<td colspan=3></td>");
2289
2290 /* throttle */
2291 if ((sv->state & SRV_WARMINGUP) &&
2292 now.tv_sec < sv->last_change + sv->slowstart &&
2293 now.tv_sec >= sv->last_change) {
2294 unsigned int ratio;
2295 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002296 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002297 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002298 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002299 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002300 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002301 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002302 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002303 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2304 "UP %d/%d,", "UP,",
2305 "NOLB %d/%d,", "NOLB,",
2306 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002307 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002308 /* pxid, name */
2309 "%s,%s,"
2310 /* queue : current, max */
2311 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002312 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002313 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002314 /* bytes : in, out */
2315 "%lld,%lld,"
2316 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002317 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002318 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002319 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002320 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002321 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002322 "",
2323 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002324 sv->nbpend, sv->counters.nbpend_max,
2325 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002326 sv->counters.bytes_in, sv->counters.bytes_out,
2327 sv->counters.failed_secu,
2328 sv->counters.failed_conns, sv->counters.failed_resp,
2329 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002330
Willy Tarreau55bb8452007-10-17 18:44:57 +02002331 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002332 if (sv->state & SRV_MAINTAIN) {
2333 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002334 }
2335 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2336 chunk_printf(&msg, "MAINT(via),");
2337 }
2338 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002339 chunk_printf(&msg,
2340 srv_hlt_st[sv_state],
2341 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2342 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2343 }
Willy Tarreau91861262007-10-17 17:06:05 +02002344
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002345 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002346 /* weight, active, backup */
2347 "%d,%d,%d,"
2348 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002349 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002350 (sv->state & SRV_BACKUP) ? 0 : 1,
2351 (sv->state & SRV_BACKUP) ? 1 : 0);
2352
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002353 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002354 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002355 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002356 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002357 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002358 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002359 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002360 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002361 ",,,,");
2362
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002363 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002364 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002365 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002366 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002367 LIM2A0(sv->maxqueue, ""),
2368 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002369
2370 /* throttle */
2371 if ((sv->state & SRV_WARMINGUP) &&
2372 now.tv_sec < sv->last_change + sv->slowstart &&
2373 now.tv_sec >= sv->last_change) {
2374 unsigned int ratio;
2375 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002376 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002377 }
2378
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002379 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002380 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002381
2382 /* tracked */
2383 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002384 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002385 sv->tracked->proxy->id, sv->tracked->id);
2386 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002387 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002388
Willy Tarreau7f062c42009-03-05 18:43:00 +01002389 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002390 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002391
2392 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002393 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002394 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002395 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002396
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002397 if (sv->state & SRV_CHECKED) {
2398 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002399 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002400
2401 /* check_code */
2402 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002403 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002404 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002405 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002406
2407 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002408 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002409 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002410 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002411 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002412
2413 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002414 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002415 }
2416
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002417 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2418 if (px->mode == PR_MODE_HTTP) {
2419 int i;
2420
2421 for (i=1; i<6; i++)
2422 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2423
2424 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2425 } else {
2426 chunk_printf(&msg, ",,,,,,");
2427 }
2428
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002429 /* failed health analyses */
2430 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2431
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002432 /* requests : req_rate, req_rate_max, req_tot, */
2433 chunk_printf(&msg, ",,,");
2434
Willy Tarreauae526782010-03-04 20:34:23 +01002435 /* errors: cli_aborts, srv_aborts */
2436 chunk_printf(&msg, "%lld,%lld,",
2437 sv->counters.cli_aborts, sv->counters.srv_aborts);
2438
Willy Tarreau7f062c42009-03-05 18:43:00 +01002439 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002440 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002441 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002442 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002443 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002444 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002445
Willy Tarreau295a8372011-03-10 11:25:07 +01002446 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02002447 /* fall through */
2448
Willy Tarreau295a8372011-03-10 11:25:07 +01002449 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02002450 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002451 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002452 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2453 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002454 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01002455 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002456 /* Column sub-heading for Enable or Disable server */
2457 chunk_printf(&msg, "<td></td>");
2458 }
2459 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002460
2461 if (uri->flags&ST_SHLGNDS) {
2462 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002463 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002464 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002465
2466 /* cookie */
2467 if (px->cookie_name) {
2468 struct chunk src;
2469
2470 chunk_printf(&msg, ", cookie: '");
2471
2472 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2473 chunk_htmlencode(&msg, &src);
2474
2475 chunk_printf(&msg, "'");
2476 }
2477
2478 chunk_printf(&msg, "\"");
2479
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002480 }
2481
2482 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002483 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01002484 ">%s<a name=\"%s/Backend\"></a>"
2485 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002486 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002487 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002488 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002489 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002490 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002491 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002492 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002493 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002494 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
2495 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02002496
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002497 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002498 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002499 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002500 "<td"
2501 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002502 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002503
2504 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2505 if (px->mode == PR_MODE_HTTP) {
2506 int i;
2507
2508 chunk_printf(&msg, " title=\"rsp codes:");
2509
2510 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002511 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002512
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002513 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002514 }
2515
2516 chunk_printf(&msg,
2517 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002518 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002519 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002520 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002521 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002522 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002523 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01002524 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002525 U2H7(px->be_counters.cum_lbconn),
2526 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002527
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002528 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002529 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002530 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002531 /* errors : request, connect */
2532 "<td></td><td>%s</td>"
2533 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002534 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002535 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002536 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002537 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02002538 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002539 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02002540 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002541 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2542 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002543 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002544 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
2545 U2H2(px->be_counters.failed_conns),
2546 px->be_counters.cli_aborts,
2547 px->be_counters.srv_aborts,
2548 U2H5(px->be_counters.failed_resp),
2549 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002550 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01002551 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2552 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01002553 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002554 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002555
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002556 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002557 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002558 "<td class=ac>&nbsp;</td><td>%d</td>"
2559 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002560 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002561 "</tr>",
2562 px->down_trans,
2563 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002564 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002565 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002566 /* pxid, name */
2567 "%s,BACKEND,"
2568 /* queue : current, max */
2569 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002570 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002571 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002572 /* bytes : in, out */
2573 "%lld,%lld,"
2574 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002575 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002576 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002577 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002578 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002579 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002580 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02002581 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002582 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02002583 * active and backups. */
2584 "%s,"
2585 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002586 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002587 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002588 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002589 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002590 /* rate, rate_lim, rate_max, */
2591 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002592 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002593 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002594 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002595 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2596 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2597 px->be_counters.bytes_in, px->be_counters.bytes_out,
2598 px->be_counters.denied_req, px->be_counters.denied_resp,
2599 px->be_counters.failed_conns, px->be_counters.failed_resp,
2600 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01002601 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01002602 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002603 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002604 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01002605 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002606 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002607 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002608 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002609 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002610
2611 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2612 if (px->mode == PR_MODE_HTTP) {
2613 int i;
2614
2615 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002616 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002617
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002618 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002619 } else {
2620 chunk_printf(&msg, ",,,,,,");
2621 }
2622
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002623 /* failed health analyses */
2624 chunk_printf(&msg, ",");
2625
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002626 /* requests : req_rate, req_rate_max, req_tot, */
2627 chunk_printf(&msg, ",,,");
2628
Willy Tarreauae526782010-03-04 20:34:23 +01002629 /* errors: cli_aborts, srv_aborts */
2630 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002631 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01002632
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002633 /* finish with EOL */
2634 chunk_printf(&msg, "\n");
2635
Willy Tarreau55bb8452007-10-17 18:44:57 +02002636 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002637 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002638 return 0;
2639 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002640
Willy Tarreau295a8372011-03-10 11:25:07 +01002641 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002642 /* fall through */
2643
Willy Tarreau295a8372011-03-10 11:25:07 +01002644 case STAT_PX_ST_END:
2645 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002646 chunk_printf(&msg, "</table>");
2647
Willy Tarreau295a8372011-03-10 11:25:07 +01002648 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002649 /* close the form used to enable/disable this proxy servers */
2650 chunk_printf(&msg,
2651 "Choose the action to perform on the checked servers : "
2652 "<select name=action>"
2653 "<option value=\"\"></option>"
2654 "<option value=\"disable\">Disable</option>"
2655 "<option value=\"enable\">Enable</option>"
2656 "</select>"
2657 "<input type=\"hidden\" name=\"b\" value=\"%s\">"
2658 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2659 "</form>",
2660 px->id);
2661 }
2662
2663 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02002664
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002665 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002666 return 0;
2667 }
Willy Tarreau91861262007-10-17 17:06:05 +02002668
Willy Tarreau295a8372011-03-10 11:25:07 +01002669 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002670 /* fall through */
2671
Willy Tarreau295a8372011-03-10 11:25:07 +01002672 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002673 return 1;
2674
2675 default:
2676 /* unknown state, we should put an abort() here ! */
2677 return 1;
2678 }
2679}
2680
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002681/* This function dumps a complete session state onto the stream intreface's
2682 * read buffer. The data_ctx must have been zeroed first, and the flags
2683 * properly set. The session has to be set in data_ctx.sess.target. It returns
2684 * 0 if the output buffer is full and it needs to be called again, otherwise
2685 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002686 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002687int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002688{
2689 struct tm tm;
2690 struct chunk msg;
2691 struct session *sess;
2692 extern const char *monthname[12];
2693 char pn[INET6_ADDRSTRLEN];
2694
2695 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau295a8372011-03-10 11:25:07 +01002696 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002697
Willy Tarreau295a8372011-03-10 11:25:07 +01002698 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002699 /* session changed, no need to go any further */
2700 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002701 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002702 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002703 si->applet.ctx.sess.target = NULL;
2704 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002705 return 1;
2706 }
2707
Willy Tarreau295a8372011-03-10 11:25:07 +01002708 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002709 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01002710 si->applet.ctx.sess.uid = sess->uniq_id;
2711 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002712 /* fall through */
2713
2714 case 1:
2715 chunk_printf(&msg,
2716 "%p: id=%u, proto=%s",
2717 sess,
2718 sess->uniq_id,
2719 sess->listener->proto->name);
2720
2721 switch (sess->listener->proto->sock_family) {
2722 case AF_INET:
2723 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002724 (const void *)&((struct sockaddr_in *)&sess->si[0].addr.c.from)->sin_addr,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002725 pn, sizeof(pn));
2726
2727 chunk_printf(&msg,
2728 " source=%s:%d\n",
2729 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002730 ntohs(((struct sockaddr_in *)&sess->si[0].addr.c.from)->sin_port));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002731 break;
2732 case AF_INET6:
2733 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002734 (const void *)&((struct sockaddr_in6 *)(&sess->si[0].addr.c.from))->sin6_addr,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002735 pn, sizeof(pn));
2736
2737 chunk_printf(&msg,
2738 " source=%s:%d\n",
2739 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002740 ntohs(((struct sockaddr_in6 *)&sess->si[0].addr.c.from)->sin6_port));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002741 break;
2742 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02002743 chunk_printf(&msg,
2744 " source=unix:%d\n", sess->listener->luid);
2745 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002746 default:
2747 /* no more information to print right now */
2748 chunk_printf(&msg, "\n");
2749 break;
2750 }
2751
2752 chunk_printf(&msg,
2753 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02002754 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002755
2756 chunk_printf(&msg,
2757 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)\n",
2758 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
2759 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
2760 sess->listener ? sess->listener->luid : 0);
2761
2762 chunk_printf(&msg,
2763 " backend=%s (id=%u mode=%s) server=%s (id=%u)\n",
2764 sess->be->id, sess->be->uuid, sess->be->mode ? "http" : "tcp",
Willy Tarreau827aee92011-03-10 16:55:02 +01002765 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
2766 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002767
2768 chunk_printf(&msg,
2769 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
2770 sess->task,
2771 sess->task->state,
2772 sess->task->nice, sess->task->calls,
2773 sess->task->expire ?
2774 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
2775 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
2776 TICKS_TO_MS(1000)) : "<NEVER>",
2777 task_in_rq(sess->task) ? ", running" : "");
2778
2779 get_localtime(sess->logs.accept_date.tv_sec, &tm);
2780 chunk_printf(&msg,
2781 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
2782 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2783 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
2784 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
2785
2786 chunk_printf(&msg,
2787 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
2788 &sess->si[0],
2789 sess->si[0].state,
2790 sess->si[0].flags,
2791 sess->si[0].fd,
2792 sess->si[0].exp ?
2793 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
2794 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
2795 TICKS_TO_MS(1000)) : "<NEVER>",
2796 sess->si[0].err_type);
2797
2798 chunk_printf(&msg,
2799 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
2800 &sess->si[1],
2801 sess->si[1].state,
2802 sess->si[1].flags,
2803 sess->si[1].fd,
2804 sess->si[1].exp ?
2805 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
2806 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
2807 TICKS_TO_MS(1000)) : "<NEVER>",
2808 sess->si[1].err_type);
2809
2810 chunk_printf(&msg,
2811 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
2812 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
2813 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
2814
2815
2816 chunk_printf(&msg,
2817 " req=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%ld)\n"
2818 " an_exp=%s",
2819 sess->req,
2820 sess->req->flags, sess->req->analysers,
2821 sess->req->l, sess->req->send_max,
2822 sess->req->pipe ? sess->req->pipe->data : 0,
2823 sess->req->to_forward,
2824 sess->req->analyse_exp ?
2825 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
2826 TICKS_TO_MS(1000)) : "<NEVER>");
2827
2828 chunk_printf(&msg,
2829 " rex=%s",
2830 sess->req->rex ?
2831 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
2832 TICKS_TO_MS(1000)) : "<NEVER>");
2833
2834 chunk_printf(&msg,
2835 " wex=%s\n"
2836 " data=%p r=%d w=%d lr=%d total=%lld\n",
2837 sess->req->wex ?
2838 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
2839 TICKS_TO_MS(1000)) : "<NEVER>",
2840 sess->req->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01002841 (int)(sess->req->r - sess->req->data),
2842 (int)(sess->req->w - sess->req->data),
2843 (int)(sess->req->lr - sess->req->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002844 sess->req->total);
2845
2846 chunk_printf(&msg,
2847 " res=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%ld)\n"
2848 " an_exp=%s",
2849 sess->rep,
2850 sess->rep->flags, sess->rep->analysers,
2851 sess->rep->l, sess->rep->send_max,
2852 sess->rep->pipe ? sess->rep->pipe->data : 0,
2853 sess->rep->to_forward,
2854 sess->rep->analyse_exp ?
2855 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
2856 TICKS_TO_MS(1000)) : "<NEVER>");
2857
2858 chunk_printf(&msg,
2859 " rex=%s",
2860 sess->rep->rex ?
2861 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
2862 TICKS_TO_MS(1000)) : "<NEVER>");
2863
2864 chunk_printf(&msg,
2865 " wex=%s\n"
2866 " data=%p r=%d w=%d lr=%d total=%lld\n",
2867 sess->rep->wex ?
2868 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
2869 TICKS_TO_MS(1000)) : "<NEVER>",
2870 sess->rep->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01002871 (int)(sess->rep->r - sess->rep->data),
2872 (int)(sess->rep->w - sess->rep->data),
2873 (int)(sess->rep->lr - sess->rep->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002874 sess->rep->total);
2875
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002876 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002877 return 0;
2878
2879 /* use other states to dump the contents */
2880 }
2881 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01002882 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002883 return 1;
2884}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002885
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002886/* This function dumps all sessions' states onto the stream intreface's
2887 * read buffer. The data_ctx must have been zeroed first, and the flags
2888 * properly set. It returns 0 if the output buffer is full and it needs
2889 * to be called again, otherwise non-zero. It is designed to be called
2890 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002891 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002892int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002893{
2894 struct chunk msg;
2895
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002896 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002897 /* If we're forced to shut down, we might have to remove our
2898 * reference to the last session being dumped.
2899 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002900 if (si->applet.state == STAT_ST_LIST) {
2901 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
2902 LIST_DEL(&si->applet.ctx.sess.bref.users);
2903 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002904 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002905 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002906 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002907 }
2908
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002909 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002910
Willy Tarreau295a8372011-03-10 11:25:07 +01002911 switch (si->applet.state) {
2912 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002913 /* the function had not been called yet, let's prepare the
2914 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002915 * pointer to the first in the global list. When a target
2916 * session is being destroyed, it is responsible for updating
2917 * this pointer. We know we have reached the end when this
2918 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002919 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002920 LIST_INIT(&si->applet.ctx.sess.bref.users);
2921 si->applet.ctx.sess.bref.ref = sessions.n;
2922 si->applet.state = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002923 /* fall through */
2924
Willy Tarreau295a8372011-03-10 11:25:07 +01002925 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002926 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01002927 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
2928 LIST_DEL(&si->applet.ctx.sess.bref.users);
2929 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002930 }
2931
2932 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01002933 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01002934 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002935 struct session *curr_sess;
2936
Willy Tarreau295a8372011-03-10 11:25:07 +01002937 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002938
Willy Tarreau295a8372011-03-10 11:25:07 +01002939 if (si->applet.ctx.sess.target) {
2940 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002941 goto next_sess;
2942
Willy Tarreau295a8372011-03-10 11:25:07 +01002943 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002944 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002945 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002946 return 0;
2947
2948 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01002949 LIST_DEL(&si->applet.ctx.sess.bref.users);
2950 LIST_INIT(&si->applet.ctx.sess.bref.users);
2951 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002952 break;
2953 }
2954
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002955 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002956 "%p: proto=%s",
2957 curr_sess,
2958 curr_sess->listener->proto->name);
2959
2960 switch (curr_sess->listener->proto->sock_family) {
2961 case AF_INET:
2962 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002963 (const void *)&((struct sockaddr_in *)&curr_sess->si[0].addr.c.from)->sin_addr,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002964 pn, sizeof(pn));
2965
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002966 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002967 " src=%s:%d fe=%s be=%s srv=%s",
2968 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002969 ntohs(((struct sockaddr_in *)&curr_sess->si[0].addr.c.from)->sin_port),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002970 curr_sess->fe->id,
2971 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01002972 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002973 );
2974 break;
2975 case AF_INET6:
2976 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002977 (const void *)&((struct sockaddr_in6 *)(&curr_sess->si[0].addr.c.from))->sin6_addr,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002978 pn, sizeof(pn));
2979
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002980 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002981 " src=%s:%d fe=%s be=%s srv=%s",
2982 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002983 ntohs(((struct sockaddr_in6 *)&curr_sess->si[0].addr.c.from)->sin6_port),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002984 curr_sess->fe->id,
2985 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01002986 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002987 );
2988
2989 break;
2990 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02002991 chunk_printf(&msg,
2992 " src=unix:%d fe=%s be=%s srv=%s",
2993 curr_sess->listener->luid,
2994 curr_sess->fe->id,
2995 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01002996 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02002997 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002998 break;
2999 }
3000
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003001 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003002 " ts=%02x age=%s calls=%d",
3003 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003004 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3005 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003006
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003007 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003008 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
3009 curr_sess->req->flags,
3010 curr_sess->req->l,
3011 curr_sess->req->analysers,
3012 curr_sess->req->rex ?
3013 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3014 TICKS_TO_MS(1000)) : "");
3015
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003016 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003017 ",wx=%s",
3018 curr_sess->req->wex ?
3019 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3020 TICKS_TO_MS(1000)) : "");
3021
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003022 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003023 ",ax=%s]",
3024 curr_sess->req->analyse_exp ?
3025 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3026 TICKS_TO_MS(1000)) : "");
3027
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003028 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003029 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
3030 curr_sess->rep->flags,
3031 curr_sess->rep->l,
3032 curr_sess->rep->analysers,
3033 curr_sess->rep->rex ?
3034 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3035 TICKS_TO_MS(1000)) : "");
3036
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003037 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003038 ",wx=%s",
3039 curr_sess->rep->wex ?
3040 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3041 TICKS_TO_MS(1000)) : "");
3042
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003043 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003044 ",ax=%s]",
3045 curr_sess->rep->analyse_exp ?
3046 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3047 TICKS_TO_MS(1000)) : "");
3048
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003049 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003050 " s0=[%d,%1xh,fd=%d,ex=%s]",
3051 curr_sess->si[0].state,
3052 curr_sess->si[0].flags,
3053 curr_sess->si[0].fd,
3054 curr_sess->si[0].exp ?
3055 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3056 TICKS_TO_MS(1000)) : "");
3057
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003058 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003059 " s1=[%d,%1xh,fd=%d,ex=%s]",
3060 curr_sess->si[1].state,
3061 curr_sess->si[1].flags,
3062 curr_sess->si[1].fd,
3063 curr_sess->si[1].exp ?
3064 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3065 TICKS_TO_MS(1000)) : "");
3066
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003067 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003068 " exp=%s",
3069 curr_sess->task->expire ?
3070 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3071 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003072 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003073 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003074
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003075 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003076
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003077 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003078 /* let's try again later from this session. We add ourselves into
3079 * this session's users so that it can remove us upon termination.
3080 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003081 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003082 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003083 }
3084
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003085 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003086 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003087 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003088
Willy Tarreau295a8372011-03-10 11:25:07 +01003089 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003090 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003091 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003092 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3093 else
3094 chunk_printf(&msg, "Session not found.\n");
3095
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003096 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003097 return 0;
3098
Willy Tarreau295a8372011-03-10 11:25:07 +01003099 si->applet.ctx.sess.target = NULL;
3100 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003101 return 1;
3102 }
3103
Willy Tarreau295a8372011-03-10 11:25:07 +01003104 si->applet.state = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003105 /* fall through */
3106
3107 default:
Willy Tarreau295a8372011-03-10 11:25:07 +01003108 si->applet.state = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003109 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003110 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003111}
3112
Emeric Brun1e029aa2010-09-23 18:12:53 +02003113
3114/* print a string of text buffer to <out>. The format is :
3115 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
3116 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
3117 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
3118 */
3119
3120static int dump_text(struct chunk *out, const char *buf, int bsize)
3121{
3122 unsigned char c;
3123 int ptr = 0;
3124
3125 while (buf[ptr] && ptr < bsize) {
3126 c = buf[ptr];
3127 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
3128 if (out->len > out->size - 1)
3129 break;
3130 out->str[out->len++] = c;
3131 }
3132 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
3133 if (out->len > out->size - 2)
3134 break;
3135 out->str[out->len++] = '\\';
3136 switch (c) {
3137 case ' ': c = ' '; break;
3138 case '\t': c = 't'; break;
3139 case '\n': c = 'n'; break;
3140 case '\r': c = 'r'; break;
3141 case '\e': c = 'e'; break;
3142 case '\\': c = '\\'; break;
3143 }
3144 out->str[out->len++] = c;
3145 }
3146 else {
3147 if (out->len > out->size - 4)
3148 break;
3149 out->str[out->len++] = '\\';
3150 out->str[out->len++] = 'x';
3151 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3152 out->str[out->len++] = hextab[c & 0xF];
3153 }
3154 ptr++;
3155 }
3156
3157 return ptr;
3158}
3159
3160/* print a buffer in hexa.
3161 * Print stopped if <bsize> is reached, or if no more place in the chunk.
3162 */
3163
3164static int dump_binary(struct chunk *out, const char *buf, int bsize)
3165{
3166 unsigned char c;
3167 int ptr = 0;
3168
3169 while (ptr < bsize) {
3170 c = buf[ptr];
3171
3172 if (out->len > out->size - 2)
3173 break;
3174 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3175 out->str[out->len++] = hextab[c & 0xF];
3176
3177 ptr++;
3178 }
3179 return ptr;
3180}
3181
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003182/* This function dumps all tables' states onto the stream intreface's
3183 * read buffer. The data_ctx must have been zeroed first, and the flags
3184 * properly set. It returns 0 if the output buffer is full and it needs
3185 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003186 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003187int stats_dump_table_to_buffer(struct stream_interface *si)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003188{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003189 struct session *s = si->applet.private;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003190 struct chunk msg;
3191 struct ebmb_node *eb;
3192 int dt;
3193
3194 /*
Willy Tarreau295a8372011-03-10 11:25:07 +01003195 * We have 3 possible states in si->applet.state :
3196 * - STAT_ST_INIT : the first call
3197 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003198 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003199 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003200 * and the entry pointer points to the next entry to be dumped,
3201 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003202 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003203 * data though.
3204 */
3205
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003206 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003207 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau295a8372011-03-10 11:25:07 +01003208 if (si->applet.state == STAT_ST_LIST) {
3209 si->applet.ctx.table.entry->ref_cnt--;
3210 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003211 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003212 return 1;
3213 }
3214
3215 chunk_init(&msg, trash, sizeof(trash));
3216
Willy Tarreau295a8372011-03-10 11:25:07 +01003217 while (si->applet.state != STAT_ST_FIN) {
3218 switch (si->applet.state) {
3219 case STAT_ST_INIT:
3220 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3221 if (!si->applet.ctx.table.proxy)
3222 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003223
Willy Tarreau295a8372011-03-10 11:25:07 +01003224 si->applet.ctx.table.entry = NULL;
3225 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003226 break;
3227
Willy Tarreau295a8372011-03-10 11:25:07 +01003228 case STAT_ST_INFO:
3229 if (!si->applet.ctx.table.proxy ||
3230 (si->applet.ctx.table.target &&
3231 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
3232 si->applet.state = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003233 break;
3234 }
3235
Willy Tarreau295a8372011-03-10 11:25:07 +01003236 if (si->applet.ctx.table.proxy->table.size) {
Emeric Brun1e029aa2010-09-23 18:12:53 +02003237 chunk_printf(&msg, "# table: %s, type: %s, size:%d, used:%d\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003238 si->applet.ctx.table.proxy->id,
3239 stktable_types[si->applet.ctx.table.proxy->table.type].kw,
3240 si->applet.ctx.table.proxy->table.size,
3241 si->applet.ctx.table.proxy->table.current);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003242
3243 /* any other information should be dumped here */
3244
Willy Tarreau295a8372011-03-10 11:25:07 +01003245 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003246 s->listener->perm.ux.level < ACCESS_LVL_OPER)
3247 chunk_printf(&msg, "# contents not dumped due to insufficient privileges\n");
3248
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003249 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003250 return 0;
3251
Willy Tarreau295a8372011-03-10 11:25:07 +01003252 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003253 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
3254 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003255 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003256 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003257 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3258 si->applet.ctx.table.entry->ref_cnt++;
3259 si->applet.state = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003260 break;
3261 }
3262 }
3263 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003264 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003265 break;
3266
Willy Tarreau295a8372011-03-10 11:25:07 +01003267 case STAT_ST_LIST:
3268 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003269 /* we're filtering on some data contents */
3270 void *ptr;
3271 long long data;
3272
Willy Tarreau295a8372011-03-10 11:25:07 +01003273 dt = si->applet.ctx.table.data_type;
3274 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3275 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003276 dt);
3277
3278 data = 0;
3279 switch (stktable_data_types[dt].std_type) {
3280 case STD_T_SINT:
3281 data = stktable_data_cast(ptr, std_t_sint);
3282 break;
3283 case STD_T_UINT:
3284 data = stktable_data_cast(ptr, std_t_uint);
3285 break;
3286 case STD_T_ULL:
3287 data = stktable_data_cast(ptr, std_t_ull);
3288 break;
3289 case STD_T_FRQP:
3290 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003291 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003292 break;
3293 }
3294
3295 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003296 if ((data < si->applet.ctx.table.value &&
3297 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3298 si->applet.ctx.table.data_op == STD_OP_GT ||
3299 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3300 (data == si->applet.ctx.table.value &&
3301 (si->applet.ctx.table.data_op == STD_OP_NE ||
3302 si->applet.ctx.table.data_op == STD_OP_GT ||
3303 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3304 (data > si->applet.ctx.table.value &&
3305 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3306 si->applet.ctx.table.data_op == STD_OP_LT ||
3307 si->applet.ctx.table.data_op == STD_OP_LE)))
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003308 goto skip_entry;
3309 }
3310
Willy Tarreau295a8372011-03-10 11:25:07 +01003311 chunk_printf(&msg, "%p:", si->applet.ctx.table.entry);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003312
Willy Tarreau295a8372011-03-10 11:25:07 +01003313 if (si->applet.ctx.table.proxy->table.type == STKTABLE_TYPE_IP) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003314 char addr[16];
3315 inet_ntop(AF_INET,
Willy Tarreau295a8372011-03-10 11:25:07 +01003316 (const void *)&si->applet.ctx.table.entry->key.key,
Willy Tarreau69f58c82010-07-12 17:55:33 +02003317 addr, sizeof(addr));
3318 chunk_printf(&msg, " key=%s", addr);
3319 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003320 else if (si->applet.ctx.table.proxy->table.type == STKTABLE_TYPE_INTEGER) {
3321 chunk_printf(&msg, " key=%u", *(unsigned int *)si->applet.ctx.table.entry->key.key);
Emeric Brun1e029aa2010-09-23 18:12:53 +02003322 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003323 else if (si->applet.ctx.table.proxy->table.type == STKTABLE_TYPE_STRING) {
Emeric Brun1e029aa2010-09-23 18:12:53 +02003324 chunk_printf(&msg, " key=");
Willy Tarreau295a8372011-03-10 11:25:07 +01003325 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 +02003326 }
3327 else {
3328 chunk_printf(&msg, " key=");
Willy Tarreau295a8372011-03-10 11:25:07 +01003329 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 +02003330 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003331
3332 chunk_printf(&msg, " use=%d exp=%d",
Willy Tarreau295a8372011-03-10 11:25:07 +01003333 si->applet.ctx.table.entry->ref_cnt - 1,
3334 tick_remain(now_ms, si->applet.ctx.table.entry->expire));
Willy Tarreau69f58c82010-07-12 17:55:33 +02003335
3336 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
3337 void *ptr;
3338
Willy Tarreau295a8372011-03-10 11:25:07 +01003339 if (si->applet.ctx.table.proxy->table.data_ofs[dt] == 0)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003340 continue;
3341 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
3342 chunk_printf(&msg, " %s(%d)=",
3343 stktable_data_types[dt].name,
Willy Tarreau295a8372011-03-10 11:25:07 +01003344 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003345 else
3346 chunk_printf(&msg, " %s=", stktable_data_types[dt].name);
3347
Willy Tarreau295a8372011-03-10 11:25:07 +01003348 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3349 si->applet.ctx.table.entry,
Willy Tarreau69f58c82010-07-12 17:55:33 +02003350 dt);
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02003351 switch (stktable_data_types[dt].std_type) {
3352 case STD_T_SINT:
3353 chunk_printf(&msg, "%d", stktable_data_cast(ptr, std_t_sint));
Willy Tarreau69f58c82010-07-12 17:55:33 +02003354 break;
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02003355 case STD_T_UINT:
3356 chunk_printf(&msg, "%u", stktable_data_cast(ptr, std_t_uint));
3357 break;
3358 case STD_T_ULL:
3359 chunk_printf(&msg, "%lld", stktable_data_cast(ptr, std_t_ull));
3360 break;
3361 case STD_T_FRQP:
Willy Tarreau69f58c82010-07-12 17:55:33 +02003362 chunk_printf(&msg, "%d",
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02003363 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003364 si->applet.ctx.table.proxy->table.data_arg[dt].u));
Willy Tarreau69f58c82010-07-12 17:55:33 +02003365 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003366 }
3367 }
3368 chunk_printf(&msg, "\n");
3369
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003370 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003371 return 0;
3372
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003373 skip_entry:
Willy Tarreau295a8372011-03-10 11:25:07 +01003374 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003375
Willy Tarreau295a8372011-03-10 11:25:07 +01003376 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003377 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003378 struct stksess *old = si->applet.ctx.table.entry;
3379 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3380 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3381 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003382 break;
3383 }
3384
Willy Tarreau295a8372011-03-10 11:25:07 +01003385 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3386 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
3387 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003388 break;
3389
Willy Tarreau295a8372011-03-10 11:25:07 +01003390 case STAT_ST_END:
3391 si->applet.state = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003392 break;
3393 }
3394 }
3395 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003396}
3397
Willy Tarreaud426a182010-03-05 14:58:26 +01003398/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003399 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3400 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3401 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3402 * lines are respected within the limit of 70 output chars. Lines that are
3403 * continuation of a previous truncated line begin with "+" instead of " "
3404 * after the offset. The new pointer is returned.
3405 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003406static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3407 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003408{
3409 int end;
3410 unsigned char c;
3411
3412 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003413 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003414 return ptr;
3415
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003416 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003417
Willy Tarreaud426a182010-03-05 14:58:26 +01003418 while (ptr < len && ptr < bsize) {
3419 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003420 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003421 if (out->len > end - 2)
3422 break;
3423 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003424 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003425 if (out->len > end - 3)
3426 break;
3427 out->str[out->len++] = '\\';
3428 switch (c) {
3429 case '\t': c = 't'; break;
3430 case '\n': c = 'n'; break;
3431 case '\r': c = 'r'; break;
3432 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003433 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003434 }
3435 out->str[out->len++] = c;
3436 } else {
3437 if (out->len > end - 5)
3438 break;
3439 out->str[out->len++] = '\\';
3440 out->str[out->len++] = 'x';
3441 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3442 out->str[out->len++] = hextab[c & 0xF];
3443 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003444 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003445 /* we had a line break, let's return now */
3446 out->str[out->len++] = '\n';
3447 *line = ptr;
3448 return ptr;
3449 }
3450 }
3451 /* we have an incomplete line, we return it as-is */
3452 out->str[out->len++] = '\n';
3453 return ptr;
3454}
3455
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003456/* This function dumps all captured errors onto the stream intreface's
3457 * read buffer. The data_ctx must have been zeroed first, and the flags
3458 * properly set. It returns 0 if the output buffer is full and it needs
3459 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003460 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003461int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003462{
3463 extern const char *monthname[12];
3464 struct chunk msg;
3465
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003466 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003467 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003468
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003469 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003470
Willy Tarreau295a8372011-03-10 11:25:07 +01003471 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003472 /* the function had not been called yet, let's prepare the
3473 * buffer for a response.
3474 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003475 struct tm tm;
3476
3477 get_localtime(date.tv_sec, &tm);
3478 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3479 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3480 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3481 error_snapshot_id);
3482
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003483 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003484 /* Socket buffer full. Let's try again later from the same point */
3485 return 0;
3486 }
3487
Willy Tarreau295a8372011-03-10 11:25:07 +01003488 si->applet.ctx.errors.px = proxy;
3489 si->applet.ctx.errors.buf = 0;
3490 si->applet.ctx.errors.bol = 0;
3491 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003492 }
3493
3494 /* we have two inner loops here, one for the proxy, the other one for
3495 * the buffer.
3496 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003497 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003498 struct error_snapshot *es;
3499
Willy Tarreau295a8372011-03-10 11:25:07 +01003500 if (si->applet.ctx.errors.buf == 0)
3501 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003502 else
Willy Tarreau295a8372011-03-10 11:25:07 +01003503 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003504
3505 if (!es->when.tv_sec)
3506 goto next;
3507
Willy Tarreau295a8372011-03-10 11:25:07 +01003508 if (si->applet.ctx.errors.iid >= 0 &&
3509 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
3510 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003511 goto next;
3512
Willy Tarreau295a8372011-03-10 11:25:07 +01003513 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003514 /* just print headers now */
3515
3516 char pn[INET6_ADDRSTRLEN];
3517 struct tm tm;
3518
3519 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003520 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003521 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003522 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003523
3524
3525 if (es->src.ss_family == AF_INET)
3526 inet_ntop(AF_INET,
3527 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
3528 pn, sizeof(pn));
3529 else
3530 inet_ntop(AF_INET6,
3531 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
3532 pn, sizeof(pn));
3533
Willy Tarreau295a8372011-03-10 11:25:07 +01003534 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003535 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003536 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003537 " frontend %s (#%d): invalid request\n"
3538 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003539 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003540 " request length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003541 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau3fd6cec2010-03-25 06:45:07 +01003542 pn, es->sid, (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3543 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003544 es->srv ? es->srv->id : "<NONE>",
3545 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003546 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003547 es->len, es->pos);
3548 break;
3549 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003550 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003551 " backend %s (#%d) : invalid response\n"
3552 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003553 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003554 " response length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003555 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003556 pn, es->sid, es->oe->id, es->oe->uuid,
3557 es->srv ? es->srv->id : "<NONE>",
3558 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003559 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003560 es->len, es->pos);
3561 break;
3562 }
3563
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003564 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003565 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003566 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003567 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003568 si->applet.ctx.errors.ptr = 0;
3569 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003570 }
3571
Willy Tarreau295a8372011-03-10 11:25:07 +01003572 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003573 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003574 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003575 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003576 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau61b34732009-10-03 23:49:35 +02003577 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003578 goto next;
3579 }
3580
3581 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01003582 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003583 int newptr;
3584 int newline;
3585
Willy Tarreau295a8372011-03-10 11:25:07 +01003586 newline = si->applet.ctx.errors.bol;
3587 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
3588 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02003589 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003590
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003591 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003592 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003593 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003594 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003595 si->applet.ctx.errors.ptr = newptr;
3596 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003597 };
3598 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01003599 si->applet.ctx.errors.bol = 0;
3600 si->applet.ctx.errors.ptr = -1;
3601 si->applet.ctx.errors.buf++;
3602 if (si->applet.ctx.errors.buf > 1) {
3603 si->applet.ctx.errors.buf = 0;
3604 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003605 }
3606 }
3607
3608 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02003609 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003610}
3611
Willy Tarreaub24281b2011-02-13 13:16:36 +01003612struct si_applet http_stats_applet = {
3613 .name = "<STATS>", /* used for logging */
3614 .fct = http_stats_io_handler,
3615};
3616
3617struct si_applet cli_applet = {
3618 .name = "<CLI>", /* used for logging */
3619 .fct = cli_io_handler,
3620};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003621
Willy Tarreau10522fd2008-07-09 20:12:41 +02003622static struct cfg_kw_list cfg_kws = {{ },{
3623 { CFG_GLOBAL, "stats", stats_parse_global },
3624 { 0, NULL, NULL },
3625}};
3626
3627__attribute__((constructor))
3628static void __dumpstats_module_init(void)
3629{
3630 cfg_register_keywords(&cfg_kws);
3631}
3632
Willy Tarreau91861262007-10-17 17:06:05 +02003633/*
3634 * Local variables:
3635 * c-indent-level: 8
3636 * c-basic-offset: 8
3637 * End:
3638 */