blob: f434cdf07c8bcd6e58f915c1ab37a86a4fbbe76c [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 Tarreaudecd14d2010-06-01 18:03:19 +020080/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +020081 * a new stats socket. It returns a positive value upon success, 0 if the connection
82 * needs to be closed and ignored, or a negative value upon critical failure.
83 */
Willy Tarreaudecd14d2010-06-01 18:03:19 +020084int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +020085{
Willy Tarreaudecd14d2010-06-01 18:03:19 +020086 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +010087 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreaubc4af052011-02-13 13:25:14 +010088 s->si[1].applet.private = s;
89 s->si[1].applet.st1 = 0;
90 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +020091
Willy Tarreaudecd14d2010-06-01 18:03:19 +020092 tv_zero(&s->logs.tv_request);
93 s->logs.t_queue = 0;
94 s->logs.t_connect = 0;
95 s->logs.t_data = 0;
96 s->logs.t_close = 0;
97 s->logs.bytes_in = s->logs.bytes_out = 0;
98 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
99 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200100
101 s->data_state = DATA_ST_INIT;
102 s->data_source = DATA_SRC_NONE;
Willy Tarreaueb472682010-05-28 18:46:57 +0200103
Willy Tarreaueb472682010-05-28 18:46:57 +0200104 s->req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
105
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200106 if (s->listener->timeout) {
107 s->req->rto = *s->listener->timeout;
108 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200109 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200110 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200111}
112
Willy Tarreau07e9e642010-08-17 21:48:17 +0200113/* allocate a new stats frontend named <name>, and return it
114 * (or NULL in case of lack of memory).
115 */
116static struct proxy *alloc_stats_fe(const char *name)
117{
118 struct proxy *fe;
119
120 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
121 if (!fe)
122 return NULL;
123
124 LIST_INIT(&fe->pendconns);
125 LIST_INIT(&fe->acl);
126 LIST_INIT(&fe->block_cond);
127 LIST_INIT(&fe->redirect_rules);
128 LIST_INIT(&fe->mon_fail_cond);
129 LIST_INIT(&fe->switching_rules);
130 LIST_INIT(&fe->tcp_req.inspect_rules);
131
132 /* Timeouts are defined as -1, so we cannot use the zeroed area
133 * as a default value.
134 */
135 proxy_reset_timeouts(fe);
136
137 fe->last_change = now.tv_sec;
138 fe->id = strdup("GLOBAL");
139 fe->cap = PR_CAP_FE;
140 return fe;
141}
142
Willy Tarreaufbee7132007-10-18 13:53:22 +0200143/* This function parses a "stats" statement in the "global" section. It returns
144 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
145 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
146 * zero included. The trailing '\n' must not be written. The function must be
147 * called with <args> pointing to the first word after "stats".
148 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200149static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
150 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200151{
Willy Tarreau10522fd2008-07-09 20:12:41 +0200152 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200153 if (!strcmp(args[0], "socket")) {
Andreas Kohn16171e22011-01-19 20:29:32 +0100154 struct sockaddr_un *su;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200155 int cur_arg;
156
157 if (*args[1] == 0) {
158 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
159 return -1;
160 }
161
162 if (global.stats_sock.state != LI_NEW) {
163 snprintf(err, errlen, "'stats socket' already specified in global section");
164 return -1;
165 }
166
Andreas Kohn16171e22011-01-19 20:29:32 +0100167 su = str2sun(args[1]);
168 if (!su) {
169 snprintf(err, errlen, "'stats socket' path would require truncation");
170 return -1;
171 }
172 memcpy(&global.stats_sock.addr, su, sizeof(struct sockaddr_un)); // guaranteed to fit
Willy Tarreaufbee7132007-10-18 13:53:22 +0200173
Willy Tarreau89a63132009-08-16 17:41:45 +0200174 if (!global.stats_fe) {
Willy Tarreau07e9e642010-08-17 21:48:17 +0200175 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau89a63132009-08-16 17:41:45 +0200176 snprintf(err, errlen, "out of memory");
177 return -1;
178 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200179 global.stats_fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreau89a63132009-08-16 17:41:45 +0200180 }
181
Willy Tarreaufbee7132007-10-18 13:53:22 +0200182 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100183 global.stats_sock.options = LI_O_NONE;
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200184 global.stats_sock.accept = session_accept;
185 global.stats_fe->accept = stats_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200186 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200187 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200188 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreaueb472682010-05-28 18:46:57 +0200189 global.stats_sock.frontend = global.stats_fe;
Willy Tarreau6162db22009-10-10 17:13:00 +0200190 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau07e9e642010-08-17 21:48:17 +0200191 global.stats_fe->maxconn = global.stats_sock.maxconn;
Willy Tarreau89a63132009-08-16 17:41:45 +0200192 global.stats_sock.timeout = &global.stats_fe->timeout.client;
193
194 global.stats_sock.next = global.stats_fe->listen;
195 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200196
197 cur_arg = 2;
198 while (*args[cur_arg]) {
199 if (!strcmp(args[cur_arg], "uid")) {
200 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
201 cur_arg += 2;
202 }
203 else if (!strcmp(args[cur_arg], "gid")) {
204 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
205 cur_arg += 2;
206 }
207 else if (!strcmp(args[cur_arg], "mode")) {
208 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
209 cur_arg += 2;
210 }
211 else if (!strcmp(args[cur_arg], "user")) {
212 struct passwd *user;
213 user = getpwnam(args[cur_arg + 1]);
214 if (!user) {
215 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
216 args[cur_arg + 1]);
217 return -1;
218 }
219 global.stats_sock.perm.ux.uid = user->pw_uid;
220 cur_arg += 2;
221 }
222 else if (!strcmp(args[cur_arg], "group")) {
223 struct group *group;
224 group = getgrnam(args[cur_arg + 1]);
225 if (!group) {
226 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
227 args[cur_arg + 1]);
228 return -1;
229 }
230 global.stats_sock.perm.ux.gid = group->gr_gid;
231 cur_arg += 2;
232 }
Willy Tarreau6162db22009-10-10 17:13:00 +0200233 else if (!strcmp(args[cur_arg], "level")) {
234 if (!strcmp(args[cur_arg+1], "user"))
235 global.stats_sock.perm.ux.level = ACCESS_LVL_USER;
236 else if (!strcmp(args[cur_arg+1], "operator"))
237 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER;
238 else if (!strcmp(args[cur_arg+1], "admin"))
239 global.stats_sock.perm.ux.level = ACCESS_LVL_ADMIN;
240 else {
241 snprintf(err, errlen, "'stats socket level' only supports 'user', 'operator', and 'admin'");
242 return -1;
243 }
244 cur_arg += 2;
245 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200246 else {
Willy Tarreau6162db22009-10-10 17:13:00 +0200247 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', 'level', and 'mode'");
Willy Tarreaufbee7132007-10-18 13:53:22 +0200248 return -1;
249 }
250 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100251
Willy Tarreaufbee7132007-10-18 13:53:22 +0200252 uxst_add_listener(&global.stats_sock);
253 global.maxsock++;
254 }
255 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100256 unsigned timeout;
257 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
258
259 if (res) {
260 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
261 return -1;
262 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200263
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100264 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200265 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
266 return -1;
267 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200268 if (!global.stats_fe) {
269 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
270 snprintf(err, errlen, "out of memory");
271 return -1;
272 }
273 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200274 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200275 }
276 else if (!strcmp(args[0], "maxconn")) {
277 int maxconn = atol(args[1]);
278
279 if (maxconn <= 0) {
280 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
281 return -1;
282 }
283 global.maxsock -= global.stats_sock.maxconn;
284 global.stats_sock.maxconn = maxconn;
285 global.maxsock += global.stats_sock.maxconn;
Willy Tarreaueb472682010-05-28 18:46:57 +0200286 if (global.stats_fe)
287 global.stats_fe->maxconn = global.stats_sock.maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200288 }
289 else {
290 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
291 return -1;
292 }
293 return 0;
294}
295
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200296int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100297{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200298 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100299 "# pxname,svname,"
300 "qcur,qmax,"
301 "scur,smax,slim,stot,"
302 "bin,bout,"
303 "dreq,dresp,"
304 "ereq,econ,eresp,"
305 "wretr,wredis,"
306 "status,weight,act,bck,"
307 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200308 "pid,iid,sid,throttle,lbtot,tracked,type,"
309 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200310 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100311 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100312 "req_rate,req_rate_max,req_tot,"
313 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100314 "\n");
315}
316
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200317/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200318 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100319 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200320 * designating the function which will have to process the request, which can
321 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200322 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200323int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200324{
Willy Tarreaubc4af052011-02-13 13:25:14 +0100325 struct session *s = si->applet.private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200326 char *args[MAX_STATS_ARGS + 1];
327 int arg;
328
329 while (isspace((unsigned char)*line))
330 line++;
331
332 arg = 0;
333 args[arg] = line;
334
335 while (*line && arg < MAX_STATS_ARGS) {
336 if (isspace((unsigned char)*line)) {
337 *line++ = '\0';
338
339 while (isspace((unsigned char)*line))
340 line++;
341
342 args[++arg] = line;
343 continue;
344 }
345
346 line++;
347 }
348
349 while (++arg <= MAX_STATS_ARGS)
350 args[arg] = line;
351
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200352 s->data_ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200353 if (strcmp(args[0], "show") == 0) {
354 if (strcmp(args[1], "stat") == 0) {
355 if (*args[2] && *args[3] && *args[4]) {
356 s->data_ctx.stats.flags |= STAT_BOUND;
357 s->data_ctx.stats.iid = atoi(args[2]);
358 s->data_ctx.stats.type = atoi(args[3]);
359 s->data_ctx.stats.sid = atoi(args[4]);
360 }
361
362 s->data_ctx.stats.flags |= STAT_SHOW_STAT;
363 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200364 s->data_state = DATA_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100365 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200366 }
367 else if (strcmp(args[1], "info") == 0) {
368 s->data_ctx.stats.flags |= STAT_SHOW_INFO;
369 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200370 s->data_state = DATA_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100371 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200372 }
373 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200374 s->data_state = DATA_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200375 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200376 s->data_ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100377 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200378 return 1;
379 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100380 if (*args[2])
381 s->data_ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
382 else
383 s->data_ctx.sess.target = NULL;
384 s->data_ctx.sess.section = 0; /* start with session status */
385 s->data_ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100386 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200387 }
388 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200389 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200390 s->data_ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100391 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200392 return 1;
393 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200394 if (*args[2])
395 s->data_ctx.errors.iid = atoi(args[2]);
396 else
397 s->data_ctx.errors.iid = -1;
398 s->data_ctx.errors.px = NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200399 s->data_state = DATA_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100400 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200401 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200402 else if (strcmp(args[1], "table") == 0) {
403 s->data_state = DATA_ST_INIT;
Willy Tarreau603861e2010-07-18 08:31:59 +0200404 if (*args[2]) {
405 s->data_ctx.table.target = find_stktable(args[2]);
406 if (!s->data_ctx.table.target) {
407 s->data_ctx.cli.msg = "No such table\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100408 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau603861e2010-07-18 08:31:59 +0200409 return 1;
410 }
411 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200412 else
413 s->data_ctx.table.target = NULL;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200414
415 s->data_ctx.table.data_type = -1;
416 if (s->data_ctx.table.target && strncmp(args[3], "data.", 5) == 0) {
417 /* condition on stored data value */
418 s->data_ctx.table.data_type = stktable_get_data_type(args[3] + 5);
419 if (s->data_ctx.table.data_type < 0) {
420 s->data_ctx.cli.msg = "Unknown data type\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100421 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200422 return 1;
423 }
424
425 if (!((struct proxy *)s->data_ctx.table.target)->table.data_ofs[s->data_ctx.table.data_type]) {
426 s->data_ctx.cli.msg = "Data type not stored in this table\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100427 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200428 return 1;
429 }
430
431 s->data_ctx.table.data_op = get_std_op(args[4]);
432 if (s->data_ctx.table.data_op < 0) {
433 s->data_ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100434 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200435 return 1;
436 }
437
438 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &s->data_ctx.table.value) != 0) {
439 s->data_ctx.cli.msg = "Require a valid integer value to compare against\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100440 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200441 return 1;
442 }
443 }
444 else if (*args[3]) {
445 s->data_ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value>\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100446 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200447 return 1;
448 }
449
Willy Tarreau69f58c82010-07-12 17:55:33 +0200450 s->data_ctx.table.proxy = NULL;
451 s->data_ctx.table.entry = NULL;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100452 si->applet.st0 = STAT_CLI_O_TAB; // stats_dump_table_to_buffer
Willy Tarreau69f58c82010-07-12 17:55:33 +0200453 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200454 else { /* neither "stat" nor "info" nor "sess" nor "errors" no "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200455 return 0;
456 }
457 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200458 else if (strcmp(args[0], "clear") == 0) {
459 if (strcmp(args[1], "counters") == 0) {
460 struct proxy *px;
461 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200462 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200463 int clrall = 0;
464
465 if (strcmp(args[2], "all") == 0)
466 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200467
Willy Tarreau6162db22009-10-10 17:13:00 +0200468 /* check permissions */
469 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
470 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200471 s->data_ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100472 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200473 return 1;
474 }
475
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200476 for (px = proxy; px; px = px->next) {
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200477 if (clrall)
478 memset(&px->counters, 0, sizeof(px->counters));
479 else {
480 px->counters.feconn_max = 0;
481 px->counters.beconn_max = 0;
Willy Tarreaud9b587f2010-02-26 10:05:55 +0100482 px->counters.fe_rps_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200483 px->counters.fe_sps_max = 0;
Willy Tarreaub36b4242010-06-04 20:59:39 +0200484 px->counters.fe_cps_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200485 px->counters.be_sps_max = 0;
486 px->counters.nbpend_max = 0;
487 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200488
489 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200490 if (clrall)
491 memset(&sv->counters, 0, sizeof(sv->counters));
492 else {
493 sv->counters.cur_sess_max = 0;
494 sv->counters.nbpend_max = 0;
495 sv->counters.sps_max = 0;
496 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200497
498 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200499 if (li->counters) {
500 if (clrall)
501 memset(li->counters, 0, sizeof(*li->counters));
502 else
503 li->counters->conn_max = 0;
504 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200505 }
506
507 return 1;
508 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200509 else if (strcmp(args[1], "table") == 0) {
510 struct proxy *px;
511 struct stksess *ts;
512 unsigned int ip_key;
513
514 if (!*args[2]) {
Willy Tarreau603861e2010-07-18 08:31:59 +0200515 s->data_ctx.cli.msg = "\"table\" argument expected\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100516 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200517 return 1;
518 }
519
520 px = find_stktable(args[2]);
521
522 if (!px) {
Willy Tarreau603861e2010-07-18 08:31:59 +0200523 s->data_ctx.cli.msg = "No such table\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100524 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200525 return 1;
526 }
527
528 if (strcmp(args[3], "key") != 0) {
Willy Tarreau603861e2010-07-18 08:31:59 +0200529 s->data_ctx.cli.msg = "\"key\" argument expected\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100530 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200531 return 1;
532 }
533
534 if (!*args[4]) {
Willy Tarreau603861e2010-07-18 08:31:59 +0200535 s->data_ctx.cli.msg = "Key value expected\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100536 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200537 return 1;
538 }
539
540 if (px->table.type == STKTABLE_TYPE_IP) {
541 ip_key = htonl(inetaddr_host(args[4]));
542 static_table_key.key = (void *)&ip_key;
543 }
544 else {
Willy Tarreau603861e2010-07-18 08:31:59 +0200545 s->data_ctx.cli.msg = "Removing keys from non-ip tables is not supported\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100546 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200547 return 1;
548 }
549
550 /* check permissions */
551 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
552 s->data_ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100553 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200554 return 1;
555 }
556
557 ts = stktable_lookup_key(&px->table, &static_table_key);
558 if (!ts) {
559 /* silent return, entry was already removed */
560 return 1;
561 }
562 else if (ts->ref_cnt) {
563 /* don't delete an entry which is currently referenced */
Willy Tarreau603861e2010-07-18 08:31:59 +0200564 s->data_ctx.cli.msg = "Entry currently in use, cannot remove\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100565 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau88ee3972010-07-13 13:48:00 +0200566 return 1;
567 }
568
Willy Tarreaudf39e952010-08-10 11:11:40 +0200569 stksess_kill(&px->table, ts);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200570 /* end of processing */
571 return 1;
572 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200573 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200574 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200575 return 0;
576 }
577 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200578 else if (strcmp(args[0], "get") == 0) {
579 if (strcmp(args[1], "weight") == 0) {
580 struct proxy *px;
581 struct server *sv;
582
583 /* split "backend/server" and make <line> point to server */
584 for (line = args[2]; *line; line++)
585 if (*line == '/') {
586 *line++ = '\0';
587 break;
588 }
589
590 if (!*line) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200591 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100592 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200593 return 1;
594 }
595
596 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200597 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100598 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200599 return 1;
600 }
601
602 /* return server's effective weight at the moment */
603 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
604 buffer_feed(si->ib, trash);
605 return 1;
606 }
607 else { /* not "get weight" */
608 return 0;
609 }
610 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200611 else if (strcmp(args[0], "set") == 0) {
612 if (strcmp(args[1], "weight") == 0) {
613 struct proxy *px;
614 struct server *sv;
615 int w;
616
617 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200618 s->data_ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100619 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200620 return 1;
621 }
622
623 /* split "backend/server" and make <line> point to server */
624 for (line = args[2]; *line; line++)
625 if (*line == '/') {
626 *line++ = '\0';
627 break;
628 }
629
630 if (!*line || !*args[3]) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200631 s->data_ctx.cli.msg = "Require 'backend/server' and 'weight' or 'weight%'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100632 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200633 return 1;
634 }
635
636 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200637 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100638 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200639 return 1;
640 }
641
Cyril Bonté613f0df2011-03-03 20:49:04 +0100642 if (px->state == PR_STSTOPPED) {
643 s->data_ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100644 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100645 return 1;
646 }
647
Willy Tarreau4483d432009-10-10 19:30:08 +0200648 /* if the weight is terminated with '%', it is set relative to
649 * the initial weight, otherwise it is absolute.
650 */
651 w = atoi(args[3]);
652 if (strchr(args[3], '%') != NULL) {
653 if (w < 0 || w > 100) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200654 s->data_ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100655 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200656 return 1;
657 }
658 w = sv->iweight * w / 100;
659 }
660 else {
661 if (w < 0 || w > 256) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200662 s->data_ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100663 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200664 return 1;
665 }
666 }
667
668 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200669 s->data_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 +0100670 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200671 return 1;
672 }
673
674 sv->uweight = w;
675
676 if (px->lbprm.algo & BE_LB_PROP_DYN) {
677 /* we must take care of not pushing the server to full throttle during slow starts */
678 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
679 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
680 else
681 sv->eweight = BE_WEIGHT_SCALE;
682 sv->eweight *= sv->uweight;
683 } else {
684 sv->eweight = sv->uweight;
685 }
686
687 /* static LB algorithms are a bit harder to update */
688 if (px->lbprm.update_server_eweight)
689 px->lbprm.update_server_eweight(sv);
690 else if (sv->eweight)
691 px->lbprm.set_server_status_up(sv);
692 else
693 px->lbprm.set_server_status_down(sv);
694
695 return 1;
696 }
Willy Tarreau7aabd112010-01-26 10:59:06 +0100697 else if (strcmp(args[1], "timeout") == 0) {
698 if (strcmp(args[2], "cli") == 0) {
699 unsigned timeout;
700 const char *res;
701
702 if (!*args[3]) {
703 s->data_ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100704 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100705 return 1;
706 }
707
708 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
709 if (res || timeout < 1) {
710 s->data_ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100711 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100712 return 1;
713 }
714
715 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
716 return 1;
717 }
718 else {
719 s->data_ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100720 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +0100721 return 1;
722 }
723 }
724 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +0200725 return 0;
726 }
727 }
Cyril Bontécd19e512010-01-31 22:34:03 +0100728 else if (strcmp(args[0], "enable") == 0) {
729 if (strcmp(args[1], "server") == 0) {
730 struct proxy *px;
731 struct server *sv;
732
733 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
734 s->data_ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100735 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100736 return 1;
737 }
738
739 /* split "backend/server" and make <line> point to server */
740 for (line = args[2]; *line; line++)
741 if (*line == '/') {
742 *line++ = '\0';
743 break;
744 }
745
746 if (!*line || !*args[2]) {
747 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100748 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100749 return 1;
750 }
751
752 if (!get_backend_server(args[2], line, &px, &sv)) {
753 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100754 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100755 return 1;
756 }
757
Cyril Bonté613f0df2011-03-03 20:49:04 +0100758 if (px->state == PR_STSTOPPED) {
759 s->data_ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100760 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100761 return 1;
762 }
763
Cyril Bontécd19e512010-01-31 22:34:03 +0100764 if (sv->state & SRV_MAINTAIN) {
765 /* The server is really in maintenance, we can change the server state */
766 if (sv->tracked) {
767 /* If this server tracks the status of another one,
768 * we must restore the good status.
769 */
770 if (sv->tracked->state & SRV_RUNNING) {
771 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +0200772 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +0100773 } else {
774 sv->state &= ~SRV_MAINTAIN;
775 set_server_down(sv);
776 }
777 } else {
778 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +0200779 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +0100780 }
781 }
782
783 return 1;
784 }
785 else { /* unknown "enable" parameter */
786 return 0;
787 }
788 }
789 else if (strcmp(args[0], "disable") == 0) {
790 if (strcmp(args[1], "server") == 0) {
791 struct proxy *px;
792 struct server *sv;
793
794 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
795 s->data_ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100796 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100797 return 1;
798 }
799
800 /* split "backend/server" and make <line> point to server */
801 for (line = args[2]; *line; line++)
802 if (*line == '/') {
803 *line++ = '\0';
804 break;
805 }
806
807 if (!*line || !*args[2]) {
808 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100809 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100810 return 1;
811 }
812
813 if (!get_backend_server(args[2], line, &px, &sv)) {
814 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100815 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bontécd19e512010-01-31 22:34:03 +0100816 return 1;
817 }
818
Cyril Bonté613f0df2011-03-03 20:49:04 +0100819 if (px->state == PR_STSTOPPED) {
820 s->data_ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100821 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100822 return 1;
823 }
824
Cyril Bontécd19e512010-01-31 22:34:03 +0100825 if (! (sv->state & SRV_MAINTAIN)) {
826 /* Not already in maintenance, we can change the server state */
827 sv->state |= SRV_MAINTAIN;
828 set_server_down(sv);
829 }
830
831 return 1;
832 }
833 else { /* unknown "disable" parameter */
834 return 0;
835 }
836 }
837 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200838 return 0;
839 }
840 return 1;
841}
842
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200843/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200844 * used to processes I/O from/to the stats unix socket. The system relies on a
845 * state machine handling requests and various responses. We read a request,
846 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +0100847 * Then we can read again. The state is stored in si->applet.st0 and is one of the
848 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200849 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200850 */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100851static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200852{
Willy Tarreaubc4af052011-02-13 13:25:14 +0100853 struct session *s = si->applet.private;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200854 struct buffer *req = si->ob;
855 struct buffer *res = si->ib;
856 int reql;
857 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200858
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200859 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
860 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200861
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200862 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +0100863 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200864 /* Stats output not initialized yet */
865 memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats));
866 s->data_source = DATA_SRC_STATS;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100867 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200868 }
Willy Tarreaubc4af052011-02-13 13:25:14 +0100869 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200870 /* Let's close for real now. We just close the request
871 * side, the conditions below will complete if needed.
872 */
873 si->shutw(si);
874 break;
875 }
Willy Tarreaubc4af052011-02-13 13:25:14 +0100876 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +0200877 /* ensure we have some output room left in the event we
878 * would want to return some info right after parsing.
879 */
880 if (buffer_almost_full(si->ib))
881 break;
882
Willy Tarreau74b08c92010-09-08 17:04:31 +0200883 reql = buffer_get_line(si->ob, trash, sizeof(trash));
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200884 if (reql <= 0) { /* closed or EOL not found */
885 if (reql == 0)
886 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100887 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200888 continue;
889 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200890
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200891 /* seek for a possible semi-colon. If we find one, we
892 * replace it with an LF and skip only this part.
893 */
894 for (len = 0; len < reql; len++)
895 if (trash[len] == ';') {
896 trash[len] = '\n';
897 reql = len + 1;
898 break;
899 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200900
Willy Tarreau816fc222009-10-04 07:36:58 +0200901 /* now it is time to check that we have a full line,
902 * remove the trailing \n and possibly \r, then cut the
903 * line.
904 */
905 len = reql - 1;
906 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +0100907 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200908 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200909 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200910
Willy Tarreau816fc222009-10-04 07:36:58 +0200911 if (len && trash[len-1] == '\r')
912 len--;
913
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200914 trash[len] = '\0';
915
Willy Tarreaubc4af052011-02-13 13:25:14 +0100916 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200917 if (len) {
918 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +0100919 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200920 continue;
921 }
922 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +0100923 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200924 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200925 !stats_sock_parse_request(si, trash)) {
926 s->data_ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100927 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200928 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200929 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +0100930 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200931 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200932 }
Willy Tarreaubc4af052011-02-13 13:25:14 +0100933 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200934 /* if prompt is disabled, print help on empty lines,
935 * so that the user at least knows how to enable
936 * prompt and find help.
937 */
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200938 s->data_ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100939 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200940 }
941
942 /* re-adjust req buffer */
943 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200944 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200945 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200946 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200947 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +0100948 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200949 continue;
950 }
951
Willy Tarreaubc4af052011-02-13 13:25:14 +0100952 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200953 case STAT_CLI_PRINT:
954 if (buffer_feed(si->ib, s->data_ctx.cli.msg) < 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +0100955 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200956 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200957 case STAT_CLI_O_INFO:
Willy Tarreau24955a12009-10-04 11:54:04 +0200958 if (stats_dump_raw_to_buffer(s, res))
Willy Tarreaubc4af052011-02-13 13:25:14 +0100959 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200960 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200961 case STAT_CLI_O_SESS:
Willy Tarreau7e72a8f2009-10-03 23:55:14 +0200962 if (stats_dump_sess_to_buffer(s, res))
Willy Tarreaubc4af052011-02-13 13:25:14 +0100963 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200964 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200965 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau61b34732009-10-03 23:49:35 +0200966 if (stats_dump_errors_to_buffer(s, res))
Willy Tarreaubc4af052011-02-13 13:25:14 +0100967 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200968 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +0200969 case STAT_CLI_O_TAB:
970 if (stats_dump_table_to_buffer(s, res))
Willy Tarreaubc4af052011-02-13 13:25:14 +0100971 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +0200972 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200973 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +0100974 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200975 break;
976 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200977
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200978 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +0100979 if (si->applet.st0 == STAT_CLI_PROMPT) {
980 if (buffer_feed(si->ib, si->applet.st1 ? "\n> " : "\n") < 0)
981 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200982 }
983
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200984 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +0100985 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200986 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200987
988 /* Now we close the output if one of the writers did so,
989 * or if we're not in interactive mode and the request
990 * buffer is empty. This still allows pipelined requests
991 * to be sent in non-interactive mode.
992 */
Willy Tarreaubc4af052011-02-13 13:25:14 +0100993 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->applet.st1 && !req->send_max)) {
994 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200995 continue;
996 }
997
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200998 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +0100999 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001000 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001001 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001002
Willy Tarreaubc4af052011-02-13 13:25:14 +01001003 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001004 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1005 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1006 /* Other size has closed, let's abort if we have no more processing to do
1007 * and nothing more to consume. This is comparable to a broken pipe, so
1008 * we forward the close to the request side so that it flows upstream to
1009 * the client.
1010 */
1011 si->shutw(si);
1012 }
1013
Willy Tarreaubc4af052011-02-13 13:25:14 +01001014 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001015 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1016 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1017 /* We have no more processing to do, and nothing more to send, and
1018 * the client side has closed. So we'll forward this state downstream
1019 * on the response buffer.
1020 */
1021 si->shutr(si);
1022 res->flags |= BF_READ_NULL;
1023 }
1024
1025 /* update all other flags and resync with the other side */
1026 si->update(si);
1027
1028 /* we don't want to expire timeouts while we're processing requests */
1029 si->ib->rex = TICK_ETERNITY;
1030 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001031
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001032 out:
1033 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
1034 __FUNCTION__, __LINE__,
1035 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
1036
1037 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1038 /* check that we have released everything then unregister */
1039 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001040 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001041}
1042
Willy Tarreau24955a12009-10-04 11:54:04 +02001043/* This function is called to send output to the response buffer.
1044 * It dumps statistics onto the output buffer <rep> owned by session <s>.
1045 * s->data_ctx must have been zeroed first, and the flags properly set.
1046 * It returns 0 as long as it does not complete, non-zero upon completion.
1047 * Some states are not used but it makes the code more similar to other
1048 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001049 */
Willy Tarreau24955a12009-10-04 11:54:04 +02001050int stats_dump_raw_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001051{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001052 struct proxy *px;
1053 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001054 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001055
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001056 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +02001057
1058 switch (s->data_state) {
1059 case DATA_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001060 /* the function had not been called yet */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001061 s->data_state = DATA_ST_HEAD;
1062 /* fall through */
1063
1064 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001065 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001066 print_csv_header(&msg);
Willy Tarreau24955a12009-10-04 11:54:04 +02001067 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001068 return 0;
1069 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001070
1071 s->data_state = DATA_ST_INFO;
1072 /* fall through */
1073
1074 case DATA_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001075 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001076 if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001077 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001078 "Name: " PRODUCT_NAME "\n"
1079 "Version: " HAPROXY_VERSION "\n"
1080 "Release_date: " HAPROXY_DATE "\n"
1081 "Nbproc: %d\n"
1082 "Process_num: %d\n"
1083 "Pid: %d\n"
1084 "Uptime: %dd %dh%02dm%02ds\n"
1085 "Uptime_sec: %d\n"
1086 "Memmax_MB: %d\n"
1087 "Ulimit-n: %d\n"
1088 "Maxsock: %d\n"
1089 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001090 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001091 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001092 "PipesUsed: %d\n"
1093 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001094 "Tasks: %d\n"
1095 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001096 "node: %s\n"
1097 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001098 "",
1099 global.nbproc,
1100 relative_pid,
1101 pid,
1102 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1103 up,
1104 global.rlimit_memmax,
1105 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001106 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001107 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001108 nb_tasks_cur, run_queue_cur,
1109 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001110 );
Willy Tarreau24955a12009-10-04 11:54:04 +02001111 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001112 return 0;
1113 }
1114
Willy Tarreau3e76e722007-10-17 18:57:38 +02001115 s->data_ctx.stats.px = proxy;
1116 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001117
1118 s->data_ctx.stats.sv = NULL;
1119 s->data_ctx.stats.sv_st = 0;
1120
Willy Tarreau3e76e722007-10-17 18:57:38 +02001121 s->data_state = DATA_ST_LIST;
1122 /* fall through */
1123
1124 case DATA_ST_LIST:
1125 /* dump proxies */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001126 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +01001127 while (s->data_ctx.stats.px) {
1128 px = s->data_ctx.stats.px;
1129 /* skip the disabled proxies and non-networked ones */
1130 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001131 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001132 if (stats_dump_proxy(s, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001133 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001134 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001135
Willy Tarreaua8efd362008-01-03 10:19:15 +01001136 s->data_ctx.stats.px = px->next;
1137 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1138 }
1139 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001140 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001141
1142 s->data_state = DATA_ST_END;
1143 /* fall through */
1144
1145 case DATA_ST_END:
1146 s->data_state = DATA_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001147 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001148
1149 case DATA_ST_FIN:
1150 return 1;
1151
1152 default:
1153 /* unknown state ! */
Willy Tarreau24955a12009-10-04 11:54:04 +02001154 s->data_state = DATA_ST_FIN;
1155 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001156 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001157}
1158
1159
Cyril Bonté70be45d2010-10-12 00:14:35 +02001160/* We don't want to land on the posted stats page because a refresh will
1161 * repost the data. We don't want this to happen on accident so we redirect
1162 * the browse to the stats page with a GET.
1163 */
1164int stats_http_redir(struct session *s, struct buffer *rep, struct uri_auth *uri)
1165{
1166 struct chunk msg;
1167
1168 chunk_init(&msg, trash, sizeof(trash));
1169
1170 switch (s->data_state) {
1171 case DATA_ST_INIT:
1172 chunk_printf(&msg,
1173 "HTTP/1.0 303 See Other\r\n"
1174 "Cache-Control: no-cache\r\n"
1175 "Content-Type: text/plain\r\n"
1176 "Connection: close\r\n"
1177 "Location: %s;st=%s",
1178 uri->uri_prefix, s->data_ctx.stats.st_code);
1179 chunk_printf(&msg, "\r\n\r\n");
1180
1181 if (buffer_feed_chunk(rep, &msg) >= 0)
1182 return 0;
1183
1184 s->txn.status = 303;
1185
1186 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1187 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1188 if (!(s->flags & SN_FINST_MASK))
1189 s->flags |= SN_FINST_R;
1190
1191 s->data_state = DATA_ST_FIN;
1192 return 1;
1193 }
1194 return 1;
1195}
1196
1197
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001198/* This I/O handler runs as an applet embedded in a stream interface. It is
1199 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001200 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001201 * automatically unregisters itself once transfer is complete.
1202 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001203static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001204{
Willy Tarreaubc4af052011-02-13 13:25:14 +01001205 struct session *s = si->applet.private;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001206 struct buffer *req = si->ob;
1207 struct buffer *res = si->ib;
1208
1209 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1210 goto out;
1211
1212 /* check that the output is not closed */
1213 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001214 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001215
Willy Tarreaubc4af052011-02-13 13:25:14 +01001216 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001217 if (s->txn.meth == HTTP_METH_POST) {
1218 if (stats_http_redir(s, res, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001219 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001220 si->shutw(si);
1221 }
1222 } else {
1223 if (stats_dump_http(s, res, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001224 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001225 si->shutw(si);
1226 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001227 }
1228 }
1229
1230 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
1231 si->shutw(si);
1232
Willy Tarreaubc4af052011-02-13 13:25:14 +01001233 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001234 si->shutr(si);
1235 res->flags |= BF_READ_NULL;
1236 }
1237
1238 /* update all other flags and resync with the other side */
1239 si->update(si);
1240
1241 /* we don't want to expire timeouts while we're processing requests */
1242 si->ib->rex = TICK_ETERNITY;
1243 si->ob->wex = TICK_ETERNITY;
1244
1245 out:
1246 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1247 /* check that we have released everything then unregister */
1248 stream_int_unregister_handler(si);
1249 }
1250}
1251
1252
Willy Tarreau3e76e722007-10-17 18:57:38 +02001253/*
1254 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +02001255 * client socket shut down on input. It stops by itself by unsetting the
Willy Tarreau72b179a2008-08-28 16:01:32 +02001256 * BF_HIJACK flag from the buffer, which it uses to keep on being called
Willy Tarreau1ae3a052008-08-16 10:56:30 +02001257 * when there is free space in the buffer, of simply by letting an empty buffer
1258 * upon return.s->data_ctx must have been zeroed before the first call, and the
1259 * flags set. It returns 0 if it had to stop writing data and an I/O is needed,
1260 * 1 if the dump is finished and the session must be closed, or -1 in case of
1261 * any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001262 */
Willy Tarreau0a464892008-12-07 18:30:00 +01001263int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001264{
Willy Tarreau91861262007-10-17 17:06:05 +02001265 struct proxy *px;
1266 struct chunk msg;
1267 unsigned int up;
1268
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001269 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001270
1271 switch (s->data_state) {
1272 case DATA_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001273 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001274 "HTTP/1.0 200 OK\r\n"
1275 "Cache-Control: no-cache\r\n"
1276 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001277 "Content-Type: %s\r\n",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001278 (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001279
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001280 if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001281 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001282 uri->refresh);
1283
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001284 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001285
1286 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001287 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001288 return 0;
1289
Willy Tarreau91861262007-10-17 17:06:05 +02001290 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1291 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1292 if (!(s->flags & SN_FINST_MASK))
1293 s->flags |= SN_FINST_R;
1294
1295 if (s->txn.meth == HTTP_METH_HEAD) {
1296 /* that's all we return in case of HEAD request */
1297 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001298 return 1;
1299 }
1300
1301 s->data_state = DATA_ST_HEAD; /* let's start producing data */
1302 /* fall through */
1303
1304 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001305 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001306 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001307 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001308 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1309 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001310 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001311 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1312 "<style type=\"text/css\"><!--\n"
1313 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001314 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001315 " font-size: 12px;"
1316 " font-weight: normal;"
1317 " color: black;"
1318 " background: white;"
1319 "}\n"
1320 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001321 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001322 "}\n"
1323 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001324 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001325 " margin-bottom: 0.5em;"
1326 "}\n"
1327 "h2 {"
1328 " font-family: helvetica, arial;"
1329 " font-size: x-large;"
1330 " font-weight: bold;"
1331 " font-style: italic;"
1332 " color: #6020a0;"
1333 " margin-top: 0em;"
1334 " margin-bottom: 0em;"
1335 "}\n"
1336 "h3 {"
1337 " font-family: helvetica, arial;"
1338 " font-size: 16px;"
1339 " font-weight: bold;"
1340 " color: #b00040;"
1341 " background: #e8e8d0;"
1342 " margin-top: 0em;"
1343 " margin-bottom: 0em;"
1344 "}\n"
1345 "li {"
1346 " margin-top: 0.25em;"
1347 " margin-right: 2em;"
1348 "}\n"
1349 ".hr {margin-top: 0.25em;"
1350 " border-color: black;"
1351 " border-bottom-style: solid;"
1352 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001353 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001354 ".total {background: #20D0D0;color: #ffff80;}\n"
1355 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001356 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001357 ".backend {background: #e8e8d0;}\n"
1358 ".active0 {background: #ff9090;}\n"
1359 ".active1 {background: #ffd020;}\n"
1360 ".active2 {background: #ffffa0;}\n"
1361 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001362 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1363 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1364 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001365 ".backup0 {background: #ff9090;}\n"
1366 ".backup1 {background: #ff80ff;}\n"
1367 ".backup2 {background: #c060ff;}\n"
1368 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001369 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1370 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1371 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001372 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001373 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001374 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001375 "a.px:link {color: #ffff40; text-decoration: none;}"
1376 "a.px:visited {color: #ffff40; text-decoration: none;}"
1377 "a.px:hover {color: #ffffff; text-decoration: none;}"
1378 "a.lfsb:link {color: #000000; text-decoration: none;}"
1379 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1380 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1381 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001382 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001383 "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"
1384 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001385 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001386 "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 +02001387 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001388 "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 +02001389 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001390 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1391 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1392 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001393 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001394 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001395 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001396 (uri->flags&ST_SHNODE) ? " on " : "",
1397 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001398 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001399 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001400 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001401 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001402 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001403 return 0;
1404
1405 s->data_state = DATA_ST_INFO;
1406 /* fall through */
1407
1408 case DATA_ST_INFO:
1409 up = (now.tv_sec - start_date.tv_sec);
1410
1411 /* WARNING! this has to fit the first packet too.
1412 * We are around 3.5 kB, add adding entries will
1413 * become tricky if we want to support 4kB buffers !
1414 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001415 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001416 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001417 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1418 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001419 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001420 "<hr width=\"100%%\" class=\"hr\">\n"
1421 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001422 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001423 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001424 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001425 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1426 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
1427 "current conns = %d; current pipes = %d/%d<br>\n"
1428 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001429 "</td><td align=\"center\" nowrap>\n"
1430 "<table class=\"lgd\"><tr>\n"
1431 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1432 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1433 "</tr><tr>\n"
1434 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1435 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1436 "</tr><tr>\n"
1437 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1438 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1439 "</tr><tr>\n"
1440 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001441 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001442 "</tr><tr>\n"
1443 "<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 +02001444 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001445 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001446 "</td>"
1447 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1448 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1449 "",
1450 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001451 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1452 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1453 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001454 up / 86400, (up % 86400) / 3600,
1455 (up % 3600) / 60, (up % 60),
1456 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1457 global.rlimit_memmax ? " MB" : "",
1458 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001459 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001460 actconn, pipes_used, pipes_used+pipes_free,
1461 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +02001462 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001463
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001464 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001465 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001466 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1467 uri->uri_prefix,
1468 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001469 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001470 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001471 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001472 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1473 uri->uri_prefix,
1474 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001475 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001476
Willy Tarreau55bb8452007-10-17 18:44:57 +02001477 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001478 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001479 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001480 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1481 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001482 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001483 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001484 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001485 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001486 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1487 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001488 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001489 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001490 }
Willy Tarreau91861262007-10-17 17:06:05 +02001491
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001492 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001493 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
1494 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001495 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
1496 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001497
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001498 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02001499 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
1500 uri->uri_prefix,
1501 (uri->refresh > 0) ? ";norefresh" : "");
1502
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001503 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001504 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001505 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1506 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
1507 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
1508 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
1509 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
1510 "</ul>"
1511 "</td>"
1512 "</tr></table>\n"
1513 ""
1514 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001515
Cyril Bonté70be45d2010-10-12 00:14:35 +02001516 if (s->data_ctx.stats.st_code) {
1517 if (strcmp(s->data_ctx.stats.st_code, STAT_STATUS_DONE) == 0) {
1518 chunk_printf(&msg,
1519 "<p><div class=active3>"
1520 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1521 "Action processed successfully."
1522 "</div>\n", uri->uri_prefix);
1523 }
1524 else if (strcmp(s->data_ctx.stats.st_code, STAT_STATUS_NONE) == 0) {
1525 chunk_printf(&msg,
1526 "<p><div class=active2>"
1527 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1528 "Nothing has changed."
1529 "</div>\n", uri->uri_prefix);
1530 }
1531 else if (strcmp(s->data_ctx.stats.st_code, STAT_STATUS_EXCD) == 0) {
1532 chunk_printf(&msg,
1533 "<p><div class=active0>"
1534 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1535 "<b>Action not processed : the buffer couldn't store all the data.<br>"
1536 "You should retry with less servers at a time.</b>"
1537 "</div>\n", uri->uri_prefix);
1538 }
Cyril Bonté474be412010-10-12 00:14:36 +02001539 else if (strcmp(s->data_ctx.stats.st_code, STAT_STATUS_DENY) == 0) {
1540 chunk_printf(&msg,
1541 "<p><div class=active0>"
1542 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1543 "<b>Action denied.</b>"
1544 "</div>\n", uri->uri_prefix);
1545 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001546 else {
1547 chunk_printf(&msg,
1548 "<p><div class=active6>"
1549 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
1550 "Unexpected result."
1551 "</div>\n", uri->uri_prefix);
1552 }
1553 chunk_printf(&msg,"<p>\n");
1554 }
1555
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001556 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001557 return 0;
1558 }
Willy Tarreau91861262007-10-17 17:06:05 +02001559
Willy Tarreau91861262007-10-17 17:06:05 +02001560 s->data_ctx.stats.px = proxy;
1561 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1562 s->data_state = DATA_ST_LIST;
1563 /* fall through */
1564
1565 case DATA_ST_LIST:
1566 /* dump proxies */
1567 while (s->data_ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001568 if (buffer_almost_full(rep))
1569 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02001570 px = s->data_ctx.stats.px;
1571 /* skip the disabled proxies and non-networked ones */
1572 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001573 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001574 return 0;
1575
1576 s->data_ctx.stats.px = px->next;
1577 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1578 }
1579 /* here, we just have reached the last proxy */
1580
1581 s->data_state = DATA_ST_END;
1582 /* fall through */
1583
1584 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001585 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001586 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001587 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001588 return 0;
1589 }
Willy Tarreau91861262007-10-17 17:06:05 +02001590
1591 s->data_state = DATA_ST_FIN;
1592 /* fall through */
1593
1594 case DATA_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02001595 return 1;
1596
1597 default:
1598 /* unknown state ! */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001599 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001600 return -1;
1601 }
1602}
1603
1604
1605/*
1606 * Dumps statistics for a proxy.
1607 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1608 * ot non-zero if everything completed.
1609 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001610int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001611{
1612 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001613 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001614 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02001615 struct chunk msg;
1616
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001617 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001618
1619 switch (s->data_ctx.stats.px_st) {
1620 case DATA_ST_PX_INIT:
1621 /* we are on a new proxy */
1622
1623 if (uri && uri->scope) {
1624 /* we have a limited scope, we have to check the proxy name */
1625 struct stat_scope *scope;
1626 int len;
1627
1628 len = strlen(px->id);
1629 scope = uri->scope;
1630
1631 while (scope) {
1632 /* match exact proxy name */
1633 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1634 break;
1635
1636 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001637 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001638 break;
1639 scope = scope->next;
1640 }
1641
1642 /* proxy name not found : don't dump anything */
1643 if (scope == NULL)
1644 return 1;
1645 }
1646
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001647 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001648 (px->uuid != s->data_ctx.stats.iid))
1649 return 1;
1650
Willy Tarreau91861262007-10-17 17:06:05 +02001651 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
1652 /* fall through */
1653
1654 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001655 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté474be412010-10-12 00:14:36 +02001656 if (px->cap & PR_CAP_BE && px->srv && (s->data_ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001657 /* A form to enable/disable this proxy servers */
1658 chunk_printf(&msg,
1659 "<form action=\"%s\" method=\"post\">",
1660 uri->uri_prefix);
1661 }
1662
Willy Tarreau55bb8452007-10-17 18:44:57 +02001663 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001664 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001665 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001666 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001667 "<th class=\"pxname\" width=\"10%%\"");
1668
1669 if (uri->flags&ST_SHLGNDS) {
1670 /* cap, mode, id */
1671 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
1672 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1673 px->uuid);
1674
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001675 chunk_printf(&msg, "\"");
1676 }
1677
1678 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001679 ">%s<a name=\"%s\"></a>"
1680 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001681 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001682 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001683 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001684 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001685 "<tr class=\"titre\">",
1686 (uri->flags & ST_SHLGNDS)?"<u>":"",
1687 px->id, px->id, px->id,
1688 (uri->flags & ST_SHLGNDS)?"</u>":"",
1689 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1690
Cyril Bonté474be412010-10-12 00:14:36 +02001691 if (px->cap & PR_CAP_BE && px->srv && (s->data_ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001692 /* Column heading for Enable or Disable server */
1693 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
1694 }
1695
1696 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001697 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001698 "<th colspan=3>Queue</th>"
1699 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001700 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001701 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001702 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001703 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001704 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001705 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001706 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001707 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001708 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001709 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001710 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001711 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1712 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001713 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001714
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001715 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001716 return 0;
1717 }
Willy Tarreau91861262007-10-17 17:06:05 +02001718
1719 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
1720 /* fall through */
1721
1722 case DATA_ST_PX_FE:
1723 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001724 if ((px->cap & PR_CAP_FE) &&
1725 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
1726 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001727 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001728 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02001729 "<tr class=\"frontend\">");
1730
Cyril Bonté474be412010-10-12 00:14:36 +02001731 if (px->cap & PR_CAP_BE && px->srv && (s->data_ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001732 /* Column sub-heading for Enable or Disable server */
1733 chunk_printf(&msg, "<td></td>");
1734 }
1735
1736 chunk_printf(&msg,
1737 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001738 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02001739 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
1740 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01001741 "",
1742 px->id, px->id);
1743
1744 if (px->mode == PR_MODE_HTTP) {
1745 chunk_printf(&msg,
1746 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01001747 "<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 +01001748 "",
1749 read_freq_ctr(&px->fe_req_per_sec),
1750 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
1751 px->counters.fe_rps_max,
Cyril Bonté4d179eb2010-07-23 18:59:11 +02001752 U2H1(px->counters.fe_sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01001753 LIM2A2(px->fe_sps_lim, "-"));
1754 } else {
1755 chunk_printf(&msg,
1756 /* sessions rate : current, max, limit */
1757 "<td>%s</td><td>%s</td><td>%s</td>"
1758 "",
1759 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
1760 U2H1(px->counters.fe_sps_max), LIM2A2(px->fe_sps_lim, "-"));
1761 }
1762
1763 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001764 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001765 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001766 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001767 "",
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001768 U2H3(px->feconn), U2H4(px->counters.feconn_max), U2H5(px->maxconn));
1769
1770 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1771 if (px->mode == PR_MODE_HTTP) {
1772 int i;
1773
Willy Tarreaub44939a2010-02-26 11:35:39 +01001774 chunk_printf(&msg, " title=\"%lld requests:", px->counters.cum_fe_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001775
1776 for (i = 1; i < 6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01001777 chunk_printf(&msg, " %dxx=%lld,", i, px->counters.fe.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001778
Willy Tarreau24657792010-02-26 10:30:28 +01001779 chunk_printf(&msg, " other=%lld\"", px->counters.fe.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001780 }
1781
1782 chunk_printf(&msg,
1783 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01001784 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001785 /* bytes : in, out */
1786 "<td>%s</td><td>%s</td>"
1787 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001788 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreaub36b4242010-06-04 20:59:39 +02001789 U2H6(px->counters.cum_fesess),
Willy Tarreaue0454092010-02-26 12:29:07 +01001790 (px->mode == PR_MODE_HTTP)?"</u>":"",
1791 U2H7(px->counters.bytes_in), U2H8(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001792
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001793 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001794 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001795 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001796 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001797 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001798 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001799 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001800 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001801 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001802 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001803 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02001804 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001805 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
1806 U2H2(px->counters.failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +02001807 px->state == PR_STRUN ? "OPEN" :
1808 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001809 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001810 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001811 /* pxid, name, queue cur, queue max, */
1812 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001813 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001814 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001815 /* bytes : in, out */
1816 "%lld,%lld,"
1817 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001818 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001819 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001820 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001821 /* warnings: retries, redispatches */
1822 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001823 /* server status : reflect frontend status */
1824 "%s,"
1825 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001826 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001827 /* pid, iid, sid, throttle, lbtot, tracked, type */
1828 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001829 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001830 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001831 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001832 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02001833 px->id,
Willy Tarreaub36b4242010-06-04 20:59:39 +02001834 px->feconn, px->counters.feconn_max, px->maxconn, px->counters.cum_fesess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001835 px->counters.bytes_in, px->counters.bytes_out,
1836 px->counters.denied_req, px->counters.denied_resp,
1837 px->counters.failed_req,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001838 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +01001839 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01001840 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001841 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001842 px->fe_sps_lim, px->counters.fe_sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001843
1844 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1845 if (px->mode == PR_MODE_HTTP) {
1846 int i;
1847
1848 for (i=1; i<6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01001849 chunk_printf(&msg, "%lld,", px->counters.fe.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001850
Willy Tarreau24657792010-02-26 10:30:28 +01001851 chunk_printf(&msg, "%lld,", px->counters.fe.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001852 } else {
1853 chunk_printf(&msg, ",,,,,,");
1854 }
1855
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001856 /* failed health analyses */
1857 chunk_printf(&msg, ",");
1858
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001859 /* requests : req_rate, req_rate_max, req_tot, */
1860 chunk_printf(&msg, "%u,%u,%lld,",
1861 read_freq_ctr(&px->fe_req_per_sec),
1862 px->counters.fe_rps_max, px->counters.cum_fe_req);
1863
Willy Tarreauae526782010-03-04 20:34:23 +01001864 /* errors: cli_aborts, srv_aborts */
1865 chunk_printf(&msg, ",,");
1866
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001867 /* finish with EOL */
1868 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001869 }
Willy Tarreau91861262007-10-17 17:06:05 +02001870
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001871 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001872 return 0;
1873 }
1874
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001875 s->data_ctx.stats.l = px->listen; /* may be NULL */
1876 s->data_ctx.stats.px_st = DATA_ST_PX_LI;
1877 /* fall through */
1878
1879 case DATA_ST_PX_LI:
1880 /* stats.l has been initialized above */
1881 for (; s->data_ctx.stats.l != NULL; s->data_ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001882 if (buffer_almost_full(rep))
1883 return 0;
1884
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001885 l = s->data_ctx.stats.l;
1886 if (!l->counters)
1887 continue;
1888
1889 if (s->data_ctx.stats.flags & STAT_BOUND) {
1890 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SO)))
1891 break;
1892
1893 if (s->data_ctx.stats.sid != -1 && l->luid != s->data_ctx.stats.sid)
1894 continue;
1895 }
1896
1897 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001898 chunk_printf(&msg, "<tr class=socket>");
Cyril Bonté474be412010-10-12 00:14:36 +02001899 if (px->cap & PR_CAP_BE && px->srv && (s->data_ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001900 /* Column sub-heading for Enable or Disable server */
1901 chunk_printf(&msg, "<td></td>");
1902 }
1903 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001904
1905 if (uri->flags&ST_SHLGNDS) {
1906 char str[INET6_ADDRSTRLEN], *fmt = NULL;
1907 int port;
1908
1909 chunk_printf(&msg, " title=\"IP: ");
1910
1911 port = (l->addr.ss_family == AF_INET6)
1912 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
1913 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
1914
1915 if (l->addr.ss_family == AF_INET) {
1916 if (inet_ntop(AF_INET,
1917 (const void *)&((struct sockaddr_in *)&l->addr)->sin_addr,
1918 str, sizeof(str)))
1919 fmt = "%s:%d";
1920 } else {
1921 if (inet_ntop(AF_INET6,
1922 (const void *)&((struct sockaddr_in6 *)(&l->addr))->sin6_addr,
1923 str, sizeof(str)))
1924 fmt = "[%s]:%d";
1925 }
1926
1927 if (fmt)
1928 chunk_printf(&msg, fmt, str, port);
1929 else
1930 chunk_printf(&msg, "(%s)", strerror(errno));
1931
1932 /* id */
1933 chunk_printf(&msg, ", id: %d", l->luid);
1934
1935 chunk_printf(&msg, "\"");
1936 }
1937
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001938 chunk_printf(&msg,
1939 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01001940 ">%s<a name=\"%s/+%s\"></a>"
1941 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001942 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001943 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001944 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001945 "<td>%s</td><td>%s</td><td>%s</td>"
1946 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001947 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001948 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001949 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001950 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01001951 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01001952 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001953 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
1954 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
1955
1956 chunk_printf(&msg,
1957 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001958 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001959 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001960 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001961 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001962 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001963 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001964 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001965 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001966 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001967 "",
1968 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
1969 U2H2(l->counters->failed_req),
1970 (l->nbconn < l->maxconn) ? "OPEN" : "FULL");
1971 } else {
1972 chunk_printf(&msg,
1973 /* pxid, name, queue cur, queue max, */
1974 "%s,%s,,,"
1975 /* sessions: current, max, limit, total */
1976 "%d,%d,%d,%lld,"
1977 /* bytes: in, out */
1978 "%lld,%lld,"
1979 /* denied: req, resp */
1980 "%lld,%lld,"
1981 /* errors: request, connect, response */
1982 "%lld,,,"
1983 /* warnings: retries, redispatches */
1984 ",,"
1985 /* server status: reflect listener status */
1986 "%s,"
1987 /* rest of server: nothing */
1988 ",,,,,,,,"
1989 /* pid, iid, sid, throttle, lbtot, tracked, type */
1990 "%d,%d,%d,,,,%d,"
1991 /* rate, rate_lim, rate_max */
1992 ",,,"
1993 /* check_status, check_code, check_duration */
1994 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001995 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1996 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001997 /* failed health analyses */
1998 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001999 /* requests : req_rate, req_rate_max, req_tot, */
2000 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002001 /* errors: cli_aborts, srv_aborts */
2002 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002003 "\n",
2004 px->id, l->name,
2005 l->nbconn, l->counters->conn_max,
2006 l->maxconn, l->counters->cum_conn,
2007 l->counters->bytes_in, l->counters->bytes_out,
2008 l->counters->denied_req, l->counters->denied_resp,
2009 l->counters->failed_req,
2010 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2011 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2012 }
2013
2014 if (buffer_feed_chunk(rep, &msg) >= 0)
2015 return 0;
2016 }
2017
Willy Tarreau91861262007-10-17 17:06:05 +02002018 s->data_ctx.stats.sv = px->srv; /* may be NULL */
2019 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
2020 /* fall through */
2021
2022 case DATA_ST_PX_SV:
2023 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002024 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002025 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 +02002026
Willy Tarreau4e33d862009-10-11 23:35:10 +02002027 if (buffer_almost_full(rep))
2028 return 0;
2029
Willy Tarreau91861262007-10-17 17:06:05 +02002030 sv = s->data_ctx.stats.sv;
2031
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002032 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002033 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
2034 break;
2035
2036 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
2037 continue;
2038 }
2039
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002040 if (sv->tracked)
2041 svs = sv->tracked;
2042 else
2043 svs = sv;
2044
Willy Tarreau91861262007-10-17 17:06:05 +02002045 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002046 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002047 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002048 else if (svs->state & SRV_RUNNING) {
2049 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002050 sv_state = 3; /* UP */
2051 else
2052 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002053
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002054 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002055 sv_state += 2;
2056 }
Willy Tarreau91861262007-10-17 17:06:05 +02002057 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002058 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002059 sv_state = 1; /* going up */
2060 else
2061 sv_state = 0; /* DOWN */
2062
Cyril Bonté0dae5852010-02-03 00:26:28 +01002063 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002064 /* do not report servers which are DOWN */
2065 s->data_ctx.stats.sv = sv->next;
2066 continue;
2067 }
2068
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002069 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002070 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2071 "UP %d/%d &darr;", "UP",
2072 "NOLB %d/%d &darr;", "NOLB",
2073 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002074 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002075 chunk_printf(&msg,
2076 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002077 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002078 );
2079 }
2080 else {
2081 chunk_printf(&msg,
2082 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002083 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002084 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2085 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002086
Cyril Bonté474be412010-10-12 00:14:36 +02002087 if (px->cap & PR_CAP_BE && px->srv && (s->data_ctx.stats.flags & STAT_ADMIN)) {
2088 chunk_printf(&msg,
2089 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2090 sv->id);
2091 }
2092
2093 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002094
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002095 if (uri->flags&ST_SHLGNDS) {
2096 char str[INET6_ADDRSTRLEN];
2097
2098 chunk_printf(&msg, " title=\"IP: ");
2099
2100 /* IP */
2101 if (inet_ntop(sv->addr.sin_family, &sv->addr.sin_addr, str, sizeof(str)))
2102 chunk_printf(&msg, "%s:%d", str, htons(sv->addr.sin_port));
2103 else
2104 chunk_printf(&msg, "(%s)", strerror(errno));
2105
2106 /* id */
2107 chunk_printf(&msg, ", id: %d", sv->puid);
2108
2109 /* cookie */
2110 if (sv->cookie) {
2111 struct chunk src;
2112
2113 chunk_printf(&msg, ", cookie: '");
2114
2115 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2116 chunk_htmlencode(&msg, &src);
2117
2118 chunk_printf(&msg, "'");
2119 }
2120
2121 chunk_printf(&msg, "\"");
2122 }
2123
2124 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002125 ">%s<a name=\"%s/%s\"></a>"
2126 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002127 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002128 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002129 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002130 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002131 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002132 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002133 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002134 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002135 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002136 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002137 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002138 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2139 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002140 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2141
2142 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2143 if (px->mode == PR_MODE_HTTP) {
2144 int i;
2145
2146 chunk_printf(&msg, " title=\"rsp codes:");
2147
2148 for (i = 1; i < 6; i++)
2149 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2150
2151 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2152 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002153
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002154 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002155 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002156 ">%s%s%s</td><td>%s</td>",
2157 (px->mode == PR_MODE_HTTP)?"<u>":"",
2158 U2H0(sv->counters.cum_sess),
2159 (px->mode == PR_MODE_HTTP)?"</u>":"",
2160 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002161
2162 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002163 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002164 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002165 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002166 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002167 /* errors : request, connect */
2168 "<td></td><td>%s</td>"
2169 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002170 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002171 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002172 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002173 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002174 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2175 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002176 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002177 sv->counters.cli_aborts,
2178 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002179 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002180 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002181
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002182 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002183 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002184
Cyril Bontécd19e512010-01-31 22:34:03 +01002185 if (sv->state & SRV_MAINTAIN) {
2186 chunk_printf(&msg, "%s ",
2187 human_time(now.tv_sec - sv->last_change, 1));
2188 chunk_printf(&msg, "MAINT");
2189 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002190 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2191 chunk_printf(&msg, "%s ",
2192 human_time(now.tv_sec - svs->last_change, 1));
2193 chunk_printf(&msg, "MAINT(via)");
2194 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002195 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002196 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002197 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002198
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002199 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002200 srv_hlt_st[sv_state],
2201 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2202 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002203 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002204
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002205 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002206 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002207 get_check_status_description(sv->check_status));
2208
2209 if (*sv->check_desc) {
2210 struct chunk src;
2211
2212 chunk_printf(&msg, ": ");
2213
2214 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2215 chunk_htmlencode(&msg, &src);
2216 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002217
Willy Tarreaue0454092010-02-26 12:29:07 +01002218 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002219 tv_iszero(&sv->check_start)?"":"* ",
2220 get_check_status_info(sv->check_status));
2221
2222 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002223 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002224
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002225 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002226 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002227 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002228 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002229
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002230 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002231 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002232 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002233 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002234 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002235 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002236 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002237 (sv->state & SRV_BACKUP) ? "-" : "Y",
2238 (sv->state & SRV_BACKUP) ? "Y" : "-");
2239
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002240 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002241 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002242 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002243 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2244
2245 if (svs->observe)
2246 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2247
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002248 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002249 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002250 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002251 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002252 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002253 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002254 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002255 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2256 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002257 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002258 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002259 "<td colspan=3></td>");
2260
2261 /* throttle */
2262 if ((sv->state & SRV_WARMINGUP) &&
2263 now.tv_sec < sv->last_change + sv->slowstart &&
2264 now.tv_sec >= sv->last_change) {
2265 unsigned int ratio;
2266 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002267 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002268 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002269 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002270 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002271 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002272 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002273 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002274 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2275 "UP %d/%d,", "UP,",
2276 "NOLB %d/%d,", "NOLB,",
2277 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002278 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002279 /* pxid, name */
2280 "%s,%s,"
2281 /* queue : current, max */
2282 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002283 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002284 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002285 /* bytes : in, out */
2286 "%lld,%lld,"
2287 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002288 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002289 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002290 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002291 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002292 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002293 "",
2294 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002295 sv->nbpend, sv->counters.nbpend_max,
2296 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002297 sv->counters.bytes_in, sv->counters.bytes_out,
2298 sv->counters.failed_secu,
2299 sv->counters.failed_conns, sv->counters.failed_resp,
2300 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002301
Willy Tarreau55bb8452007-10-17 18:44:57 +02002302 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002303 if (sv->state & SRV_MAINTAIN) {
2304 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002305 }
2306 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2307 chunk_printf(&msg, "MAINT(via),");
2308 }
2309 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002310 chunk_printf(&msg,
2311 srv_hlt_st[sv_state],
2312 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2313 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2314 }
Willy Tarreau91861262007-10-17 17:06:05 +02002315
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002316 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002317 /* weight, active, backup */
2318 "%d,%d,%d,"
2319 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002320 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002321 (sv->state & SRV_BACKUP) ? 0 : 1,
2322 (sv->state & SRV_BACKUP) ? 1 : 0);
2323
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002324 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002325 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002326 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002327 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002328 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002329 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002330 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002331 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002332 ",,,,");
2333
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002334 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002335 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002336 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002337 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002338 LIM2A0(sv->maxqueue, ""),
2339 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002340
2341 /* throttle */
2342 if ((sv->state & SRV_WARMINGUP) &&
2343 now.tv_sec < sv->last_change + sv->slowstart &&
2344 now.tv_sec >= sv->last_change) {
2345 unsigned int ratio;
2346 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002347 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002348 }
2349
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002350 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002351 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002352
2353 /* tracked */
2354 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002355 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002356 sv->tracked->proxy->id, sv->tracked->id);
2357 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002358 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002359
Willy Tarreau7f062c42009-03-05 18:43:00 +01002360 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002361 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002362
2363 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002364 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002365 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002366 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002367
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002368 if (sv->state & SRV_CHECKED) {
2369 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002370 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002371
2372 /* check_code */
2373 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002374 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002375 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002376 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002377
2378 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002379 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002380 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002381 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002382 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002383
2384 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002385 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002386 }
2387
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002388 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2389 if (px->mode == PR_MODE_HTTP) {
2390 int i;
2391
2392 for (i=1; i<6; i++)
2393 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2394
2395 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2396 } else {
2397 chunk_printf(&msg, ",,,,,,");
2398 }
2399
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002400 /* failed health analyses */
2401 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2402
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002403 /* requests : req_rate, req_rate_max, req_tot, */
2404 chunk_printf(&msg, ",,,");
2405
Willy Tarreauae526782010-03-04 20:34:23 +01002406 /* errors: cli_aborts, srv_aborts */
2407 chunk_printf(&msg, "%lld,%lld,",
2408 sv->counters.cli_aborts, sv->counters.srv_aborts);
2409
Willy Tarreau7f062c42009-03-05 18:43:00 +01002410 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002411 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002412 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002413 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002414 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002415 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002416
2417 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
2418 /* fall through */
2419
2420 case DATA_ST_PX_BE:
2421 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002422 if ((px->cap & PR_CAP_BE) &&
2423 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2424 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002425 chunk_printf(&msg, "<tr class=\"backend\">");
Cyril Bonté474be412010-10-12 00:14:36 +02002426 if (px->cap & PR_CAP_BE && px->srv && (s->data_ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002427 /* Column sub-heading for Enable or Disable server */
2428 chunk_printf(&msg, "<td></td>");
2429 }
2430 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002431
2432 if (uri->flags&ST_SHLGNDS) {
2433 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002434 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002435 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002436
2437 /* cookie */
2438 if (px->cookie_name) {
2439 struct chunk src;
2440
2441 chunk_printf(&msg, ", cookie: '");
2442
2443 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2444 chunk_htmlencode(&msg, &src);
2445
2446 chunk_printf(&msg, "'");
2447 }
2448
2449 chunk_printf(&msg, "\"");
2450
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002451 }
2452
2453 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002454 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01002455 ">%s<a name=\"%s/Backend\"></a>"
2456 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002457 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002458 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002459 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002460 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002461 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002462 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002463 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002464 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002465 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->counters.nbpend_max),
2466 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->counters.be_sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02002467
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002468 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002469 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002470 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002471 "<td"
2472 "",
2473 U2H2(px->beconn), U2H3(px->counters.beconn_max), U2H4(px->fullconn));
2474
2475 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2476 if (px->mode == PR_MODE_HTTP) {
2477 int i;
2478
2479 chunk_printf(&msg, " title=\"rsp codes:");
2480
2481 for (i = 1; i < 6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01002482 chunk_printf(&msg, " %dxx=%lld", i, px->counters.be.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002483
Willy Tarreau24657792010-02-26 10:30:28 +01002484 chunk_printf(&msg, " other=%lld\"", px->counters.be.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002485 }
2486
2487 chunk_printf(&msg,
2488 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002489 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002490 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002491 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002492 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002493 (px->mode == PR_MODE_HTTP)?"<u>":"",
2494 U2H6(px->counters.cum_beconn),
2495 (px->mode == PR_MODE_HTTP)?"</u>":"",
2496 U2H7(px->counters.cum_lbconn),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002497 U2H8(px->counters.bytes_in), U2H9(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002498
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002499 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002500 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002501 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002502 /* errors : request, connect */
2503 "<td></td><td>%s</td>"
2504 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002505 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002506 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002507 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002508 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02002509 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002510 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02002511 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002512 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2513 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002514 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002515 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
Willy Tarreauae526782010-03-04 20:34:23 +01002516 U2H2(px->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002517 px->counters.cli_aborts,
2518 px->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002519 U2H5(px->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002520 px->counters.retries, px->counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002521 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01002522 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2523 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01002524 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002525 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002526
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002527 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002528 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002529 "<td class=ac>&nbsp;</td><td>%d</td>"
2530 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002531 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002532 "</tr>",
2533 px->down_trans,
2534 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002535 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002536 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002537 /* pxid, name */
2538 "%s,BACKEND,"
2539 /* queue : current, max */
2540 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002541 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002542 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002543 /* bytes : in, out */
2544 "%lld,%lld,"
2545 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002546 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002547 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002548 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002549 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002550 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002551 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02002552 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002553 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02002554 * active and backups. */
2555 "%s,"
2556 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002557 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002558 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002559 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002560 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002561 /* rate, rate_lim, rate_max, */
2562 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002563 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002564 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002565 px->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002566 px->nbpend /* or px->totpend ? */, px->counters.nbpend_max,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002567 px->beconn, px->counters.beconn_max, px->fullconn, px->counters.cum_beconn,
2568 px->counters.bytes_in, px->counters.bytes_out,
2569 px->counters.denied_req, px->counters.denied_resp,
2570 px->counters.failed_conns, px->counters.failed_resp,
2571 px->counters.retries, px->counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01002572 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01002573 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002574 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002575 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01002576 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002577 relative_pid, px->uuid,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002578 px->counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002579 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002580 px->counters.be_sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002581
2582 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2583 if (px->mode == PR_MODE_HTTP) {
2584 int i;
2585
2586 for (i=1; i<6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01002587 chunk_printf(&msg, "%lld,", px->counters.be.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002588
Willy Tarreau24657792010-02-26 10:30:28 +01002589 chunk_printf(&msg, "%lld,", px->counters.be.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002590 } else {
2591 chunk_printf(&msg, ",,,,,,");
2592 }
2593
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002594 /* failed health analyses */
2595 chunk_printf(&msg, ",");
2596
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002597 /* requests : req_rate, req_rate_max, req_tot, */
2598 chunk_printf(&msg, ",,,");
2599
Willy Tarreauae526782010-03-04 20:34:23 +01002600 /* errors: cli_aborts, srv_aborts */
2601 chunk_printf(&msg, "%lld,%lld,",
2602 px->counters.cli_aborts, px->counters.srv_aborts);
2603
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002604 /* finish with EOL */
2605 chunk_printf(&msg, "\n");
2606
Willy Tarreau55bb8452007-10-17 18:44:57 +02002607 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002608 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002609 return 0;
2610 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002611
Willy Tarreau91861262007-10-17 17:06:05 +02002612 s->data_ctx.stats.px_st = DATA_ST_PX_END;
2613 /* fall through */
2614
2615 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002616 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002617 chunk_printf(&msg, "</table>");
2618
Cyril Bonté474be412010-10-12 00:14:36 +02002619 if (px->cap & PR_CAP_BE && px->srv && (s->data_ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002620 /* close the form used to enable/disable this proxy servers */
2621 chunk_printf(&msg,
2622 "Choose the action to perform on the checked servers : "
2623 "<select name=action>"
2624 "<option value=\"\"></option>"
2625 "<option value=\"disable\">Disable</option>"
2626 "<option value=\"enable\">Enable</option>"
2627 "</select>"
2628 "<input type=\"hidden\" name=\"b\" value=\"%s\">"
2629 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2630 "</form>",
2631 px->id);
2632 }
2633
2634 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02002635
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002636 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002637 return 0;
2638 }
Willy Tarreau91861262007-10-17 17:06:05 +02002639
2640 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
2641 /* fall through */
2642
2643 case DATA_ST_PX_FIN:
2644 return 1;
2645
2646 default:
2647 /* unknown state, we should put an abort() here ! */
2648 return 1;
2649 }
2650}
2651
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002652/* This function is called to send output to the response buffer. It dumps a
2653 * complete session state onto the output buffer <rep>. The session has to be
2654 * set in data_ctx.sess.target. It returns 0 if the output buffer is full and
2655 * it needs to be called again, otherwise non-zero. It is designed to be called
2656 * from stats_dump_sess_to_buffer() below.
2657 */
2658
2659/* returns 1 if dump is not complete */
2660int stats_dump_full_sess_to_buffer(struct session *s, struct buffer *rep)
2661{
2662 struct tm tm;
2663 struct chunk msg;
2664 struct session *sess;
2665 extern const char *monthname[12];
2666 char pn[INET6_ADDRSTRLEN];
2667
2668 chunk_init(&msg, trash, sizeof(trash));
2669 sess = s->data_ctx.sess.target;
2670
2671 if (s->data_ctx.sess.section > 0 && s->data_ctx.sess.uid != sess->uniq_id) {
2672 /* session changed, no need to go any further */
2673 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
2674 if (buffer_feed_chunk(rep, &msg) >= 0)
2675 return 0;
2676 s->data_ctx.sess.target = NULL;
2677 s->data_ctx.sess.uid = 0;
2678 return 1;
2679 }
2680
2681 switch (s->data_ctx.sess.section) {
2682 case 0: /* main status of the session */
2683 s->data_ctx.sess.uid = sess->uniq_id;
2684 s->data_ctx.sess.section = 1;
2685 /* fall through */
2686
2687 case 1:
2688 chunk_printf(&msg,
2689 "%p: id=%u, proto=%s",
2690 sess,
2691 sess->uniq_id,
2692 sess->listener->proto->name);
2693
2694 switch (sess->listener->proto->sock_family) {
2695 case AF_INET:
2696 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002697 (const void *)&((struct sockaddr_in *)&sess->si[0].addr.c.from)->sin_addr,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002698 pn, sizeof(pn));
2699
2700 chunk_printf(&msg,
2701 " source=%s:%d\n",
2702 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002703 ntohs(((struct sockaddr_in *)&sess->si[0].addr.c.from)->sin_port));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002704 break;
2705 case AF_INET6:
2706 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002707 (const void *)&((struct sockaddr_in6 *)(&sess->si[0].addr.c.from))->sin6_addr,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002708 pn, sizeof(pn));
2709
2710 chunk_printf(&msg,
2711 " source=%s:%d\n",
2712 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002713 ntohs(((struct sockaddr_in6 *)&sess->si[0].addr.c.from)->sin6_port));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002714 break;
2715 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02002716 chunk_printf(&msg,
2717 " source=unix:%d\n", sess->listener->luid);
2718 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002719 default:
2720 /* no more information to print right now */
2721 chunk_printf(&msg, "\n");
2722 break;
2723 }
2724
2725 chunk_printf(&msg,
2726 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02002727 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002728
2729 chunk_printf(&msg,
2730 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)\n",
2731 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
2732 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
2733 sess->listener ? sess->listener->luid : 0);
2734
2735 chunk_printf(&msg,
2736 " backend=%s (id=%u mode=%s) server=%s (id=%u)\n",
2737 sess->be->id, sess->be->uuid, sess->be->mode ? "http" : "tcp",
2738 sess->srv ? sess->srv->id : "<none>",
2739 sess->srv ? sess->srv->puid : 0);
2740
2741 chunk_printf(&msg,
2742 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
2743 sess->task,
2744 sess->task->state,
2745 sess->task->nice, sess->task->calls,
2746 sess->task->expire ?
2747 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
2748 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
2749 TICKS_TO_MS(1000)) : "<NEVER>",
2750 task_in_rq(sess->task) ? ", running" : "");
2751
2752 get_localtime(sess->logs.accept_date.tv_sec, &tm);
2753 chunk_printf(&msg,
2754 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
2755 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2756 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
2757 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
2758
2759 chunk_printf(&msg,
2760 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
2761 &sess->si[0],
2762 sess->si[0].state,
2763 sess->si[0].flags,
2764 sess->si[0].fd,
2765 sess->si[0].exp ?
2766 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
2767 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
2768 TICKS_TO_MS(1000)) : "<NEVER>",
2769 sess->si[0].err_type);
2770
2771 chunk_printf(&msg,
2772 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
2773 &sess->si[1],
2774 sess->si[1].state,
2775 sess->si[1].flags,
2776 sess->si[1].fd,
2777 sess->si[1].exp ?
2778 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
2779 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
2780 TICKS_TO_MS(1000)) : "<NEVER>",
2781 sess->si[1].err_type);
2782
2783 chunk_printf(&msg,
2784 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
2785 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
2786 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
2787
2788
2789 chunk_printf(&msg,
2790 " req=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%ld)\n"
2791 " an_exp=%s",
2792 sess->req,
2793 sess->req->flags, sess->req->analysers,
2794 sess->req->l, sess->req->send_max,
2795 sess->req->pipe ? sess->req->pipe->data : 0,
2796 sess->req->to_forward,
2797 sess->req->analyse_exp ?
2798 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
2799 TICKS_TO_MS(1000)) : "<NEVER>");
2800
2801 chunk_printf(&msg,
2802 " rex=%s",
2803 sess->req->rex ?
2804 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
2805 TICKS_TO_MS(1000)) : "<NEVER>");
2806
2807 chunk_printf(&msg,
2808 " wex=%s\n"
2809 " data=%p r=%d w=%d lr=%d total=%lld\n",
2810 sess->req->wex ?
2811 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
2812 TICKS_TO_MS(1000)) : "<NEVER>",
2813 sess->req->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01002814 (int)(sess->req->r - sess->req->data),
2815 (int)(sess->req->w - sess->req->data),
2816 (int)(sess->req->lr - sess->req->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002817 sess->req->total);
2818
2819 chunk_printf(&msg,
2820 " res=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%ld)\n"
2821 " an_exp=%s",
2822 sess->rep,
2823 sess->rep->flags, sess->rep->analysers,
2824 sess->rep->l, sess->rep->send_max,
2825 sess->rep->pipe ? sess->rep->pipe->data : 0,
2826 sess->rep->to_forward,
2827 sess->rep->analyse_exp ?
2828 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
2829 TICKS_TO_MS(1000)) : "<NEVER>");
2830
2831 chunk_printf(&msg,
2832 " rex=%s",
2833 sess->rep->rex ?
2834 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
2835 TICKS_TO_MS(1000)) : "<NEVER>");
2836
2837 chunk_printf(&msg,
2838 " wex=%s\n"
2839 " data=%p r=%d w=%d lr=%d total=%lld\n",
2840 sess->rep->wex ?
2841 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
2842 TICKS_TO_MS(1000)) : "<NEVER>",
2843 sess->rep->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01002844 (int)(sess->rep->r - sess->rep->data),
2845 (int)(sess->rep->w - sess->rep->data),
2846 (int)(sess->rep->lr - sess->rep->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002847 sess->rep->total);
2848
2849 if (buffer_feed_chunk(rep, &msg) >= 0)
2850 return 0;
2851
2852 /* use other states to dump the contents */
2853 }
2854 /* end of dump */
2855 s->data_ctx.sess.uid = 0;
2856 return 1;
2857}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002858
2859/* This function is called to send output to the response buffer.
2860 * It dumps the sessions states onto the output buffer <rep>.
2861 * Expects to be called with client socket shut down on input.
2862 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002863 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002864 */
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002865int stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002866{
2867 struct chunk msg;
2868
2869 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
2870 /* If we're forced to shut down, we might have to remove our
2871 * reference to the last session being dumped.
2872 */
2873 if (s->data_state == DATA_ST_LIST) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002874 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002875 LIST_DEL(&s->data_ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002876 LIST_INIT(&s->data_ctx.sess.bref.users);
2877 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002878 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002879 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002880 }
2881
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002882 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002883
2884 switch (s->data_state) {
2885 case DATA_ST_INIT:
2886 /* the function had not been called yet, let's prepare the
2887 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002888 * pointer to the first in the global list. When a target
2889 * session is being destroyed, it is responsible for updating
2890 * this pointer. We know we have reached the end when this
2891 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002892 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002893 LIST_INIT(&s->data_ctx.sess.bref.users);
2894 s->data_ctx.sess.bref.ref = sessions.n;
2895 s->data_state = DATA_ST_LIST;
2896 /* fall through */
2897
2898 case DATA_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002899 /* first, let's detach the back-ref from a possible previous session */
2900 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
2901 LIST_DEL(&s->data_ctx.sess.bref.users);
2902 LIST_INIT(&s->data_ctx.sess.bref.users);
2903 }
2904
2905 /* and start from where we stopped */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002906 while (s->data_ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01002907 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002908 struct session *curr_sess;
2909
2910 curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list);
2911
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002912 if (s->data_ctx.sess.target) {
2913 if (s->data_ctx.sess.target != curr_sess)
2914 goto next_sess;
2915
2916 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
2917 /* call the proper dump() function and return if we're missing space */
2918 if (!stats_dump_full_sess_to_buffer(s, rep))
2919 return 0;
2920
2921 /* session dump complete */
2922 LIST_DEL(&s->data_ctx.sess.bref.users);
2923 LIST_INIT(&s->data_ctx.sess.bref.users);
2924 s->data_ctx.sess.target = NULL;
2925 break;
2926 }
2927
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002928 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002929 "%p: proto=%s",
2930 curr_sess,
2931 curr_sess->listener->proto->name);
2932
2933 switch (curr_sess->listener->proto->sock_family) {
2934 case AF_INET:
2935 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002936 (const void *)&((struct sockaddr_in *)&curr_sess->si[0].addr.c.from)->sin_addr,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002937 pn, sizeof(pn));
2938
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002939 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002940 " src=%s:%d fe=%s be=%s srv=%s",
2941 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002942 ntohs(((struct sockaddr_in *)&curr_sess->si[0].addr.c.from)->sin_port),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002943 curr_sess->fe->id,
2944 curr_sess->be->id,
2945 curr_sess->srv ? curr_sess->srv->id : "<none>"
2946 );
2947 break;
2948 case AF_INET6:
2949 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002950 (const void *)&((struct sockaddr_in6 *)(&curr_sess->si[0].addr.c.from))->sin6_addr,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002951 pn, sizeof(pn));
2952
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002953 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002954 " src=%s:%d fe=%s be=%s srv=%s",
2955 pn,
Willy Tarreau957c0a52011-03-03 17:42:23 +01002956 ntohs(((struct sockaddr_in6 *)&curr_sess->si[0].addr.c.from)->sin6_port),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002957 curr_sess->fe->id,
2958 curr_sess->be->id,
2959 curr_sess->srv ? curr_sess->srv->id : "<none>"
2960 );
2961
2962 break;
2963 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02002964 chunk_printf(&msg,
2965 " src=unix:%d fe=%s be=%s srv=%s",
2966 curr_sess->listener->luid,
2967 curr_sess->fe->id,
2968 curr_sess->be->id,
2969 curr_sess->srv ? curr_sess->srv->id : "<none>"
2970 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002971 break;
2972 }
2973
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002974 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02002975 " ts=%02x age=%s calls=%d",
2976 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01002977 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
2978 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002979
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002980 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002981 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
2982 curr_sess->req->flags,
2983 curr_sess->req->l,
2984 curr_sess->req->analysers,
2985 curr_sess->req->rex ?
2986 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
2987 TICKS_TO_MS(1000)) : "");
2988
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002989 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002990 ",wx=%s",
2991 curr_sess->req->wex ?
2992 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
2993 TICKS_TO_MS(1000)) : "");
2994
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002995 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002996 ",ax=%s]",
2997 curr_sess->req->analyse_exp ?
2998 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
2999 TICKS_TO_MS(1000)) : "");
3000
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003001 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003002 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
3003 curr_sess->rep->flags,
3004 curr_sess->rep->l,
3005 curr_sess->rep->analysers,
3006 curr_sess->rep->rex ?
3007 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3008 TICKS_TO_MS(1000)) : "");
3009
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003010 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003011 ",wx=%s",
3012 curr_sess->rep->wex ?
3013 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3014 TICKS_TO_MS(1000)) : "");
3015
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003016 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003017 ",ax=%s]",
3018 curr_sess->rep->analyse_exp ?
3019 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3020 TICKS_TO_MS(1000)) : "");
3021
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003022 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003023 " s0=[%d,%1xh,fd=%d,ex=%s]",
3024 curr_sess->si[0].state,
3025 curr_sess->si[0].flags,
3026 curr_sess->si[0].fd,
3027 curr_sess->si[0].exp ?
3028 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3029 TICKS_TO_MS(1000)) : "");
3030
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003031 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003032 " s1=[%d,%1xh,fd=%d,ex=%s]",
3033 curr_sess->si[1].state,
3034 curr_sess->si[1].flags,
3035 curr_sess->si[1].fd,
3036 curr_sess->si[1].exp ?
3037 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3038 TICKS_TO_MS(1000)) : "");
3039
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003040 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003041 " exp=%s",
3042 curr_sess->task->expire ?
3043 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3044 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003045 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003046 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003047
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003048 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003049
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003050 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003051 /* let's try again later from this session. We add ourselves into
3052 * this session's users so that it can remove us upon termination.
3053 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003054 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003055 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003056 }
3057
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003058 next_sess:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003059 s->data_ctx.sess.bref.ref = curr_sess->list.n;
3060 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003061
3062 if (s->data_ctx.sess.target) {
3063 /* specified session not found */
3064 if (s->data_ctx.sess.section > 0)
3065 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3066 else
3067 chunk_printf(&msg, "Session not found.\n");
3068
3069 if (buffer_feed_chunk(rep, &msg) >= 0)
3070 return 0;
3071
3072 s->data_ctx.sess.target = NULL;
3073 s->data_ctx.sess.uid = 0;
3074 return 1;
3075 }
3076
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003077 s->data_state = DATA_ST_FIN;
3078 /* fall through */
3079
3080 default:
3081 s->data_state = DATA_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003082 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003083 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003084}
3085
Emeric Brun1e029aa2010-09-23 18:12:53 +02003086
3087/* print a string of text buffer to <out>. The format is :
3088 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
3089 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
3090 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
3091 */
3092
3093static int dump_text(struct chunk *out, const char *buf, int bsize)
3094{
3095 unsigned char c;
3096 int ptr = 0;
3097
3098 while (buf[ptr] && ptr < bsize) {
3099 c = buf[ptr];
3100 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
3101 if (out->len > out->size - 1)
3102 break;
3103 out->str[out->len++] = c;
3104 }
3105 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
3106 if (out->len > out->size - 2)
3107 break;
3108 out->str[out->len++] = '\\';
3109 switch (c) {
3110 case ' ': c = ' '; break;
3111 case '\t': c = 't'; break;
3112 case '\n': c = 'n'; break;
3113 case '\r': c = 'r'; break;
3114 case '\e': c = 'e'; break;
3115 case '\\': c = '\\'; break;
3116 }
3117 out->str[out->len++] = c;
3118 }
3119 else {
3120 if (out->len > out->size - 4)
3121 break;
3122 out->str[out->len++] = '\\';
3123 out->str[out->len++] = 'x';
3124 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3125 out->str[out->len++] = hextab[c & 0xF];
3126 }
3127 ptr++;
3128 }
3129
3130 return ptr;
3131}
3132
3133/* print a buffer in hexa.
3134 * Print stopped if <bsize> is reached, or if no more place in the chunk.
3135 */
3136
3137static int dump_binary(struct chunk *out, const char *buf, int bsize)
3138{
3139 unsigned char c;
3140 int ptr = 0;
3141
3142 while (ptr < bsize) {
3143 c = buf[ptr];
3144
3145 if (out->len > out->size - 2)
3146 break;
3147 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3148 out->str[out->len++] = hextab[c & 0xF];
3149
3150 ptr++;
3151 }
3152 return ptr;
3153}
3154
Willy Tarreau69f58c82010-07-12 17:55:33 +02003155/* This function is called to send output to the response buffer.
3156 * It dumps the tables states onto the output buffer <rep>.
3157 * Expects to be called with client socket shut down on input.
3158 * s->data_ctx must have been zeroed first, and the flags properly set.
3159 * It returns 0 as long as it does not complete, non-zero upon completion.
3160 */
3161int stats_dump_table_to_buffer(struct session *s, struct buffer *rep)
3162{
3163 struct chunk msg;
3164 struct ebmb_node *eb;
3165 int dt;
3166
3167 /*
3168 * We have 3 possible states in s->data_state :
3169 * - DATA_ST_INIT : the first call
3170 * - DATA_ST_INFO : the proxy pointer points to the next table to
3171 * dump, the entry pointer is NULL ;
3172 * - DATA_ST_LIST : the proxy pointer points to the current table
3173 * and the entry pointer points to the next entry to be dumped,
3174 * and the refcount on the next entry is held ;
3175 * - DATA_ST_END : nothing left to dump, the buffer may contain some
3176 * data though.
3177 */
3178
3179 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
3180 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreauf6efda12010-08-03 20:34:06 +02003181 if (s->data_state == DATA_ST_LIST) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003182 s->data_ctx.table.entry->ref_cnt--;
Willy Tarreauf6efda12010-08-03 20:34:06 +02003183 stksess_kill_if_expired(&s->data_ctx.table.proxy->table, s->data_ctx.table.entry);
3184 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003185 return 1;
3186 }
3187
3188 chunk_init(&msg, trash, sizeof(trash));
3189
3190 while (s->data_state != DATA_ST_FIN) {
3191 switch (s->data_state) {
3192 case DATA_ST_INIT:
3193 s->data_ctx.table.proxy = s->data_ctx.table.target;
3194 if (!s->data_ctx.table.proxy)
3195 s->data_ctx.table.proxy = proxy;
3196
3197 s->data_ctx.table.entry = NULL;
3198 s->data_state = DATA_ST_INFO;
3199 break;
3200
3201 case DATA_ST_INFO:
3202 if (!s->data_ctx.table.proxy ||
3203 (s->data_ctx.table.target &&
3204 s->data_ctx.table.proxy != s->data_ctx.table.target)) {
3205 s->data_state = DATA_ST_END;
3206 break;
3207 }
3208
3209 if (s->data_ctx.table.proxy->table.size) {
Emeric Brun1e029aa2010-09-23 18:12:53 +02003210 chunk_printf(&msg, "# table: %s, type: %s, size:%d, used:%d\n",
Willy Tarreau69f58c82010-07-12 17:55:33 +02003211 s->data_ctx.table.proxy->id,
Emeric Brun1e029aa2010-09-23 18:12:53 +02003212 stktable_types[s->data_ctx.table.proxy->table.type].kw,
Willy Tarreau69f58c82010-07-12 17:55:33 +02003213 s->data_ctx.table.proxy->table.size,
3214 s->data_ctx.table.proxy->table.current);
3215
3216 /* any other information should be dumped here */
3217
3218 if (s->data_ctx.table.target &&
3219 s->listener->perm.ux.level < ACCESS_LVL_OPER)
3220 chunk_printf(&msg, "# contents not dumped due to insufficient privileges\n");
3221
3222 if (buffer_feed_chunk(rep, &msg) >= 0)
3223 return 0;
3224
3225 if (s->data_ctx.table.target &&
3226 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
3227 /* dump entries only if table explicitly requested */
3228 eb = ebmb_first(&s->data_ctx.table.proxy->table.keys);
3229 if (eb) {
3230 s->data_ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3231 s->data_ctx.table.entry->ref_cnt++;
3232 s->data_state = DATA_ST_LIST;
3233 break;
3234 }
3235 }
3236 }
3237 s->data_ctx.table.proxy = s->data_ctx.table.proxy->next;
3238 break;
3239
3240 case DATA_ST_LIST:
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003241 if (s->data_ctx.table.data_type >= 0) {
3242 /* we're filtering on some data contents */
3243 void *ptr;
3244 long long data;
3245
3246 dt = s->data_ctx.table.data_type;
3247 ptr = stktable_data_ptr(&s->data_ctx.table.proxy->table,
3248 s->data_ctx.table.entry,
3249 dt);
3250
3251 data = 0;
3252 switch (stktable_data_types[dt].std_type) {
3253 case STD_T_SINT:
3254 data = stktable_data_cast(ptr, std_t_sint);
3255 break;
3256 case STD_T_UINT:
3257 data = stktable_data_cast(ptr, std_t_uint);
3258 break;
3259 case STD_T_ULL:
3260 data = stktable_data_cast(ptr, std_t_ull);
3261 break;
3262 case STD_T_FRQP:
3263 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
3264 s->data_ctx.table.proxy->table.data_arg[dt].u);
3265 break;
3266 }
3267
3268 /* skip the entry if the data does not match the test and the value */
3269 if ((data < s->data_ctx.table.value &&
3270 (s->data_ctx.table.data_op == STD_OP_EQ ||
3271 s->data_ctx.table.data_op == STD_OP_GT ||
3272 s->data_ctx.table.data_op == STD_OP_GE)) ||
3273 (data == s->data_ctx.table.value &&
3274 (s->data_ctx.table.data_op == STD_OP_NE ||
3275 s->data_ctx.table.data_op == STD_OP_GT ||
3276 s->data_ctx.table.data_op == STD_OP_LT)) ||
3277 (data > s->data_ctx.table.value &&
3278 (s->data_ctx.table.data_op == STD_OP_EQ ||
3279 s->data_ctx.table.data_op == STD_OP_LT ||
3280 s->data_ctx.table.data_op == STD_OP_LE)))
3281 goto skip_entry;
3282 }
3283
Willy Tarreau69f58c82010-07-12 17:55:33 +02003284 chunk_printf(&msg, "%p:", s->data_ctx.table.entry);
3285
3286 if (s->data_ctx.table.proxy->table.type == STKTABLE_TYPE_IP) {
3287 char addr[16];
3288 inet_ntop(AF_INET,
3289 (const void *)&s->data_ctx.table.entry->key.key,
3290 addr, sizeof(addr));
3291 chunk_printf(&msg, " key=%s", addr);
3292 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003293 else if (s->data_ctx.table.proxy->table.type == STKTABLE_TYPE_INTEGER) {
3294 chunk_printf(&msg, " key=%u", *(unsigned int *)s->data_ctx.table.entry->key.key);
3295 }
3296 else if (s->data_ctx.table.proxy->table.type == STKTABLE_TYPE_STRING) {
3297 chunk_printf(&msg, " key=");
3298 dump_text(&msg, (const char *)s->data_ctx.table.entry->key.key, s->data_ctx.table.proxy->table.key_size);
3299 }
3300 else {
3301 chunk_printf(&msg, " key=");
3302 dump_binary(&msg, (const char *)s->data_ctx.table.entry->key.key, s->data_ctx.table.proxy->table.key_size);
3303 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003304
3305 chunk_printf(&msg, " use=%d exp=%d",
3306 s->data_ctx.table.entry->ref_cnt - 1,
3307 tick_remain(now_ms, s->data_ctx.table.entry->expire));
3308
3309 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
3310 void *ptr;
3311
3312 if (s->data_ctx.table.proxy->table.data_ofs[dt] == 0)
3313 continue;
3314 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
3315 chunk_printf(&msg, " %s(%d)=",
3316 stktable_data_types[dt].name,
3317 s->data_ctx.table.proxy->table.data_arg[dt].u);
3318 else
3319 chunk_printf(&msg, " %s=", stktable_data_types[dt].name);
3320
3321 ptr = stktable_data_ptr(&s->data_ctx.table.proxy->table,
3322 s->data_ctx.table.entry,
3323 dt);
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02003324 switch (stktable_data_types[dt].std_type) {
3325 case STD_T_SINT:
3326 chunk_printf(&msg, "%d", stktable_data_cast(ptr, std_t_sint));
Willy Tarreau69f58c82010-07-12 17:55:33 +02003327 break;
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02003328 case STD_T_UINT:
3329 chunk_printf(&msg, "%u", stktable_data_cast(ptr, std_t_uint));
3330 break;
3331 case STD_T_ULL:
3332 chunk_printf(&msg, "%lld", stktable_data_cast(ptr, std_t_ull));
3333 break;
3334 case STD_T_FRQP:
Willy Tarreau69f58c82010-07-12 17:55:33 +02003335 chunk_printf(&msg, "%d",
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02003336 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau69f58c82010-07-12 17:55:33 +02003337 s->data_ctx.table.proxy->table.data_arg[dt].u));
3338 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003339 }
3340 }
3341 chunk_printf(&msg, "\n");
3342
3343 if (buffer_feed_chunk(rep, &msg) >= 0)
3344 return 0;
3345
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003346 skip_entry:
Willy Tarreau69f58c82010-07-12 17:55:33 +02003347 s->data_ctx.table.entry->ref_cnt--;
3348
3349 eb = ebmb_next(&s->data_ctx.table.entry->key);
3350 if (eb) {
Willy Tarreauf6efda12010-08-03 20:34:06 +02003351 struct stksess *old = s->data_ctx.table.entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003352 s->data_ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003353 stksess_kill_if_expired(&s->data_ctx.table.proxy->table, old);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003354 s->data_ctx.table.entry->ref_cnt++;
3355 break;
3356 }
3357
Willy Tarreauf6efda12010-08-03 20:34:06 +02003358 stksess_kill_if_expired(&s->data_ctx.table.proxy->table, s->data_ctx.table.entry);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003359 s->data_ctx.table.proxy = s->data_ctx.table.proxy->next;
3360 s->data_state = DATA_ST_INFO;
3361 break;
3362
3363 case DATA_ST_END:
3364 s->data_state = DATA_ST_FIN;
3365 break;
3366 }
3367 }
3368 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003369}
3370
Willy Tarreaud426a182010-03-05 14:58:26 +01003371/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003372 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3373 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3374 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3375 * lines are respected within the limit of 70 output chars. Lines that are
3376 * continuation of a previous truncated line begin with "+" instead of " "
3377 * after the offset. The new pointer is returned.
3378 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003379static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3380 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003381{
3382 int end;
3383 unsigned char c;
3384
3385 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003386 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003387 return ptr;
3388
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003389 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003390
Willy Tarreaud426a182010-03-05 14:58:26 +01003391 while (ptr < len && ptr < bsize) {
3392 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003393 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003394 if (out->len > end - 2)
3395 break;
3396 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003397 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003398 if (out->len > end - 3)
3399 break;
3400 out->str[out->len++] = '\\';
3401 switch (c) {
3402 case '\t': c = 't'; break;
3403 case '\n': c = 'n'; break;
3404 case '\r': c = 'r'; break;
3405 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003406 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003407 }
3408 out->str[out->len++] = c;
3409 } else {
3410 if (out->len > end - 5)
3411 break;
3412 out->str[out->len++] = '\\';
3413 out->str[out->len++] = 'x';
3414 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3415 out->str[out->len++] = hextab[c & 0xF];
3416 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003417 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003418 /* we had a line break, let's return now */
3419 out->str[out->len++] = '\n';
3420 *line = ptr;
3421 return ptr;
3422 }
3423 }
3424 /* we have an incomplete line, we return it as-is */
3425 out->str[out->len++] = '\n';
3426 return ptr;
3427}
3428
3429/* This function is called to send output to the response buffer.
3430 * It dumps the errors logged in proxies onto the output buffer <rep>.
3431 * Expects to be called with client socket shut down on input.
3432 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau61b34732009-10-03 23:49:35 +02003433 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003434 */
Willy Tarreau61b34732009-10-03 23:49:35 +02003435int stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003436{
3437 extern const char *monthname[12];
3438 struct chunk msg;
3439
Willy Tarreau61b34732009-10-03 23:49:35 +02003440 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW)))
3441 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003442
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003443 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003444
3445 if (!s->data_ctx.errors.px) {
3446 /* the function had not been called yet, let's prepare the
3447 * buffer for a response.
3448 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003449 struct tm tm;
3450
3451 get_localtime(date.tv_sec, &tm);
3452 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3453 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3454 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3455 error_snapshot_id);
3456
3457 if (buffer_feed_chunk(rep, &msg) >= 0) {
3458 /* Socket buffer full. Let's try again later from the same point */
3459 return 0;
3460 }
3461
Willy Tarreau74808cb2009-03-04 15:53:18 +01003462 s->data_ctx.errors.px = proxy;
3463 s->data_ctx.errors.buf = 0;
3464 s->data_ctx.errors.bol = 0;
3465 s->data_ctx.errors.ptr = -1;
3466 }
3467
3468 /* we have two inner loops here, one for the proxy, the other one for
3469 * the buffer.
3470 */
3471 while (s->data_ctx.errors.px) {
3472 struct error_snapshot *es;
3473
3474 if (s->data_ctx.errors.buf == 0)
3475 es = &s->data_ctx.errors.px->invalid_req;
3476 else
3477 es = &s->data_ctx.errors.px->invalid_rep;
3478
3479 if (!es->when.tv_sec)
3480 goto next;
3481
3482 if (s->data_ctx.errors.iid >= 0 &&
3483 s->data_ctx.errors.px->uuid != s->data_ctx.errors.iid &&
3484 es->oe->uuid != s->data_ctx.errors.iid)
3485 goto next;
3486
3487 if (s->data_ctx.errors.ptr < 0) {
3488 /* just print headers now */
3489
3490 char pn[INET6_ADDRSTRLEN];
3491 struct tm tm;
3492
3493 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003494 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003495 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003496 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003497
3498
3499 if (es->src.ss_family == AF_INET)
3500 inet_ntop(AF_INET,
3501 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
3502 pn, sizeof(pn));
3503 else
3504 inet_ntop(AF_INET6,
3505 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
3506 pn, sizeof(pn));
3507
3508 switch (s->data_ctx.errors.buf) {
3509 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003510 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003511 " frontend %s (#%d): invalid request\n"
3512 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003513 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003514 " request length %d bytes, error at position %d:\n \n",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003515 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
Willy Tarreau3fd6cec2010-03-25 06:45:07 +01003516 pn, es->sid, (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3517 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003518 es->srv ? es->srv->id : "<NONE>",
3519 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003520 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003521 es->len, es->pos);
3522 break;
3523 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003524 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003525 " backend %s (#%d) : invalid response\n"
3526 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003527 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003528 " response length %d bytes, error at position %d:\n \n",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003529 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
3530 pn, es->sid, es->oe->id, es->oe->uuid,
3531 es->srv ? es->srv->id : "<NONE>",
3532 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003533 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003534 es->len, es->pos);
3535 break;
3536 }
3537
Willy Tarreau61b34732009-10-03 23:49:35 +02003538 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003539 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003540 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003541 }
3542 s->data_ctx.errors.ptr = 0;
3543 s->data_ctx.errors.sid = es->sid;
3544 }
3545
3546 if (s->data_ctx.errors.sid != es->sid) {
3547 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003548 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003549 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau61b34732009-10-03 23:49:35 +02003550 if (buffer_feed_chunk(rep, &msg) >= 0)
3551 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003552 goto next;
3553 }
3554
3555 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau61b34732009-10-03 23:49:35 +02003556 while (s->data_ctx.errors.ptr < es->len && s->data_ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003557 int newptr;
3558 int newline;
3559
3560 newline = s->data_ctx.errors.bol;
Willy Tarreaud426a182010-03-05 14:58:26 +01003561 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, s->data_ctx.errors.ptr);
Willy Tarreau74808cb2009-03-04 15:53:18 +01003562 if (newptr == s->data_ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02003563 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003564
Willy Tarreau61b34732009-10-03 23:49:35 +02003565 if (buffer_feed_chunk(rep, &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 }
3569 s->data_ctx.errors.ptr = newptr;
3570 s->data_ctx.errors.bol = newline;
3571 };
3572 next:
3573 s->data_ctx.errors.bol = 0;
3574 s->data_ctx.errors.ptr = -1;
3575 s->data_ctx.errors.buf++;
3576 if (s->data_ctx.errors.buf > 1) {
3577 s->data_ctx.errors.buf = 0;
3578 s->data_ctx.errors.px = s->data_ctx.errors.px->next;
3579 }
3580 }
3581
3582 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02003583 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003584}
3585
Willy Tarreaub24281b2011-02-13 13:16:36 +01003586struct si_applet http_stats_applet = {
3587 .name = "<STATS>", /* used for logging */
3588 .fct = http_stats_io_handler,
3589};
3590
3591struct si_applet cli_applet = {
3592 .name = "<CLI>", /* used for logging */
3593 .fct = cli_io_handler,
3594};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003595
Willy Tarreau10522fd2008-07-09 20:12:41 +02003596static struct cfg_kw_list cfg_kws = {{ },{
3597 { CFG_GLOBAL, "stats", stats_parse_global },
3598 { 0, NULL, NULL },
3599}};
3600
3601__attribute__((constructor))
3602static void __dumpstats_module_init(void)
3603{
3604 cfg_register_keywords(&cfg_kws);
3605}
3606
Willy Tarreau91861262007-10-17 17:06:05 +02003607/*
3608 * Local variables:
3609 * c-indent-level: 8
3610 * c-basic-offset: 8
3611 * End:
3612 */