blob: 6c12a1800cb4917902cb9b5a9c9e1c367fa2fdab [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 Tarreau7b7a8e92011-03-27 19:53:06 +0200110 copy_target(&s->target, &s->si[1].target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +0100111 s->si[1].applet.private = s;
112 s->si[1].applet.st1 = 0;
113 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200114
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200115 tv_zero(&s->logs.tv_request);
116 s->logs.t_queue = 0;
117 s->logs.t_connect = 0;
118 s->logs.t_data = 0;
119 s->logs.t_close = 0;
120 s->logs.bytes_in = s->logs.bytes_out = 0;
121 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
122 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200123
Willy Tarreaueb472682010-05-28 18:46:57 +0200124 s->req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
125
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200126 if (s->listener->timeout) {
127 s->req->rto = *s->listener->timeout;
128 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200129 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200130 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200131}
132
Willy Tarreau07e9e642010-08-17 21:48:17 +0200133/* allocate a new stats frontend named <name>, and return it
134 * (or NULL in case of lack of memory).
135 */
136static struct proxy *alloc_stats_fe(const char *name)
137{
138 struct proxy *fe;
139
140 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
141 if (!fe)
142 return NULL;
143
144 LIST_INIT(&fe->pendconns);
145 LIST_INIT(&fe->acl);
146 LIST_INIT(&fe->block_cond);
147 LIST_INIT(&fe->redirect_rules);
148 LIST_INIT(&fe->mon_fail_cond);
149 LIST_INIT(&fe->switching_rules);
150 LIST_INIT(&fe->tcp_req.inspect_rules);
151
152 /* Timeouts are defined as -1, so we cannot use the zeroed area
153 * as a default value.
154 */
155 proxy_reset_timeouts(fe);
156
157 fe->last_change = now.tv_sec;
158 fe->id = strdup("GLOBAL");
159 fe->cap = PR_CAP_FE;
160 return fe;
161}
162
Willy Tarreaufbee7132007-10-18 13:53:22 +0200163/* This function parses a "stats" statement in the "global" section. It returns
164 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
165 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
166 * zero included. The trailing '\n' must not be written. The function must be
167 * called with <args> pointing to the first word after "stats".
168 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200169static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
170 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200171{
Willy Tarreau10522fd2008-07-09 20:12:41 +0200172 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200173 if (!strcmp(args[0], "socket")) {
Andreas Kohn16171e22011-01-19 20:29:32 +0100174 struct sockaddr_un *su;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200175 int cur_arg;
176
177 if (*args[1] == 0) {
178 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
179 return -1;
180 }
181
182 if (global.stats_sock.state != LI_NEW) {
183 snprintf(err, errlen, "'stats socket' already specified in global section");
184 return -1;
185 }
186
Andreas Kohn16171e22011-01-19 20:29:32 +0100187 su = str2sun(args[1]);
188 if (!su) {
189 snprintf(err, errlen, "'stats socket' path would require truncation");
190 return -1;
191 }
192 memcpy(&global.stats_sock.addr, su, sizeof(struct sockaddr_un)); // guaranteed to fit
Willy Tarreaufbee7132007-10-18 13:53:22 +0200193
Willy Tarreau89a63132009-08-16 17:41:45 +0200194 if (!global.stats_fe) {
Willy Tarreau07e9e642010-08-17 21:48:17 +0200195 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau89a63132009-08-16 17:41:45 +0200196 snprintf(err, errlen, "out of memory");
197 return -1;
198 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200199 global.stats_fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreau89a63132009-08-16 17:41:45 +0200200 }
201
Willy Tarreaufbee7132007-10-18 13:53:22 +0200202 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100203 global.stats_sock.options = LI_O_NONE;
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200204 global.stats_sock.accept = session_accept;
205 global.stats_fe->accept = stats_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200206 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200207 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200208 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreaueb472682010-05-28 18:46:57 +0200209 global.stats_sock.frontend = global.stats_fe;
Willy Tarreau6162db22009-10-10 17:13:00 +0200210 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau07e9e642010-08-17 21:48:17 +0200211 global.stats_fe->maxconn = global.stats_sock.maxconn;
Willy Tarreau89a63132009-08-16 17:41:45 +0200212 global.stats_sock.timeout = &global.stats_fe->timeout.client;
213
214 global.stats_sock.next = global.stats_fe->listen;
215 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200216
217 cur_arg = 2;
218 while (*args[cur_arg]) {
219 if (!strcmp(args[cur_arg], "uid")) {
220 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
221 cur_arg += 2;
222 }
223 else if (!strcmp(args[cur_arg], "gid")) {
224 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
225 cur_arg += 2;
226 }
227 else if (!strcmp(args[cur_arg], "mode")) {
228 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
229 cur_arg += 2;
230 }
231 else if (!strcmp(args[cur_arg], "user")) {
232 struct passwd *user;
233 user = getpwnam(args[cur_arg + 1]);
234 if (!user) {
235 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
236 args[cur_arg + 1]);
237 return -1;
238 }
239 global.stats_sock.perm.ux.uid = user->pw_uid;
240 cur_arg += 2;
241 }
242 else if (!strcmp(args[cur_arg], "group")) {
243 struct group *group;
244 group = getgrnam(args[cur_arg + 1]);
245 if (!group) {
246 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
247 args[cur_arg + 1]);
248 return -1;
249 }
250 global.stats_sock.perm.ux.gid = group->gr_gid;
251 cur_arg += 2;
252 }
Willy Tarreau6162db22009-10-10 17:13:00 +0200253 else if (!strcmp(args[cur_arg], "level")) {
254 if (!strcmp(args[cur_arg+1], "user"))
255 global.stats_sock.perm.ux.level = ACCESS_LVL_USER;
256 else if (!strcmp(args[cur_arg+1], "operator"))
257 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER;
258 else if (!strcmp(args[cur_arg+1], "admin"))
259 global.stats_sock.perm.ux.level = ACCESS_LVL_ADMIN;
260 else {
261 snprintf(err, errlen, "'stats socket level' only supports 'user', 'operator', and 'admin'");
262 return -1;
263 }
264 cur_arg += 2;
265 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200266 else {
Willy Tarreau6162db22009-10-10 17:13:00 +0200267 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', 'level', and 'mode'");
Willy Tarreaufbee7132007-10-18 13:53:22 +0200268 return -1;
269 }
270 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100271
Willy Tarreaufbee7132007-10-18 13:53:22 +0200272 uxst_add_listener(&global.stats_sock);
273 global.maxsock++;
274 }
275 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100276 unsigned timeout;
277 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
278
279 if (res) {
280 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
281 return -1;
282 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200283
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100284 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200285 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
286 return -1;
287 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200288 if (!global.stats_fe) {
289 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
290 snprintf(err, errlen, "out of memory");
291 return -1;
292 }
293 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200294 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200295 }
296 else if (!strcmp(args[0], "maxconn")) {
297 int maxconn = atol(args[1]);
298
299 if (maxconn <= 0) {
300 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
301 return -1;
302 }
303 global.maxsock -= global.stats_sock.maxconn;
304 global.stats_sock.maxconn = maxconn;
305 global.maxsock += global.stats_sock.maxconn;
Willy Tarreaueb472682010-05-28 18:46:57 +0200306 if (global.stats_fe)
307 global.stats_fe->maxconn = global.stats_sock.maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200308 }
309 else {
310 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
311 return -1;
312 }
313 return 0;
314}
315
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200316int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100317{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200318 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100319 "# pxname,svname,"
320 "qcur,qmax,"
321 "scur,smax,slim,stot,"
322 "bin,bout,"
323 "dreq,dresp,"
324 "ereq,econ,eresp,"
325 "wretr,wredis,"
326 "status,weight,act,bck,"
327 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200328 "pid,iid,sid,throttle,lbtot,tracked,type,"
329 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200330 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100331 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100332 "req_rate,req_rate_max,req_tot,"
333 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100334 "\n");
335}
336
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200337/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200338 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100339 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200340 * designating the function which will have to process the request, which can
341 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200342 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200343int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200344{
Willy Tarreaubc4af052011-02-13 13:25:14 +0100345 struct session *s = si->applet.private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200346 char *args[MAX_STATS_ARGS + 1];
347 int arg;
348
349 while (isspace((unsigned char)*line))
350 line++;
351
352 arg = 0;
353 args[arg] = line;
354
355 while (*line && arg < MAX_STATS_ARGS) {
356 if (isspace((unsigned char)*line)) {
357 *line++ = '\0';
358
359 while (isspace((unsigned char)*line))
360 line++;
361
362 args[++arg] = line;
363 continue;
364 }
365
366 line++;
367 }
368
369 while (++arg <= MAX_STATS_ARGS)
370 args[arg] = line;
371
Willy Tarreau295a8372011-03-10 11:25:07 +0100372 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200373 if (strcmp(args[0], "show") == 0) {
374 if (strcmp(args[1], "stat") == 0) {
375 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100376 si->applet.ctx.stats.flags |= STAT_BOUND;
377 si->applet.ctx.stats.iid = atoi(args[2]);
378 si->applet.ctx.stats.type = atoi(args[3]);
379 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200380 }
381
Willy Tarreau295a8372011-03-10 11:25:07 +0100382 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
383 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
384 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100385 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200386 }
387 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100388 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
389 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
390 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100391 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200392 }
393 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100394 si->applet.state = STAT_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200395 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100396 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100397 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200398 return 1;
399 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100400 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100401 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100402 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100403 si->applet.ctx.sess.target = NULL;
404 si->applet.ctx.sess.section = 0; /* start with session status */
405 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100406 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200407 }
408 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200409 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100410 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100411 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200412 return 1;
413 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200414 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100415 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200416 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100417 si->applet.ctx.errors.iid = -1;
418 si->applet.ctx.errors.px = NULL;
419 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100420 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200421 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200422 else if (strcmp(args[1], "table") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100423 si->applet.state = STAT_ST_INIT;
Willy Tarreau603861e2010-07-18 08:31:59 +0200424 if (*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100425 si->applet.ctx.table.target = find_stktable(args[2]);
426 if (!si->applet.ctx.table.target) {
427 si->applet.ctx.cli.msg = "No such table\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100428 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau603861e2010-07-18 08:31:59 +0200429 return 1;
430 }
431 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200432 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100433 si->applet.ctx.table.target = NULL;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200434
Willy Tarreau295a8372011-03-10 11:25:07 +0100435 si->applet.ctx.table.data_type = -1;
436 if (si->applet.ctx.table.target && strncmp(args[3], "data.", 5) == 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200437 /* condition on stored data value */
Willy Tarreau295a8372011-03-10 11:25:07 +0100438 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
439 if (si->applet.ctx.table.data_type < 0) {
440 si->applet.ctx.cli.msg = "Unknown data type\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100441 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200442 return 1;
443 }
444
Willy Tarreau295a8372011-03-10 11:25:07 +0100445 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
446 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100447 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200448 return 1;
449 }
450
Willy Tarreau295a8372011-03-10 11:25:07 +0100451 si->applet.ctx.table.data_op = get_std_op(args[4]);
452 if (si->applet.ctx.table.data_op < 0) {
453 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100454 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200455 return 1;
456 }
457
Willy Tarreau295a8372011-03-10 11:25:07 +0100458 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
459 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100460 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200461 return 1;
462 }
463 }
464 else if (*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100465 si->applet.ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value>\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100466 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200467 return 1;
468 }
469
Willy Tarreau295a8372011-03-10 11:25:07 +0100470 si->applet.ctx.table.proxy = NULL;
471 si->applet.ctx.table.entry = NULL;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100472 si->applet.st0 = STAT_CLI_O_TAB; // stats_dump_table_to_buffer
Willy Tarreau69f58c82010-07-12 17:55:33 +0200473 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200474 else { /* neither "stat" nor "info" nor "sess" nor "errors" no "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200475 return 0;
476 }
477 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200478 else if (strcmp(args[0], "clear") == 0) {
479 if (strcmp(args[1], "counters") == 0) {
480 struct proxy *px;
481 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200482 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200483 int clrall = 0;
484
485 if (strcmp(args[2], "all") == 0)
486 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200487
Willy Tarreau6162db22009-10-10 17:13:00 +0200488 /* check permissions */
489 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
490 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100491 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100492 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200493 return 1;
494 }
495
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200496 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100497 if (clrall) {
498 memset(&px->be_counters, 0, sizeof(px->be_counters));
499 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
500 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200501 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100502 px->be_counters.conn_max = 0;
503 px->be_counters.p.http.rps_max = 0;
504 px->be_counters.sps_max = 0;
505 px->be_counters.cps_max = 0;
506 px->be_counters.nbpend_max = 0;
507
508 px->fe_counters.conn_max = 0;
509 px->fe_counters.p.http.rps_max = 0;
510 px->fe_counters.sps_max = 0;
511 px->fe_counters.cps_max = 0;
512 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200513 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200514
515 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200516 if (clrall)
517 memset(&sv->counters, 0, sizeof(sv->counters));
518 else {
519 sv->counters.cur_sess_max = 0;
520 sv->counters.nbpend_max = 0;
521 sv->counters.sps_max = 0;
522 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200523
524 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200525 if (li->counters) {
526 if (clrall)
527 memset(li->counters, 0, sizeof(*li->counters));
528 else
529 li->counters->conn_max = 0;
530 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200531 }
532
533 return 1;
534 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200535 else if (strcmp(args[1], "table") == 0) {
536 struct proxy *px;
537 struct stksess *ts;
538 unsigned int ip_key;
539
540 if (!*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100541 si->applet.ctx.cli.msg = "\"table\" argument expected\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100542 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200543 return 1;
544 }
545
546 px = find_stktable(args[2]);
547
548 if (!px) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100549 si->applet.ctx.cli.msg = "No such table\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100550 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200551 return 1;
552 }
553
554 if (strcmp(args[3], "key") != 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100555 si->applet.ctx.cli.msg = "\"key\" argument expected\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100556 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200557 return 1;
558 }
559
560 if (!*args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100561 si->applet.ctx.cli.msg = "Key value expected\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100562 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200563 return 1;
564 }
565
566 if (px->table.type == STKTABLE_TYPE_IP) {
567 ip_key = htonl(inetaddr_host(args[4]));
568 static_table_key.key = (void *)&ip_key;
569 }
570 else {
Willy Tarreau295a8372011-03-10 11:25:07 +0100571 si->applet.ctx.cli.msg = "Removing keys from non-ip tables is not supported\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100572 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200573 return 1;
574 }
575
576 /* check permissions */
577 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100578 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100579 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200580 return 1;
581 }
582
583 ts = stktable_lookup_key(&px->table, &static_table_key);
584 if (!ts) {
585 /* silent return, entry was already removed */
586 return 1;
587 }
588 else if (ts->ref_cnt) {
589 /* don't delete an entry which is currently referenced */
Willy Tarreau295a8372011-03-10 11:25:07 +0100590 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100591 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200592 return 1;
593 }
594
Willy Tarreaudf39e952010-08-10 11:11:40 +0200595 stksess_kill(&px->table, ts);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200596 /* end of processing */
597 return 1;
598 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200599 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200600 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200601 return 0;
602 }
603 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200604 else if (strcmp(args[0], "get") == 0) {
605 if (strcmp(args[1], "weight") == 0) {
606 struct proxy *px;
607 struct server *sv;
608
609 /* split "backend/server" and make <line> point to server */
610 for (line = args[2]; *line; line++)
611 if (*line == '/') {
612 *line++ = '\0';
613 break;
614 }
615
616 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100617 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100618 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200619 return 1;
620 }
621
622 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100623 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100624 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200625 return 1;
626 }
627
628 /* return server's effective weight at the moment */
629 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
630 buffer_feed(si->ib, trash);
631 return 1;
632 }
633 else { /* not "get weight" */
634 return 0;
635 }
636 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200637 else if (strcmp(args[0], "set") == 0) {
638 if (strcmp(args[1], "weight") == 0) {
639 struct proxy *px;
640 struct server *sv;
641 int w;
642
643 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100644 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100645 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200646 return 1;
647 }
648
649 /* split "backend/server" and make <line> point to server */
650 for (line = args[2]; *line; line++)
651 if (*line == '/') {
652 *line++ = '\0';
653 break;
654 }
655
656 if (!*line || !*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100657 si->applet.ctx.cli.msg = "Require 'backend/server' and 'weight' or 'weight%'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100658 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200659 return 1;
660 }
661
662 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100663 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100664 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200665 return 1;
666 }
667
Cyril Bonté613f0df2011-03-03 20:49:04 +0100668 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100669 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100670 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100671 return 1;
672 }
673
Willy Tarreau4483d432009-10-10 19:30:08 +0200674 /* if the weight is terminated with '%', it is set relative to
675 * the initial weight, otherwise it is absolute.
676 */
677 w = atoi(args[3]);
678 if (strchr(args[3], '%') != NULL) {
679 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100680 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100681 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200682 return 1;
683 }
684 w = sv->iweight * w / 100;
685 }
686 else {
687 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100688 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100689 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200690 return 1;
691 }
692 }
693
694 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100695 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 +0100696 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200697 return 1;
698 }
699
700 sv->uweight = w;
701
702 if (px->lbprm.algo & BE_LB_PROP_DYN) {
703 /* we must take care of not pushing the server to full throttle during slow starts */
704 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
705 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
706 else
707 sv->eweight = BE_WEIGHT_SCALE;
708 sv->eweight *= sv->uweight;
709 } else {
710 sv->eweight = sv->uweight;
711 }
712
713 /* static LB algorithms are a bit harder to update */
714 if (px->lbprm.update_server_eweight)
715 px->lbprm.update_server_eweight(sv);
716 else if (sv->eweight)
717 px->lbprm.set_server_status_up(sv);
718 else
719 px->lbprm.set_server_status_down(sv);
720
721 return 1;
722 }
Willy Tarreau7aabd112010-01-26 10:59:06 +0100723 else if (strcmp(args[1], "timeout") == 0) {
724 if (strcmp(args[2], "cli") == 0) {
725 unsigned timeout;
726 const char *res;
727
728 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100729 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100730 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100731 return 1;
732 }
733
734 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
735 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100736 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100737 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100738 return 1;
739 }
740
741 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
742 return 1;
743 }
744 else {
Willy Tarreau295a8372011-03-10 11:25:07 +0100745 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100746 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100747 return 1;
748 }
749 }
750 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +0200751 return 0;
752 }
753 }
Cyril Bontécd19e512010-01-31 22:34:03 +0100754 else if (strcmp(args[0], "enable") == 0) {
755 if (strcmp(args[1], "server") == 0) {
756 struct proxy *px;
757 struct server *sv;
758
759 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100760 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100761 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100762 return 1;
763 }
764
765 /* split "backend/server" and make <line> point to server */
766 for (line = args[2]; *line; line++)
767 if (*line == '/') {
768 *line++ = '\0';
769 break;
770 }
771
772 if (!*line || !*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100773 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100774 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100775 return 1;
776 }
777
778 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100779 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100780 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100781 return 1;
782 }
783
Cyril Bonté613f0df2011-03-03 20:49:04 +0100784 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100785 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100786 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100787 return 1;
788 }
789
Cyril Bontécd19e512010-01-31 22:34:03 +0100790 if (sv->state & SRV_MAINTAIN) {
791 /* The server is really in maintenance, we can change the server state */
792 if (sv->tracked) {
793 /* If this server tracks the status of another one,
794 * we must restore the good status.
795 */
796 if (sv->tracked->state & SRV_RUNNING) {
797 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +0200798 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +0100799 } else {
800 sv->state &= ~SRV_MAINTAIN;
801 set_server_down(sv);
802 }
803 } else {
804 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +0200805 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +0100806 }
807 }
808
809 return 1;
810 }
811 else { /* unknown "enable" parameter */
812 return 0;
813 }
814 }
815 else if (strcmp(args[0], "disable") == 0) {
816 if (strcmp(args[1], "server") == 0) {
817 struct proxy *px;
818 struct server *sv;
819
820 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100821 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100822 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100823 return 1;
824 }
825
826 /* split "backend/server" and make <line> point to server */
827 for (line = args[2]; *line; line++)
828 if (*line == '/') {
829 *line++ = '\0';
830 break;
831 }
832
833 if (!*line || !*args[2]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100834 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100835 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100836 return 1;
837 }
838
839 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100840 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100841 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100842 return 1;
843 }
844
Cyril Bonté613f0df2011-03-03 20:49:04 +0100845 if (px->state == PR_STSTOPPED) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100846 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100847 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100848 return 1;
849 }
850
Cyril Bontécd19e512010-01-31 22:34:03 +0100851 if (! (sv->state & SRV_MAINTAIN)) {
852 /* Not already in maintenance, we can change the server state */
853 sv->state |= SRV_MAINTAIN;
854 set_server_down(sv);
855 }
856
857 return 1;
858 }
859 else { /* unknown "disable" parameter */
860 return 0;
861 }
862 }
863 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200864 return 0;
865 }
866 return 1;
867}
868
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200869/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200870 * used to processes I/O from/to the stats unix socket. The system relies on a
871 * state machine handling requests and various responses. We read a request,
872 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +0100873 * Then we can read again. The state is stored in si->applet.st0 and is one of the
874 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200875 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200876 */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100877static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200878{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200879 struct buffer *req = si->ob;
880 struct buffer *res = si->ib;
881 int reql;
882 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200883
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200884 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
885 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200886
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200887 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +0100888 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200889 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +0100890 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +0100891 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200892 }
Willy Tarreaubc4af052011-02-13 13:25:14 +0100893 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200894 /* Let's close for real now. We just close the request
895 * side, the conditions below will complete if needed.
896 */
897 si->shutw(si);
898 break;
899 }
Willy Tarreaubc4af052011-02-13 13:25:14 +0100900 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +0200901 /* ensure we have some output room left in the event we
902 * would want to return some info right after parsing.
903 */
904 if (buffer_almost_full(si->ib))
905 break;
906
Willy Tarreau74b08c92010-09-08 17:04:31 +0200907 reql = buffer_get_line(si->ob, trash, sizeof(trash));
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200908 if (reql <= 0) { /* closed or EOL not found */
909 if (reql == 0)
910 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100911 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200912 continue;
913 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200914
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200915 /* seek for a possible semi-colon. If we find one, we
916 * replace it with an LF and skip only this part.
917 */
918 for (len = 0; len < reql; len++)
919 if (trash[len] == ';') {
920 trash[len] = '\n';
921 reql = len + 1;
922 break;
923 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200924
Willy Tarreau816fc222009-10-04 07:36:58 +0200925 /* now it is time to check that we have a full line,
926 * remove the trailing \n and possibly \r, then cut the
927 * line.
928 */
929 len = reql - 1;
930 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +0100931 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200932 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200933 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200934
Willy Tarreau816fc222009-10-04 07:36:58 +0200935 if (len && trash[len-1] == '\r')
936 len--;
937
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200938 trash[len] = '\0';
939
Willy Tarreaubc4af052011-02-13 13:25:14 +0100940 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200941 if (len) {
942 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +0100943 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200944 continue;
945 }
946 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +0100947 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200948 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200949 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100950 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100951 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200952 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200953 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +0100954 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200955 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200956 }
Willy Tarreaubc4af052011-02-13 13:25:14 +0100957 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200958 /* if prompt is disabled, print help on empty lines,
959 * so that the user at least knows how to enable
960 * prompt and find help.
961 */
Willy Tarreau295a8372011-03-10 11:25:07 +0100962 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100963 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200964 }
965
966 /* re-adjust req buffer */
967 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200968 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200969 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200970 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200971 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +0100972 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200973 continue;
974 }
975
Willy Tarreaubc4af052011-02-13 13:25:14 +0100976 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200977 case STAT_CLI_PRINT:
Willy Tarreau295a8372011-03-10 11:25:07 +0100978 if (buffer_feed(si->ib, si->applet.ctx.cli.msg) < 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +0100979 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200980 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200981 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +0100982 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +0100983 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200984 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200985 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +0100986 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +0100987 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200988 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200989 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +0100990 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +0100991 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200992 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +0200993 case STAT_CLI_O_TAB:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +0100994 if (stats_dump_table_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +0100995 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +0200996 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200997 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +0100998 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200999 break;
1000 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001001
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001002 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001003 if (si->applet.st0 == STAT_CLI_PROMPT) {
1004 if (buffer_feed(si->ib, si->applet.st1 ? "\n> " : "\n") < 0)
1005 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001006 }
1007
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001008 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001009 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001010 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001011
1012 /* Now we close the output if one of the writers did so,
1013 * or if we're not in interactive mode and the request
1014 * buffer is empty. This still allows pipelined requests
1015 * to be sent in non-interactive mode.
1016 */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001017 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->applet.st1 && !req->send_max)) {
1018 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001019 continue;
1020 }
1021
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001022 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001023 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001024 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001025 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001026
Willy Tarreaubc4af052011-02-13 13:25:14 +01001027 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001028 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1029 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1030 /* Other size has closed, let's abort if we have no more processing to do
1031 * and nothing more to consume. This is comparable to a broken pipe, so
1032 * we forward the close to the request side so that it flows upstream to
1033 * the client.
1034 */
1035 si->shutw(si);
1036 }
1037
Willy Tarreaubc4af052011-02-13 13:25:14 +01001038 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001039 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1040 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1041 /* We have no more processing to do, and nothing more to send, and
1042 * the client side has closed. So we'll forward this state downstream
1043 * on the response buffer.
1044 */
1045 si->shutr(si);
1046 res->flags |= BF_READ_NULL;
1047 }
1048
1049 /* update all other flags and resync with the other side */
1050 si->update(si);
1051
1052 /* we don't want to expire timeouts while we're processing requests */
1053 si->ib->rex = TICK_ETERNITY;
1054 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001055
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001056 out:
1057 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
1058 __FUNCTION__, __LINE__,
1059 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
1060
1061 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1062 /* check that we have released everything then unregister */
1063 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001064 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001065}
1066
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001067/* This function dumps statistics onto the stream interface's read buffer.
1068 * The data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001069 * It returns 0 as long as it does not complete, non-zero upon completion.
1070 * Some states are not used but it makes the code more similar to other
1071 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001072 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001073int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001074{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001075 struct proxy *px;
1076 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001077 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001078
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001079 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +02001080
Willy Tarreau295a8372011-03-10 11:25:07 +01001081 switch (si->applet.state) {
1082 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001083 /* the function had not been called yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001084 si->applet.state = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001085 /* fall through */
1086
Willy Tarreau295a8372011-03-10 11:25:07 +01001087 case STAT_ST_HEAD:
1088 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001089 print_csv_header(&msg);
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001090 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001091 return 0;
1092 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001093
Willy Tarreau295a8372011-03-10 11:25:07 +01001094 si->applet.state = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001095 /* fall through */
1096
Willy Tarreau295a8372011-03-10 11:25:07 +01001097 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001098 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001099 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001100 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001101 "Name: " PRODUCT_NAME "\n"
1102 "Version: " HAPROXY_VERSION "\n"
1103 "Release_date: " HAPROXY_DATE "\n"
1104 "Nbproc: %d\n"
1105 "Process_num: %d\n"
1106 "Pid: %d\n"
1107 "Uptime: %dd %dh%02dm%02ds\n"
1108 "Uptime_sec: %d\n"
1109 "Memmax_MB: %d\n"
1110 "Ulimit-n: %d\n"
1111 "Maxsock: %d\n"
1112 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001113 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001114 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001115 "PipesUsed: %d\n"
1116 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001117 "Tasks: %d\n"
1118 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001119 "node: %s\n"
1120 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001121 "",
1122 global.nbproc,
1123 relative_pid,
1124 pid,
1125 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1126 up,
1127 global.rlimit_memmax,
1128 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001129 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001130 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001131 nb_tasks_cur, run_queue_cur,
1132 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001133 );
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001134 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001135 return 0;
1136 }
1137
Willy Tarreau295a8372011-03-10 11:25:07 +01001138 si->applet.ctx.stats.px = proxy;
1139 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1140 si->applet.ctx.stats.sv = NULL;
1141 si->applet.state = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001142 /* fall through */
1143
Willy Tarreau295a8372011-03-10 11:25:07 +01001144 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001145 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001146 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1147 while (si->applet.ctx.stats.px) {
1148 px = si->applet.ctx.stats.px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001149 /* skip the disabled proxies and non-networked ones */
1150 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001151 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001152 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001153 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001154 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001155
Willy Tarreau295a8372011-03-10 11:25:07 +01001156 si->applet.ctx.stats.px = px->next;
1157 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001158 }
1159 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001160 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001161
Willy Tarreau295a8372011-03-10 11:25:07 +01001162 si->applet.state = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001163 /* fall through */
1164
Willy Tarreau295a8372011-03-10 11:25:07 +01001165 case STAT_ST_END:
1166 si->applet.state = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001167 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001168
Willy Tarreau295a8372011-03-10 11:25:07 +01001169 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001170 return 1;
1171
1172 default:
1173 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001174 si->applet.state = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001175 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001176 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001177}
1178
1179
Cyril Bonté70be45d2010-10-12 00:14:35 +02001180/* We don't want to land on the posted stats page because a refresh will
1181 * repost the data. We don't want this to happen on accident so we redirect
1182 * the browse to the stats page with a GET.
1183 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001184int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001185{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001186 struct session *s = si->applet.private;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001187 struct chunk msg;
1188
1189 chunk_init(&msg, trash, sizeof(trash));
1190
Willy Tarreau295a8372011-03-10 11:25:07 +01001191 switch (si->applet.state) {
1192 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001193 chunk_printf(&msg,
1194 "HTTP/1.0 303 See Other\r\n"
1195 "Cache-Control: no-cache\r\n"
1196 "Content-Type: text/plain\r\n"
1197 "Connection: close\r\n"
1198 "Location: %s;st=%s",
Willy Tarreau295a8372011-03-10 11:25:07 +01001199 uri->uri_prefix, si->applet.ctx.stats.st_code);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001200 chunk_printf(&msg, "\r\n\r\n");
1201
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001202 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001203 return 0;
1204
1205 s->txn.status = 303;
1206
1207 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1208 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1209 if (!(s->flags & SN_FINST_MASK))
1210 s->flags |= SN_FINST_R;
1211
Willy Tarreau295a8372011-03-10 11:25:07 +01001212 si->applet.state = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001213 return 1;
1214 }
1215 return 1;
1216}
1217
1218
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001219/* This I/O handler runs as an applet embedded in a stream interface. It is
1220 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001221 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001222 * automatically unregisters itself once transfer is complete.
1223 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001224static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001225{
Willy Tarreaubc4af052011-02-13 13:25:14 +01001226 struct session *s = si->applet.private;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001227 struct buffer *req = si->ob;
1228 struct buffer *res = si->ib;
1229
1230 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1231 goto out;
1232
1233 /* check that the output is not closed */
1234 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001235 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001236
Willy Tarreaubc4af052011-02-13 13:25:14 +01001237 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001238 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001239 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001240 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001241 si->shutw(si);
1242 }
1243 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001244 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001245 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001246 si->shutw(si);
1247 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001248 }
1249 }
1250
1251 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
1252 si->shutw(si);
1253
Willy Tarreaubc4af052011-02-13 13:25:14 +01001254 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001255 si->shutr(si);
1256 res->flags |= BF_READ_NULL;
1257 }
1258
1259 /* update all other flags and resync with the other side */
1260 si->update(si);
1261
1262 /* we don't want to expire timeouts while we're processing requests */
1263 si->ib->rex = TICK_ETERNITY;
1264 si->ob->wex = TICK_ETERNITY;
1265
1266 out:
1267 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1268 /* check that we have released everything then unregister */
1269 stream_int_unregister_handler(si);
1270 }
1271}
1272
1273
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001274/* This function dumps statistics in HTTP format onto the stream interface's
1275 * read buffer. The data_ctx must have been zeroed first, and the flags
1276 * properly set. It returns 0 if it had to stop writing data and an I/O is
1277 * needed, 1 if the dump is finished and the session must be closed, or -1
1278 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001279 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001280int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001281{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001282 struct session *s = si->applet.private;
1283 struct buffer *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001284 struct proxy *px;
1285 struct chunk msg;
1286 unsigned int up;
1287
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001288 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001289
Willy Tarreau295a8372011-03-10 11:25:07 +01001290 switch (si->applet.state) {
1291 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001292 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001293 "HTTP/1.0 200 OK\r\n"
1294 "Cache-Control: no-cache\r\n"
1295 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001296 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001297 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001298
Willy Tarreau295a8372011-03-10 11:25:07 +01001299 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001300 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001301 uri->refresh);
1302
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001303 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001304
1305 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001306 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001307 return 0;
1308
Willy Tarreau91861262007-10-17 17:06:05 +02001309 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1310 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1311 if (!(s->flags & SN_FINST_MASK))
1312 s->flags |= SN_FINST_R;
1313
1314 if (s->txn.meth == HTTP_METH_HEAD) {
1315 /* that's all we return in case of HEAD request */
Willy Tarreau295a8372011-03-10 11:25:07 +01001316 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001317 return 1;
1318 }
1319
Willy Tarreau295a8372011-03-10 11:25:07 +01001320 si->applet.state = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001321 /* fall through */
1322
Willy Tarreau295a8372011-03-10 11:25:07 +01001323 case STAT_ST_HEAD:
1324 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001325 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001326 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001327 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1328 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001329 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001330 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1331 "<style type=\"text/css\"><!--\n"
1332 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001333 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001334 " font-size: 12px;"
1335 " font-weight: normal;"
1336 " color: black;"
1337 " background: white;"
1338 "}\n"
1339 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001340 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001341 "}\n"
1342 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001343 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001344 " margin-bottom: 0.5em;"
1345 "}\n"
1346 "h2 {"
1347 " font-family: helvetica, arial;"
1348 " font-size: x-large;"
1349 " font-weight: bold;"
1350 " font-style: italic;"
1351 " color: #6020a0;"
1352 " margin-top: 0em;"
1353 " margin-bottom: 0em;"
1354 "}\n"
1355 "h3 {"
1356 " font-family: helvetica, arial;"
1357 " font-size: 16px;"
1358 " font-weight: bold;"
1359 " color: #b00040;"
1360 " background: #e8e8d0;"
1361 " margin-top: 0em;"
1362 " margin-bottom: 0em;"
1363 "}\n"
1364 "li {"
1365 " margin-top: 0.25em;"
1366 " margin-right: 2em;"
1367 "}\n"
1368 ".hr {margin-top: 0.25em;"
1369 " border-color: black;"
1370 " border-bottom-style: solid;"
1371 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001372 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001373 ".total {background: #20D0D0;color: #ffff80;}\n"
1374 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001375 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001376 ".backend {background: #e8e8d0;}\n"
1377 ".active0 {background: #ff9090;}\n"
1378 ".active1 {background: #ffd020;}\n"
1379 ".active2 {background: #ffffa0;}\n"
1380 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001381 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1382 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1383 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001384 ".backup0 {background: #ff9090;}\n"
1385 ".backup1 {background: #ff80ff;}\n"
1386 ".backup2 {background: #c060ff;}\n"
1387 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001388 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1389 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1390 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001391 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001392 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001393 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001394 "a.px:link {color: #ffff40; text-decoration: none;}"
1395 "a.px:visited {color: #ffff40; text-decoration: none;}"
1396 "a.px:hover {color: #ffffff; text-decoration: none;}"
1397 "a.lfsb:link {color: #000000; text-decoration: none;}"
1398 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1399 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1400 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001401 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001402 "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"
1403 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001404 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001405 "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 +02001406 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001407 "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 +02001408 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001409 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1410 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1411 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001412 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001413 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001414 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001415 (uri->flags&ST_SHNODE) ? " on " : "",
1416 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001417 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001418 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001419 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001420 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001421 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001422 return 0;
1423
Willy Tarreau295a8372011-03-10 11:25:07 +01001424 si->applet.state = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02001425 /* fall through */
1426
Willy Tarreau295a8372011-03-10 11:25:07 +01001427 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02001428 up = (now.tv_sec - start_date.tv_sec);
1429
1430 /* WARNING! this has to fit the first packet too.
1431 * We are around 3.5 kB, add adding entries will
1432 * become tricky if we want to support 4kB buffers !
1433 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001434 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001435 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001436 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1437 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001438 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001439 "<hr width=\"100%%\" class=\"hr\">\n"
1440 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001441 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001442 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001443 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001444 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1445 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
1446 "current conns = %d; current pipes = %d/%d<br>\n"
1447 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001448 "</td><td align=\"center\" nowrap>\n"
1449 "<table class=\"lgd\"><tr>\n"
1450 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1451 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1452 "</tr><tr>\n"
1453 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1454 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1455 "</tr><tr>\n"
1456 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1457 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1458 "</tr><tr>\n"
1459 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001460 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001461 "</tr><tr>\n"
1462 "<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 +02001463 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001464 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001465 "</td>"
1466 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1467 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1468 "",
1469 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001470 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1471 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1472 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001473 up / 86400, (up % 86400) / 3600,
1474 (up % 3600) / 60, (up % 60),
1475 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1476 global.rlimit_memmax ? " MB" : "",
1477 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001478 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001479 actconn, pipes_used, pipes_used+pipes_free,
1480 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +02001481 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001482
Willy Tarreau295a8372011-03-10 11:25:07 +01001483 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001484 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001485 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1486 uri->uri_prefix,
1487 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01001488 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001489 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001490 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001491 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1492 uri->uri_prefix,
1493 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01001494 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001495
Willy Tarreau55bb8452007-10-17 18:44:57 +02001496 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001497 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001498 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001499 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1500 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001501 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001502 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001503 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001504 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001505 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1506 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001507 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001508 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001509 }
Willy Tarreau91861262007-10-17 17:06:05 +02001510
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001511 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001512 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
1513 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001514 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
1515 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001516
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001517 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02001518 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
1519 uri->uri_prefix,
1520 (uri->refresh > 0) ? ";norefresh" : "");
1521
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001522 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001523 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001524 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1525 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
1526 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
1527 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
1528 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
1529 "</ul>"
1530 "</td>"
1531 "</tr></table>\n"
1532 ""
1533 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001534
Willy Tarreau295a8372011-03-10 11:25:07 +01001535 if (si->applet.ctx.stats.st_code) {
1536 if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001537 chunk_printf(&msg,
1538 "<p><div class=active3>"
1539 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1540 "Action processed successfully."
1541 "</div>\n", uri->uri_prefix);
1542 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001543 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_NONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001544 chunk_printf(&msg,
1545 "<p><div class=active2>"
1546 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1547 "Nothing has changed."
1548 "</div>\n", uri->uri_prefix);
1549 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001550 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_EXCD) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001551 chunk_printf(&msg,
1552 "<p><div class=active0>"
1553 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1554 "<b>Action not processed : the buffer couldn't store all the data.<br>"
1555 "You should retry with less servers at a time.</b>"
1556 "</div>\n", uri->uri_prefix);
1557 }
Willy Tarreau295a8372011-03-10 11:25:07 +01001558 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DENY) == 0) {
Cyril Bonté474be412010-10-12 00:14:36 +02001559 chunk_printf(&msg,
1560 "<p><div class=active0>"
1561 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1562 "<b>Action denied.</b>"
1563 "</div>\n", uri->uri_prefix);
1564 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001565 else {
1566 chunk_printf(&msg,
1567 "<p><div class=active6>"
1568 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1569 "Unexpected result."
1570 "</div>\n", uri->uri_prefix);
1571 }
1572 chunk_printf(&msg,"<p>\n");
1573 }
1574
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001575 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001576 return 0;
1577 }
Willy Tarreau91861262007-10-17 17:06:05 +02001578
Willy Tarreau295a8372011-03-10 11:25:07 +01001579 si->applet.ctx.stats.px = proxy;
1580 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1581 si->applet.state = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02001582 /* fall through */
1583
Willy Tarreau295a8372011-03-10 11:25:07 +01001584 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02001585 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001586 while (si->applet.ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001587 if (buffer_almost_full(rep))
1588 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01001589 px = si->applet.ctx.stats.px;
Willy Tarreau91861262007-10-17 17:06:05 +02001590 /* skip the disabled proxies and non-networked ones */
1591 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001592 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001593 return 0;
1594
Willy Tarreau295a8372011-03-10 11:25:07 +01001595 si->applet.ctx.stats.px = px->next;
1596 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02001597 }
1598 /* here, we just have reached the last proxy */
1599
Willy Tarreau295a8372011-03-10 11:25:07 +01001600 si->applet.state = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02001601 /* fall through */
1602
Willy Tarreau295a8372011-03-10 11:25:07 +01001603 case STAT_ST_END:
1604 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001605 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001606 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001607 return 0;
1608 }
Willy Tarreau91861262007-10-17 17:06:05 +02001609
Willy Tarreau295a8372011-03-10 11:25:07 +01001610 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001611 /* fall through */
1612
Willy Tarreau295a8372011-03-10 11:25:07 +01001613 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02001614 return 1;
1615
1616 default:
1617 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001618 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001619 return -1;
1620 }
1621}
1622
1623
1624/*
1625 * Dumps statistics for a proxy.
1626 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1627 * ot non-zero if everything completed.
1628 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001629int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001630{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001631 struct session *s = si->applet.private;
1632 struct buffer *rep = si->ib;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001633 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001634 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02001635 struct chunk msg;
1636
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001637 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001638
Willy Tarreau295a8372011-03-10 11:25:07 +01001639 switch (si->applet.ctx.stats.px_st) {
1640 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02001641 /* we are on a new proxy */
1642
1643 if (uri && uri->scope) {
1644 /* we have a limited scope, we have to check the proxy name */
1645 struct stat_scope *scope;
1646 int len;
1647
1648 len = strlen(px->id);
1649 scope = uri->scope;
1650
1651 while (scope) {
1652 /* match exact proxy name */
1653 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1654 break;
1655
1656 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001657 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001658 break;
1659 scope = scope->next;
1660 }
1661
1662 /* proxy name not found : don't dump anything */
1663 if (scope == NULL)
1664 return 1;
1665 }
1666
Willy Tarreau295a8372011-03-10 11:25:07 +01001667 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
1668 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001669 return 1;
1670
Willy Tarreau295a8372011-03-10 11:25:07 +01001671 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02001672 /* fall through */
1673
Willy Tarreau295a8372011-03-10 11:25:07 +01001674 case STAT_PX_ST_TH:
1675 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
1676 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001677 /* A form to enable/disable this proxy servers */
1678 chunk_printf(&msg,
1679 "<form action=\"%s\" method=\"post\">",
1680 uri->uri_prefix);
1681 }
1682
Willy Tarreau55bb8452007-10-17 18:44:57 +02001683 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001684 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001685 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001686 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001687 "<th class=\"pxname\" width=\"10%%\"");
1688
1689 if (uri->flags&ST_SHLGNDS) {
1690 /* cap, mode, id */
1691 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
1692 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1693 px->uuid);
1694
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001695 chunk_printf(&msg, "\"");
1696 }
1697
1698 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001699 ">%s<a name=\"%s\"></a>"
1700 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001701 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001702 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001703 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001704 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001705 "<tr class=\"titre\">",
1706 (uri->flags & ST_SHLGNDS)?"<u>":"",
1707 px->id, px->id, px->id,
1708 (uri->flags & ST_SHLGNDS)?"</u>":"",
1709 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1710
Willy Tarreau295a8372011-03-10 11:25:07 +01001711 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001712 /* Column heading for Enable or Disable server */
1713 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
1714 }
1715
1716 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001717 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001718 "<th colspan=3>Queue</th>"
1719 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001720 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001721 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001722 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001723 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001724 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001725 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001726 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001727 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001728 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001729 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001730 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001731 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1732 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001733 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001734
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001735 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001736 return 0;
1737 }
Willy Tarreau91861262007-10-17 17:06:05 +02001738
Willy Tarreau295a8372011-03-10 11:25:07 +01001739 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02001740 /* fall through */
1741
Willy Tarreau295a8372011-03-10 11:25:07 +01001742 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02001743 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001744 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01001745 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
1746 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001747 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001748 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02001749 "<tr class=\"frontend\">");
1750
Willy Tarreau295a8372011-03-10 11:25:07 +01001751 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001752 /* Column sub-heading for Enable or Disable server */
1753 chunk_printf(&msg, "<td></td>");
1754 }
1755
1756 chunk_printf(&msg,
1757 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001758 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001759 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
1760 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01001761 "",
1762 px->id, px->id);
1763
1764 if (px->mode == PR_MODE_HTTP) {
1765 chunk_printf(&msg,
1766 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01001767 "<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 +01001768 "",
1769 read_freq_ctr(&px->fe_req_per_sec),
1770 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001771 px->fe_counters.p.http.rps_max,
1772 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01001773 LIM2A2(px->fe_sps_lim, "-"));
1774 } else {
1775 chunk_printf(&msg,
1776 /* sessions rate : current, max, limit */
1777 "<td>%s</td><td>%s</td><td>%s</td>"
1778 "",
1779 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001780 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01001781 }
1782
1783 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001784 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001785 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001786 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001787 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001788 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001789
1790 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1791 if (px->mode == PR_MODE_HTTP) {
1792 int i;
1793
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001794 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001795
1796 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001797 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001798
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001799 chunk_printf(&msg, " other=%lld\"", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001800 }
1801
1802 chunk_printf(&msg,
1803 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01001804 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001805 /* bytes : in, out */
1806 "<td>%s</td><td>%s</td>"
1807 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001808 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001809 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01001810 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001811 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001812
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001813 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001814 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001815 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001816 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001817 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001818 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001819 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001820 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001821 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001822 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001823 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02001824 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001825 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
1826 U2H2(px->fe_counters.failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +02001827 px->state == PR_STRUN ? "OPEN" :
1828 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001829 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001830 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001831 /* pxid, name, queue cur, queue max, */
1832 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001833 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001834 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001835 /* bytes : in, out */
1836 "%lld,%lld,"
1837 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001838 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001839 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001840 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001841 /* warnings: retries, redispatches */
1842 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001843 /* server status : reflect frontend status */
1844 "%s,"
1845 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001846 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001847 /* pid, iid, sid, throttle, lbtot, tracked, type */
1848 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001849 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001850 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001851 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001852 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02001853 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001854 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
1855 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
1856 px->fe_counters.denied_req, px->fe_counters.denied_resp,
1857 px->fe_counters.failed_req,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001858 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +01001859 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01001860 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001861 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001862 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001863
1864 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1865 if (px->mode == PR_MODE_HTTP) {
1866 int i;
1867
1868 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001869 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001870
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001871 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001872 } else {
1873 chunk_printf(&msg, ",,,,,,");
1874 }
1875
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001876 /* failed health analyses */
1877 chunk_printf(&msg, ",");
1878
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001879 /* requests : req_rate, req_rate_max, req_tot, */
1880 chunk_printf(&msg, "%u,%u,%lld,",
1881 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001882 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001883
Willy Tarreauae526782010-03-04 20:34:23 +01001884 /* errors: cli_aborts, srv_aborts */
1885 chunk_printf(&msg, ",,");
1886
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001887 /* finish with EOL */
1888 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001889 }
Willy Tarreau91861262007-10-17 17:06:05 +02001890
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001891 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001892 return 0;
1893 }
1894
Willy Tarreau295a8372011-03-10 11:25:07 +01001895 si->applet.ctx.stats.l = px->listen; /* may be NULL */
1896 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001897 /* fall through */
1898
Willy Tarreau295a8372011-03-10 11:25:07 +01001899 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001900 /* stats.l has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01001901 for (; si->applet.ctx.stats.l != NULL; si->applet.ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001902 if (buffer_almost_full(rep))
1903 return 0;
1904
Willy Tarreau295a8372011-03-10 11:25:07 +01001905 l = si->applet.ctx.stats.l;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001906 if (!l->counters)
1907 continue;
1908
Willy Tarreau295a8372011-03-10 11:25:07 +01001909 if (si->applet.ctx.stats.flags & STAT_BOUND) {
1910 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001911 break;
1912
Willy Tarreau295a8372011-03-10 11:25:07 +01001913 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001914 continue;
1915 }
1916
Willy Tarreau295a8372011-03-10 11:25:07 +01001917 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001918 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01001919 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001920 /* Column sub-heading for Enable or Disable server */
1921 chunk_printf(&msg, "<td></td>");
1922 }
1923 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001924
1925 if (uri->flags&ST_SHLGNDS) {
1926 char str[INET6_ADDRSTRLEN], *fmt = NULL;
1927 int port;
1928
1929 chunk_printf(&msg, " title=\"IP: ");
1930
1931 port = (l->addr.ss_family == AF_INET6)
1932 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
1933 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
1934
1935 if (l->addr.ss_family == AF_INET) {
1936 if (inet_ntop(AF_INET,
1937 (const void *)&((struct sockaddr_in *)&l->addr)->sin_addr,
1938 str, sizeof(str)))
1939 fmt = "%s:%d";
1940 } else {
1941 if (inet_ntop(AF_INET6,
1942 (const void *)&((struct sockaddr_in6 *)(&l->addr))->sin6_addr,
1943 str, sizeof(str)))
1944 fmt = "[%s]:%d";
1945 }
1946
1947 if (fmt)
1948 chunk_printf(&msg, fmt, str, port);
1949 else
1950 chunk_printf(&msg, "(%s)", strerror(errno));
1951
1952 /* id */
1953 chunk_printf(&msg, ", id: %d", l->luid);
1954
1955 chunk_printf(&msg, "\"");
1956 }
1957
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001958 chunk_printf(&msg,
1959 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01001960 ">%s<a name=\"%s/+%s\"></a>"
1961 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001962 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001963 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001964 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001965 "<td>%s</td><td>%s</td><td>%s</td>"
1966 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001967 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001968 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001969 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001970 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01001971 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01001972 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001973 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
1974 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
1975
1976 chunk_printf(&msg,
1977 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001978 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001979 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001980 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001981 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001982 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001983 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001984 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001985 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001986 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001987 "",
1988 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
1989 U2H2(l->counters->failed_req),
1990 (l->nbconn < l->maxconn) ? "OPEN" : "FULL");
1991 } else {
1992 chunk_printf(&msg,
1993 /* pxid, name, queue cur, queue max, */
1994 "%s,%s,,,"
1995 /* sessions: current, max, limit, total */
1996 "%d,%d,%d,%lld,"
1997 /* bytes: in, out */
1998 "%lld,%lld,"
1999 /* denied: req, resp */
2000 "%lld,%lld,"
2001 /* errors: request, connect, response */
2002 "%lld,,,"
2003 /* warnings: retries, redispatches */
2004 ",,"
2005 /* server status: reflect listener status */
2006 "%s,"
2007 /* rest of server: nothing */
2008 ",,,,,,,,"
2009 /* pid, iid, sid, throttle, lbtot, tracked, type */
2010 "%d,%d,%d,,,,%d,"
2011 /* rate, rate_lim, rate_max */
2012 ",,,"
2013 /* check_status, check_code, check_duration */
2014 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002015 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2016 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002017 /* failed health analyses */
2018 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002019 /* requests : req_rate, req_rate_max, req_tot, */
2020 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002021 /* errors: cli_aborts, srv_aborts */
2022 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002023 "\n",
2024 px->id, l->name,
2025 l->nbconn, l->counters->conn_max,
2026 l->maxconn, l->counters->cum_conn,
2027 l->counters->bytes_in, l->counters->bytes_out,
2028 l->counters->denied_req, l->counters->denied_resp,
2029 l->counters->failed_req,
2030 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2031 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2032 }
2033
2034 if (buffer_feed_chunk(rep, &msg) >= 0)
2035 return 0;
2036 }
2037
Willy Tarreau295a8372011-03-10 11:25:07 +01002038 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2039 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002040 /* fall through */
2041
Willy Tarreau295a8372011-03-10 11:25:07 +01002042 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002043 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002044 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002045 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 +02002046
Willy Tarreau4e33d862009-10-11 23:35:10 +02002047 if (buffer_almost_full(rep))
2048 return 0;
2049
Willy Tarreau295a8372011-03-10 11:25:07 +01002050 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002051
Willy Tarreau295a8372011-03-10 11:25:07 +01002052 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2053 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002054 break;
2055
Willy Tarreau295a8372011-03-10 11:25:07 +01002056 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002057 continue;
2058 }
2059
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002060 if (sv->tracked)
2061 svs = sv->tracked;
2062 else
2063 svs = sv;
2064
Willy Tarreau91861262007-10-17 17:06:05 +02002065 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002066 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002067 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002068 else if (svs->state & SRV_RUNNING) {
2069 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002070 sv_state = 3; /* UP */
2071 else
2072 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002073
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002074 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002075 sv_state += 2;
2076 }
Willy Tarreau91861262007-10-17 17:06:05 +02002077 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002078 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002079 sv_state = 1; /* going up */
2080 else
2081 sv_state = 0; /* DOWN */
2082
Willy Tarreau295a8372011-03-10 11:25:07 +01002083 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002084 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002085 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002086 continue;
2087 }
2088
Willy Tarreau295a8372011-03-10 11:25:07 +01002089 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002090 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2091 "UP %d/%d &darr;", "UP",
2092 "NOLB %d/%d &darr;", "NOLB",
2093 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002094 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002095 chunk_printf(&msg,
2096 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002097 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002098 );
2099 }
2100 else {
2101 chunk_printf(&msg,
2102 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002103 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002104 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2105 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002106
Willy Tarreau295a8372011-03-10 11:25:07 +01002107 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002108 chunk_printf(&msg,
2109 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2110 sv->id);
2111 }
2112
2113 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002114
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002115 if (uri->flags&ST_SHLGNDS) {
2116 char str[INET6_ADDRSTRLEN];
2117
2118 chunk_printf(&msg, " title=\"IP: ");
2119
2120 /* IP */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002121 switch (sv->addr.ss_family) {
2122 case AF_INET:
2123 if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&sv->addr)->sin_addr, str, sizeof(str)))
2124 chunk_printf(&msg, "%s:%d", str, htons(((struct sockaddr_in *)&sv->addr)->sin_port));
2125 else
2126 chunk_printf(&msg, "(%s)", strerror(errno));
2127 break;
2128 case AF_INET6:
2129 if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&sv->addr)->sin6_addr, str, sizeof(str)))
2130 chunk_printf(&msg, "%s:%d", str, htons(((struct sockaddr_in6 *)&sv->addr)->sin6_port));
2131 else
2132 chunk_printf(&msg, "(%s)", strerror(errno));
2133 break;
2134 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002135
2136 /* id */
2137 chunk_printf(&msg, ", id: %d", sv->puid);
2138
2139 /* cookie */
2140 if (sv->cookie) {
2141 struct chunk src;
2142
2143 chunk_printf(&msg, ", cookie: '");
2144
2145 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2146 chunk_htmlencode(&msg, &src);
2147
2148 chunk_printf(&msg, "'");
2149 }
2150
2151 chunk_printf(&msg, "\"");
2152 }
2153
2154 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002155 ">%s<a name=\"%s/%s\"></a>"
2156 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002157 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002158 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002159 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002160 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002161 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002162 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002163 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002164 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002165 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002166 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002167 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002168 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2169 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002170 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2171
2172 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2173 if (px->mode == PR_MODE_HTTP) {
2174 int i;
2175
2176 chunk_printf(&msg, " title=\"rsp codes:");
2177
2178 for (i = 1; i < 6; i++)
2179 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2180
2181 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2182 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002183
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002184 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002185 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002186 ">%s%s%s</td><td>%s</td>",
2187 (px->mode == PR_MODE_HTTP)?"<u>":"",
2188 U2H0(sv->counters.cum_sess),
2189 (px->mode == PR_MODE_HTTP)?"</u>":"",
2190 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002191
2192 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002193 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002194 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002195 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002196 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002197 /* errors : request, connect */
2198 "<td></td><td>%s</td>"
2199 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002200 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002201 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002202 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002203 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002204 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2205 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002206 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002207 sv->counters.cli_aborts,
2208 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002209 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002210 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002211
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002212 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002213 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002214
Cyril Bontécd19e512010-01-31 22:34:03 +01002215 if (sv->state & SRV_MAINTAIN) {
2216 chunk_printf(&msg, "%s ",
2217 human_time(now.tv_sec - sv->last_change, 1));
2218 chunk_printf(&msg, "MAINT");
2219 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002220 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2221 chunk_printf(&msg, "%s ",
2222 human_time(now.tv_sec - svs->last_change, 1));
2223 chunk_printf(&msg, "MAINT(via)");
2224 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002225 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002226 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002227 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002228
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002229 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002230 srv_hlt_st[sv_state],
2231 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2232 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002233 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002234
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002235 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002236 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002237 get_check_status_description(sv->check_status));
2238
2239 if (*sv->check_desc) {
2240 struct chunk src;
2241
2242 chunk_printf(&msg, ": ");
2243
2244 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2245 chunk_htmlencode(&msg, &src);
2246 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002247
Willy Tarreaue0454092010-02-26 12:29:07 +01002248 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002249 tv_iszero(&sv->check_start)?"":"* ",
2250 get_check_status_info(sv->check_status));
2251
2252 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002253 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002254
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002255 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002256 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002257 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002258 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002259
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002260 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002261 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002262 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002263 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002264 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002265 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002266 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002267 (sv->state & SRV_BACKUP) ? "-" : "Y",
2268 (sv->state & SRV_BACKUP) ? "Y" : "-");
2269
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002270 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002271 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002272 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002273 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2274
2275 if (svs->observe)
2276 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2277
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002278 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002279 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002280 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002281 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002282 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002283 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002284 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002285 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2286 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002287 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002288 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002289 "<td colspan=3></td>");
2290
2291 /* throttle */
2292 if ((sv->state & SRV_WARMINGUP) &&
2293 now.tv_sec < sv->last_change + sv->slowstart &&
2294 now.tv_sec >= sv->last_change) {
2295 unsigned int ratio;
2296 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002297 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002298 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002299 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002300 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002301 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002302 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002303 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002304 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2305 "UP %d/%d,", "UP,",
2306 "NOLB %d/%d,", "NOLB,",
2307 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002308 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002309 /* pxid, name */
2310 "%s,%s,"
2311 /* queue : current, max */
2312 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002313 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002314 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002315 /* bytes : in, out */
2316 "%lld,%lld,"
2317 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002318 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002319 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002320 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002321 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002322 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002323 "",
2324 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002325 sv->nbpend, sv->counters.nbpend_max,
2326 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002327 sv->counters.bytes_in, sv->counters.bytes_out,
2328 sv->counters.failed_secu,
2329 sv->counters.failed_conns, sv->counters.failed_resp,
2330 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002331
Willy Tarreau55bb8452007-10-17 18:44:57 +02002332 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002333 if (sv->state & SRV_MAINTAIN) {
2334 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002335 }
2336 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2337 chunk_printf(&msg, "MAINT(via),");
2338 }
2339 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002340 chunk_printf(&msg,
2341 srv_hlt_st[sv_state],
2342 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2343 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2344 }
Willy Tarreau91861262007-10-17 17:06:05 +02002345
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002346 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002347 /* weight, active, backup */
2348 "%d,%d,%d,"
2349 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002350 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002351 (sv->state & SRV_BACKUP) ? 0 : 1,
2352 (sv->state & SRV_BACKUP) ? 1 : 0);
2353
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002354 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002355 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002356 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002357 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002358 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002359 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002360 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002361 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002362 ",,,,");
2363
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002364 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002365 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002366 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002367 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002368 LIM2A0(sv->maxqueue, ""),
2369 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002370
2371 /* throttle */
2372 if ((sv->state & SRV_WARMINGUP) &&
2373 now.tv_sec < sv->last_change + sv->slowstart &&
2374 now.tv_sec >= sv->last_change) {
2375 unsigned int ratio;
2376 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002377 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002378 }
2379
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002380 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002381 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002382
2383 /* tracked */
2384 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002385 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002386 sv->tracked->proxy->id, sv->tracked->id);
2387 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002388 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002389
Willy Tarreau7f062c42009-03-05 18:43:00 +01002390 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002391 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002392
2393 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002394 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002395 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002396 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002397
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002398 if (sv->state & SRV_CHECKED) {
2399 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002400 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002401
2402 /* check_code */
2403 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002404 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002405 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002406 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002407
2408 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002409 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002410 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002411 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002412 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002413
2414 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002415 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002416 }
2417
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002418 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2419 if (px->mode == PR_MODE_HTTP) {
2420 int i;
2421
2422 for (i=1; i<6; i++)
2423 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2424
2425 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2426 } else {
2427 chunk_printf(&msg, ",,,,,,");
2428 }
2429
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002430 /* failed health analyses */
2431 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2432
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002433 /* requests : req_rate, req_rate_max, req_tot, */
2434 chunk_printf(&msg, ",,,");
2435
Willy Tarreauae526782010-03-04 20:34:23 +01002436 /* errors: cli_aborts, srv_aborts */
2437 chunk_printf(&msg, "%lld,%lld,",
2438 sv->counters.cli_aborts, sv->counters.srv_aborts);
2439
Willy Tarreau7f062c42009-03-05 18:43:00 +01002440 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002441 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002442 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002443 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002444 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002445 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002446
Willy Tarreau295a8372011-03-10 11:25:07 +01002447 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02002448 /* fall through */
2449
Willy Tarreau295a8372011-03-10 11:25:07 +01002450 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02002451 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002452 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002453 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2454 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002455 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01002456 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002457 /* Column sub-heading for Enable or Disable server */
2458 chunk_printf(&msg, "<td></td>");
2459 }
2460 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002461
2462 if (uri->flags&ST_SHLGNDS) {
2463 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002464 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002465 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002466
2467 /* cookie */
2468 if (px->cookie_name) {
2469 struct chunk src;
2470
2471 chunk_printf(&msg, ", cookie: '");
2472
2473 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2474 chunk_htmlencode(&msg, &src);
2475
2476 chunk_printf(&msg, "'");
2477 }
2478
2479 chunk_printf(&msg, "\"");
2480
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002481 }
2482
2483 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002484 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01002485 ">%s<a name=\"%s/Backend\"></a>"
2486 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002487 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002488 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002489 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002490 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002491 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002492 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002493 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002494 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002495 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
2496 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02002497
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002498 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002499 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002500 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002501 "<td"
2502 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002503 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002504
2505 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2506 if (px->mode == PR_MODE_HTTP) {
2507 int i;
2508
2509 chunk_printf(&msg, " title=\"rsp codes:");
2510
2511 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002512 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002513
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002514 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002515 }
2516
2517 chunk_printf(&msg,
2518 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002519 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002520 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002521 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002522 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002523 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002524 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01002525 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002526 U2H7(px->be_counters.cum_lbconn),
2527 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002528
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002529 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002530 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002531 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002532 /* errors : request, connect */
2533 "<td></td><td>%s</td>"
2534 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002535 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002536 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002537 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002538 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02002539 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002540 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02002541 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002542 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2543 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002544 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002545 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
2546 U2H2(px->be_counters.failed_conns),
2547 px->be_counters.cli_aborts,
2548 px->be_counters.srv_aborts,
2549 U2H5(px->be_counters.failed_resp),
2550 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002551 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01002552 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2553 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01002554 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002555 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002556
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002557 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002558 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002559 "<td class=ac>&nbsp;</td><td>%d</td>"
2560 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002561 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002562 "</tr>",
2563 px->down_trans,
2564 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002565 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002566 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002567 /* pxid, name */
2568 "%s,BACKEND,"
2569 /* queue : current, max */
2570 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002571 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002572 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002573 /* bytes : in, out */
2574 "%lld,%lld,"
2575 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002576 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002577 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002578 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002579 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002580 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002581 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02002582 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002583 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02002584 * active and backups. */
2585 "%s,"
2586 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002587 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002588 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002589 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002590 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002591 /* rate, rate_lim, rate_max, */
2592 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002593 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002594 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002595 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002596 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2597 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2598 px->be_counters.bytes_in, px->be_counters.bytes_out,
2599 px->be_counters.denied_req, px->be_counters.denied_resp,
2600 px->be_counters.failed_conns, px->be_counters.failed_resp,
2601 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01002602 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01002603 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002604 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002605 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01002606 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002607 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002608 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002609 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002610 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002611
2612 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2613 if (px->mode == PR_MODE_HTTP) {
2614 int i;
2615
2616 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002617 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002618
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002619 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002620 } else {
2621 chunk_printf(&msg, ",,,,,,");
2622 }
2623
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002624 /* failed health analyses */
2625 chunk_printf(&msg, ",");
2626
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002627 /* requests : req_rate, req_rate_max, req_tot, */
2628 chunk_printf(&msg, ",,,");
2629
Willy Tarreauae526782010-03-04 20:34:23 +01002630 /* errors: cli_aborts, srv_aborts */
2631 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002632 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01002633
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002634 /* finish with EOL */
2635 chunk_printf(&msg, "\n");
2636
Willy Tarreau55bb8452007-10-17 18:44:57 +02002637 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002638 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002639 return 0;
2640 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002641
Willy Tarreau295a8372011-03-10 11:25:07 +01002642 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002643 /* fall through */
2644
Willy Tarreau295a8372011-03-10 11:25:07 +01002645 case STAT_PX_ST_END:
2646 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002647 chunk_printf(&msg, "</table>");
2648
Willy Tarreau295a8372011-03-10 11:25:07 +01002649 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002650 /* close the form used to enable/disable this proxy servers */
2651 chunk_printf(&msg,
2652 "Choose the action to perform on the checked servers : "
2653 "<select name=action>"
2654 "<option value=\"\"></option>"
2655 "<option value=\"disable\">Disable</option>"
2656 "<option value=\"enable\">Enable</option>"
2657 "</select>"
2658 "<input type=\"hidden\" name=\"b\" value=\"%s\">"
2659 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2660 "</form>",
2661 px->id);
2662 }
2663
2664 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02002665
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002666 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002667 return 0;
2668 }
Willy Tarreau91861262007-10-17 17:06:05 +02002669
Willy Tarreau295a8372011-03-10 11:25:07 +01002670 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002671 /* fall through */
2672
Willy Tarreau295a8372011-03-10 11:25:07 +01002673 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002674 return 1;
2675
2676 default:
2677 /* unknown state, we should put an abort() here ! */
2678 return 1;
2679 }
2680}
2681
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002682/* This function dumps a complete session state onto the stream intreface's
2683 * read buffer. The data_ctx must have been zeroed first, and the flags
2684 * properly set. The session has to be set in data_ctx.sess.target. It returns
2685 * 0 if the output buffer is full and it needs to be called again, otherwise
2686 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002687 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002688int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002689{
2690 struct tm tm;
2691 struct chunk msg;
2692 struct session *sess;
2693 extern const char *monthname[12];
2694 char pn[INET6_ADDRSTRLEN];
2695
2696 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau295a8372011-03-10 11:25:07 +01002697 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002698
Willy Tarreau295a8372011-03-10 11:25:07 +01002699 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002700 /* session changed, no need to go any further */
2701 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002702 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002703 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002704 si->applet.ctx.sess.target = NULL;
2705 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002706 return 1;
2707 }
2708
Willy Tarreau295a8372011-03-10 11:25:07 +01002709 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002710 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01002711 si->applet.ctx.sess.uid = sess->uniq_id;
2712 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002713 /* fall through */
2714
2715 case 1:
2716 chunk_printf(&msg,
2717 "%p: id=%u, proto=%s",
2718 sess,
2719 sess->uniq_id,
2720 sess->listener->proto->name);
2721
2722 switch (sess->listener->proto->sock_family) {
2723 case AF_INET:
2724 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002725 (const void *)&((struct sockaddr_in *)&sess->si[0].addr.c.from)->sin_addr,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002726 pn, sizeof(pn));
2727
2728 chunk_printf(&msg,
2729 " source=%s:%d\n",
2730 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002731 ntohs(((struct sockaddr_in *)&sess->si[0].addr.c.from)->sin_port));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002732 break;
2733 case AF_INET6:
2734 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002735 (const void *)&((struct sockaddr_in6 *)(&sess->si[0].addr.c.from))->sin6_addr,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002736 pn, sizeof(pn));
2737
2738 chunk_printf(&msg,
2739 " source=%s:%d\n",
2740 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002741 ntohs(((struct sockaddr_in6 *)&sess->si[0].addr.c.from)->sin6_port));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002742 break;
2743 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02002744 chunk_printf(&msg,
2745 " source=unix:%d\n", sess->listener->luid);
2746 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002747 default:
2748 /* no more information to print right now */
2749 chunk_printf(&msg, "\n");
2750 break;
2751 }
2752
2753 chunk_printf(&msg,
2754 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02002755 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002756
2757 chunk_printf(&msg,
2758 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)\n",
2759 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
2760 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
2761 sess->listener ? sess->listener->luid : 0);
2762
2763 chunk_printf(&msg,
2764 " backend=%s (id=%u mode=%s) server=%s (id=%u)\n",
2765 sess->be->id, sess->be->uuid, sess->be->mode ? "http" : "tcp",
Willy Tarreau827aee92011-03-10 16:55:02 +01002766 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
2767 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002768
2769 chunk_printf(&msg,
2770 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
2771 sess->task,
2772 sess->task->state,
2773 sess->task->nice, sess->task->calls,
2774 sess->task->expire ?
2775 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
2776 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
2777 TICKS_TO_MS(1000)) : "<NEVER>",
2778 task_in_rq(sess->task) ? ", running" : "");
2779
2780 get_localtime(sess->logs.accept_date.tv_sec, &tm);
2781 chunk_printf(&msg,
2782 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
2783 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2784 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
2785 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
2786
2787 chunk_printf(&msg,
2788 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
2789 &sess->si[0],
2790 sess->si[0].state,
2791 sess->si[0].flags,
2792 sess->si[0].fd,
2793 sess->si[0].exp ?
2794 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
2795 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
2796 TICKS_TO_MS(1000)) : "<NEVER>",
2797 sess->si[0].err_type);
2798
2799 chunk_printf(&msg,
2800 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
2801 &sess->si[1],
2802 sess->si[1].state,
2803 sess->si[1].flags,
2804 sess->si[1].fd,
2805 sess->si[1].exp ?
2806 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
2807 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
2808 TICKS_TO_MS(1000)) : "<NEVER>",
2809 sess->si[1].err_type);
2810
2811 chunk_printf(&msg,
2812 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
2813 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
2814 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
2815
2816
2817 chunk_printf(&msg,
2818 " req=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%ld)\n"
2819 " an_exp=%s",
2820 sess->req,
2821 sess->req->flags, sess->req->analysers,
2822 sess->req->l, sess->req->send_max,
2823 sess->req->pipe ? sess->req->pipe->data : 0,
2824 sess->req->to_forward,
2825 sess->req->analyse_exp ?
2826 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
2827 TICKS_TO_MS(1000)) : "<NEVER>");
2828
2829 chunk_printf(&msg,
2830 " rex=%s",
2831 sess->req->rex ?
2832 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
2833 TICKS_TO_MS(1000)) : "<NEVER>");
2834
2835 chunk_printf(&msg,
2836 " wex=%s\n"
2837 " data=%p r=%d w=%d lr=%d total=%lld\n",
2838 sess->req->wex ?
2839 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
2840 TICKS_TO_MS(1000)) : "<NEVER>",
2841 sess->req->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01002842 (int)(sess->req->r - sess->req->data),
2843 (int)(sess->req->w - sess->req->data),
2844 (int)(sess->req->lr - sess->req->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002845 sess->req->total);
2846
2847 chunk_printf(&msg,
2848 " res=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%ld)\n"
2849 " an_exp=%s",
2850 sess->rep,
2851 sess->rep->flags, sess->rep->analysers,
2852 sess->rep->l, sess->rep->send_max,
2853 sess->rep->pipe ? sess->rep->pipe->data : 0,
2854 sess->rep->to_forward,
2855 sess->rep->analyse_exp ?
2856 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
2857 TICKS_TO_MS(1000)) : "<NEVER>");
2858
2859 chunk_printf(&msg,
2860 " rex=%s",
2861 sess->rep->rex ?
2862 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
2863 TICKS_TO_MS(1000)) : "<NEVER>");
2864
2865 chunk_printf(&msg,
2866 " wex=%s\n"
2867 " data=%p r=%d w=%d lr=%d total=%lld\n",
2868 sess->rep->wex ?
2869 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
2870 TICKS_TO_MS(1000)) : "<NEVER>",
2871 sess->rep->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01002872 (int)(sess->rep->r - sess->rep->data),
2873 (int)(sess->rep->w - sess->rep->data),
2874 (int)(sess->rep->lr - sess->rep->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002875 sess->rep->total);
2876
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002877 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002878 return 0;
2879
2880 /* use other states to dump the contents */
2881 }
2882 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01002883 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002884 return 1;
2885}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002886
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002887/* This function dumps all sessions' states onto the stream intreface's
2888 * read buffer. The data_ctx must have been zeroed first, and the flags
2889 * properly set. It returns 0 if the output buffer is full and it needs
2890 * to be called again, otherwise non-zero. It is designed to be called
2891 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002892 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002893int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002894{
2895 struct chunk msg;
2896
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002897 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002898 /* If we're forced to shut down, we might have to remove our
2899 * reference to the last session being dumped.
2900 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002901 if (si->applet.state == STAT_ST_LIST) {
2902 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
2903 LIST_DEL(&si->applet.ctx.sess.bref.users);
2904 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002905 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002906 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002907 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002908 }
2909
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002910 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002911
Willy Tarreau295a8372011-03-10 11:25:07 +01002912 switch (si->applet.state) {
2913 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002914 /* the function had not been called yet, let's prepare the
2915 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002916 * pointer to the first in the global list. When a target
2917 * session is being destroyed, it is responsible for updating
2918 * this pointer. We know we have reached the end when this
2919 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002920 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002921 LIST_INIT(&si->applet.ctx.sess.bref.users);
2922 si->applet.ctx.sess.bref.ref = sessions.n;
2923 si->applet.state = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002924 /* fall through */
2925
Willy Tarreau295a8372011-03-10 11:25:07 +01002926 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002927 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01002928 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
2929 LIST_DEL(&si->applet.ctx.sess.bref.users);
2930 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002931 }
2932
2933 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01002934 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01002935 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002936 struct session *curr_sess;
2937
Willy Tarreau295a8372011-03-10 11:25:07 +01002938 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002939
Willy Tarreau295a8372011-03-10 11:25:07 +01002940 if (si->applet.ctx.sess.target) {
2941 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002942 goto next_sess;
2943
Willy Tarreau295a8372011-03-10 11:25:07 +01002944 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002945 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002946 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002947 return 0;
2948
2949 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01002950 LIST_DEL(&si->applet.ctx.sess.bref.users);
2951 LIST_INIT(&si->applet.ctx.sess.bref.users);
2952 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002953 break;
2954 }
2955
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002956 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002957 "%p: proto=%s",
2958 curr_sess,
2959 curr_sess->listener->proto->name);
2960
2961 switch (curr_sess->listener->proto->sock_family) {
2962 case AF_INET:
2963 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002964 (const void *)&((struct sockaddr_in *)&curr_sess->si[0].addr.c.from)->sin_addr,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002965 pn, sizeof(pn));
2966
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002967 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002968 " src=%s:%d fe=%s be=%s srv=%s",
2969 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002970 ntohs(((struct sockaddr_in *)&curr_sess->si[0].addr.c.from)->sin_port),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002971 curr_sess->fe->id,
2972 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01002973 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002974 );
2975 break;
2976 case AF_INET6:
2977 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002978 (const void *)&((struct sockaddr_in6 *)(&curr_sess->si[0].addr.c.from))->sin6_addr,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002979 pn, sizeof(pn));
2980
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002981 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002982 " src=%s:%d fe=%s be=%s srv=%s",
2983 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002984 ntohs(((struct sockaddr_in6 *)&curr_sess->si[0].addr.c.from)->sin6_port),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002985 curr_sess->fe->id,
2986 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01002987 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002988 );
2989
2990 break;
2991 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02002992 chunk_printf(&msg,
2993 " src=unix:%d fe=%s be=%s srv=%s",
2994 curr_sess->listener->luid,
2995 curr_sess->fe->id,
2996 curr_sess->be->id,
Willy Tarreau827aee92011-03-10 16:55:02 +01002997 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02002998 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002999 break;
3000 }
3001
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003002 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003003 " ts=%02x age=%s calls=%d",
3004 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003005 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3006 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003007
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003008 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003009 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
3010 curr_sess->req->flags,
3011 curr_sess->req->l,
3012 curr_sess->req->analysers,
3013 curr_sess->req->rex ?
3014 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3015 TICKS_TO_MS(1000)) : "");
3016
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003017 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003018 ",wx=%s",
3019 curr_sess->req->wex ?
3020 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3021 TICKS_TO_MS(1000)) : "");
3022
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003023 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003024 ",ax=%s]",
3025 curr_sess->req->analyse_exp ?
3026 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3027 TICKS_TO_MS(1000)) : "");
3028
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003029 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003030 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
3031 curr_sess->rep->flags,
3032 curr_sess->rep->l,
3033 curr_sess->rep->analysers,
3034 curr_sess->rep->rex ?
3035 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3036 TICKS_TO_MS(1000)) : "");
3037
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003038 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003039 ",wx=%s",
3040 curr_sess->rep->wex ?
3041 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3042 TICKS_TO_MS(1000)) : "");
3043
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003044 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003045 ",ax=%s]",
3046 curr_sess->rep->analyse_exp ?
3047 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3048 TICKS_TO_MS(1000)) : "");
3049
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003050 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003051 " s0=[%d,%1xh,fd=%d,ex=%s]",
3052 curr_sess->si[0].state,
3053 curr_sess->si[0].flags,
3054 curr_sess->si[0].fd,
3055 curr_sess->si[0].exp ?
3056 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3057 TICKS_TO_MS(1000)) : "");
3058
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003059 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003060 " s1=[%d,%1xh,fd=%d,ex=%s]",
3061 curr_sess->si[1].state,
3062 curr_sess->si[1].flags,
3063 curr_sess->si[1].fd,
3064 curr_sess->si[1].exp ?
3065 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3066 TICKS_TO_MS(1000)) : "");
3067
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003068 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003069 " exp=%s",
3070 curr_sess->task->expire ?
3071 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3072 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003073 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003074 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003075
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003076 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003077
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003078 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003079 /* let's try again later from this session. We add ourselves into
3080 * this session's users so that it can remove us upon termination.
3081 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003082 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003083 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003084 }
3085
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003086 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003087 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003088 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003089
Willy Tarreau295a8372011-03-10 11:25:07 +01003090 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003091 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003092 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003093 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3094 else
3095 chunk_printf(&msg, "Session not found.\n");
3096
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003097 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003098 return 0;
3099
Willy Tarreau295a8372011-03-10 11:25:07 +01003100 si->applet.ctx.sess.target = NULL;
3101 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003102 return 1;
3103 }
3104
Willy Tarreau295a8372011-03-10 11:25:07 +01003105 si->applet.state = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003106 /* fall through */
3107
3108 default:
Willy Tarreau295a8372011-03-10 11:25:07 +01003109 si->applet.state = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003110 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003111 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003112}
3113
Emeric Brun1e029aa2010-09-23 18:12:53 +02003114
3115/* print a string of text buffer to <out>. The format is :
3116 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
3117 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
3118 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
3119 */
3120
3121static int dump_text(struct chunk *out, const char *buf, int bsize)
3122{
3123 unsigned char c;
3124 int ptr = 0;
3125
3126 while (buf[ptr] && ptr < bsize) {
3127 c = buf[ptr];
3128 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
3129 if (out->len > out->size - 1)
3130 break;
3131 out->str[out->len++] = c;
3132 }
3133 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
3134 if (out->len > out->size - 2)
3135 break;
3136 out->str[out->len++] = '\\';
3137 switch (c) {
3138 case ' ': c = ' '; break;
3139 case '\t': c = 't'; break;
3140 case '\n': c = 'n'; break;
3141 case '\r': c = 'r'; break;
3142 case '\e': c = 'e'; break;
3143 case '\\': c = '\\'; break;
3144 }
3145 out->str[out->len++] = c;
3146 }
3147 else {
3148 if (out->len > out->size - 4)
3149 break;
3150 out->str[out->len++] = '\\';
3151 out->str[out->len++] = 'x';
3152 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3153 out->str[out->len++] = hextab[c & 0xF];
3154 }
3155 ptr++;
3156 }
3157
3158 return ptr;
3159}
3160
3161/* print a buffer in hexa.
3162 * Print stopped if <bsize> is reached, or if no more place in the chunk.
3163 */
3164
3165static int dump_binary(struct chunk *out, const char *buf, int bsize)
3166{
3167 unsigned char c;
3168 int ptr = 0;
3169
3170 while (ptr < bsize) {
3171 c = buf[ptr];
3172
3173 if (out->len > out->size - 2)
3174 break;
3175 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3176 out->str[out->len++] = hextab[c & 0xF];
3177
3178 ptr++;
3179 }
3180 return ptr;
3181}
3182
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003183/* This function dumps all tables' states onto the stream intreface's
3184 * read buffer. The data_ctx must have been zeroed first, and the flags
3185 * properly set. It returns 0 if the output buffer is full and it needs
3186 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003187 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003188int stats_dump_table_to_buffer(struct stream_interface *si)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003189{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003190 struct session *s = si->applet.private;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003191 struct chunk msg;
3192 struct ebmb_node *eb;
3193 int dt;
3194
3195 /*
Willy Tarreau295a8372011-03-10 11:25:07 +01003196 * We have 3 possible states in si->applet.state :
3197 * - STAT_ST_INIT : the first call
3198 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003199 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003200 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003201 * and the entry pointer points to the next entry to be dumped,
3202 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003203 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003204 * data though.
3205 */
3206
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003207 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003208 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau295a8372011-03-10 11:25:07 +01003209 if (si->applet.state == STAT_ST_LIST) {
3210 si->applet.ctx.table.entry->ref_cnt--;
3211 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003212 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003213 return 1;
3214 }
3215
3216 chunk_init(&msg, trash, sizeof(trash));
3217
Willy Tarreau295a8372011-03-10 11:25:07 +01003218 while (si->applet.state != STAT_ST_FIN) {
3219 switch (si->applet.state) {
3220 case STAT_ST_INIT:
3221 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3222 if (!si->applet.ctx.table.proxy)
3223 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003224
Willy Tarreau295a8372011-03-10 11:25:07 +01003225 si->applet.ctx.table.entry = NULL;
3226 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003227 break;
3228
Willy Tarreau295a8372011-03-10 11:25:07 +01003229 case STAT_ST_INFO:
3230 if (!si->applet.ctx.table.proxy ||
3231 (si->applet.ctx.table.target &&
3232 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
3233 si->applet.state = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003234 break;
3235 }
3236
Willy Tarreau295a8372011-03-10 11:25:07 +01003237 if (si->applet.ctx.table.proxy->table.size) {
Emeric Brun1e029aa2010-09-23 18:12:53 +02003238 chunk_printf(&msg, "# table: %s, type: %s, size:%d, used:%d\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003239 si->applet.ctx.table.proxy->id,
3240 stktable_types[si->applet.ctx.table.proxy->table.type].kw,
3241 si->applet.ctx.table.proxy->table.size,
3242 si->applet.ctx.table.proxy->table.current);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003243
3244 /* any other information should be dumped here */
3245
Willy Tarreau295a8372011-03-10 11:25:07 +01003246 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003247 s->listener->perm.ux.level < ACCESS_LVL_OPER)
3248 chunk_printf(&msg, "# contents not dumped due to insufficient privileges\n");
3249
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003250 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003251 return 0;
3252
Willy Tarreau295a8372011-03-10 11:25:07 +01003253 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003254 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
3255 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003256 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003257 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003258 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3259 si->applet.ctx.table.entry->ref_cnt++;
3260 si->applet.state = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003261 break;
3262 }
3263 }
3264 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003265 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003266 break;
3267
Willy Tarreau295a8372011-03-10 11:25:07 +01003268 case STAT_ST_LIST:
3269 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003270 /* we're filtering on some data contents */
3271 void *ptr;
3272 long long data;
3273
Willy Tarreau295a8372011-03-10 11:25:07 +01003274 dt = si->applet.ctx.table.data_type;
3275 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3276 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003277 dt);
3278
3279 data = 0;
3280 switch (stktable_data_types[dt].std_type) {
3281 case STD_T_SINT:
3282 data = stktable_data_cast(ptr, std_t_sint);
3283 break;
3284 case STD_T_UINT:
3285 data = stktable_data_cast(ptr, std_t_uint);
3286 break;
3287 case STD_T_ULL:
3288 data = stktable_data_cast(ptr, std_t_ull);
3289 break;
3290 case STD_T_FRQP:
3291 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003292 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003293 break;
3294 }
3295
3296 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003297 if ((data < si->applet.ctx.table.value &&
3298 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3299 si->applet.ctx.table.data_op == STD_OP_GT ||
3300 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3301 (data == si->applet.ctx.table.value &&
3302 (si->applet.ctx.table.data_op == STD_OP_NE ||
3303 si->applet.ctx.table.data_op == STD_OP_GT ||
3304 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3305 (data > si->applet.ctx.table.value &&
3306 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3307 si->applet.ctx.table.data_op == STD_OP_LT ||
3308 si->applet.ctx.table.data_op == STD_OP_LE)))
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003309 goto skip_entry;
3310 }
3311
Willy Tarreau295a8372011-03-10 11:25:07 +01003312 chunk_printf(&msg, "%p:", si->applet.ctx.table.entry);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003313
Willy Tarreau295a8372011-03-10 11:25:07 +01003314 if (si->applet.ctx.table.proxy->table.type == STKTABLE_TYPE_IP) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003315 char addr[16];
3316 inet_ntop(AF_INET,
Willy Tarreau295a8372011-03-10 11:25:07 +01003317 (const void *)&si->applet.ctx.table.entry->key.key,
Willy Tarreau69f58c82010-07-12 17:55:33 +02003318 addr, sizeof(addr));
3319 chunk_printf(&msg, " key=%s", addr);
3320 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003321 else if (si->applet.ctx.table.proxy->table.type == STKTABLE_TYPE_INTEGER) {
3322 chunk_printf(&msg, " key=%u", *(unsigned int *)si->applet.ctx.table.entry->key.key);
Emeric Brun1e029aa2010-09-23 18:12:53 +02003323 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003324 else if (si->applet.ctx.table.proxy->table.type == STKTABLE_TYPE_STRING) {
Emeric Brun1e029aa2010-09-23 18:12:53 +02003325 chunk_printf(&msg, " key=");
Willy Tarreau295a8372011-03-10 11:25:07 +01003326 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 +02003327 }
3328 else {
3329 chunk_printf(&msg, " key=");
Willy Tarreau295a8372011-03-10 11:25:07 +01003330 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 +02003331 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003332
3333 chunk_printf(&msg, " use=%d exp=%d",
Willy Tarreau295a8372011-03-10 11:25:07 +01003334 si->applet.ctx.table.entry->ref_cnt - 1,
3335 tick_remain(now_ms, si->applet.ctx.table.entry->expire));
Willy Tarreau69f58c82010-07-12 17:55:33 +02003336
3337 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
3338 void *ptr;
3339
Willy Tarreau295a8372011-03-10 11:25:07 +01003340 if (si->applet.ctx.table.proxy->table.data_ofs[dt] == 0)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003341 continue;
3342 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
3343 chunk_printf(&msg, " %s(%d)=",
3344 stktable_data_types[dt].name,
Willy Tarreau295a8372011-03-10 11:25:07 +01003345 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003346 else
3347 chunk_printf(&msg, " %s=", stktable_data_types[dt].name);
3348
Willy Tarreau295a8372011-03-10 11:25:07 +01003349 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3350 si->applet.ctx.table.entry,
Willy Tarreau69f58c82010-07-12 17:55:33 +02003351 dt);
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02003352 switch (stktable_data_types[dt].std_type) {
3353 case STD_T_SINT:
3354 chunk_printf(&msg, "%d", stktable_data_cast(ptr, std_t_sint));
Willy Tarreau69f58c82010-07-12 17:55:33 +02003355 break;
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02003356 case STD_T_UINT:
3357 chunk_printf(&msg, "%u", stktable_data_cast(ptr, std_t_uint));
3358 break;
3359 case STD_T_ULL:
3360 chunk_printf(&msg, "%lld", stktable_data_cast(ptr, std_t_ull));
3361 break;
3362 case STD_T_FRQP:
Willy Tarreau69f58c82010-07-12 17:55:33 +02003363 chunk_printf(&msg, "%d",
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02003364 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003365 si->applet.ctx.table.proxy->table.data_arg[dt].u));
Willy Tarreau69f58c82010-07-12 17:55:33 +02003366 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003367 }
3368 }
3369 chunk_printf(&msg, "\n");
3370
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003371 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003372 return 0;
3373
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003374 skip_entry:
Willy Tarreau295a8372011-03-10 11:25:07 +01003375 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003376
Willy Tarreau295a8372011-03-10 11:25:07 +01003377 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003378 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003379 struct stksess *old = si->applet.ctx.table.entry;
3380 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3381 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3382 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003383 break;
3384 }
3385
Willy Tarreau295a8372011-03-10 11:25:07 +01003386 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3387 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
3388 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003389 break;
3390
Willy Tarreau295a8372011-03-10 11:25:07 +01003391 case STAT_ST_END:
3392 si->applet.state = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003393 break;
3394 }
3395 }
3396 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003397}
3398
Willy Tarreaud426a182010-03-05 14:58:26 +01003399/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003400 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3401 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3402 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3403 * lines are respected within the limit of 70 output chars. Lines that are
3404 * continuation of a previous truncated line begin with "+" instead of " "
3405 * after the offset. The new pointer is returned.
3406 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003407static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3408 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003409{
3410 int end;
3411 unsigned char c;
3412
3413 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003414 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003415 return ptr;
3416
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003417 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003418
Willy Tarreaud426a182010-03-05 14:58:26 +01003419 while (ptr < len && ptr < bsize) {
3420 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003421 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003422 if (out->len > end - 2)
3423 break;
3424 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003425 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003426 if (out->len > end - 3)
3427 break;
3428 out->str[out->len++] = '\\';
3429 switch (c) {
3430 case '\t': c = 't'; break;
3431 case '\n': c = 'n'; break;
3432 case '\r': c = 'r'; break;
3433 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003434 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003435 }
3436 out->str[out->len++] = c;
3437 } else {
3438 if (out->len > end - 5)
3439 break;
3440 out->str[out->len++] = '\\';
3441 out->str[out->len++] = 'x';
3442 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3443 out->str[out->len++] = hextab[c & 0xF];
3444 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003445 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003446 /* we had a line break, let's return now */
3447 out->str[out->len++] = '\n';
3448 *line = ptr;
3449 return ptr;
3450 }
3451 }
3452 /* we have an incomplete line, we return it as-is */
3453 out->str[out->len++] = '\n';
3454 return ptr;
3455}
3456
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003457/* This function dumps all captured errors onto the stream intreface's
3458 * read buffer. The data_ctx must have been zeroed first, and the flags
3459 * properly set. It returns 0 if the output buffer is full and it needs
3460 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003461 */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003462int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003463{
3464 extern const char *monthname[12];
3465 struct chunk msg;
3466
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003467 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003468 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003469
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003470 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003471
Willy Tarreau295a8372011-03-10 11:25:07 +01003472 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003473 /* the function had not been called yet, let's prepare the
3474 * buffer for a response.
3475 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003476 struct tm tm;
3477
3478 get_localtime(date.tv_sec, &tm);
3479 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3480 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3481 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3482 error_snapshot_id);
3483
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003484 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003485 /* Socket buffer full. Let's try again later from the same point */
3486 return 0;
3487 }
3488
Willy Tarreau295a8372011-03-10 11:25:07 +01003489 si->applet.ctx.errors.px = proxy;
3490 si->applet.ctx.errors.buf = 0;
3491 si->applet.ctx.errors.bol = 0;
3492 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003493 }
3494
3495 /* we have two inner loops here, one for the proxy, the other one for
3496 * the buffer.
3497 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003498 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003499 struct error_snapshot *es;
3500
Willy Tarreau295a8372011-03-10 11:25:07 +01003501 if (si->applet.ctx.errors.buf == 0)
3502 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003503 else
Willy Tarreau295a8372011-03-10 11:25:07 +01003504 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003505
3506 if (!es->when.tv_sec)
3507 goto next;
3508
Willy Tarreau295a8372011-03-10 11:25:07 +01003509 if (si->applet.ctx.errors.iid >= 0 &&
3510 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
3511 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003512 goto next;
3513
Willy Tarreau295a8372011-03-10 11:25:07 +01003514 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003515 /* just print headers now */
3516
3517 char pn[INET6_ADDRSTRLEN];
3518 struct tm tm;
3519
3520 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003521 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003522 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003523 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003524
3525
3526 if (es->src.ss_family == AF_INET)
3527 inet_ntop(AF_INET,
3528 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
3529 pn, sizeof(pn));
3530 else
3531 inet_ntop(AF_INET6,
3532 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
3533 pn, sizeof(pn));
3534
Willy Tarreau295a8372011-03-10 11:25:07 +01003535 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003536 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003537 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003538 " frontend %s (#%d): invalid request\n"
3539 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003540 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003541 " request length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003542 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau3fd6cec2010-03-25 06:45:07 +01003543 pn, es->sid, (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3544 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003545 es->srv ? es->srv->id : "<NONE>",
3546 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003547 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003548 es->len, es->pos);
3549 break;
3550 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003551 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003552 " backend %s (#%d) : invalid response\n"
3553 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003554 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003555 " response length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003556 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003557 pn, es->sid, es->oe->id, es->oe->uuid,
3558 es->srv ? es->srv->id : "<NONE>",
3559 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003560 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003561 es->len, es->pos);
3562 break;
3563 }
3564
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003565 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003566 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003567 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003568 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003569 si->applet.ctx.errors.ptr = 0;
3570 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003571 }
3572
Willy Tarreau295a8372011-03-10 11:25:07 +01003573 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003574 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003575 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003576 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003577 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau61b34732009-10-03 23:49:35 +02003578 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003579 goto next;
3580 }
3581
3582 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01003583 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003584 int newptr;
3585 int newline;
3586
Willy Tarreau295a8372011-03-10 11:25:07 +01003587 newline = si->applet.ctx.errors.bol;
3588 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
3589 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02003590 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003591
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003592 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003593 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003594 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003595 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003596 si->applet.ctx.errors.ptr = newptr;
3597 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003598 };
3599 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01003600 si->applet.ctx.errors.bol = 0;
3601 si->applet.ctx.errors.ptr = -1;
3602 si->applet.ctx.errors.buf++;
3603 if (si->applet.ctx.errors.buf > 1) {
3604 si->applet.ctx.errors.buf = 0;
3605 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003606 }
3607 }
3608
3609 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02003610 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003611}
3612
Willy Tarreaub24281b2011-02-13 13:16:36 +01003613struct si_applet http_stats_applet = {
3614 .name = "<STATS>", /* used for logging */
3615 .fct = http_stats_io_handler,
3616};
3617
3618struct si_applet cli_applet = {
3619 .name = "<CLI>", /* used for logging */
3620 .fct = cli_io_handler,
3621};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003622
Willy Tarreau10522fd2008-07-09 20:12:41 +02003623static struct cfg_kw_list cfg_kws = {{ },{
3624 { CFG_GLOBAL, "stats", stats_parse_global },
3625 { 0, NULL, NULL },
3626}};
3627
3628__attribute__((constructor))
3629static void __dumpstats_module_init(void)
3630{
3631 cfg_register_keywords(&cfg_kws);
3632}
3633
Willy Tarreau91861262007-10-17 17:06:05 +02003634/*
3635 * Local variables:
3636 * c-indent-level: 8
3637 * c-basic-offset: 8
3638 * End:
3639 */