blob: 97de0e9f26948b1b68e5361c669d2a8c2f5dadb2 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
2 * Functions dedicated to statistics output
3 *
Willy Tarreaua206fa92009-01-25 14:02:00 +01004 * Copyright 2000-2009 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 Tarreaua206fa92009-01-25 14:02:00 +010047#include <proto/pipe.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020048#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020049#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020050#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020051#include <proto/server.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010052#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010053#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020054
Willy Tarreau5ca791d2009-08-16 19:06:42 +020055const char stats_sock_usage_msg[] =
56 "Unknown command. Please enter one of the following commands only :\n"
57 " show info : report information about the running process\n"
58 " show stat : report counters for each proxy and server\n"
59 " show errors : report last request and response errors for each proxy\n"
60 " show sess : report the list of current sessions\n"
61 "\n";
62
63const struct chunk stats_sock_usage = {
64 .str = (char *)&stats_sock_usage_msg,
65 .len = sizeof(stats_sock_usage_msg)-1
66};
67
Willy Tarreaufbee7132007-10-18 13:53:22 +020068/* This function parses a "stats" statement in the "global" section. It returns
69 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
70 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
71 * zero included. The trailing '\n' must not be written. The function must be
72 * called with <args> pointing to the first word after "stats".
73 */
Willy Tarreau10522fd2008-07-09 20:12:41 +020074static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
75 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +020076{
Willy Tarreau10522fd2008-07-09 20:12:41 +020077 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +020078 if (!strcmp(args[0], "socket")) {
79 struct sockaddr_un su;
80 int cur_arg;
81
82 if (*args[1] == 0) {
83 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
84 return -1;
85 }
86
87 if (global.stats_sock.state != LI_NEW) {
88 snprintf(err, errlen, "'stats socket' already specified in global section");
89 return -1;
90 }
91
92 su.sun_family = AF_UNIX;
93 strncpy(su.sun_path, args[1], sizeof(su.sun_path));
94 su.sun_path[sizeof(su.sun_path) - 1] = 0;
95 memcpy(&global.stats_sock.addr, &su, sizeof(su)); // guaranteed to fit
96
Willy Tarreau89a63132009-08-16 17:41:45 +020097 if (!global.stats_fe) {
98 if ((global.stats_fe = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) {
99 snprintf(err, errlen, "out of memory");
100 return -1;
101 }
102
103 LIST_INIT(&global.stats_fe->pendconns);
104 LIST_INIT(&global.stats_fe->acl);
105 LIST_INIT(&global.stats_fe->block_cond);
106 LIST_INIT(&global.stats_fe->redirect_rules);
107 LIST_INIT(&global.stats_fe->mon_fail_cond);
108 LIST_INIT(&global.stats_fe->switching_rules);
109 LIST_INIT(&global.stats_fe->tcp_req.inspect_rules);
110
111 /* Timeouts are defined as -1, so we cannot use the zeroed area
112 * as a default value.
113 */
114 proxy_reset_timeouts(global.stats_fe);
115
116 global.stats_fe->last_change = now.tv_sec;
117 global.stats_fe->id = strdup("GLOBAL");
118 global.stats_fe->cap = PR_CAP_FE;
119 }
120
Willy Tarreaufbee7132007-10-18 13:53:22 +0200121 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100122 global.stats_sock.options = LI_O_NONE;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200123 global.stats_sock.accept = uxst_event_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200124 global.stats_sock.handler = process_session;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200125 global.stats_sock.analysers = AN_REQ_STATS_SOCK;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200126 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreau89a63132009-08-16 17:41:45 +0200127 global.stats_sock.private = global.stats_fe; /* must point to the frontend */
128
129 global.stats_fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
130 global.stats_sock.timeout = &global.stats_fe->timeout.client;
131
132 global.stats_sock.next = global.stats_fe->listen;
133 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200134
135 cur_arg = 2;
136 while (*args[cur_arg]) {
137 if (!strcmp(args[cur_arg], "uid")) {
138 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
139 cur_arg += 2;
140 }
141 else if (!strcmp(args[cur_arg], "gid")) {
142 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
143 cur_arg += 2;
144 }
145 else if (!strcmp(args[cur_arg], "mode")) {
146 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
147 cur_arg += 2;
148 }
149 else if (!strcmp(args[cur_arg], "user")) {
150 struct passwd *user;
151 user = getpwnam(args[cur_arg + 1]);
152 if (!user) {
153 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
154 args[cur_arg + 1]);
155 return -1;
156 }
157 global.stats_sock.perm.ux.uid = user->pw_uid;
158 cur_arg += 2;
159 }
160 else if (!strcmp(args[cur_arg], "group")) {
161 struct group *group;
162 group = getgrnam(args[cur_arg + 1]);
163 if (!group) {
164 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
165 args[cur_arg + 1]);
166 return -1;
167 }
168 global.stats_sock.perm.ux.gid = group->gr_gid;
169 cur_arg += 2;
170 }
171 else {
172 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', and 'mode'");
173 return -1;
174 }
175 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100176
Willy Tarreaufbee7132007-10-18 13:53:22 +0200177 uxst_add_listener(&global.stats_sock);
178 global.maxsock++;
179 }
180 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100181 unsigned timeout;
182 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
183
184 if (res) {
185 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
186 return -1;
187 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200188
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100189 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200190 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
191 return -1;
192 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200193 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200194 }
195 else if (!strcmp(args[0], "maxconn")) {
196 int maxconn = atol(args[1]);
197
198 if (maxconn <= 0) {
199 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
200 return -1;
201 }
202 global.maxsock -= global.stats_sock.maxconn;
203 global.stats_sock.maxconn = maxconn;
204 global.maxsock += global.stats_sock.maxconn;
205 }
206 else {
207 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
208 return -1;
209 }
210 return 0;
211}
212
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100213int print_csv_header(struct chunk *msg, int size)
214{
215 return chunk_printf(msg, size,
216 "# pxname,svname,"
217 "qcur,qmax,"
218 "scur,smax,slim,stot,"
219 "bin,bout,"
220 "dreq,dresp,"
221 "ereq,econ,eresp,"
222 "wretr,wredis,"
223 "status,weight,act,bck,"
224 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200225 "pid,iid,sid,throttle,lbtot,tracked,type,"
226 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200227 "check_status,check_code,check_duration,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100228 "\n");
229}
230
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200231/* Parses the request line in <cmd> and possibly starts dumping stats on
232 * s->rep with the hijack bit set. Returns 1 if OK, 0 in case of any error.
233 * The line is modified after parsing.
234 */
235int stats_sock_parse_request(struct session *s, char *line)
236{
237 char *args[MAX_STATS_ARGS + 1];
238 int arg;
239
240 while (isspace((unsigned char)*line))
241 line++;
242
243 arg = 0;
244 args[arg] = line;
245
246 while (*line && arg < MAX_STATS_ARGS) {
247 if (isspace((unsigned char)*line)) {
248 *line++ = '\0';
249
250 while (isspace((unsigned char)*line))
251 line++;
252
253 args[++arg] = line;
254 continue;
255 }
256
257 line++;
258 }
259
260 while (++arg <= MAX_STATS_ARGS)
261 args[arg] = line;
262
263 if (strcmp(args[0], "show") == 0) {
264 if (strcmp(args[1], "stat") == 0) {
265 if (*args[2] && *args[3] && *args[4]) {
266 s->data_ctx.stats.flags |= STAT_BOUND;
267 s->data_ctx.stats.iid = atoi(args[2]);
268 s->data_ctx.stats.type = atoi(args[3]);
269 s->data_ctx.stats.sid = atoi(args[4]);
270 }
271
272 s->data_ctx.stats.flags |= STAT_SHOW_STAT;
273 s->data_ctx.stats.flags |= STAT_FMT_CSV;
274 s->ana_state = STATS_ST_REP;
Willy Tarreau56a560a2009-09-22 19:27:35 +0200275 stream_int_retnclose(s->rep->cons, NULL);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200276 buffer_install_hijacker(s, s->rep, stats_dump_raw_to_buffer);
277 }
278 else if (strcmp(args[1], "info") == 0) {
279 s->data_ctx.stats.flags |= STAT_SHOW_INFO;
280 s->data_ctx.stats.flags |= STAT_FMT_CSV;
281 s->ana_state = STATS_ST_REP;
Willy Tarreau56a560a2009-09-22 19:27:35 +0200282 stream_int_retnclose(s->rep->cons, NULL);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200283 buffer_install_hijacker(s, s->rep, stats_dump_raw_to_buffer);
284 }
285 else if (strcmp(args[1], "sess") == 0) {
286 s->ana_state = STATS_ST_REP;
Willy Tarreau56a560a2009-09-22 19:27:35 +0200287 stream_int_retnclose(s->rep->cons, NULL);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200288 buffer_install_hijacker(s, s->rep, stats_dump_sess_to_buffer);
289 }
290 else if (strcmp(args[1], "errors") == 0) {
291 if (*args[2])
292 s->data_ctx.errors.iid = atoi(args[2]);
293 else
294 s->data_ctx.errors.iid = -1;
295 s->data_ctx.errors.px = NULL;
296 s->ana_state = STATS_ST_REP;
Willy Tarreau56a560a2009-09-22 19:27:35 +0200297 stream_int_retnclose(s->rep->cons, NULL);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200298 buffer_install_hijacker(s, s->rep, stats_dump_errors_to_buffer);
299 }
300 else { /* neither "stat" nor "info" nor "sess" */
301 return 0;
302 }
303 }
304 else { /* not "show" */
305 return 0;
306 }
307 return 1;
308}
309
310/* Processes the stats interpreter on the statistics socket.
311 * In order to ease the transition, we simply simulate the server status
312 * for now. It only knows states STATS_ST_INIT, STATS_ST_REQ, STATS_ST_REP, and
313 * STATS_ST_CLOSE. It removes its analyser bit from req->analysers once done.
314 * It always returns 0.
315 */
316int stats_sock_req_analyser(struct session *s, struct buffer *req, int an_bit)
317{
318 char *line, *p;
319
320 switch (s->ana_state) {
321 case STATS_ST_INIT:
322 /* Stats output not initialized yet */
323 memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats));
324 s->data_source = DATA_SRC_STATS;
325 s->ana_state = STATS_ST_REQ;
Willy Tarreau520d95e2009-09-19 21:04:57 +0200326 buffer_dont_connect(s->req);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200327 /* fall through */
328
329 case STATS_ST_REQ:
330 /* Now, stats are initialized, hijack is not set, and
331 * we are waiting for a complete request line.
332 */
333
334 line = s->req->data;
335 p = memchr(line, '\n', s->req->l);
336
337 if (p) {
338 *p = '\0';
339 if (!stats_sock_parse_request(s, line)) {
340 /* invalid request */
341 stream_int_retnclose(s->req->prod, &stats_sock_usage);
342 s->ana_state = 0;
343 req->analysers = 0;
344 return 0;
345 }
346 }
347
348 /* processing a valid or incomplete request */
349 if ((req->flags & BF_FULL) || /* invalid request */
350 (req->flags & BF_READ_ERROR) || /* input error */
351 (req->flags & BF_READ_TIMEOUT) || /* read timeout */
352 tick_is_expired(req->analyse_exp, now_ms) || /* request timeout */
353 (req->flags & BF_SHUTR)) { /* input closed */
354 buffer_shutw_now(s->rep);
355 s->ana_state = 0;
356 req->analysers = 0;
357 return 0;
358 }
359 /* don't forward nor abort */
360 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
361 return 0;
362
363 case STATS_ST_REP:
364 /* do nothing while response is being processed */
365 return 0;
366
367 case STATS_ST_CLOSE:
368 /* end of dump */
369 s->req->analysers &= ~an_bit;
370 s->ana_state = 0;
371 break;
372 }
373 return 0;
374}
375
Willy Tarreau91861262007-10-17 17:06:05 +0200376/*
377 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200378 * client socket shut down on input. It *may* make use of informations from
379 * <uri>. s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200380 * It returns 0 if it had to stop writing data and an I/O is needed, 1 if the
381 * dump is finished and the session must be closed, or -1 in case of any error.
Willy Tarreau0a464892008-12-07 18:30:00 +0100382 * It automatically clears the HIJACK bit from the response buffer.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200383 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100384int stats_dump_raw(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau3e76e722007-10-17 18:57:38 +0200385{
Willy Tarreau3e76e722007-10-17 18:57:38 +0200386 struct proxy *px;
387 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +0100388 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200389
390 msg.len = 0;
391 msg.str = trash;
392
393 switch (s->data_state) {
394 case DATA_ST_INIT:
395 /* the function had not been called yet, let's prepare the
396 * buffer for a response.
397 */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200398 s->data_state = DATA_ST_HEAD;
399 /* fall through */
400
401 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100402 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100403 print_csv_header(&msg, sizeof(trash));
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200404 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100405 return 0;
406 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200407
408 s->data_state = DATA_ST_INFO;
409 /* fall through */
410
411 case DATA_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +0100412 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100413 if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100414 chunk_printf(&msg, sizeof(trash),
415 "Name: " PRODUCT_NAME "\n"
416 "Version: " HAPROXY_VERSION "\n"
417 "Release_date: " HAPROXY_DATE "\n"
418 "Nbproc: %d\n"
419 "Process_num: %d\n"
420 "Pid: %d\n"
421 "Uptime: %dd %dh%02dm%02ds\n"
422 "Uptime_sec: %d\n"
423 "Memmax_MB: %d\n"
424 "Ulimit-n: %d\n"
425 "Maxsock: %d\n"
426 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100427 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100428 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100429 "PipesUsed: %d\n"
430 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100431 "Tasks: %d\n"
432 "Run_queue: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100433 "",
434 global.nbproc,
435 relative_pid,
436 pid,
437 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
438 up,
439 global.rlimit_memmax,
440 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100441 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100442 actconn, pipes_used, pipes_free,
443 nb_tasks_cur, run_queue_cur
Willy Tarreaua8efd362008-01-03 10:19:15 +0100444 );
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200445 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100446 return 0;
447 }
448
Willy Tarreau3e76e722007-10-17 18:57:38 +0200449 s->data_ctx.stats.px = proxy;
450 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100451
452 s->data_ctx.stats.sv = NULL;
453 s->data_ctx.stats.sv_st = 0;
454
Willy Tarreau3e76e722007-10-17 18:57:38 +0200455 s->data_state = DATA_ST_LIST;
456 /* fall through */
457
458 case DATA_ST_LIST:
459 /* dump proxies */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100460 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100461 while (s->data_ctx.stats.px) {
462 px = s->data_ctx.stats.px;
463 /* skip the disabled proxies and non-networked ones */
464 if (px->state != PR_STSTOPPED &&
465 (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100466 if (stats_dump_proxy(s, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100467 return 0;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200468
Willy Tarreaua8efd362008-01-03 10:19:15 +0100469 s->data_ctx.stats.px = px->next;
470 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
471 }
472 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200473 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200474
475 s->data_state = DATA_ST_END;
476 /* fall through */
477
478 case DATA_ST_END:
479 s->data_state = DATA_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +0100480 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200481
482 case DATA_ST_FIN:
Willy Tarreau0a464892008-12-07 18:30:00 +0100483 buffer_stop_hijack(rep);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200484 return 1;
485
486 default:
487 /* unknown state ! */
Willy Tarreau0a464892008-12-07 18:30:00 +0100488 buffer_stop_hijack(rep);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200489 return -1;
490 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100491}
492
493
494/* This function is called to send output to the response buffer. It simply
495 * calls stats_dump_raw(), and releases the buffer's hijack bit when the dump
Willy Tarreau01bf8672008-12-07 18:03:29 +0100496 * is finished.
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100497 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100498void stats_dump_raw_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100499{
500 if (s->ana_state != STATS_ST_REP)
Willy Tarreau01bf8672008-12-07 18:03:29 +0100501 return;
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100502
Willy Tarreau0a464892008-12-07 18:30:00 +0100503 if (stats_dump_raw(s, rep, NULL) != 0)
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100504 s->ana_state = STATS_ST_CLOSE;
Willy Tarreau01bf8672008-12-07 18:03:29 +0100505 return;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200506}
507
508
509/*
510 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200511 * client socket shut down on input. It stops by itself by unsetting the
Willy Tarreau72b179a2008-08-28 16:01:32 +0200512 * BF_HIJACK flag from the buffer, which it uses to keep on being called
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200513 * when there is free space in the buffer, of simply by letting an empty buffer
514 * upon return.s->data_ctx must have been zeroed before the first call, and the
515 * flags set. It returns 0 if it had to stop writing data and an I/O is needed,
516 * 1 if the dump is finished and the session must be closed, or -1 in case of
517 * any error.
Willy Tarreau91861262007-10-17 17:06:05 +0200518 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100519int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200520{
Willy Tarreau91861262007-10-17 17:06:05 +0200521 struct proxy *px;
522 struct chunk msg;
523 unsigned int up;
524
525 msg.len = 0;
526 msg.str = trash;
527
528 switch (s->data_state) {
529 case DATA_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +0200530 chunk_printf(&msg, sizeof(trash),
531 "HTTP/1.0 200 OK\r\n"
532 "Cache-Control: no-cache\r\n"
533 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200534 "Content-Type: %s\r\n",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100535 (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +0200536
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100537 if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau91861262007-10-17 17:06:05 +0200538 chunk_printf(&msg, sizeof(trash), "Refresh: %d\r\n",
539 uri->refresh);
540
541 chunk_printf(&msg, sizeof(trash), "\r\n");
542
543 s->txn.status = 200;
Willy Tarreau56a560a2009-09-22 19:27:35 +0200544 if (buffer_write_chunk(rep, &msg) >= 0)
545 return 0;
546
Willy Tarreau91861262007-10-17 17:06:05 +0200547 msg.len = 0;
548
549 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
550 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
551 if (!(s->flags & SN_FINST_MASK))
552 s->flags |= SN_FINST_R;
553
554 if (s->txn.meth == HTTP_METH_HEAD) {
555 /* that's all we return in case of HEAD request */
556 s->data_state = DATA_ST_FIN;
Willy Tarreau72b179a2008-08-28 16:01:32 +0200557 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200558 return 1;
559 }
560
561 s->data_state = DATA_ST_HEAD; /* let's start producing data */
562 /* fall through */
563
564 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100565 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200566 /* WARNING! This must fit in the first buffer !!! */
567 chunk_printf(&msg, sizeof(trash),
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200568 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200569 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
570 "<style type=\"text/css\"><!--\n"
571 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +0200572 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +0200573 " font-size: 12px;"
574 " font-weight: normal;"
575 " color: black;"
576 " background: white;"
577 "}\n"
578 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +0200579 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +0200580 " align: center;"
581 "}\n"
582 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +0200583 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +0200584 " margin-bottom: 0.5em;"
585 "}\n"
586 "h2 {"
587 " font-family: helvetica, arial;"
588 " font-size: x-large;"
589 " font-weight: bold;"
590 " font-style: italic;"
591 " color: #6020a0;"
592 " margin-top: 0em;"
593 " margin-bottom: 0em;"
594 "}\n"
595 "h3 {"
596 " font-family: helvetica, arial;"
597 " font-size: 16px;"
598 " font-weight: bold;"
599 " color: #b00040;"
600 " background: #e8e8d0;"
601 " margin-top: 0em;"
602 " margin-bottom: 0em;"
603 "}\n"
604 "li {"
605 " margin-top: 0.25em;"
606 " margin-right: 2em;"
607 "}\n"
608 ".hr {margin-top: 0.25em;"
609 " border-color: black;"
610 " border-bottom-style: solid;"
611 "}\n"
612 ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n"
613 ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n"
614 ".total {background: #20D0D0;color: #ffff80;}\n"
615 ".frontend {background: #e8e8d0;}\n"
616 ".backend {background: #e8e8d0;}\n"
617 ".active0 {background: #ff9090;}\n"
618 ".active1 {background: #ffd020;}\n"
619 ".active2 {background: #ffffa0;}\n"
620 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100621 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
622 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
623 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200624 ".backup0 {background: #ff9090;}\n"
625 ".backup1 {background: #ff80ff;}\n"
626 ".backup2 {background: #c060ff;}\n"
627 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100628 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
629 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
630 ".backup6 {background: #e0e0e0;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +0200631 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Willy Tarreau91861262007-10-17 17:06:05 +0200632 "table.tbl { border-collapse: collapse; border-style: none;}\n"
633 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray;}\n"
634 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +0200635 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200636 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
637 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
638 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
639 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200640 "</style></head>\n",
641 uri->node_name ? " on " : "",
642 uri->node_name ? uri->node_name : ""
643 );
Willy Tarreau55bb8452007-10-17 18:44:57 +0200644 } else {
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100645 print_csv_header(&msg, sizeof(trash));
Willy Tarreau55bb8452007-10-17 18:44:57 +0200646 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200647 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200648 return 0;
649
650 s->data_state = DATA_ST_INFO;
651 /* fall through */
652
653 case DATA_ST_INFO:
654 up = (now.tv_sec - start_date.tv_sec);
655
656 /* WARNING! this has to fit the first packet too.
657 * We are around 3.5 kB, add adding entries will
658 * become tricky if we want to support 4kB buffers !
659 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100660 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200661 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200662 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
663 PRODUCT_NAME "%s</a></h1>\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200664 "<h2>Statistics Report for pid %d%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200665 "<hr width=\"100%%\" class=\"hr\">\n"
666 "<h3>&gt; General process information</h3>\n"
667 "<table border=0 cols=4><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100668 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200669 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
670 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100671 "<b>maxsock = </b> %d ; <b>maxconn = </b> %d ; <b>maxpipes = </b> %d<br>\n"
672 "current conns = %d ; current pipes = %d/%d<br>\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100673 "Running tasks : %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200674 "</td><td align=\"center\" nowrap>\n"
675 "<table class=\"lgd\"><tr>\n"
676 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
677 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
678 "</tr><tr>\n"
679 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
680 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
681 "</tr><tr>\n"
682 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
683 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
684 "</tr><tr>\n"
685 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100686 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200687 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100688 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +0200689 "</td>"
690 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
691 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
692 "",
693 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200694 pid, uri->node_name ? " on " : "", uri->node_name ? uri->node_name : "",
695 pid,
Willy Tarreaua8efd362008-01-03 10:19:15 +0100696 relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +0200697 up / 86400, (up % 86400) / 3600,
698 (up % 3600) / 60, (up % 60),
699 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
700 global.rlimit_memmax ? " MB" : "",
701 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100702 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100703 actconn, pipes_used, pipes_used+pipes_free,
704 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +0200705 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100706
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100707 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200708 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200709 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
710 uri->uri_prefix,
711 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100712 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200713 else
714 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200715 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
716 uri->uri_prefix,
717 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100718 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200719
Willy Tarreau55bb8452007-10-17 18:44:57 +0200720 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100721 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200722 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200723 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
724 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100725 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200726 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200727 else
728 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200729 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
730 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100731 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200732 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200733 }
Willy Tarreau91861262007-10-17 17:06:05 +0200734
Willy Tarreau55bb8452007-10-17 18:44:57 +0200735 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200736 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
737 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100738 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
739 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200740
Willy Tarreau55bb8452007-10-17 18:44:57 +0200741 chunk_printf(&msg, sizeof(trash),
Willy Tarreau5031e6a2007-10-18 11:05:48 +0200742 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
743 uri->uri_prefix,
744 (uri->refresh > 0) ? ";norefresh" : "");
745
746 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200747 "</td>"
748 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
749 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
750 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
751 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
752 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
753 "</ul>"
754 "</td>"
755 "</tr></table>\n"
756 ""
757 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100758
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200759 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200760 return 0;
761 }
Willy Tarreau91861262007-10-17 17:06:05 +0200762
Willy Tarreau91861262007-10-17 17:06:05 +0200763 s->data_ctx.stats.px = proxy;
764 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
765 s->data_state = DATA_ST_LIST;
766 /* fall through */
767
768 case DATA_ST_LIST:
769 /* dump proxies */
770 while (s->data_ctx.stats.px) {
771 px = s->data_ctx.stats.px;
772 /* skip the disabled proxies and non-networked ones */
773 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100774 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200775 return 0;
776
777 s->data_ctx.stats.px = px->next;
778 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
779 }
780 /* here, we just have reached the last proxy */
781
782 s->data_state = DATA_ST_END;
783 /* fall through */
784
785 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100786 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200787 chunk_printf(&msg, sizeof(trash), "</body></html>\n");
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200788 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200789 return 0;
790 }
Willy Tarreau91861262007-10-17 17:06:05 +0200791
792 s->data_state = DATA_ST_FIN;
793 /* fall through */
794
795 case DATA_ST_FIN:
Willy Tarreau72b179a2008-08-28 16:01:32 +0200796 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200797 return 1;
798
799 default:
800 /* unknown state ! */
Willy Tarreau72b179a2008-08-28 16:01:32 +0200801 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200802 return -1;
803 }
804}
805
806
807/*
808 * Dumps statistics for a proxy.
809 * Returns 0 if it had to stop dumping data because of lack of buffer space,
810 * ot non-zero if everything completed.
811 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100812int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200813{
814 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100815 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Willy Tarreau91861262007-10-17 17:06:05 +0200816 struct chunk msg;
817
818 msg.len = 0;
819 msg.str = trash;
820
821 switch (s->data_ctx.stats.px_st) {
822 case DATA_ST_PX_INIT:
823 /* we are on a new proxy */
824
825 if (uri && uri->scope) {
826 /* we have a limited scope, we have to check the proxy name */
827 struct stat_scope *scope;
828 int len;
829
830 len = strlen(px->id);
831 scope = uri->scope;
832
833 while (scope) {
834 /* match exact proxy name */
835 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
836 break;
837
838 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +0200839 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +0200840 break;
841 scope = scope->next;
842 }
843
844 /* proxy name not found : don't dump anything */
845 if (scope == NULL)
846 return 1;
847 }
848
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100849 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100850 (px->uuid != s->data_ctx.stats.iid))
851 return 1;
852
Willy Tarreau91861262007-10-17 17:06:05 +0200853 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
854 /* fall through */
855
856 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100857 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200858 /* print a new table */
859 chunk_printf(&msg, sizeof(trash),
Willy Tarreaua3e49422009-05-10 19:19:41 +0200860 "<table cols=\"29\" class=\"tbl\" width=\"100%%\">\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200861 "<tr align=\"center\" class=\"titre\">"
862 "<th colspan=2 class=\"pxname\">%s</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +0200863 "<th colspan=27 class=\"empty\"></th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200864 "</tr>\n"
865 "<tr align=\"center\" class=\"titre\">"
866 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +0200867 "<th colspan=3>Queue</th>"
868 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200869 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200870 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200871 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200872 "</tr>\n"
873 "<tr align=\"center\" class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +0200874 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200875 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +0200876 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200877 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200878 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200879 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100880 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
881 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200882 "</tr>",
Willy Tarreau55bb8452007-10-17 18:44:57 +0200883 px->id);
884
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200885 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200886 return 0;
887 }
Willy Tarreau91861262007-10-17 17:06:05 +0200888
889 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
890 /* fall through */
891
892 case DATA_ST_PX_FE:
893 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100894 if ((px->cap & PR_CAP_FE) &&
895 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
896 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200897 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200898 /* name, queue */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200899 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=3></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +0200900 /* sessions rate : current, max, limit */
901 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
902 /* sessions : current, max, limit, total, lbtot */
903 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200904 "<td align=right>%s</td><td align=right></td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200905 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200906 "<td align=right>%s</td><td align=right>%s</td>"
907 "",
Willy Tarreaua3e49422009-05-10 19:19:41 +0200908 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
909 U2H1(px->fe_sps_max), LIM2A2(px->fe_sps_lim, "-"),
910 U2H3(px->feconn), U2H4(px->feconn_max), U2H5(px->maxconn),
911 U2H6(px->cum_feconn), U2H7(px->bytes_in), U2H8(px->bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200912
913 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200914 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200915 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200916 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200917 "<td align=right>%s</td><td align=right></td><td align=right></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200918 /* warnings: retries, redispatches */
919 "<td align=right></td><td align=right></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200920 /* server status : reflect frontend status */
Willy Tarreau91861262007-10-17 17:06:05 +0200921 "<td align=center>%s</td>"
922 /* rest of server: nothing */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200923 "<td align=center colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +0200924 "",
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200925 U2H0(px->denied_req), U2H1(px->denied_resp),
926 U2H2(px->failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +0200927 px->state == PR_STRUN ? "OPEN" :
928 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200929 } else {
930 chunk_printf(&msg, sizeof(trash),
931 /* pxid, name, queue cur, queue max, */
932 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100933 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200934 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200935 /* bytes : in, out */
936 "%lld,%lld,"
937 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200938 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200939 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200940 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200941 /* warnings: retries, redispatches */
942 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200943 /* server status : reflect frontend status */
944 "%s,"
945 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200946 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100947 /* pid, iid, sid, throttle, lbtot, tracked, type */
948 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200949 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200950 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200951 /* check_status, check_code, check_duration */
952 ",,,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200953 "\n",
954 px->id,
955 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
956 px->bytes_in, px->bytes_out,
957 px->denied_req, px->denied_resp,
958 px->failed_req,
959 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +0100960 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +0100961 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200962 read_freq_ctr(&px->fe_sess_per_sec),
963 px->fe_sps_lim, px->fe_sps_max);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200964 }
Willy Tarreau91861262007-10-17 17:06:05 +0200965
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200966 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200967 return 0;
968 }
969
970 s->data_ctx.stats.sv = px->srv; /* may be NULL */
971 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
972 /* fall through */
973
974 case DATA_ST_PX_SV:
975 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100976 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
977
Willy Tarreau2ea81932007-11-30 12:04:38 +0100978 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 +0200979
980 sv = s->data_ctx.stats.sv;
981
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100982 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100983 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
984 break;
985
986 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
987 continue;
988 }
989
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100990 if (sv->tracked)
991 svs = sv->tracked;
992 else
993 svs = sv;
994
Willy Tarreau91861262007-10-17 17:06:05 +0200995 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100996 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +0100997 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100998 else if (svs->state & SRV_RUNNING) {
999 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02001000 sv_state = 3; /* UP */
1001 else
1002 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01001003
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001004 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01001005 sv_state += 2;
1006 }
Willy Tarreau91861262007-10-17 17:06:05 +02001007 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001008 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02001009 sv_state = 1; /* going up */
1010 else
1011 sv_state = 0; /* DOWN */
1012
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001013 if ((sv_state == 0) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02001014 /* do not report servers which are DOWN */
1015 s->data_ctx.stats.sv = sv->next;
1016 continue;
1017 }
1018
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001019 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001020 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
1021 "UP %d/%d &darr;", "UP",
1022 "NOLB %d/%d &darr;", "NOLB",
1023 "<i>no check</i>" };
Willy Tarreau55bb8452007-10-17 18:44:57 +02001024 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +02001025 /* name */
1026 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001027 /* queue : current, max, limit */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001028 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001029 /* sessions rate : current, max, limit */
1030 "<td align=right>%s</td><td align=right>%s</td><td align=right></td>"
1031 /* sessions : current, max, limit, total, lbtot */
1032 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001033 "<td align=right>%s</td><td align=right>%s</td>"
1034 "",
1035 (sv->state & SRV_BACKUP) ? "backup" : "active",
1036 sv_state, sv->id,
1037 U2H0(sv->nbpend), U2H1(sv->nbpend_max), LIM2A2(sv->maxqueue, "-"),
Willy Tarreaua3e49422009-05-10 19:19:41 +02001038 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->sps_max),
1039 U2H5(sv->cur_sess), U2H6(sv->cur_sess_max), LIM2A7(sv->maxconn, "-"),
1040 U2H8(sv->cum_sess), U2H9(sv->cum_lbconn));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001041
1042 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +02001043 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001044 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001045 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001046 "<td align=right></td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001047 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001048 "<td align=right></td><td align=right>%s</td><td align=right>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001049 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001050 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001051 "",
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001052 U2H0(sv->bytes_in), U2H1(sv->bytes_out),
1053 U2H2(sv->failed_secu),
1054 U2H3(sv->failed_conns), U2H4(sv->failed_resp),
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001055 sv->retries, sv->redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001056
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001057 /* status, lest check */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001058 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001059
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001060 if (sv->state & SRV_CHECKED) {
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001061 chunk_printf(&msg, sizeof(trash), "%s ",
1062 human_time(now.tv_sec - sv->last_change, 1));
1063
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001064 chunk_printf(&msg, sizeof(trash),
1065 srv_hlt_st[sv_state],
1066 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
1067 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
1068
1069 chunk_printf(&msg, sizeof(trash), "</td><td title=\"%s\" nowrap> %s%s",
1070 get_check_status_description(sv->check_status),
1071 tv_iszero(&sv->check_start)?"":"* ",
1072 get_check_status_info(sv->check_status));
1073
1074 if (sv->check_status >= HCHK_STATUS_L57DATA)
1075 chunk_printf(&msg, sizeof(trash), "/%d", sv->check_code);
1076
1077 if (sv->check_status >= HCHK_STATUS_CHECKED)
1078 chunk_printf(&msg, sizeof(trash), " in %lums", sv->check_duration);
1079 } else {
1080 chunk_printf(&msg, sizeof(trash), "</td><td>");
1081 }
Willy Tarreau91861262007-10-17 17:06:05 +02001082
Willy Tarreau55bb8452007-10-17 18:44:57 +02001083 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +02001084 /* weight */
1085 "</td><td>%d</td>"
1086 /* act, bck */
1087 "<td>%s</td><td>%s</td>"
1088 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001089 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02001090 (sv->state & SRV_BACKUP) ? "-" : "Y",
1091 (sv->state & SRV_BACKUP) ? "Y" : "-");
1092
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001093 /* check failures: unique, fatal, down time */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001094 if (sv->state & SRV_CHECKED)
1095 chunk_printf(&msg, sizeof(trash),
Willy Tarreau3b88d442009-04-11 20:44:08 +02001096 "<td align=right>%lld</td><td align=right>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001097 "<td nowrap align=right>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001098 "",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001099 svs->failed_checks, svs->down_trans,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001100 human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001101 else if (sv != svs)
1102 chunk_printf(&msg, sizeof(trash),
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001103 "<td nowrap colspan=3>via %s/%s</td>", svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001104 else
1105 chunk_printf(&msg, sizeof(trash),
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001106 "<td colspan=3></td>");
1107
1108 /* throttle */
1109 if ((sv->state & SRV_WARMINGUP) &&
1110 now.tv_sec < sv->last_change + sv->slowstart &&
1111 now.tv_sec >= sv->last_change) {
1112 unsigned int ratio;
1113 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
1114 chunk_printf(&msg, sizeof(trash),
1115 "<td>%d %%</td></tr>\n", ratio);
1116 } else {
1117 chunk_printf(&msg, sizeof(trash),
1118 "<td>-</td></tr>\n");
1119 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02001120 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001121 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
1122 "UP %d/%d,", "UP,",
1123 "NOLB %d/%d,", "NOLB,",
1124 "no check," };
Willy Tarreau55bb8452007-10-17 18:44:57 +02001125 chunk_printf(&msg, sizeof(trash),
1126 /* pxid, name */
1127 "%s,%s,"
1128 /* queue : current, max */
1129 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001130 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001131 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001132 /* bytes : in, out */
1133 "%lld,%lld,"
1134 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001135 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001136 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001137 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001138 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001139 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001140 "",
1141 px->id, sv->id,
1142 sv->nbpend, sv->nbpend_max,
Willy Tarreaudcd47712007-11-04 23:35:08 +01001143 sv->cur_sess, sv->cur_sess_max, LIM2A0(sv->maxconn, ""), sv->cum_sess,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001144 sv->bytes_in, sv->bytes_out,
1145 sv->failed_secu,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001146 sv->failed_conns, sv->failed_resp,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001147 sv->retries, sv->redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001148
Willy Tarreau55bb8452007-10-17 18:44:57 +02001149 /* status */
1150 chunk_printf(&msg, sizeof(trash),
1151 srv_hlt_st[sv_state],
1152 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
1153 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
Willy Tarreau91861262007-10-17 17:06:05 +02001154
Willy Tarreau55bb8452007-10-17 18:44:57 +02001155 chunk_printf(&msg, sizeof(trash),
1156 /* weight, active, backup */
1157 "%d,%d,%d,"
1158 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001159 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001160 (sv->state & SRV_BACKUP) ? 0 : 1,
1161 (sv->state & SRV_BACKUP) ? 1 : 0);
1162
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001163 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001164 if (sv->state & SRV_CHECKED)
1165 chunk_printf(&msg, sizeof(trash),
Willy Tarreau3b88d442009-04-11 20:44:08 +02001166 "%lld,%lld,%d,%d,",
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001167 sv->failed_checks, sv->down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001168 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02001169 else
1170 chunk_printf(&msg, sizeof(trash),
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001171 ",,,,");
1172
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001173 /* queue limit, pid, iid, sid, */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001174 chunk_printf(&msg, sizeof(trash),
Willy Tarreaudcd47712007-11-04 23:35:08 +01001175 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001176 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01001177 LIM2A0(sv->maxqueue, ""),
1178 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001179
1180 /* throttle */
1181 if ((sv->state & SRV_WARMINGUP) &&
1182 now.tv_sec < sv->last_change + sv->slowstart &&
1183 now.tv_sec >= sv->last_change) {
1184 unsigned int ratio;
1185 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001186 chunk_printf(&msg, sizeof(trash), "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001187 }
1188
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001189 /* sessions: lbtot */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001190 chunk_printf(&msg, sizeof(trash), ",%lld,", sv->cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001191
1192 /* tracked */
1193 if (sv->tracked)
Krzysztof Piotr Oledzkif58a9622008-02-23 01:19:10 +01001194 chunk_printf(&msg, sizeof(trash), "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001195 sv->tracked->proxy->id, sv->tracked->id);
1196 else
1197 chunk_printf(&msg, sizeof(trash), ",");
1198
Willy Tarreau7f062c42009-03-05 18:43:00 +01001199 /* type */
1200 chunk_printf(&msg, sizeof(trash), "%d,", STATS_TYPE_SV);
1201
1202 /* rate */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001203 chunk_printf(&msg, sizeof(trash), "%u,,%u,",
1204 read_freq_ctr(&sv->sess_per_sec),
1205 sv->sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01001206
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001207 if (sv->state & SRV_CHECKED) {
1208 /* check_status */
1209 chunk_printf(&msg, sizeof(trash), "%s,", get_check_status_info(sv->check_status));
1210
1211 /* check_code */
1212 if (sv->check_status >= HCHK_STATUS_L57DATA)
1213 chunk_printf(&msg, sizeof(trash), "%u,", sv->check_code);
1214 else
1215 chunk_printf(&msg, sizeof(trash), ",");
1216
1217 /* check_duration */
1218 if (sv->check_status >= HCHK_STATUS_CHECKED)
1219 chunk_printf(&msg, sizeof(trash), "%lu,", sv->check_duration);
1220 else
1221 chunk_printf(&msg, sizeof(trash), ",");
1222
1223 } else {
1224 chunk_printf(&msg, sizeof(trash), ",,,");
1225 }
1226
Willy Tarreau7f062c42009-03-05 18:43:00 +01001227 /* finish with EOL */
1228 chunk_printf(&msg, sizeof(trash), "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001229 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001230 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001231 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001232 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02001233
1234 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
1235 /* fall through */
1236
1237 case DATA_ST_PX_BE:
1238 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001239 if ((px->cap & PR_CAP_BE) &&
1240 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
1241 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001242 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +02001243 /* name */
1244 "<tr align=center class=\"backend\"><td>Backend</td>"
1245 /* queue : current, max */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001246 "<td align=right>%s</td><td align=right>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001247 /* sessions rate : current, max, limit */
1248 "<td align=right>%s</td><td align=right>%s</td><td align=right></td>"
1249 "",
1250 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->nbpend_max),
1251 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_sps_max));
1252
1253 chunk_printf(&msg, sizeof(trash),
1254 /* sessions : current, max, limit, total, lbtot */
1255 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001256 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001257 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001258 "<td align=right>%s</td><td align=right>%s</td>"
1259 "",
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001260 U2H2(px->beconn), U2H3(px->beconn_max), U2H4(px->fullconn),
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001261 U2H6(px->cum_beconn), U2H7(px->cum_lbconn),
1262 U2H8(px->bytes_in), U2H9(px->bytes_out));
1263
1264 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +02001265 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001266 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001267 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001268 "<td align=right></td><td align=right>%s</td><td align=right>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001269 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001270 "<td align=right>%lld</td><td align=right>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001271 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02001272 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001273 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02001274 * active and backups. */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001275 "<td align=center nowrap>%s %s</td><td align=center>&nbsp;</td><td align=center>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001276 "<td align=center>%d</td><td align=center>%d</td>"
1277 "",
1278 U2H0(px->denied_req), U2H1(px->denied_resp),
1279 U2H2(px->failed_conns), U2H3(px->failed_resp),
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001280 px->retries, px->redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001281 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01001282 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
1283 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01001284 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001285 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001286
1287 chunk_printf(&msg, sizeof(trash),
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001288 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001289 "<td align=center>&nbsp;</td><td align=\"right\">%d</td>"
1290 "<td align=\"right\" nowrap>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001291 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001292 "</tr>",
1293 px->down_trans,
1294 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001295 } else {
1296 chunk_printf(&msg, sizeof(trash),
1297 /* pxid, name */
1298 "%s,BACKEND,"
1299 /* queue : current, max */
1300 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001301 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001302 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001303 /* bytes : in, out */
1304 "%lld,%lld,"
1305 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001306 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001307 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001308 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001309 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001310 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001311 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02001312 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001313 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02001314 * active and backups. */
1315 "%s,"
1316 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001317 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001318 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001319 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001320 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001321 /* rate, rate_lim, rate_max, */
1322 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001323 /* check_status, check_code, check_duration */
1324 ",,,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001325 "\n",
1326 px->id,
1327 px->nbpend /* or px->totpend ? */, px->nbpend_max,
1328 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
1329 px->bytes_in, px->bytes_out,
1330 px->denied_req, px->denied_resp,
1331 px->failed_conns, px->failed_resp,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001332 px->retries, px->redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01001333 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01001334 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001335 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001336 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01001337 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001338 relative_pid, px->uuid,
Willy Tarreau7f062c42009-03-05 18:43:00 +01001339 px->cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001340 read_freq_ctr(&px->be_sess_per_sec),
1341 px->be_sps_max);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001342 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001343 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001344 return 0;
1345 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001346
Willy Tarreau91861262007-10-17 17:06:05 +02001347 s->data_ctx.stats.px_st = DATA_ST_PX_END;
1348 /* fall through */
1349
1350 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001351 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001352 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001353
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001354 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001355 return 0;
1356 }
Willy Tarreau91861262007-10-17 17:06:05 +02001357
1358 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
1359 /* fall through */
1360
1361 case DATA_ST_PX_FIN:
1362 return 1;
1363
1364 default:
1365 /* unknown state, we should put an abort() here ! */
1366 return 1;
1367 }
1368}
1369
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001370
1371/* This function is called to send output to the response buffer.
1372 * It dumps the sessions states onto the output buffer <rep>.
1373 * Expects to be called with client socket shut down on input.
1374 * s->data_ctx must have been zeroed first, and the flags properly set.
1375 * It automatically clears the HIJACK bit from the response buffer.
1376 */
1377void stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
1378{
1379 struct chunk msg;
1380
1381 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
1382 /* If we're forced to shut down, we might have to remove our
1383 * reference to the last session being dumped.
1384 */
1385 if (s->data_state == DATA_ST_LIST) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001386 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001387 LIST_DEL(&s->data_ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001388 LIST_INIT(&s->data_ctx.sess.bref.users);
1389 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001390 }
1391 s->data_state = DATA_ST_FIN;
1392 buffer_stop_hijack(rep);
1393 s->ana_state = STATS_ST_CLOSE;
1394 return;
1395 }
1396
1397 if (s->ana_state != STATS_ST_REP)
1398 return;
1399
1400 msg.len = 0;
1401 msg.str = trash;
1402
1403 switch (s->data_state) {
1404 case DATA_ST_INIT:
1405 /* the function had not been called yet, let's prepare the
1406 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001407 * pointer to the first in the global list. When a target
1408 * session is being destroyed, it is responsible for updating
1409 * this pointer. We know we have reached the end when this
1410 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001411 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001412 LIST_INIT(&s->data_ctx.sess.bref.users);
1413 s->data_ctx.sess.bref.ref = sessions.n;
1414 s->data_state = DATA_ST_LIST;
1415 /* fall through */
1416
1417 case DATA_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001418 /* first, let's detach the back-ref from a possible previous session */
1419 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
1420 LIST_DEL(&s->data_ctx.sess.bref.users);
1421 LIST_INIT(&s->data_ctx.sess.bref.users);
1422 }
1423
1424 /* and start from where we stopped */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001425 while (s->data_ctx.sess.bref.ref != &sessions) {
1426 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1427 struct session *curr_sess;
1428
1429 curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list);
1430
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001431 chunk_printf(&msg, sizeof(trash),
1432 "%p: proto=%s",
1433 curr_sess,
1434 curr_sess->listener->proto->name);
1435
1436 switch (curr_sess->listener->proto->sock_family) {
1437 case AF_INET:
1438 inet_ntop(AF_INET,
1439 (const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr,
1440 pn, sizeof(pn));
1441
1442 chunk_printf(&msg, sizeof(trash),
1443 " src=%s:%d fe=%s be=%s srv=%s",
1444 pn,
1445 ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port),
1446 curr_sess->fe->id,
1447 curr_sess->be->id,
1448 curr_sess->srv ? curr_sess->srv->id : "<none>"
1449 );
1450 break;
1451 case AF_INET6:
1452 inet_ntop(AF_INET6,
1453 (const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr,
1454 pn, sizeof(pn));
1455
1456 chunk_printf(&msg, sizeof(trash),
1457 " src=%s:%d fe=%s be=%s srv=%s",
1458 pn,
1459 ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port),
1460 curr_sess->fe->id,
1461 curr_sess->be->id,
1462 curr_sess->srv ? curr_sess->srv->id : "<none>"
1463 );
1464
1465 break;
1466 case AF_UNIX:
1467 /* no more information to print right now */
1468 break;
1469 }
1470
1471 chunk_printf(&msg, sizeof(trash),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001472 " as=%d ts=%02x age=%s calls=%d",
Willy Tarreau8a5c6262008-12-08 00:16:21 +01001473 curr_sess->ana_state, curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01001474 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
1475 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001476
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001477 chunk_printf(&msg, sizeof(trash),
1478 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
1479 curr_sess->req->flags,
1480 curr_sess->req->l,
1481 curr_sess->req->analysers,
1482 curr_sess->req->rex ?
1483 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
1484 TICKS_TO_MS(1000)) : "");
1485
1486 chunk_printf(&msg, sizeof(trash),
1487 ",wx=%s",
1488 curr_sess->req->wex ?
1489 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
1490 TICKS_TO_MS(1000)) : "");
1491
1492 chunk_printf(&msg, sizeof(trash),
1493 ",ax=%s]",
1494 curr_sess->req->analyse_exp ?
1495 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
1496 TICKS_TO_MS(1000)) : "");
1497
1498 chunk_printf(&msg, sizeof(trash),
1499 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
1500 curr_sess->rep->flags,
1501 curr_sess->rep->l,
1502 curr_sess->rep->analysers,
1503 curr_sess->rep->rex ?
1504 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
1505 TICKS_TO_MS(1000)) : "");
1506
1507 chunk_printf(&msg, sizeof(trash),
1508 ",wx=%s",
1509 curr_sess->rep->wex ?
1510 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
1511 TICKS_TO_MS(1000)) : "");
1512
1513 chunk_printf(&msg, sizeof(trash),
1514 ",ax=%s]",
1515 curr_sess->rep->analyse_exp ?
1516 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
1517 TICKS_TO_MS(1000)) : "");
1518
1519 chunk_printf(&msg, sizeof(trash),
1520 " s0=[%d,%1xh,fd=%d,ex=%s]",
1521 curr_sess->si[0].state,
1522 curr_sess->si[0].flags,
1523 curr_sess->si[0].fd,
1524 curr_sess->si[0].exp ?
1525 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
1526 TICKS_TO_MS(1000)) : "");
1527
1528 chunk_printf(&msg, sizeof(trash),
1529 " s1=[%d,%1xh,fd=%d,ex=%s]",
1530 curr_sess->si[1].state,
1531 curr_sess->si[1].flags,
1532 curr_sess->si[1].fd,
1533 curr_sess->si[1].exp ?
1534 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
1535 TICKS_TO_MS(1000)) : "");
1536
1537 chunk_printf(&msg, sizeof(trash),
1538 " exp=%s",
1539 curr_sess->task->expire ?
1540 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
1541 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01001542 if (task_in_rq(curr_sess->task))
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001543 chunk_printf(&msg, sizeof(trash), " run(nice=%d)", curr_sess->task->nice);
1544
1545 chunk_printf(&msg, sizeof(trash), "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001546
1547 if (buffer_write_chunk(rep, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001548 /* let's try again later from this session. We add ourselves into
1549 * this session's users so that it can remove us upon termination.
1550 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001551 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
1552 return;
1553 }
1554
1555 s->data_ctx.sess.bref.ref = curr_sess->list.n;
1556 }
1557 s->data_state = DATA_ST_FIN;
1558 /* fall through */
1559
1560 default:
1561 s->data_state = DATA_ST_FIN;
1562 buffer_stop_hijack(rep);
1563 s->ana_state = STATS_ST_CLOSE;
1564 return;
1565 }
1566}
1567
Willy Tarreau74808cb2009-03-04 15:53:18 +01001568/* print a line of error buffer (limited to 70 bytes) to <out>. The format is :
1569 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
1570 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
1571 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
1572 * lines are respected within the limit of 70 output chars. Lines that are
1573 * continuation of a previous truncated line begin with "+" instead of " "
1574 * after the offset. The new pointer is returned.
1575 */
1576static int dump_error_line(struct chunk *out, int size,
1577 struct error_snapshot *err, int *line, int ptr)
1578{
1579 int end;
1580 unsigned char c;
1581
1582 end = out->len + 80;
1583 if (end > size)
1584 return ptr;
1585
1586 chunk_printf(out, size, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
1587
1588 while (ptr < err->len) {
1589 c = err->buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01001590 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001591 if (out->len > end - 2)
1592 break;
1593 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01001594 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001595 if (out->len > end - 3)
1596 break;
1597 out->str[out->len++] = '\\';
1598 switch (c) {
1599 case '\t': c = 't'; break;
1600 case '\n': c = 'n'; break;
1601 case '\r': c = 'r'; break;
1602 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01001603 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001604 }
1605 out->str[out->len++] = c;
1606 } else {
1607 if (out->len > end - 5)
1608 break;
1609 out->str[out->len++] = '\\';
1610 out->str[out->len++] = 'x';
1611 out->str[out->len++] = hextab[(c >> 4) & 0xF];
1612 out->str[out->len++] = hextab[c & 0xF];
1613 }
1614 if (err->buf[ptr++] == '\n') {
1615 /* we had a line break, let's return now */
1616 out->str[out->len++] = '\n';
1617 *line = ptr;
1618 return ptr;
1619 }
1620 }
1621 /* we have an incomplete line, we return it as-is */
1622 out->str[out->len++] = '\n';
1623 return ptr;
1624}
1625
1626/* This function is called to send output to the response buffer.
1627 * It dumps the errors logged in proxies onto the output buffer <rep>.
1628 * Expects to be called with client socket shut down on input.
1629 * s->data_ctx must have been zeroed first, and the flags properly set.
1630 * It automatically clears the HIJACK bit from the response buffer.
1631 */
1632void stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
1633{
1634 extern const char *monthname[12];
1635 struct chunk msg;
1636
1637 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
1638 s->data_state = DATA_ST_FIN;
1639 buffer_stop_hijack(rep);
1640 s->ana_state = STATS_ST_CLOSE;
1641 return;
1642 }
1643
1644 if (s->ana_state != STATS_ST_REP)
1645 return;
1646
1647 msg.len = 0;
1648 msg.str = trash;
1649
1650 if (!s->data_ctx.errors.px) {
1651 /* the function had not been called yet, let's prepare the
1652 * buffer for a response.
1653 */
Willy Tarreau74808cb2009-03-04 15:53:18 +01001654 s->data_ctx.errors.px = proxy;
1655 s->data_ctx.errors.buf = 0;
1656 s->data_ctx.errors.bol = 0;
1657 s->data_ctx.errors.ptr = -1;
1658 }
1659
1660 /* we have two inner loops here, one for the proxy, the other one for
1661 * the buffer.
1662 */
1663 while (s->data_ctx.errors.px) {
1664 struct error_snapshot *es;
1665
1666 if (s->data_ctx.errors.buf == 0)
1667 es = &s->data_ctx.errors.px->invalid_req;
1668 else
1669 es = &s->data_ctx.errors.px->invalid_rep;
1670
1671 if (!es->when.tv_sec)
1672 goto next;
1673
1674 if (s->data_ctx.errors.iid >= 0 &&
1675 s->data_ctx.errors.px->uuid != s->data_ctx.errors.iid &&
1676 es->oe->uuid != s->data_ctx.errors.iid)
1677 goto next;
1678
1679 if (s->data_ctx.errors.ptr < 0) {
1680 /* just print headers now */
1681
1682 char pn[INET6_ADDRSTRLEN];
1683 struct tm tm;
1684
1685 get_localtime(es->when.tv_sec, &tm);
1686 chunk_printf(&msg, sizeof(trash), "\n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
1687 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001688 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01001689
1690
1691 if (es->src.ss_family == AF_INET)
1692 inet_ntop(AF_INET,
1693 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
1694 pn, sizeof(pn));
1695 else
1696 inet_ntop(AF_INET6,
1697 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
1698 pn, sizeof(pn));
1699
1700 switch (s->data_ctx.errors.buf) {
1701 case 0:
1702 chunk_printf(&msg, sizeof(trash),
1703 " frontend %s (#%d): invalid request\n"
1704 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
1705 " request length %d bytes, error at position %d:\n\n",
1706 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
1707 pn, es->sid, es->oe->id, es->oe->uuid,
1708 es->srv ? es->srv->id : "<NONE>",
1709 es->srv ? es->srv->puid : -1,
1710 es->len, es->pos);
1711 break;
1712 case 1:
1713 chunk_printf(&msg, sizeof(trash),
1714 " backend %s (#%d) : invalid response\n"
1715 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
1716 " response length %d bytes, error at position %d:\n\n",
1717 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
1718 pn, es->sid, es->oe->id, es->oe->uuid,
1719 es->srv ? es->srv->id : "<NONE>",
1720 es->srv ? es->srv->puid : -1,
1721 es->len, es->pos);
1722 break;
1723 }
1724
1725 if (buffer_write_chunk(rep, &msg) >= 0) {
1726 /* Socket buffer full. Let's try again later from the same point */
1727 return;
1728 }
1729 s->data_ctx.errors.ptr = 0;
1730 s->data_ctx.errors.sid = es->sid;
1731 }
1732
1733 if (s->data_ctx.errors.sid != es->sid) {
1734 /* the snapshot changed while we were dumping it */
1735 chunk_printf(&msg, sizeof(trash),
1736 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
1737 if (buffer_write_chunk(rep, &msg) >= 0)
1738 return;
1739 goto next;
1740 }
1741
1742 /* OK, ptr >= 0, so we have to dump the current line */
1743 while (s->data_ctx.errors.ptr < es->len) {
1744 int newptr;
1745 int newline;
1746
1747 newline = s->data_ctx.errors.bol;
1748 newptr = dump_error_line(&msg, sizeof(trash), es, &newline, s->data_ctx.errors.ptr);
1749 if (newptr == s->data_ctx.errors.ptr)
1750 return;
1751
1752 if (buffer_write_chunk(rep, &msg) >= 0) {
1753 /* Socket buffer full. Let's try again later from the same point */
1754 return;
1755 }
1756 s->data_ctx.errors.ptr = newptr;
1757 s->data_ctx.errors.bol = newline;
1758 };
1759 next:
1760 s->data_ctx.errors.bol = 0;
1761 s->data_ctx.errors.ptr = -1;
1762 s->data_ctx.errors.buf++;
1763 if (s->data_ctx.errors.buf > 1) {
1764 s->data_ctx.errors.buf = 0;
1765 s->data_ctx.errors.px = s->data_ctx.errors.px->next;
1766 }
1767 }
1768
1769 /* dump complete */
1770 buffer_stop_hijack(rep);
1771 s->ana_state = STATS_ST_CLOSE;
1772}
1773
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001774
Willy Tarreau10522fd2008-07-09 20:12:41 +02001775static struct cfg_kw_list cfg_kws = {{ },{
1776 { CFG_GLOBAL, "stats", stats_parse_global },
1777 { 0, NULL, NULL },
1778}};
1779
1780__attribute__((constructor))
1781static void __dumpstats_module_init(void)
1782{
1783 cfg_register_keywords(&cfg_kws);
1784}
1785
Willy Tarreau91861262007-10-17 17:06:05 +02001786/*
1787 * Local variables:
1788 * c-indent-level: 8
1789 * c-basic-offset: 8
1790 * End:
1791 */