blob: 24aab05946e9425dc20975dd022c20e414795849 [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>
Willy Tarreau91861262007-10-17 17:06:05 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020019#include <pwd.h>
20#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau10522fd2008-07-09 20:12:41 +020026#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020027#include <common/compat.h>
28#include <common/config.h>
29#include <common/debug.h>
30#include <common/memory.h>
31#include <common/mini-clist.h>
32#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020033#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020034#include <common/time.h>
35#include <common/uri_auth.h>
36#include <common/version.h>
37
Willy Tarreau91861262007-10-17 17:06:05 +020038#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020039
40#include <proto/backend.h>
41#include <proto/buffers.h>
42#include <proto/dumpstats.h>
43#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010044#include <proto/freq_ctr.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010045#include <proto/pipe.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020046#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020047#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020048#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020049#include <proto/server.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010050#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010051#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052
Willy Tarreau5ca791d2009-08-16 19:06:42 +020053const char stats_sock_usage_msg[] =
54 "Unknown command. Please enter one of the following commands only :\n"
55 " show info : report information about the running process\n"
56 " show stat : report counters for each proxy and server\n"
57 " show errors : report last request and response errors for each proxy\n"
58 " show sess : report the list of current sessions\n"
59 "\n";
60
61const struct chunk stats_sock_usage = {
62 .str = (char *)&stats_sock_usage_msg,
63 .len = sizeof(stats_sock_usage_msg)-1
64};
65
Willy Tarreaufbee7132007-10-18 13:53:22 +020066/* This function parses a "stats" statement in the "global" section. It returns
67 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
68 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
69 * zero included. The trailing '\n' must not be written. The function must be
70 * called with <args> pointing to the first word after "stats".
71 */
Willy Tarreau10522fd2008-07-09 20:12:41 +020072static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
73 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +020074{
Willy Tarreau10522fd2008-07-09 20:12:41 +020075 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +020076 if (!strcmp(args[0], "socket")) {
77 struct sockaddr_un su;
78 int cur_arg;
79
80 if (*args[1] == 0) {
81 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
82 return -1;
83 }
84
85 if (global.stats_sock.state != LI_NEW) {
86 snprintf(err, errlen, "'stats socket' already specified in global section");
87 return -1;
88 }
89
90 su.sun_family = AF_UNIX;
91 strncpy(su.sun_path, args[1], sizeof(su.sun_path));
92 su.sun_path[sizeof(su.sun_path) - 1] = 0;
93 memcpy(&global.stats_sock.addr, &su, sizeof(su)); // guaranteed to fit
94
Willy Tarreau89a63132009-08-16 17:41:45 +020095 if (!global.stats_fe) {
96 if ((global.stats_fe = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) {
97 snprintf(err, errlen, "out of memory");
98 return -1;
99 }
100
101 LIST_INIT(&global.stats_fe->pendconns);
102 LIST_INIT(&global.stats_fe->acl);
103 LIST_INIT(&global.stats_fe->block_cond);
104 LIST_INIT(&global.stats_fe->redirect_rules);
105 LIST_INIT(&global.stats_fe->mon_fail_cond);
106 LIST_INIT(&global.stats_fe->switching_rules);
107 LIST_INIT(&global.stats_fe->tcp_req.inspect_rules);
108
109 /* Timeouts are defined as -1, so we cannot use the zeroed area
110 * as a default value.
111 */
112 proxy_reset_timeouts(global.stats_fe);
113
114 global.stats_fe->last_change = now.tv_sec;
115 global.stats_fe->id = strdup("GLOBAL");
116 global.stats_fe->cap = PR_CAP_FE;
117 }
118
Willy Tarreaufbee7132007-10-18 13:53:22 +0200119 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100120 global.stats_sock.options = LI_O_NONE;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200121 global.stats_sock.accept = uxst_event_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200122 global.stats_sock.handler = process_session;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200123 global.stats_sock.analysers = AN_REQ_STATS_SOCK;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200124 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreau89a63132009-08-16 17:41:45 +0200125 global.stats_sock.private = global.stats_fe; /* must point to the frontend */
126
127 global.stats_fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
128 global.stats_sock.timeout = &global.stats_fe->timeout.client;
129
130 global.stats_sock.next = global.stats_fe->listen;
131 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200132
133 cur_arg = 2;
134 while (*args[cur_arg]) {
135 if (!strcmp(args[cur_arg], "uid")) {
136 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
137 cur_arg += 2;
138 }
139 else if (!strcmp(args[cur_arg], "gid")) {
140 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
141 cur_arg += 2;
142 }
143 else if (!strcmp(args[cur_arg], "mode")) {
144 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
145 cur_arg += 2;
146 }
147 else if (!strcmp(args[cur_arg], "user")) {
148 struct passwd *user;
149 user = getpwnam(args[cur_arg + 1]);
150 if (!user) {
151 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
152 args[cur_arg + 1]);
153 return -1;
154 }
155 global.stats_sock.perm.ux.uid = user->pw_uid;
156 cur_arg += 2;
157 }
158 else if (!strcmp(args[cur_arg], "group")) {
159 struct group *group;
160 group = getgrnam(args[cur_arg + 1]);
161 if (!group) {
162 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
163 args[cur_arg + 1]);
164 return -1;
165 }
166 global.stats_sock.perm.ux.gid = group->gr_gid;
167 cur_arg += 2;
168 }
169 else {
170 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', and 'mode'");
171 return -1;
172 }
173 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100174
Willy Tarreaufbee7132007-10-18 13:53:22 +0200175 uxst_add_listener(&global.stats_sock);
176 global.maxsock++;
177 }
178 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100179 unsigned timeout;
180 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
181
182 if (res) {
183 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
184 return -1;
185 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200186
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100187 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200188 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
189 return -1;
190 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200191 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200192 }
193 else if (!strcmp(args[0], "maxconn")) {
194 int maxconn = atol(args[1]);
195
196 if (maxconn <= 0) {
197 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
198 return -1;
199 }
200 global.maxsock -= global.stats_sock.maxconn;
201 global.stats_sock.maxconn = maxconn;
202 global.maxsock += global.stats_sock.maxconn;
203 }
204 else {
205 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
206 return -1;
207 }
208 return 0;
209}
210
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100211int print_csv_header(struct chunk *msg, int size)
212{
213 return chunk_printf(msg, size,
214 "# pxname,svname,"
215 "qcur,qmax,"
216 "scur,smax,slim,stot,"
217 "bin,bout,"
218 "dreq,dresp,"
219 "ereq,econ,eresp,"
220 "wretr,wredis,"
221 "status,weight,act,bck,"
222 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200223 "pid,iid,sid,throttle,lbtot,tracked,type,"
224 "rate,rate_lim,rate_max,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100225 "\n");
226}
227
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200228/* Parses the request line in <cmd> and possibly starts dumping stats on
229 * s->rep with the hijack bit set. Returns 1 if OK, 0 in case of any error.
230 * The line is modified after parsing.
231 */
232int stats_sock_parse_request(struct session *s, char *line)
233{
234 char *args[MAX_STATS_ARGS + 1];
235 int arg;
236
237 while (isspace((unsigned char)*line))
238 line++;
239
240 arg = 0;
241 args[arg] = line;
242
243 while (*line && arg < MAX_STATS_ARGS) {
244 if (isspace((unsigned char)*line)) {
245 *line++ = '\0';
246
247 while (isspace((unsigned char)*line))
248 line++;
249
250 args[++arg] = line;
251 continue;
252 }
253
254 line++;
255 }
256
257 while (++arg <= MAX_STATS_ARGS)
258 args[arg] = line;
259
260 if (strcmp(args[0], "show") == 0) {
261 if (strcmp(args[1], "stat") == 0) {
262 if (*args[2] && *args[3] && *args[4]) {
263 s->data_ctx.stats.flags |= STAT_BOUND;
264 s->data_ctx.stats.iid = atoi(args[2]);
265 s->data_ctx.stats.type = atoi(args[3]);
266 s->data_ctx.stats.sid = atoi(args[4]);
267 }
268
269 s->data_ctx.stats.flags |= STAT_SHOW_STAT;
270 s->data_ctx.stats.flags |= STAT_FMT_CSV;
271 s->ana_state = STATS_ST_REP;
272 buffer_install_hijacker(s, s->rep, stats_dump_raw_to_buffer);
273 }
274 else if (strcmp(args[1], "info") == 0) {
275 s->data_ctx.stats.flags |= STAT_SHOW_INFO;
276 s->data_ctx.stats.flags |= STAT_FMT_CSV;
277 s->ana_state = STATS_ST_REP;
278 buffer_install_hijacker(s, s->rep, stats_dump_raw_to_buffer);
279 }
280 else if (strcmp(args[1], "sess") == 0) {
281 s->ana_state = STATS_ST_REP;
282 buffer_install_hijacker(s, s->rep, stats_dump_sess_to_buffer);
283 }
284 else if (strcmp(args[1], "errors") == 0) {
285 if (*args[2])
286 s->data_ctx.errors.iid = atoi(args[2]);
287 else
288 s->data_ctx.errors.iid = -1;
289 s->data_ctx.errors.px = NULL;
290 s->ana_state = STATS_ST_REP;
291 buffer_install_hijacker(s, s->rep, stats_dump_errors_to_buffer);
292 }
293 else { /* neither "stat" nor "info" nor "sess" */
294 return 0;
295 }
296 }
297 else { /* not "show" */
298 return 0;
299 }
300 return 1;
301}
302
303/* Processes the stats interpreter on the statistics socket.
304 * In order to ease the transition, we simply simulate the server status
305 * for now. It only knows states STATS_ST_INIT, STATS_ST_REQ, STATS_ST_REP, and
306 * STATS_ST_CLOSE. It removes its analyser bit from req->analysers once done.
307 * It always returns 0.
308 */
309int stats_sock_req_analyser(struct session *s, struct buffer *req, int an_bit)
310{
311 char *line, *p;
312
313 switch (s->ana_state) {
314 case STATS_ST_INIT:
315 /* Stats output not initialized yet */
316 memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats));
317 s->data_source = DATA_SRC_STATS;
318 s->ana_state = STATS_ST_REQ;
319 buffer_write_dis(s->req);
320 /* fall through */
321
322 case STATS_ST_REQ:
323 /* Now, stats are initialized, hijack is not set, and
324 * we are waiting for a complete request line.
325 */
326
327 line = s->req->data;
328 p = memchr(line, '\n', s->req->l);
329
330 if (p) {
331 *p = '\0';
332 if (!stats_sock_parse_request(s, line)) {
333 /* invalid request */
334 stream_int_retnclose(s->req->prod, &stats_sock_usage);
335 s->ana_state = 0;
336 req->analysers = 0;
337 return 0;
338 }
339 }
340
341 /* processing a valid or incomplete request */
342 if ((req->flags & BF_FULL) || /* invalid request */
343 (req->flags & BF_READ_ERROR) || /* input error */
344 (req->flags & BF_READ_TIMEOUT) || /* read timeout */
345 tick_is_expired(req->analyse_exp, now_ms) || /* request timeout */
346 (req->flags & BF_SHUTR)) { /* input closed */
347 buffer_shutw_now(s->rep);
348 s->ana_state = 0;
349 req->analysers = 0;
350 return 0;
351 }
352 /* don't forward nor abort */
353 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
354 return 0;
355
356 case STATS_ST_REP:
357 /* do nothing while response is being processed */
358 return 0;
359
360 case STATS_ST_CLOSE:
361 /* end of dump */
362 s->req->analysers &= ~an_bit;
363 s->ana_state = 0;
364 break;
365 }
366 return 0;
367}
368
Willy Tarreau91861262007-10-17 17:06:05 +0200369/*
370 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200371 * client socket shut down on input. It *may* make use of informations from
372 * <uri>. s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200373 * It returns 0 if it had to stop writing data and an I/O is needed, 1 if the
374 * dump is finished and the session must be closed, or -1 in case of any error.
Willy Tarreau0a464892008-12-07 18:30:00 +0100375 * It automatically clears the HIJACK bit from the response buffer.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200376 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100377int stats_dump_raw(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau3e76e722007-10-17 18:57:38 +0200378{
Willy Tarreau3e76e722007-10-17 18:57:38 +0200379 struct proxy *px;
380 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +0100381 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200382
383 msg.len = 0;
384 msg.str = trash;
385
386 switch (s->data_state) {
387 case DATA_ST_INIT:
388 /* the function had not been called yet, let's prepare the
389 * buffer for a response.
390 */
Willy Tarreaudded32d2008-11-30 19:48:07 +0100391 stream_int_retnclose(rep->cons, &msg);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200392 s->data_state = DATA_ST_HEAD;
393 /* fall through */
394
395 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100396 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100397 print_csv_header(&msg, sizeof(trash));
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200398 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100399 return 0;
400 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200401
402 s->data_state = DATA_ST_INFO;
403 /* fall through */
404
405 case DATA_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +0100406 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100407 if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100408 chunk_printf(&msg, sizeof(trash),
409 "Name: " PRODUCT_NAME "\n"
410 "Version: " HAPROXY_VERSION "\n"
411 "Release_date: " HAPROXY_DATE "\n"
412 "Nbproc: %d\n"
413 "Process_num: %d\n"
414 "Pid: %d\n"
415 "Uptime: %dd %dh%02dm%02ds\n"
416 "Uptime_sec: %d\n"
417 "Memmax_MB: %d\n"
418 "Ulimit-n: %d\n"
419 "Maxsock: %d\n"
420 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100421 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100422 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100423 "PipesUsed: %d\n"
424 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100425 "Tasks: %d\n"
426 "Run_queue: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100427 "",
428 global.nbproc,
429 relative_pid,
430 pid,
431 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
432 up,
433 global.rlimit_memmax,
434 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100435 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100436 actconn, pipes_used, pipes_free,
437 nb_tasks_cur, run_queue_cur
Willy Tarreaua8efd362008-01-03 10:19:15 +0100438 );
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200439 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100440 return 0;
441 }
442
Willy Tarreau3e76e722007-10-17 18:57:38 +0200443 s->data_ctx.stats.px = proxy;
444 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100445
446 s->data_ctx.stats.sv = NULL;
447 s->data_ctx.stats.sv_st = 0;
448
Willy Tarreau3e76e722007-10-17 18:57:38 +0200449 s->data_state = DATA_ST_LIST;
450 /* fall through */
451
452 case DATA_ST_LIST:
453 /* dump proxies */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100454 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100455 while (s->data_ctx.stats.px) {
456 px = s->data_ctx.stats.px;
457 /* skip the disabled proxies and non-networked ones */
458 if (px->state != PR_STSTOPPED &&
459 (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100460 if (stats_dump_proxy(s, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100461 return 0;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200462
Willy Tarreaua8efd362008-01-03 10:19:15 +0100463 s->data_ctx.stats.px = px->next;
464 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
465 }
466 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200467 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200468
469 s->data_state = DATA_ST_END;
470 /* fall through */
471
472 case DATA_ST_END:
473 s->data_state = DATA_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +0100474 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200475
476 case DATA_ST_FIN:
Willy Tarreau0a464892008-12-07 18:30:00 +0100477 buffer_stop_hijack(rep);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200478 return 1;
479
480 default:
481 /* unknown state ! */
Willy Tarreau0a464892008-12-07 18:30:00 +0100482 buffer_stop_hijack(rep);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200483 return -1;
484 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100485}
486
487
488/* This function is called to send output to the response buffer. It simply
489 * calls stats_dump_raw(), and releases the buffer's hijack bit when the dump
Willy Tarreau01bf8672008-12-07 18:03:29 +0100490 * is finished.
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100491 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100492void stats_dump_raw_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100493{
494 if (s->ana_state != STATS_ST_REP)
Willy Tarreau01bf8672008-12-07 18:03:29 +0100495 return;
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100496
Willy Tarreau0a464892008-12-07 18:30:00 +0100497 if (stats_dump_raw(s, rep, NULL) != 0)
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100498 s->ana_state = STATS_ST_CLOSE;
Willy Tarreau01bf8672008-12-07 18:03:29 +0100499 return;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200500}
501
502
503/*
504 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200505 * client socket shut down on input. It stops by itself by unsetting the
Willy Tarreau72b179a2008-08-28 16:01:32 +0200506 * BF_HIJACK flag from the buffer, which it uses to keep on being called
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200507 * when there is free space in the buffer, of simply by letting an empty buffer
508 * upon return.s->data_ctx must have been zeroed before the first call, and the
509 * flags set. It returns 0 if it had to stop writing data and an I/O is needed,
510 * 1 if the dump is finished and the session must be closed, or -1 in case of
511 * any error.
Willy Tarreau91861262007-10-17 17:06:05 +0200512 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100513int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200514{
Willy Tarreau91861262007-10-17 17:06:05 +0200515 struct proxy *px;
516 struct chunk msg;
517 unsigned int up;
518
519 msg.len = 0;
520 msg.str = trash;
521
522 switch (s->data_state) {
523 case DATA_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +0200524 chunk_printf(&msg, sizeof(trash),
525 "HTTP/1.0 200 OK\r\n"
526 "Cache-Control: no-cache\r\n"
527 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200528 "Content-Type: %s\r\n",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100529 (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +0200530
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100531 if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau91861262007-10-17 17:06:05 +0200532 chunk_printf(&msg, sizeof(trash), "Refresh: %d\r\n",
533 uri->refresh);
534
535 chunk_printf(&msg, sizeof(trash), "\r\n");
536
537 s->txn.status = 200;
Willy Tarreaudded32d2008-11-30 19:48:07 +0100538 stream_int_retnclose(rep->cons, &msg); // send the start of the response.
Willy Tarreau91861262007-10-17 17:06:05 +0200539 msg.len = 0;
540
541 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
542 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
543 if (!(s->flags & SN_FINST_MASK))
544 s->flags |= SN_FINST_R;
545
546 if (s->txn.meth == HTTP_METH_HEAD) {
547 /* that's all we return in case of HEAD request */
548 s->data_state = DATA_ST_FIN;
Willy Tarreau72b179a2008-08-28 16:01:32 +0200549 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200550 return 1;
551 }
552
553 s->data_state = DATA_ST_HEAD; /* let's start producing data */
554 /* fall through */
555
556 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100557 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200558 /* WARNING! This must fit in the first buffer !!! */
559 chunk_printf(&msg, sizeof(trash),
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200560 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200561 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
562 "<style type=\"text/css\"><!--\n"
563 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +0200564 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +0200565 " font-size: 12px;"
566 " font-weight: normal;"
567 " color: black;"
568 " background: white;"
569 "}\n"
570 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +0200571 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +0200572 " align: center;"
573 "}\n"
574 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +0200575 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +0200576 " margin-bottom: 0.5em;"
577 "}\n"
578 "h2 {"
579 " font-family: helvetica, arial;"
580 " font-size: x-large;"
581 " font-weight: bold;"
582 " font-style: italic;"
583 " color: #6020a0;"
584 " margin-top: 0em;"
585 " margin-bottom: 0em;"
586 "}\n"
587 "h3 {"
588 " font-family: helvetica, arial;"
589 " font-size: 16px;"
590 " font-weight: bold;"
591 " color: #b00040;"
592 " background: #e8e8d0;"
593 " margin-top: 0em;"
594 " margin-bottom: 0em;"
595 "}\n"
596 "li {"
597 " margin-top: 0.25em;"
598 " margin-right: 2em;"
599 "}\n"
600 ".hr {margin-top: 0.25em;"
601 " border-color: black;"
602 " border-bottom-style: solid;"
603 "}\n"
604 ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n"
605 ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n"
606 ".total {background: #20D0D0;color: #ffff80;}\n"
607 ".frontend {background: #e8e8d0;}\n"
608 ".backend {background: #e8e8d0;}\n"
609 ".active0 {background: #ff9090;}\n"
610 ".active1 {background: #ffd020;}\n"
611 ".active2 {background: #ffffa0;}\n"
612 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100613 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
614 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
615 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200616 ".backup0 {background: #ff9090;}\n"
617 ".backup1 {background: #ff80ff;}\n"
618 ".backup2 {background: #c060ff;}\n"
619 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100620 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
621 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
622 ".backup6 {background: #e0e0e0;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +0200623 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Willy Tarreau91861262007-10-17 17:06:05 +0200624 "table.tbl { border-collapse: collapse; border-style: none;}\n"
625 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray;}\n"
626 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +0200627 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200628 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
629 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
630 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
631 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200632 "</style></head>\n",
633 uri->node_name ? " on " : "",
634 uri->node_name ? uri->node_name : ""
635 );
Willy Tarreau55bb8452007-10-17 18:44:57 +0200636 } else {
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100637 print_csv_header(&msg, sizeof(trash));
Willy Tarreau55bb8452007-10-17 18:44:57 +0200638 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200639 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200640 return 0;
641
642 s->data_state = DATA_ST_INFO;
643 /* fall through */
644
645 case DATA_ST_INFO:
646 up = (now.tv_sec - start_date.tv_sec);
647
648 /* WARNING! this has to fit the first packet too.
649 * We are around 3.5 kB, add adding entries will
650 * become tricky if we want to support 4kB buffers !
651 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100652 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200653 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200654 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
655 PRODUCT_NAME "%s</a></h1>\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200656 "<h2>Statistics Report for pid %d%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200657 "<hr width=\"100%%\" class=\"hr\">\n"
658 "<h3>&gt; General process information</h3>\n"
659 "<table border=0 cols=4><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100660 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200661 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
662 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100663 "<b>maxsock = </b> %d ; <b>maxconn = </b> %d ; <b>maxpipes = </b> %d<br>\n"
664 "current conns = %d ; current pipes = %d/%d<br>\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100665 "Running tasks : %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200666 "</td><td align=\"center\" nowrap>\n"
667 "<table class=\"lgd\"><tr>\n"
668 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
669 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
670 "</tr><tr>\n"
671 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
672 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
673 "</tr><tr>\n"
674 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
675 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
676 "</tr><tr>\n"
677 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100678 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200679 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100680 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +0200681 "</td>"
682 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
683 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
684 "",
685 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200686 pid, uri->node_name ? " on " : "", uri->node_name ? uri->node_name : "",
687 pid,
Willy Tarreaua8efd362008-01-03 10:19:15 +0100688 relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +0200689 up / 86400, (up % 86400) / 3600,
690 (up % 3600) / 60, (up % 60),
691 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
692 global.rlimit_memmax ? " MB" : "",
693 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100694 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100695 actconn, pipes_used, pipes_used+pipes_free,
696 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +0200697 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100698
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100699 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200700 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200701 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
702 uri->uri_prefix,
703 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100704 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200705 else
706 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200707 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
708 uri->uri_prefix,
709 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100710 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200711
Willy Tarreau55bb8452007-10-17 18:44:57 +0200712 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100713 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200714 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200715 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
716 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100717 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200718 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200719 else
720 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200721 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
722 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100723 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200724 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200725 }
Willy Tarreau91861262007-10-17 17:06:05 +0200726
Willy Tarreau55bb8452007-10-17 18:44:57 +0200727 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200728 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
729 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100730 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
731 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200732
Willy Tarreau55bb8452007-10-17 18:44:57 +0200733 chunk_printf(&msg, sizeof(trash),
Willy Tarreau5031e6a2007-10-18 11:05:48 +0200734 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
735 uri->uri_prefix,
736 (uri->refresh > 0) ? ";norefresh" : "");
737
738 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200739 "</td>"
740 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
741 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
742 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
743 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
744 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
745 "</ul>"
746 "</td>"
747 "</tr></table>\n"
748 ""
749 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100750
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200751 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200752 return 0;
753 }
Willy Tarreau91861262007-10-17 17:06:05 +0200754
Willy Tarreau91861262007-10-17 17:06:05 +0200755 s->data_ctx.stats.px = proxy;
756 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
757 s->data_state = DATA_ST_LIST;
758 /* fall through */
759
760 case DATA_ST_LIST:
761 /* dump proxies */
762 while (s->data_ctx.stats.px) {
763 px = s->data_ctx.stats.px;
764 /* skip the disabled proxies and non-networked ones */
765 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100766 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200767 return 0;
768
769 s->data_ctx.stats.px = px->next;
770 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
771 }
772 /* here, we just have reached the last proxy */
773
774 s->data_state = DATA_ST_END;
775 /* fall through */
776
777 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100778 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200779 chunk_printf(&msg, sizeof(trash), "</body></html>\n");
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200780 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200781 return 0;
782 }
Willy Tarreau91861262007-10-17 17:06:05 +0200783
784 s->data_state = DATA_ST_FIN;
785 /* fall through */
786
787 case DATA_ST_FIN:
Willy Tarreau72b179a2008-08-28 16:01:32 +0200788 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200789 return 1;
790
791 default:
792 /* unknown state ! */
Willy Tarreau72b179a2008-08-28 16:01:32 +0200793 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200794 return -1;
795 }
796}
797
798
799/*
800 * Dumps statistics for a proxy.
801 * Returns 0 if it had to stop dumping data because of lack of buffer space,
802 * ot non-zero if everything completed.
803 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100804int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200805{
806 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100807 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Willy Tarreau91861262007-10-17 17:06:05 +0200808 struct chunk msg;
809
810 msg.len = 0;
811 msg.str = trash;
812
813 switch (s->data_ctx.stats.px_st) {
814 case DATA_ST_PX_INIT:
815 /* we are on a new proxy */
816
817 if (uri && uri->scope) {
818 /* we have a limited scope, we have to check the proxy name */
819 struct stat_scope *scope;
820 int len;
821
822 len = strlen(px->id);
823 scope = uri->scope;
824
825 while (scope) {
826 /* match exact proxy name */
827 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
828 break;
829
830 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +0200831 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +0200832 break;
833 scope = scope->next;
834 }
835
836 /* proxy name not found : don't dump anything */
837 if (scope == NULL)
838 return 1;
839 }
840
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100841 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100842 (px->uuid != s->data_ctx.stats.iid))
843 return 1;
844
Willy Tarreau91861262007-10-17 17:06:05 +0200845 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
846 /* fall through */
847
848 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100849 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200850 /* print a new table */
851 chunk_printf(&msg, sizeof(trash),
Willy Tarreaua3e49422009-05-10 19:19:41 +0200852 "<table cols=\"29\" class=\"tbl\" width=\"100%%\">\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200853 "<tr align=\"center\" class=\"titre\">"
854 "<th colspan=2 class=\"pxname\">%s</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +0200855 "<th colspan=27 class=\"empty\"></th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200856 "</tr>\n"
857 "<tr align=\"center\" class=\"titre\">"
858 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +0200859 "<th colspan=3>Queue</th>"
860 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200861 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200862 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100863 "<th colspan=8>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200864 "</tr>\n"
865 "<tr align=\"center\" class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +0200866 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200867 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +0200868 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200869 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200870 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200871 "<th>Status</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100872 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
873 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200874 "</tr>",
Willy Tarreau55bb8452007-10-17 18:44:57 +0200875 px->id);
876
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200877 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200878 return 0;
879 }
Willy Tarreau91861262007-10-17 17:06:05 +0200880
881 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
882 /* fall through */
883
884 case DATA_ST_PX_FE:
885 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100886 if ((px->cap & PR_CAP_FE) &&
887 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
888 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200889 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200890 /* name, queue */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200891 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=3></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +0200892 /* sessions rate : current, max, limit */
893 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
894 /* sessions : current, max, limit, total, lbtot */
895 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200896 "<td align=right>%s</td><td align=right></td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200897 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200898 "<td align=right>%s</td><td align=right>%s</td>"
899 "",
Willy Tarreaua3e49422009-05-10 19:19:41 +0200900 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
901 U2H1(px->fe_sps_max), LIM2A2(px->fe_sps_lim, "-"),
902 U2H3(px->feconn), U2H4(px->feconn_max), U2H5(px->maxconn),
903 U2H6(px->cum_feconn), U2H7(px->bytes_in), U2H8(px->bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200904
905 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200906 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200907 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200908 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200909 "<td align=right>%s</td><td align=right></td><td align=right></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200910 /* warnings: retries, redispatches */
911 "<td align=right></td><td align=right></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200912 /* server status : reflect frontend status */
Willy Tarreau91861262007-10-17 17:06:05 +0200913 "<td align=center>%s</td>"
914 /* rest of server: nothing */
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100915 "<td align=center colspan=7></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +0200916 "",
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200917 U2H0(px->denied_req), U2H1(px->denied_resp),
918 U2H2(px->failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +0200919 px->state == PR_STRUN ? "OPEN" :
920 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200921 } else {
922 chunk_printf(&msg, sizeof(trash),
923 /* pxid, name, queue cur, queue max, */
924 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100925 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200926 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200927 /* bytes : in, out */
928 "%lld,%lld,"
929 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200930 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200931 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200932 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200933 /* warnings: retries, redispatches */
934 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200935 /* server status : reflect frontend status */
936 "%s,"
937 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200938 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100939 /* pid, iid, sid, throttle, lbtot, tracked, type */
940 "%d,%d,0,,,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200941 /* rate, rate_lim, rate_max, */
942 "%u,%u,%u,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200943 "\n",
944 px->id,
945 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
946 px->bytes_in, px->bytes_out,
947 px->denied_req, px->denied_resp,
948 px->failed_req,
949 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +0100950 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +0100951 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200952 read_freq_ctr(&px->fe_sess_per_sec),
953 px->fe_sps_lim, px->fe_sps_max);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200954 }
Willy Tarreau91861262007-10-17 17:06:05 +0200955
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200956 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200957 return 0;
958 }
959
960 s->data_ctx.stats.sv = px->srv; /* may be NULL */
961 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
962 /* fall through */
963
964 case DATA_ST_PX_SV:
965 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100966 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
967
Willy Tarreau2ea81932007-11-30 12:04:38 +0100968 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 +0200969
970 sv = s->data_ctx.stats.sv;
971
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100972 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100973 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
974 break;
975
976 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
977 continue;
978 }
979
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100980 if (sv->tracked)
981 svs = sv->tracked;
982 else
983 svs = sv;
984
Willy Tarreau91861262007-10-17 17:06:05 +0200985 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100986 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +0100987 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100988 else if (svs->state & SRV_RUNNING) {
989 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +0200990 sv_state = 3; /* UP */
991 else
992 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +0100993
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100994 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +0100995 sv_state += 2;
996 }
Willy Tarreau91861262007-10-17 17:06:05 +0200997 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100998 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +0200999 sv_state = 1; /* going up */
1000 else
1001 sv_state = 0; /* DOWN */
1002
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001003 if ((sv_state == 0) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02001004 /* do not report servers which are DOWN */
1005 s->data_ctx.stats.sv = sv->next;
1006 continue;
1007 }
1008
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001009 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001010 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
1011 "UP %d/%d &darr;", "UP",
1012 "NOLB %d/%d &darr;", "NOLB",
1013 "<i>no check</i>" };
Willy Tarreau55bb8452007-10-17 18:44:57 +02001014 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +02001015 /* name */
1016 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001017 /* queue : current, max, limit */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001018 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001019 /* sessions rate : current, max, limit */
1020 "<td align=right>%s</td><td align=right>%s</td><td align=right></td>"
1021 /* sessions : current, max, limit, total, lbtot */
1022 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001023 "<td align=right>%s</td><td align=right>%s</td>"
1024 "",
1025 (sv->state & SRV_BACKUP) ? "backup" : "active",
1026 sv_state, sv->id,
1027 U2H0(sv->nbpend), U2H1(sv->nbpend_max), LIM2A2(sv->maxqueue, "-"),
Willy Tarreaua3e49422009-05-10 19:19:41 +02001028 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->sps_max),
1029 U2H5(sv->cur_sess), U2H6(sv->cur_sess_max), LIM2A7(sv->maxconn, "-"),
1030 U2H8(sv->cum_sess), U2H9(sv->cum_lbconn));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001031
1032 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +02001033 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001034 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001035 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001036 "<td align=right></td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001037 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001038 "<td align=right></td><td align=right>%s</td><td align=right>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001039 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001040 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001041 "",
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001042 U2H0(sv->bytes_in), U2H1(sv->bytes_out),
1043 U2H2(sv->failed_secu),
1044 U2H3(sv->failed_conns), U2H4(sv->failed_resp),
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001045 sv->retries, sv->redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001046
Willy Tarreau55bb8452007-10-17 18:44:57 +02001047 /* status */
1048 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001049
1050 if (sv->state & SRV_CHECKED)
1051 chunk_printf(&msg, sizeof(trash), "%s ",
1052 human_time(now.tv_sec - sv->last_change, 1));
1053
Willy Tarreau55bb8452007-10-17 18:44:57 +02001054 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +02001055 srv_hlt_st[sv_state],
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001056 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
1057 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Willy Tarreau91861262007-10-17 17:06:05 +02001058
Willy Tarreau55bb8452007-10-17 18:44:57 +02001059 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +02001060 /* weight */
1061 "</td><td>%d</td>"
1062 /* act, bck */
1063 "<td>%s</td><td>%s</td>"
1064 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001065 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02001066 (sv->state & SRV_BACKUP) ? "-" : "Y",
1067 (sv->state & SRV_BACKUP) ? "Y" : "-");
1068
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001069 /* check failures: unique, fatal, down time */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001070 if (sv->state & SRV_CHECKED)
1071 chunk_printf(&msg, sizeof(trash),
Willy Tarreau3b88d442009-04-11 20:44:08 +02001072 "<td align=right>%lld</td><td align=right>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001073 "<td nowrap align=right>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001074 "",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001075 svs->failed_checks, svs->down_trans,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001076 human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001077 else if (sv != svs)
1078 chunk_printf(&msg, sizeof(trash),
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001079 "<td nowrap colspan=3>via %s/%s</td>", svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001080 else
1081 chunk_printf(&msg, sizeof(trash),
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001082 "<td colspan=3></td>");
1083
1084 /* throttle */
1085 if ((sv->state & SRV_WARMINGUP) &&
1086 now.tv_sec < sv->last_change + sv->slowstart &&
1087 now.tv_sec >= sv->last_change) {
1088 unsigned int ratio;
1089 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
1090 chunk_printf(&msg, sizeof(trash),
1091 "<td>%d %%</td></tr>\n", ratio);
1092 } else {
1093 chunk_printf(&msg, sizeof(trash),
1094 "<td>-</td></tr>\n");
1095 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02001096 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001097 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
1098 "UP %d/%d,", "UP,",
1099 "NOLB %d/%d,", "NOLB,",
1100 "no check," };
Willy Tarreau55bb8452007-10-17 18:44:57 +02001101 chunk_printf(&msg, sizeof(trash),
1102 /* pxid, name */
1103 "%s,%s,"
1104 /* queue : current, max */
1105 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001106 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001107 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001108 /* bytes : in, out */
1109 "%lld,%lld,"
1110 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001111 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001112 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001113 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001114 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001115 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001116 "",
1117 px->id, sv->id,
1118 sv->nbpend, sv->nbpend_max,
Willy Tarreaudcd47712007-11-04 23:35:08 +01001119 sv->cur_sess, sv->cur_sess_max, LIM2A0(sv->maxconn, ""), sv->cum_sess,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001120 sv->bytes_in, sv->bytes_out,
1121 sv->failed_secu,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001122 sv->failed_conns, sv->failed_resp,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001123 sv->retries, sv->redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001124
Willy Tarreau55bb8452007-10-17 18:44:57 +02001125 /* status */
1126 chunk_printf(&msg, sizeof(trash),
1127 srv_hlt_st[sv_state],
1128 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
1129 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
Willy Tarreau91861262007-10-17 17:06:05 +02001130
Willy Tarreau55bb8452007-10-17 18:44:57 +02001131 chunk_printf(&msg, sizeof(trash),
1132 /* weight, active, backup */
1133 "%d,%d,%d,"
1134 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001135 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001136 (sv->state & SRV_BACKUP) ? 0 : 1,
1137 (sv->state & SRV_BACKUP) ? 1 : 0);
1138
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001139 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001140 if (sv->state & SRV_CHECKED)
1141 chunk_printf(&msg, sizeof(trash),
Willy Tarreau3b88d442009-04-11 20:44:08 +02001142 "%lld,%lld,%d,%d,",
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001143 sv->failed_checks, sv->down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001144 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02001145 else
1146 chunk_printf(&msg, sizeof(trash),
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001147 ",,,,");
1148
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001149 /* queue limit, pid, iid, sid, */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001150 chunk_printf(&msg, sizeof(trash),
Willy Tarreaudcd47712007-11-04 23:35:08 +01001151 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001152 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01001153 LIM2A0(sv->maxqueue, ""),
1154 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001155
1156 /* throttle */
1157 if ((sv->state & SRV_WARMINGUP) &&
1158 now.tv_sec < sv->last_change + sv->slowstart &&
1159 now.tv_sec >= sv->last_change) {
1160 unsigned int ratio;
1161 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001162 chunk_printf(&msg, sizeof(trash), "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001163 }
1164
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001165 /* sessions: lbtot */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001166 chunk_printf(&msg, sizeof(trash), ",%lld,", sv->cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001167
1168 /* tracked */
1169 if (sv->tracked)
Krzysztof Piotr Oledzkif58a9622008-02-23 01:19:10 +01001170 chunk_printf(&msg, sizeof(trash), "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001171 sv->tracked->proxy->id, sv->tracked->id);
1172 else
1173 chunk_printf(&msg, sizeof(trash), ",");
1174
Willy Tarreau7f062c42009-03-05 18:43:00 +01001175 /* type */
1176 chunk_printf(&msg, sizeof(trash), "%d,", STATS_TYPE_SV);
1177
1178 /* rate */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001179 chunk_printf(&msg, sizeof(trash), "%u,,%u,",
1180 read_freq_ctr(&sv->sess_per_sec),
1181 sv->sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01001182
1183 /* finish with EOL */
1184 chunk_printf(&msg, sizeof(trash), "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001185 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001186 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001187 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001188 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02001189
1190 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
1191 /* fall through */
1192
1193 case DATA_ST_PX_BE:
1194 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001195 if ((px->cap & PR_CAP_BE) &&
1196 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
1197 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001198 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +02001199 /* name */
1200 "<tr align=center class=\"backend\"><td>Backend</td>"
1201 /* queue : current, max */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001202 "<td align=right>%s</td><td align=right>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001203 /* sessions rate : current, max, limit */
1204 "<td align=right>%s</td><td align=right>%s</td><td align=right></td>"
1205 "",
1206 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->nbpend_max),
1207 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_sps_max));
1208
1209 chunk_printf(&msg, sizeof(trash),
1210 /* sessions : current, max, limit, total, lbtot */
1211 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001212 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001213 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001214 "<td align=right>%s</td><td align=right>%s</td>"
1215 "",
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001216 U2H2(px->beconn), U2H3(px->beconn_max), U2H4(px->fullconn),
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001217 U2H6(px->cum_beconn), U2H7(px->cum_lbconn),
1218 U2H8(px->bytes_in), U2H9(px->bytes_out));
1219
1220 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +02001221 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001222 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001223 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001224 "<td align=right></td><td align=right>%s</td><td align=right>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001225 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001226 "<td align=right>%lld</td><td align=right>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001227 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02001228 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001229 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02001230 * active and backups. */
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001231 "<td align=center nowrap>%s %s</td><td align=center>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001232 "<td align=center>%d</td><td align=center>%d</td>"
1233 "",
1234 U2H0(px->denied_req), U2H1(px->denied_resp),
1235 U2H2(px->failed_conns), U2H3(px->failed_resp),
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001236 px->retries, px->redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001237 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01001238 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
1239 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01001240 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001241 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001242
1243 chunk_printf(&msg, sizeof(trash),
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001244 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001245 "<td align=center>&nbsp;</td><td align=\"right\">%d</td>"
1246 "<td align=\"right\" nowrap>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001247 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001248 "</tr>",
1249 px->down_trans,
1250 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001251 } else {
1252 chunk_printf(&msg, sizeof(trash),
1253 /* pxid, name */
1254 "%s,BACKEND,"
1255 /* queue : current, max */
1256 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001257 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001258 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001259 /* bytes : in, out */
1260 "%lld,%lld,"
1261 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001262 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001263 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001264 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001265 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001266 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001267 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02001268 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001269 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02001270 * active and backups. */
1271 "%s,"
1272 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001273 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001274 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001275 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001276 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001277 /* rate, rate_lim, rate_max, */
1278 "%u,,%u,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001279 "\n",
1280 px->id,
1281 px->nbpend /* or px->totpend ? */, px->nbpend_max,
1282 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
1283 px->bytes_in, px->bytes_out,
1284 px->denied_req, px->denied_resp,
1285 px->failed_conns, px->failed_resp,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001286 px->retries, px->redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01001287 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01001288 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001289 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001290 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01001291 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001292 relative_pid, px->uuid,
Willy Tarreau7f062c42009-03-05 18:43:00 +01001293 px->cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001294 read_freq_ctr(&px->be_sess_per_sec),
1295 px->be_sps_max);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001296 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001297 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001298 return 0;
1299 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001300
Willy Tarreau91861262007-10-17 17:06:05 +02001301 s->data_ctx.stats.px_st = DATA_ST_PX_END;
1302 /* fall through */
1303
1304 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001305 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001306 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001307
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001308 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001309 return 0;
1310 }
Willy Tarreau91861262007-10-17 17:06:05 +02001311
1312 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
1313 /* fall through */
1314
1315 case DATA_ST_PX_FIN:
1316 return 1;
1317
1318 default:
1319 /* unknown state, we should put an abort() here ! */
1320 return 1;
1321 }
1322}
1323
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001324
1325/* This function is called to send output to the response buffer.
1326 * It dumps the sessions states onto the output buffer <rep>.
1327 * Expects to be called with client socket shut down on input.
1328 * s->data_ctx must have been zeroed first, and the flags properly set.
1329 * It automatically clears the HIJACK bit from the response buffer.
1330 */
1331void stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
1332{
1333 struct chunk msg;
1334
1335 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
1336 /* If we're forced to shut down, we might have to remove our
1337 * reference to the last session being dumped.
1338 */
1339 if (s->data_state == DATA_ST_LIST) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001340 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001341 LIST_DEL(&s->data_ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001342 LIST_INIT(&s->data_ctx.sess.bref.users);
1343 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001344 }
1345 s->data_state = DATA_ST_FIN;
1346 buffer_stop_hijack(rep);
1347 s->ana_state = STATS_ST_CLOSE;
1348 return;
1349 }
1350
1351 if (s->ana_state != STATS_ST_REP)
1352 return;
1353
1354 msg.len = 0;
1355 msg.str = trash;
1356
1357 switch (s->data_state) {
1358 case DATA_ST_INIT:
1359 /* the function had not been called yet, let's prepare the
1360 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001361 * pointer to the first in the global list. When a target
1362 * session is being destroyed, it is responsible for updating
1363 * this pointer. We know we have reached the end when this
1364 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001365 */
1366 stream_int_retnclose(rep->cons, &msg);
1367 LIST_INIT(&s->data_ctx.sess.bref.users);
1368 s->data_ctx.sess.bref.ref = sessions.n;
1369 s->data_state = DATA_ST_LIST;
1370 /* fall through */
1371
1372 case DATA_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001373 /* first, let's detach the back-ref from a possible previous session */
1374 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
1375 LIST_DEL(&s->data_ctx.sess.bref.users);
1376 LIST_INIT(&s->data_ctx.sess.bref.users);
1377 }
1378
1379 /* and start from where we stopped */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001380 while (s->data_ctx.sess.bref.ref != &sessions) {
1381 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1382 struct session *curr_sess;
1383
1384 curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list);
1385
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001386 chunk_printf(&msg, sizeof(trash),
1387 "%p: proto=%s",
1388 curr_sess,
1389 curr_sess->listener->proto->name);
1390
1391 switch (curr_sess->listener->proto->sock_family) {
1392 case AF_INET:
1393 inet_ntop(AF_INET,
1394 (const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr,
1395 pn, sizeof(pn));
1396
1397 chunk_printf(&msg, sizeof(trash),
1398 " src=%s:%d fe=%s be=%s srv=%s",
1399 pn,
1400 ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port),
1401 curr_sess->fe->id,
1402 curr_sess->be->id,
1403 curr_sess->srv ? curr_sess->srv->id : "<none>"
1404 );
1405 break;
1406 case AF_INET6:
1407 inet_ntop(AF_INET6,
1408 (const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr,
1409 pn, sizeof(pn));
1410
1411 chunk_printf(&msg, sizeof(trash),
1412 " src=%s:%d fe=%s be=%s srv=%s",
1413 pn,
1414 ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port),
1415 curr_sess->fe->id,
1416 curr_sess->be->id,
1417 curr_sess->srv ? curr_sess->srv->id : "<none>"
1418 );
1419
1420 break;
1421 case AF_UNIX:
1422 /* no more information to print right now */
1423 break;
1424 }
1425
1426 chunk_printf(&msg, sizeof(trash),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001427 " as=%d ts=%02x age=%s calls=%d",
Willy Tarreau8a5c6262008-12-08 00:16:21 +01001428 curr_sess->ana_state, curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01001429 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
1430 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001431
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001432 chunk_printf(&msg, sizeof(trash),
1433 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
1434 curr_sess->req->flags,
1435 curr_sess->req->l,
1436 curr_sess->req->analysers,
1437 curr_sess->req->rex ?
1438 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
1439 TICKS_TO_MS(1000)) : "");
1440
1441 chunk_printf(&msg, sizeof(trash),
1442 ",wx=%s",
1443 curr_sess->req->wex ?
1444 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
1445 TICKS_TO_MS(1000)) : "");
1446
1447 chunk_printf(&msg, sizeof(trash),
1448 ",ax=%s]",
1449 curr_sess->req->analyse_exp ?
1450 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
1451 TICKS_TO_MS(1000)) : "");
1452
1453 chunk_printf(&msg, sizeof(trash),
1454 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
1455 curr_sess->rep->flags,
1456 curr_sess->rep->l,
1457 curr_sess->rep->analysers,
1458 curr_sess->rep->rex ?
1459 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
1460 TICKS_TO_MS(1000)) : "");
1461
1462 chunk_printf(&msg, sizeof(trash),
1463 ",wx=%s",
1464 curr_sess->rep->wex ?
1465 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
1466 TICKS_TO_MS(1000)) : "");
1467
1468 chunk_printf(&msg, sizeof(trash),
1469 ",ax=%s]",
1470 curr_sess->rep->analyse_exp ?
1471 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
1472 TICKS_TO_MS(1000)) : "");
1473
1474 chunk_printf(&msg, sizeof(trash),
1475 " s0=[%d,%1xh,fd=%d,ex=%s]",
1476 curr_sess->si[0].state,
1477 curr_sess->si[0].flags,
1478 curr_sess->si[0].fd,
1479 curr_sess->si[0].exp ?
1480 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
1481 TICKS_TO_MS(1000)) : "");
1482
1483 chunk_printf(&msg, sizeof(trash),
1484 " s1=[%d,%1xh,fd=%d,ex=%s]",
1485 curr_sess->si[1].state,
1486 curr_sess->si[1].flags,
1487 curr_sess->si[1].fd,
1488 curr_sess->si[1].exp ?
1489 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
1490 TICKS_TO_MS(1000)) : "");
1491
1492 chunk_printf(&msg, sizeof(trash),
1493 " exp=%s",
1494 curr_sess->task->expire ?
1495 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
1496 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01001497 if (task_in_rq(curr_sess->task))
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001498 chunk_printf(&msg, sizeof(trash), " run(nice=%d)", curr_sess->task->nice);
1499
1500 chunk_printf(&msg, sizeof(trash), "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001501
1502 if (buffer_write_chunk(rep, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001503 /* let's try again later from this session. We add ourselves into
1504 * this session's users so that it can remove us upon termination.
1505 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001506 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
1507 return;
1508 }
1509
1510 s->data_ctx.sess.bref.ref = curr_sess->list.n;
1511 }
1512 s->data_state = DATA_ST_FIN;
1513 /* fall through */
1514
1515 default:
1516 s->data_state = DATA_ST_FIN;
1517 buffer_stop_hijack(rep);
1518 s->ana_state = STATS_ST_CLOSE;
1519 return;
1520 }
1521}
1522
Willy Tarreau74808cb2009-03-04 15:53:18 +01001523/* print a line of error buffer (limited to 70 bytes) to <out>. The format is :
1524 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
1525 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
1526 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
1527 * lines are respected within the limit of 70 output chars. Lines that are
1528 * continuation of a previous truncated line begin with "+" instead of " "
1529 * after the offset. The new pointer is returned.
1530 */
1531static int dump_error_line(struct chunk *out, int size,
1532 struct error_snapshot *err, int *line, int ptr)
1533{
1534 int end;
1535 unsigned char c;
1536
1537 end = out->len + 80;
1538 if (end > size)
1539 return ptr;
1540
1541 chunk_printf(out, size, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
1542
1543 while (ptr < err->len) {
1544 c = err->buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01001545 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001546 if (out->len > end - 2)
1547 break;
1548 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01001549 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001550 if (out->len > end - 3)
1551 break;
1552 out->str[out->len++] = '\\';
1553 switch (c) {
1554 case '\t': c = 't'; break;
1555 case '\n': c = 'n'; break;
1556 case '\r': c = 'r'; break;
1557 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01001558 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001559 }
1560 out->str[out->len++] = c;
1561 } else {
1562 if (out->len > end - 5)
1563 break;
1564 out->str[out->len++] = '\\';
1565 out->str[out->len++] = 'x';
1566 out->str[out->len++] = hextab[(c >> 4) & 0xF];
1567 out->str[out->len++] = hextab[c & 0xF];
1568 }
1569 if (err->buf[ptr++] == '\n') {
1570 /* we had a line break, let's return now */
1571 out->str[out->len++] = '\n';
1572 *line = ptr;
1573 return ptr;
1574 }
1575 }
1576 /* we have an incomplete line, we return it as-is */
1577 out->str[out->len++] = '\n';
1578 return ptr;
1579}
1580
1581/* This function is called to send output to the response buffer.
1582 * It dumps the errors logged in proxies onto the output buffer <rep>.
1583 * Expects to be called with client socket shut down on input.
1584 * s->data_ctx must have been zeroed first, and the flags properly set.
1585 * It automatically clears the HIJACK bit from the response buffer.
1586 */
1587void stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
1588{
1589 extern const char *monthname[12];
1590 struct chunk msg;
1591
1592 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
1593 s->data_state = DATA_ST_FIN;
1594 buffer_stop_hijack(rep);
1595 s->ana_state = STATS_ST_CLOSE;
1596 return;
1597 }
1598
1599 if (s->ana_state != STATS_ST_REP)
1600 return;
1601
1602 msg.len = 0;
1603 msg.str = trash;
1604
1605 if (!s->data_ctx.errors.px) {
1606 /* the function had not been called yet, let's prepare the
1607 * buffer for a response.
1608 */
1609 stream_int_retnclose(rep->cons, &msg);
1610 s->data_ctx.errors.px = proxy;
1611 s->data_ctx.errors.buf = 0;
1612 s->data_ctx.errors.bol = 0;
1613 s->data_ctx.errors.ptr = -1;
1614 }
1615
1616 /* we have two inner loops here, one for the proxy, the other one for
1617 * the buffer.
1618 */
1619 while (s->data_ctx.errors.px) {
1620 struct error_snapshot *es;
1621
1622 if (s->data_ctx.errors.buf == 0)
1623 es = &s->data_ctx.errors.px->invalid_req;
1624 else
1625 es = &s->data_ctx.errors.px->invalid_rep;
1626
1627 if (!es->when.tv_sec)
1628 goto next;
1629
1630 if (s->data_ctx.errors.iid >= 0 &&
1631 s->data_ctx.errors.px->uuid != s->data_ctx.errors.iid &&
1632 es->oe->uuid != s->data_ctx.errors.iid)
1633 goto next;
1634
1635 if (s->data_ctx.errors.ptr < 0) {
1636 /* just print headers now */
1637
1638 char pn[INET6_ADDRSTRLEN];
1639 struct tm tm;
1640
1641 get_localtime(es->when.tv_sec, &tm);
1642 chunk_printf(&msg, sizeof(trash), "\n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
1643 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001644 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01001645
1646
1647 if (es->src.ss_family == AF_INET)
1648 inet_ntop(AF_INET,
1649 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
1650 pn, sizeof(pn));
1651 else
1652 inet_ntop(AF_INET6,
1653 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
1654 pn, sizeof(pn));
1655
1656 switch (s->data_ctx.errors.buf) {
1657 case 0:
1658 chunk_printf(&msg, sizeof(trash),
1659 " frontend %s (#%d): invalid request\n"
1660 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
1661 " request length %d bytes, error at position %d:\n\n",
1662 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
1663 pn, es->sid, es->oe->id, es->oe->uuid,
1664 es->srv ? es->srv->id : "<NONE>",
1665 es->srv ? es->srv->puid : -1,
1666 es->len, es->pos);
1667 break;
1668 case 1:
1669 chunk_printf(&msg, sizeof(trash),
1670 " backend %s (#%d) : invalid response\n"
1671 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
1672 " response length %d bytes, error at position %d:\n\n",
1673 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
1674 pn, es->sid, es->oe->id, es->oe->uuid,
1675 es->srv ? es->srv->id : "<NONE>",
1676 es->srv ? es->srv->puid : -1,
1677 es->len, es->pos);
1678 break;
1679 }
1680
1681 if (buffer_write_chunk(rep, &msg) >= 0) {
1682 /* Socket buffer full. Let's try again later from the same point */
1683 return;
1684 }
1685 s->data_ctx.errors.ptr = 0;
1686 s->data_ctx.errors.sid = es->sid;
1687 }
1688
1689 if (s->data_ctx.errors.sid != es->sid) {
1690 /* the snapshot changed while we were dumping it */
1691 chunk_printf(&msg, sizeof(trash),
1692 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
1693 if (buffer_write_chunk(rep, &msg) >= 0)
1694 return;
1695 goto next;
1696 }
1697
1698 /* OK, ptr >= 0, so we have to dump the current line */
1699 while (s->data_ctx.errors.ptr < es->len) {
1700 int newptr;
1701 int newline;
1702
1703 newline = s->data_ctx.errors.bol;
1704 newptr = dump_error_line(&msg, sizeof(trash), es, &newline, s->data_ctx.errors.ptr);
1705 if (newptr == s->data_ctx.errors.ptr)
1706 return;
1707
1708 if (buffer_write_chunk(rep, &msg) >= 0) {
1709 /* Socket buffer full. Let's try again later from the same point */
1710 return;
1711 }
1712 s->data_ctx.errors.ptr = newptr;
1713 s->data_ctx.errors.bol = newline;
1714 };
1715 next:
1716 s->data_ctx.errors.bol = 0;
1717 s->data_ctx.errors.ptr = -1;
1718 s->data_ctx.errors.buf++;
1719 if (s->data_ctx.errors.buf > 1) {
1720 s->data_ctx.errors.buf = 0;
1721 s->data_ctx.errors.px = s->data_ctx.errors.px->next;
1722 }
1723 }
1724
1725 /* dump complete */
1726 buffer_stop_hijack(rep);
1727 s->ana_state = STATS_ST_CLOSE;
1728}
1729
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001730
Willy Tarreau10522fd2008-07-09 20:12:41 +02001731static struct cfg_kw_list cfg_kws = {{ },{
1732 { CFG_GLOBAL, "stats", stats_parse_global },
1733 { 0, NULL, NULL },
1734}};
1735
1736__attribute__((constructor))
1737static void __dumpstats_module_init(void)
1738{
1739 cfg_register_keywords(&cfg_kws);
1740}
1741
Willy Tarreau91861262007-10-17 17:06:05 +02001742/*
1743 * Local variables:
1744 * c-indent-level: 8
1745 * c-basic-offset: 8
1746 * End:
1747 */