blob: d39bbcd3d7823c4d1ff78f3eaefd621b402bd798 [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"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020057 " help : this message\n"
58 " prompt : toggle interactive mode with prompt\n"
59 " quit : disconnect\n"
Willy Tarreau5ca791d2009-08-16 19:06:42 +020060 " show info : report information about the running process\n"
61 " show stat : report counters for each proxy and server\n"
62 " show errors : report last request and response errors for each proxy\n"
63 " show sess : report the list of current sessions\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020064 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020065
66const struct chunk stats_sock_usage = {
67 .str = (char *)&stats_sock_usage_msg,
68 .len = sizeof(stats_sock_usage_msg)-1
69};
70
Willy Tarreaufbee7132007-10-18 13:53:22 +020071/* This function parses a "stats" statement in the "global" section. It returns
72 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
73 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
74 * zero included. The trailing '\n' must not be written. The function must be
75 * called with <args> pointing to the first word after "stats".
76 */
Willy Tarreau10522fd2008-07-09 20:12:41 +020077static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
78 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +020079{
Willy Tarreau10522fd2008-07-09 20:12:41 +020080 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +020081 if (!strcmp(args[0], "socket")) {
82 struct sockaddr_un su;
83 int cur_arg;
84
85 if (*args[1] == 0) {
86 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
87 return -1;
88 }
89
90 if (global.stats_sock.state != LI_NEW) {
91 snprintf(err, errlen, "'stats socket' already specified in global section");
92 return -1;
93 }
94
95 su.sun_family = AF_UNIX;
96 strncpy(su.sun_path, args[1], sizeof(su.sun_path));
97 su.sun_path[sizeof(su.sun_path) - 1] = 0;
98 memcpy(&global.stats_sock.addr, &su, sizeof(su)); // guaranteed to fit
99
Willy Tarreau89a63132009-08-16 17:41:45 +0200100 if (!global.stats_fe) {
101 if ((global.stats_fe = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) {
102 snprintf(err, errlen, "out of memory");
103 return -1;
104 }
105
106 LIST_INIT(&global.stats_fe->pendconns);
107 LIST_INIT(&global.stats_fe->acl);
108 LIST_INIT(&global.stats_fe->block_cond);
109 LIST_INIT(&global.stats_fe->redirect_rules);
110 LIST_INIT(&global.stats_fe->mon_fail_cond);
111 LIST_INIT(&global.stats_fe->switching_rules);
112 LIST_INIT(&global.stats_fe->tcp_req.inspect_rules);
113
114 /* Timeouts are defined as -1, so we cannot use the zeroed area
115 * as a default value.
116 */
117 proxy_reset_timeouts(global.stats_fe);
118
119 global.stats_fe->last_change = now.tv_sec;
120 global.stats_fe->id = strdup("GLOBAL");
121 global.stats_fe->cap = PR_CAP_FE;
122 }
123
Willy Tarreaufbee7132007-10-18 13:53:22 +0200124 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100125 global.stats_sock.options = LI_O_NONE;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200126 global.stats_sock.accept = uxst_event_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200127 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200128 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200129 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreau89a63132009-08-16 17:41:45 +0200130 global.stats_sock.private = global.stats_fe; /* must point to the frontend */
131
132 global.stats_fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
133 global.stats_sock.timeout = &global.stats_fe->timeout.client;
134
135 global.stats_sock.next = global.stats_fe->listen;
136 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200137
138 cur_arg = 2;
139 while (*args[cur_arg]) {
140 if (!strcmp(args[cur_arg], "uid")) {
141 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
142 cur_arg += 2;
143 }
144 else if (!strcmp(args[cur_arg], "gid")) {
145 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
146 cur_arg += 2;
147 }
148 else if (!strcmp(args[cur_arg], "mode")) {
149 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
150 cur_arg += 2;
151 }
152 else if (!strcmp(args[cur_arg], "user")) {
153 struct passwd *user;
154 user = getpwnam(args[cur_arg + 1]);
155 if (!user) {
156 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
157 args[cur_arg + 1]);
158 return -1;
159 }
160 global.stats_sock.perm.ux.uid = user->pw_uid;
161 cur_arg += 2;
162 }
163 else if (!strcmp(args[cur_arg], "group")) {
164 struct group *group;
165 group = getgrnam(args[cur_arg + 1]);
166 if (!group) {
167 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
168 args[cur_arg + 1]);
169 return -1;
170 }
171 global.stats_sock.perm.ux.gid = group->gr_gid;
172 cur_arg += 2;
173 }
174 else {
175 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', and 'mode'");
176 return -1;
177 }
178 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100179
Willy Tarreaufbee7132007-10-18 13:53:22 +0200180 uxst_add_listener(&global.stats_sock);
181 global.maxsock++;
182 }
183 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100184 unsigned timeout;
185 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
186
187 if (res) {
188 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
189 return -1;
190 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200191
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100192 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200193 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
194 return -1;
195 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200196 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200197 }
198 else if (!strcmp(args[0], "maxconn")) {
199 int maxconn = atol(args[1]);
200
201 if (maxconn <= 0) {
202 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
203 return -1;
204 }
205 global.maxsock -= global.stats_sock.maxconn;
206 global.stats_sock.maxconn = maxconn;
207 global.maxsock += global.stats_sock.maxconn;
208 }
209 else {
210 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
211 return -1;
212 }
213 return 0;
214}
215
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200216int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100217{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200218 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100219 "# pxname,svname,"
220 "qcur,qmax,"
221 "scur,smax,slim,stot,"
222 "bin,bout,"
223 "dreq,dresp,"
224 "ereq,econ,eresp,"
225 "wretr,wredis,"
226 "status,weight,act,bck,"
227 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200228 "pid,iid,sid,throttle,lbtot,tracked,type,"
229 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200230 "check_status,check_code,check_duration,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100231 "\n");
232}
233
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200234/* Processes the stats interpreter on the statistics socket. This function is
235 * called from an applet running in a stream interface. Right now we still
236 * support older functions which used to emulate servers and to set
237 * STATS_ST_CLOSE upon completion, but we also support a new interactive mode.
238 * The function returns 1 if the request was understood, otherwise zero.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200239 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200240int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200241{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200242 struct session *s = si->private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200243 char *args[MAX_STATS_ARGS + 1];
244 int arg;
245
246 while (isspace((unsigned char)*line))
247 line++;
248
249 arg = 0;
250 args[arg] = line;
251
252 while (*line && arg < MAX_STATS_ARGS) {
253 if (isspace((unsigned char)*line)) {
254 *line++ = '\0';
255
256 while (isspace((unsigned char)*line))
257 line++;
258
259 args[++arg] = line;
260 continue;
261 }
262
263 line++;
264 }
265
266 while (++arg <= MAX_STATS_ARGS)
267 args[arg] = line;
268
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200269 si->st0 = 0;
270 s->data_ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200271 if (strcmp(args[0], "show") == 0) {
272 if (strcmp(args[1], "stat") == 0) {
273 if (*args[2] && *args[3] && *args[4]) {
274 s->data_ctx.stats.flags |= STAT_BOUND;
275 s->data_ctx.stats.iid = atoi(args[2]);
276 s->data_ctx.stats.type = atoi(args[3]);
277 s->data_ctx.stats.sid = atoi(args[4]);
278 }
279
280 s->data_ctx.stats.flags |= STAT_SHOW_STAT;
281 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200282 s->data_state = DATA_ST_INIT;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200283 s->ana_state = STATS_ST_REP;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200284 si->st0 = 3; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200285 }
286 else if (strcmp(args[1], "info") == 0) {
287 s->data_ctx.stats.flags |= STAT_SHOW_INFO;
288 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200289 s->data_state = DATA_ST_INIT;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200290 s->ana_state = STATS_ST_REP;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200291 si->st0 = 3; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200292 }
293 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200294 s->data_state = DATA_ST_INIT;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200295 s->ana_state = STATS_ST_REP;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200296 si->st0 = 4; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200297 }
298 else if (strcmp(args[1], "errors") == 0) {
299 if (*args[2])
300 s->data_ctx.errors.iid = atoi(args[2]);
301 else
302 s->data_ctx.errors.iid = -1;
303 s->data_ctx.errors.px = NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200304 s->data_state = DATA_ST_INIT;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200305 s->ana_state = STATS_ST_REP;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200306 si->st0 = 5; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200307 }
308 else { /* neither "stat" nor "info" nor "sess" */
309 return 0;
310 }
311 }
312 else { /* not "show" */
313 return 0;
314 }
315 return 1;
316}
317
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200318/* This I/O handler runs as an applet embedded in a stream interface. It is
319 * used to processes I/O from/to the stats unix socket. Right now we still
320 * support older functions which used to emulate servers and to set
321 * STATS_ST_CLOSE upon completion, but we also support a new interactive mode.
322 * The system relies on a request/response flip-flop state machine. We read
323 * a request, then we process it and send the response. Then we can read again.
324 * This could be enhanced a lot but we're still bound to support older output
325 * functions which were designed to work as hijackers.
326 * At the moment, we use si->st0 as the output type, and si->st1 to indicate
327 * whether we're in prompt mode or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200328 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200329void stats_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200330{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200331 struct session *s = si->private;
332 struct buffer *req = si->ob;
333 struct buffer *res = si->ib;
334 int reql;
335 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200336
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200337 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
338 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200339
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200340 while (1) {
341 if (s->ana_state == STATS_ST_INIT) {
342 /* Stats output not initialized yet */
343 memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats));
344 s->data_source = DATA_SRC_STATS;
345 s->ana_state = STATS_ST_REQ;
346 }
347 else if (s->ana_state == STATS_ST_REQ) {
348 reql = buffer_si_peekline(si->ob, trash, sizeof(trash));
349 if (reql <= 0) { /* closed or EOL not found */
350 if (reql == 0)
351 break;
352 s->ana_state = STATS_ST_CLOSE;
353 continue;
354 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200355
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200356 /* seek for a possible semi-colon. If we find one, we
357 * replace it with an LF and skip only this part.
358 */
359 for (len = 0; len < reql; len++)
360 if (trash[len] == ';') {
361 trash[len] = '\n';
362 reql = len + 1;
363 break;
364 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200365
Willy Tarreau816fc222009-10-04 07:36:58 +0200366 /* now it is time to check that we have a full line,
367 * remove the trailing \n and possibly \r, then cut the
368 * line.
369 */
370 len = reql - 1;
371 if (trash[len] != '\n') {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200372 s->ana_state = STATS_ST_CLOSE;
373 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200374 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200375
Willy Tarreau816fc222009-10-04 07:36:58 +0200376 if (len && trash[len-1] == '\r')
377 len--;
378
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200379 trash[len] = '\0';
380
381 si->st0 = 1; // default to prompt
382 if (len) {
383 if (strcmp(trash, "quit") == 0) {
384 s->ana_state = STATS_ST_CLOSE;
385 continue;
386 }
387 else if (strcmp(trash, "prompt") == 0)
388 si->st1 = !si->st1;
389 else if (strcmp(trash, "help") == 0 ||
390 !stats_sock_parse_request(si, trash))
391 si->st0 = 2; // help
392 }
393 else if (!si->st1) {
394 /* if prompt is disabled, print help on empty lines,
395 * so that the user at least knows how to enable
396 * prompt and find help.
397 */
398 si->st0 = 2;
399 }
400
401 /* re-adjust req buffer */
402 buffer_skip(si->ob, reql);
403
404 s->ana_state = STATS_ST_REP;
405 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200406 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200407 else if (s->ana_state == STATS_ST_REP) {
408 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
409 s->ana_state = STATS_ST_CLOSE;
410 continue;
411 }
412
413 switch (si->st0) {
414 case 2: /* help */
415 if (buffer_feed(si->ib, stats_sock_usage.str, stats_sock_usage.len) < 0)
416 /* message sent or too large for buffer (!) */
417 si->st0 = 1; // send prompt
418 break;
419 case 3: /* stats/info dump, should be split later ? */
420 stats_dump_raw_to_buffer(s, res);
421 si->ib->flags |= BF_READ_PARTIAL; /* remove this once we use buffer_feed */
422 if (s->ana_state == STATS_ST_CLOSE)
423 si->st0 = 1; // end of command, send prompt
424 break;
425 case 4: /* sessions dump */
Willy Tarreau7e72a8f2009-10-03 23:55:14 +0200426 if (stats_dump_sess_to_buffer(s, res))
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200427 si->st0 = 1; // end of command, send prompt
428 break;
429 case 5: /* errors dump */
Willy Tarreau61b34732009-10-03 23:49:35 +0200430 if (stats_dump_errors_to_buffer(s, res))
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200431 si->st0 = 1; // end of command, send prompt
432 break;
433 default: /* abnormal state or lack of space for prompt */
434 si->st0 = 1; // return to prompt
435 break;
436 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200437
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200438 if (si->st0 == 1) { /* post-command prompt (LF or LF + '> ') */
439 if (!si->st1) { /* non-interactive mode */
440 if (buffer_feed(si->ib, "\n", 1) < 0)
441 si->st0 = 0; // end of output
442 }
443 else { /* interactive mode */
444 if (buffer_feed(si->ib, "\n> ", 3) < 0)
445 si->st0 = 0; // end of output
446 }
447 }
448
449 /* If the output functions are still there, it means
450 * they require more room.
451 */
452 if (si->st0 > 0) {
453 s->ana_state = STATS_ST_REP; /* some old applets still force CLOSE */
454 si->flags |= SI_FL_WAIT_ROOM;
455 break;
456 }
457
458 /* Now we close the output if one of the writers did so,
459 * or if we're not in interactive mode and the request
460 * buffer is empty. This still allows pipelined requests
461 * to be sent in non-interactive mode.
462 */
463 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->st1 && !req->send_max)) {
464 s->ana_state = STATS_ST_CLOSE;
465 continue;
466 }
467
468 /* switch state back to ST_REQ to read next requests */
469 s->ana_state = STATS_ST_REQ;
470 }
471 else if (s->ana_state == STATS_ST_CLOSE) {
Willy Tarreau33b230b2009-10-04 09:16:41 +0200472 /* Let's close for real now. We just close the request
473 * side, the conditions below will complete if needed.
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200474 */
Willy Tarreau33b230b2009-10-04 09:16:41 +0200475 si->shutw(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200476 s->ana_state = 0;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200477 break;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200478 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200479 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200480
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200481 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (s->ana_state != STATS_ST_REQ)) {
482 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
483 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
484 /* Other size has closed, let's abort if we have no more processing to do
485 * and nothing more to consume. This is comparable to a broken pipe, so
486 * we forward the close to the request side so that it flows upstream to
487 * the client.
488 */
489 si->shutw(si);
490 }
491
492 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (s->ana_state != STATS_ST_REP)) {
493 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
494 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
495 /* We have no more processing to do, and nothing more to send, and
496 * the client side has closed. So we'll forward this state downstream
497 * on the response buffer.
498 */
499 si->shutr(si);
500 res->flags |= BF_READ_NULL;
501 }
502
503 /* update all other flags and resync with the other side */
504 si->update(si);
505
506 /* we don't want to expire timeouts while we're processing requests */
507 si->ib->rex = TICK_ETERNITY;
508 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200509
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200510 out:
511 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
512 __FUNCTION__, __LINE__,
513 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
514
515 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
516 /* check that we have released everything then unregister */
517 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200518 s->ana_state = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200519 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200520}
521
Willy Tarreau91861262007-10-17 17:06:05 +0200522/*
523 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200524 * client socket shut down on input. It *may* make use of informations from
525 * <uri>. s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200526 * It returns 0 if it had to stop writing data and an I/O is needed, 1 if the
527 * dump is finished and the session must be closed, or -1 in case of any error.
Willy Tarreau0a464892008-12-07 18:30:00 +0100528 * It automatically clears the HIJACK bit from the response buffer.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200529 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100530int stats_dump_raw(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau3e76e722007-10-17 18:57:38 +0200531{
Willy Tarreau3e76e722007-10-17 18:57:38 +0200532 struct proxy *px;
533 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +0100534 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200535
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200536 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +0200537
538 switch (s->data_state) {
539 case DATA_ST_INIT:
540 /* the function had not been called yet, let's prepare the
541 * buffer for a response.
542 */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200543 s->data_state = DATA_ST_HEAD;
544 /* fall through */
545
546 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100547 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200548 print_csv_header(&msg);
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200549 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100550 return 0;
551 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200552
553 s->data_state = DATA_ST_INFO;
554 /* fall through */
555
556 case DATA_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +0100557 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100558 if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200559 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +0100560 "Name: " PRODUCT_NAME "\n"
561 "Version: " HAPROXY_VERSION "\n"
562 "Release_date: " HAPROXY_DATE "\n"
563 "Nbproc: %d\n"
564 "Process_num: %d\n"
565 "Pid: %d\n"
566 "Uptime: %dd %dh%02dm%02ds\n"
567 "Uptime_sec: %d\n"
568 "Memmax_MB: %d\n"
569 "Ulimit-n: %d\n"
570 "Maxsock: %d\n"
571 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100572 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100573 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100574 "PipesUsed: %d\n"
575 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100576 "Tasks: %d\n"
577 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200578 "node: %s\n"
579 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100580 "",
581 global.nbproc,
582 relative_pid,
583 pid,
584 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
585 up,
586 global.rlimit_memmax,
587 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100588 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100589 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200590 nb_tasks_cur, run_queue_cur,
591 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +0100592 );
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200593 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100594 return 0;
595 }
596
Willy Tarreau3e76e722007-10-17 18:57:38 +0200597 s->data_ctx.stats.px = proxy;
598 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100599
600 s->data_ctx.stats.sv = NULL;
601 s->data_ctx.stats.sv_st = 0;
602
Willy Tarreau3e76e722007-10-17 18:57:38 +0200603 s->data_state = DATA_ST_LIST;
604 /* fall through */
605
606 case DATA_ST_LIST:
607 /* dump proxies */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100608 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100609 while (s->data_ctx.stats.px) {
610 px = s->data_ctx.stats.px;
611 /* skip the disabled proxies and non-networked ones */
612 if (px->state != PR_STSTOPPED &&
613 (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100614 if (stats_dump_proxy(s, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100615 return 0;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200616
Willy Tarreaua8efd362008-01-03 10:19:15 +0100617 s->data_ctx.stats.px = px->next;
618 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
619 }
620 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200621 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200622
623 s->data_state = DATA_ST_END;
624 /* fall through */
625
626 case DATA_ST_END:
627 s->data_state = DATA_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +0100628 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200629
630 case DATA_ST_FIN:
Willy Tarreau0a464892008-12-07 18:30:00 +0100631 buffer_stop_hijack(rep);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200632 return 1;
633
634 default:
635 /* unknown state ! */
Willy Tarreau0a464892008-12-07 18:30:00 +0100636 buffer_stop_hijack(rep);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200637 return -1;
638 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100639}
640
641
642/* This function is called to send output to the response buffer. It simply
643 * calls stats_dump_raw(), and releases the buffer's hijack bit when the dump
Willy Tarreau01bf8672008-12-07 18:03:29 +0100644 * is finished.
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100645 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100646void stats_dump_raw_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100647{
648 if (s->ana_state != STATS_ST_REP)
Willy Tarreau01bf8672008-12-07 18:03:29 +0100649 return;
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100650
Willy Tarreau0a464892008-12-07 18:30:00 +0100651 if (stats_dump_raw(s, rep, NULL) != 0)
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100652 s->ana_state = STATS_ST_CLOSE;
Willy Tarreau01bf8672008-12-07 18:03:29 +0100653 return;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200654}
655
656
657/*
658 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200659 * client socket shut down on input. It stops by itself by unsetting the
Willy Tarreau72b179a2008-08-28 16:01:32 +0200660 * BF_HIJACK flag from the buffer, which it uses to keep on being called
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200661 * when there is free space in the buffer, of simply by letting an empty buffer
662 * upon return.s->data_ctx must have been zeroed before the first call, and the
663 * flags set. It returns 0 if it had to stop writing data and an I/O is needed,
664 * 1 if the dump is finished and the session must be closed, or -1 in case of
665 * any error.
Willy Tarreau91861262007-10-17 17:06:05 +0200666 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100667int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200668{
Willy Tarreau91861262007-10-17 17:06:05 +0200669 struct proxy *px;
670 struct chunk msg;
671 unsigned int up;
672
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200673 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +0200674
675 switch (s->data_state) {
676 case DATA_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200677 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200678 "HTTP/1.0 200 OK\r\n"
679 "Cache-Control: no-cache\r\n"
680 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200681 "Content-Type: %s\r\n",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100682 (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +0200683
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100684 if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200685 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +0200686 uri->refresh);
687
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200688 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +0200689
690 s->txn.status = 200;
Willy Tarreau56a560a2009-09-22 19:27:35 +0200691 if (buffer_write_chunk(rep, &msg) >= 0)
692 return 0;
693
Willy Tarreau91861262007-10-17 17:06:05 +0200694 msg.len = 0;
695
696 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
697 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
698 if (!(s->flags & SN_FINST_MASK))
699 s->flags |= SN_FINST_R;
700
701 if (s->txn.meth == HTTP_METH_HEAD) {
702 /* that's all we return in case of HEAD request */
703 s->data_state = DATA_ST_FIN;
Willy Tarreau72b179a2008-08-28 16:01:32 +0200704 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200705 return 1;
706 }
707
708 s->data_state = DATA_ST_HEAD; /* let's start producing data */
709 /* fall through */
710
711 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100712 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200713 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200714 chunk_printf(&msg,
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200715 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200716 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
717 "<style type=\"text/css\"><!--\n"
718 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +0200719 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +0200720 " font-size: 12px;"
721 " font-weight: normal;"
722 " color: black;"
723 " background: white;"
724 "}\n"
725 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +0200726 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +0200727 " align: center;"
728 "}\n"
729 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +0200730 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +0200731 " margin-bottom: 0.5em;"
732 "}\n"
733 "h2 {"
734 " font-family: helvetica, arial;"
735 " font-size: x-large;"
736 " font-weight: bold;"
737 " font-style: italic;"
738 " color: #6020a0;"
739 " margin-top: 0em;"
740 " margin-bottom: 0em;"
741 "}\n"
742 "h3 {"
743 " font-family: helvetica, arial;"
744 " font-size: 16px;"
745 " font-weight: bold;"
746 " color: #b00040;"
747 " background: #e8e8d0;"
748 " margin-top: 0em;"
749 " margin-bottom: 0em;"
750 "}\n"
751 "li {"
752 " margin-top: 0.25em;"
753 " margin-right: 2em;"
754 "}\n"
755 ".hr {margin-top: 0.25em;"
756 " border-color: black;"
757 " border-bottom-style: solid;"
758 "}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200759 ".titre {background: #20D0D0;color: #000000; font-weight: bold;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200760 ".total {background: #20D0D0;color: #ffff80;}\n"
761 ".frontend {background: #e8e8d0;}\n"
762 ".backend {background: #e8e8d0;}\n"
763 ".active0 {background: #ff9090;}\n"
764 ".active1 {background: #ffd020;}\n"
765 ".active2 {background: #ffffa0;}\n"
766 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100767 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
768 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
769 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200770 ".backup0 {background: #ff9090;}\n"
771 ".backup1 {background: #ff80ff;}\n"
772 ".backup2 {background: #c060ff;}\n"
773 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100774 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
775 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
776 ".backup6 {background: #e0e0e0;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +0200777 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Willy Tarreau91861262007-10-17 17:06:05 +0200778 "table.tbl { border-collapse: collapse; border-style: none;}\n"
779 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray;}\n"
780 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzki619caca2009-10-03 15:46:08 +0200781 "table.tbl th.pxname {background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +0200782 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200783 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200784 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
785 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
786 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
787 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200788 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200789 (uri->flags&ST_SHNODE) ? " on " : "",
790 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200791 );
Willy Tarreau55bb8452007-10-17 18:44:57 +0200792 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200793 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200794 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200795 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200796 return 0;
797
798 s->data_state = DATA_ST_INFO;
799 /* fall through */
800
801 case DATA_ST_INFO:
802 up = (now.tv_sec - start_date.tv_sec);
803
804 /* WARNING! this has to fit the first packet too.
805 * We are around 3.5 kB, add adding entries will
806 * become tricky if we want to support 4kB buffers !
807 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100808 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200809 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200810 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
811 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200812 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200813 "<hr width=\"100%%\" class=\"hr\">\n"
814 "<h3>&gt; General process information</h3>\n"
815 "<table border=0 cols=4><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100816 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200817 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200818 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
819 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
820 "current conns = %d; current pipes = %d/%d<br>\n"
821 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200822 "</td><td align=\"center\" nowrap>\n"
823 "<table class=\"lgd\"><tr>\n"
824 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
825 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
826 "</tr><tr>\n"
827 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
828 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
829 "</tr><tr>\n"
830 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
831 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
832 "</tr><tr>\n"
833 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100834 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200835 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100836 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +0200837 "</td>"
838 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
839 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
840 "",
841 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200842 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
843 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
844 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +0200845 up / 86400, (up % 86400) / 3600,
846 (up % 3600) / 60, (up % 60),
847 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
848 global.rlimit_memmax ? " MB" : "",
849 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100850 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100851 actconn, pipes_used, pipes_used+pipes_free,
852 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +0200853 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100854
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100855 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200856 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200857 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
858 uri->uri_prefix,
859 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100860 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200861 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200862 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200863 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
864 uri->uri_prefix,
865 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100866 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200867
Willy Tarreau55bb8452007-10-17 18:44:57 +0200868 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100869 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200870 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200871 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
872 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100873 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200874 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200875 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200876 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200877 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
878 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100879 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200880 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200881 }
Willy Tarreau91861262007-10-17 17:06:05 +0200882
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200883 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200884 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
885 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100886 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
887 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200888
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200889 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +0200890 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
891 uri->uri_prefix,
892 (uri->refresh > 0) ? ";norefresh" : "");
893
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200894 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200895 "</td>"
896 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
897 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
898 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
899 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
900 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
901 "</ul>"
902 "</td>"
903 "</tr></table>\n"
904 ""
905 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100906
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200907 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200908 return 0;
909 }
Willy Tarreau91861262007-10-17 17:06:05 +0200910
Willy Tarreau91861262007-10-17 17:06:05 +0200911 s->data_ctx.stats.px = proxy;
912 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
913 s->data_state = DATA_ST_LIST;
914 /* fall through */
915
916 case DATA_ST_LIST:
917 /* dump proxies */
918 while (s->data_ctx.stats.px) {
919 px = s->data_ctx.stats.px;
920 /* skip the disabled proxies and non-networked ones */
921 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100922 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200923 return 0;
924
925 s->data_ctx.stats.px = px->next;
926 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
927 }
928 /* here, we just have reached the last proxy */
929
930 s->data_state = DATA_ST_END;
931 /* fall through */
932
933 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100934 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200935 chunk_printf(&msg, "</body></html>\n");
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200936 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200937 return 0;
938 }
Willy Tarreau91861262007-10-17 17:06:05 +0200939
940 s->data_state = DATA_ST_FIN;
941 /* fall through */
942
943 case DATA_ST_FIN:
Willy Tarreau72b179a2008-08-28 16:01:32 +0200944 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200945 return 1;
946
947 default:
948 /* unknown state ! */
Willy Tarreau72b179a2008-08-28 16:01:32 +0200949 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200950 return -1;
951 }
952}
953
954
955/*
956 * Dumps statistics for a proxy.
957 * Returns 0 if it had to stop dumping data because of lack of buffer space,
958 * ot non-zero if everything completed.
959 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100960int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200961{
962 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100963 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Willy Tarreau91861262007-10-17 17:06:05 +0200964 struct chunk msg;
965
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200966 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +0200967
968 switch (s->data_ctx.stats.px_st) {
969 case DATA_ST_PX_INIT:
970 /* we are on a new proxy */
971
972 if (uri && uri->scope) {
973 /* we have a limited scope, we have to check the proxy name */
974 struct stat_scope *scope;
975 int len;
976
977 len = strlen(px->id);
978 scope = uri->scope;
979
980 while (scope) {
981 /* match exact proxy name */
982 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
983 break;
984
985 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +0200986 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +0200987 break;
988 scope = scope->next;
989 }
990
991 /* proxy name not found : don't dump anything */
992 if (scope == NULL)
993 return 1;
994 }
995
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100996 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100997 (px->uuid != s->data_ctx.stats.iid))
998 return 1;
999
Willy Tarreau91861262007-10-17 17:06:05 +02001000 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
1001 /* fall through */
1002
1003 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001004 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001005 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001006 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001007 "<table class=\"tbl\" width=\"100%%\">\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001008 "<tr align=\"center\" class=\"titre\">"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001009 "<th class=\"pxname\" width=\"10%%\">%s</th>"
1010 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001011 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001012 "</table>\n"
1013 "<table cols=\"29\" class=\"tbl\" width=\"100%%\">\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001014 "<tr align=\"center\" class=\"titre\">"
1015 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001016 "<th colspan=3>Queue</th>"
1017 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001018 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001019 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001020 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001021 "</tr>\n"
1022 "<tr align=\"center\" class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001023 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001024 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001025 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001026 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001027 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001028 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001029 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1030 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001031 "</tr>",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001032 px->id,
1033 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001034
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001035 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001036 return 0;
1037 }
Willy Tarreau91861262007-10-17 17:06:05 +02001038
1039 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
1040 /* fall through */
1041
1042 case DATA_ST_PX_FE:
1043 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001044 if ((px->cap & PR_CAP_FE) &&
1045 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
1046 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001047 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001048 /* name, queue */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001049 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=3></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001050 /* sessions rate : current, max, limit */
1051 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
1052 /* sessions : current, max, limit, total, lbtot */
1053 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001054 "<td align=right>%s</td><td align=right></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001055 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001056 "<td align=right>%s</td><td align=right>%s</td>"
1057 "",
Willy Tarreaua3e49422009-05-10 19:19:41 +02001058 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
1059 U2H1(px->fe_sps_max), LIM2A2(px->fe_sps_lim, "-"),
1060 U2H3(px->feconn), U2H4(px->feconn_max), U2H5(px->maxconn),
1061 U2H6(px->cum_feconn), U2H7(px->bytes_in), U2H8(px->bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001062
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001063 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001064 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001065 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001066 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001067 "<td align=right>%s</td><td align=right></td><td align=right></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001068 /* warnings: retries, redispatches */
1069 "<td align=right></td><td align=right></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001070 /* server status : reflect frontend status */
Willy Tarreau91861262007-10-17 17:06:05 +02001071 "<td align=center>%s</td>"
1072 /* rest of server: nothing */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001073 "<td align=center colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02001074 "",
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001075 U2H0(px->denied_req), U2H1(px->denied_resp),
1076 U2H2(px->failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +02001077 px->state == PR_STRUN ? "OPEN" :
1078 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001079 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001080 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001081 /* pxid, name, queue cur, queue max, */
1082 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001083 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001084 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001085 /* bytes : in, out */
1086 "%lld,%lld,"
1087 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001088 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001089 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001090 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001091 /* warnings: retries, redispatches */
1092 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001093 /* server status : reflect frontend status */
1094 "%s,"
1095 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001096 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001097 /* pid, iid, sid, throttle, lbtot, tracked, type */
1098 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001099 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001100 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001101 /* check_status, check_code, check_duration */
1102 ",,,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001103 "\n",
1104 px->id,
1105 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
1106 px->bytes_in, px->bytes_out,
1107 px->denied_req, px->denied_resp,
1108 px->failed_req,
1109 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +01001110 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01001111 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001112 read_freq_ctr(&px->fe_sess_per_sec),
1113 px->fe_sps_lim, px->fe_sps_max);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001114 }
Willy Tarreau91861262007-10-17 17:06:05 +02001115
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001116 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001117 return 0;
1118 }
1119
1120 s->data_ctx.stats.sv = px->srv; /* may be NULL */
1121 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
1122 /* fall through */
1123
1124 case DATA_ST_PX_SV:
1125 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001126 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
1127
Willy Tarreau2ea81932007-11-30 12:04:38 +01001128 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 +02001129
1130 sv = s->data_ctx.stats.sv;
1131
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001132 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001133 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
1134 break;
1135
1136 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
1137 continue;
1138 }
1139
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001140 if (sv->tracked)
1141 svs = sv->tracked;
1142 else
1143 svs = sv;
1144
Willy Tarreau91861262007-10-17 17:06:05 +02001145 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001146 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01001147 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001148 else if (svs->state & SRV_RUNNING) {
1149 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02001150 sv_state = 3; /* UP */
1151 else
1152 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01001153
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001154 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01001155 sv_state += 2;
1156 }
Willy Tarreau91861262007-10-17 17:06:05 +02001157 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001158 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02001159 sv_state = 1; /* going up */
1160 else
1161 sv_state = 0; /* DOWN */
1162
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001163 if ((sv_state == 0) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02001164 /* do not report servers which are DOWN */
1165 s->data_ctx.stats.sv = sv->next;
1166 continue;
1167 }
1168
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001169 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001170 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
1171 "UP %d/%d &darr;", "UP",
1172 "NOLB %d/%d &darr;", "NOLB",
1173 "<i>no check</i>" };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001174 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001175 /* name */
1176 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001177 /* queue : current, max, limit */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001178 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001179 /* sessions rate : current, max, limit */
1180 "<td align=right>%s</td><td align=right>%s</td><td align=right></td>"
1181 /* sessions : current, max, limit, total, lbtot */
1182 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001183 "<td align=right>%s</td><td align=right>%s</td>"
1184 "",
1185 (sv->state & SRV_BACKUP) ? "backup" : "active",
1186 sv_state, sv->id,
1187 U2H0(sv->nbpend), U2H1(sv->nbpend_max), LIM2A2(sv->maxqueue, "-"),
Willy Tarreaua3e49422009-05-10 19:19:41 +02001188 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->sps_max),
1189 U2H5(sv->cur_sess), U2H6(sv->cur_sess_max), LIM2A7(sv->maxconn, "-"),
1190 U2H8(sv->cum_sess), U2H9(sv->cum_lbconn));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001191
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001192 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001193 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001194 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001195 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001196 "<td align=right></td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001197 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001198 "<td align=right></td><td align=right>%s</td><td align=right>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001199 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001200 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001201 "",
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001202 U2H0(sv->bytes_in), U2H1(sv->bytes_out),
1203 U2H2(sv->failed_secu),
1204 U2H3(sv->failed_conns), U2H4(sv->failed_resp),
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001205 sv->retries, sv->redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001206
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001207 /* status, lest check */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001208 chunk_printf(&msg, "<td nowrap>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001209
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001210 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001211 chunk_printf(&msg, "%s ",
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001212 human_time(now.tv_sec - sv->last_change, 1));
1213
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001214 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001215 srv_hlt_st[sv_state],
1216 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
1217 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
1218
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001219 chunk_printf(&msg, "</td><td title=\"%s\" nowrap> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001220 get_check_status_description(sv->check_status),
1221 tv_iszero(&sv->check_start)?"":"* ",
1222 get_check_status_info(sv->check_status));
1223
1224 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001225 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001226
1227 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001228 chunk_printf(&msg, " in %lums", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001229 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001230 chunk_printf(&msg, "</td><td>");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001231 }
Willy Tarreau91861262007-10-17 17:06:05 +02001232
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001233 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001234 /* weight */
1235 "</td><td>%d</td>"
1236 /* act, bck */
1237 "<td>%s</td><td>%s</td>"
1238 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001239 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02001240 (sv->state & SRV_BACKUP) ? "-" : "Y",
1241 (sv->state & SRV_BACKUP) ? "Y" : "-");
1242
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001243 /* check failures: unique, fatal, down time */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001244 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001245 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02001246 "<td align=right>%lld</td><td align=right>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001247 "<td nowrap align=right>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001248 "",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001249 svs->failed_checks, svs->down_trans,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001250 human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001251 else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001252 chunk_printf(&msg,
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001253 "<td nowrap colspan=3>via %s/%s</td>", svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001254 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001255 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001256 "<td colspan=3></td>");
1257
1258 /* throttle */
1259 if ((sv->state & SRV_WARMINGUP) &&
1260 now.tv_sec < sv->last_change + sv->slowstart &&
1261 now.tv_sec >= sv->last_change) {
1262 unsigned int ratio;
1263 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001264 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001265 "<td>%d %%</td></tr>\n", ratio);
1266 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001267 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001268 "<td>-</td></tr>\n");
1269 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02001270 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001271 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
1272 "UP %d/%d,", "UP,",
1273 "NOLB %d/%d,", "NOLB,",
1274 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001275 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001276 /* pxid, name */
1277 "%s,%s,"
1278 /* queue : current, max */
1279 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001280 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001281 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001282 /* bytes : in, out */
1283 "%lld,%lld,"
1284 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001285 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001286 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001287 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001288 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001289 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001290 "",
1291 px->id, sv->id,
1292 sv->nbpend, sv->nbpend_max,
Willy Tarreaudcd47712007-11-04 23:35:08 +01001293 sv->cur_sess, sv->cur_sess_max, LIM2A0(sv->maxconn, ""), sv->cum_sess,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001294 sv->bytes_in, sv->bytes_out,
1295 sv->failed_secu,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001296 sv->failed_conns, sv->failed_resp,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001297 sv->retries, sv->redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001298
Willy Tarreau55bb8452007-10-17 18:44:57 +02001299 /* status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001300 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001301 srv_hlt_st[sv_state],
1302 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
1303 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
Willy Tarreau91861262007-10-17 17:06:05 +02001304
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001305 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001306 /* weight, active, backup */
1307 "%d,%d,%d,"
1308 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001309 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001310 (sv->state & SRV_BACKUP) ? 0 : 1,
1311 (sv->state & SRV_BACKUP) ? 1 : 0);
1312
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001313 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001314 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001315 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02001316 "%lld,%lld,%d,%d,",
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001317 sv->failed_checks, sv->down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001318 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02001319 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001320 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001321 ",,,,");
1322
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001323 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001324 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01001325 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001326 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01001327 LIM2A0(sv->maxqueue, ""),
1328 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001329
1330 /* throttle */
1331 if ((sv->state & SRV_WARMINGUP) &&
1332 now.tv_sec < sv->last_change + sv->slowstart &&
1333 now.tv_sec >= sv->last_change) {
1334 unsigned int ratio;
1335 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001336 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001337 }
1338
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001339 /* sessions: lbtot */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001340 chunk_printf(&msg, ",%lld,", sv->cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001341
1342 /* tracked */
1343 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001344 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001345 sv->tracked->proxy->id, sv->tracked->id);
1346 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001347 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001348
Willy Tarreau7f062c42009-03-05 18:43:00 +01001349 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001350 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01001351
1352 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001353 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001354 read_freq_ctr(&sv->sess_per_sec),
1355 sv->sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01001356
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001357 if (sv->state & SRV_CHECKED) {
1358 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001359 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001360
1361 /* check_code */
1362 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001363 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001364 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001365 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001366
1367 /* check_duration */
1368 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001369 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001370 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001371 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001372
1373 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001374 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001375 }
1376
Willy Tarreau7f062c42009-03-05 18:43:00 +01001377 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001378 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001379 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001380 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001381 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001382 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02001383
1384 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
1385 /* fall through */
1386
1387 case DATA_ST_PX_BE:
1388 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001389 if ((px->cap & PR_CAP_BE) &&
1390 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
1391 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001392 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001393 /* name */
1394 "<tr align=center class=\"backend\"><td>Backend</td>"
1395 /* queue : current, max */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001396 "<td align=right>%s</td><td align=right>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001397 /* sessions rate : current, max, limit */
1398 "<td align=right>%s</td><td align=right>%s</td><td align=right></td>"
1399 "",
1400 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->nbpend_max),
1401 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_sps_max));
1402
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001403 chunk_printf(&msg,
Willy Tarreaua3e49422009-05-10 19:19:41 +02001404 /* sessions : current, max, limit, total, lbtot */
1405 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001406 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001407 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001408 "<td align=right>%s</td><td align=right>%s</td>"
1409 "",
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001410 U2H2(px->beconn), U2H3(px->beconn_max), U2H4(px->fullconn),
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001411 U2H6(px->cum_beconn), U2H7(px->cum_lbconn),
1412 U2H8(px->bytes_in), U2H9(px->bytes_out));
1413
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001414 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001415 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001416 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001417 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001418 "<td align=right></td><td align=right>%s</td><td align=right>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001419 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001420 "<td align=right>%lld</td><td align=right>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001421 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02001422 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001423 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02001424 * active and backups. */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001425 "<td align=center nowrap>%s %s</td><td align=center>&nbsp;</td><td align=center>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001426 "<td align=center>%d</td><td align=center>%d</td>"
1427 "",
1428 U2H0(px->denied_req), U2H1(px->denied_resp),
1429 U2H2(px->failed_conns), U2H3(px->failed_resp),
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001430 px->retries, px->redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001431 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01001432 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
1433 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01001434 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001435 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001436
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001437 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001438 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001439 "<td align=center>&nbsp;</td><td align=\"right\">%d</td>"
1440 "<td align=\"right\" nowrap>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001441 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001442 "</tr>",
1443 px->down_trans,
1444 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001445 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001446 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001447 /* pxid, name */
1448 "%s,BACKEND,"
1449 /* queue : current, max */
1450 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001451 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001452 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001453 /* bytes : in, out */
1454 "%lld,%lld,"
1455 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001456 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001457 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001458 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001459 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001460 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001461 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02001462 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001463 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02001464 * active and backups. */
1465 "%s,"
1466 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001467 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001468 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001469 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001470 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001471 /* rate, rate_lim, rate_max, */
1472 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001473 /* check_status, check_code, check_duration */
1474 ",,,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001475 "\n",
1476 px->id,
1477 px->nbpend /* or px->totpend ? */, px->nbpend_max,
1478 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
1479 px->bytes_in, px->bytes_out,
1480 px->denied_req, px->denied_resp,
1481 px->failed_conns, px->failed_resp,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001482 px->retries, px->redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01001483 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01001484 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001485 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001486 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01001487 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001488 relative_pid, px->uuid,
Willy Tarreau7f062c42009-03-05 18:43:00 +01001489 px->cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001490 read_freq_ctr(&px->be_sess_per_sec),
1491 px->be_sps_max);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001492 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001493 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001494 return 0;
1495 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001496
Willy Tarreau91861262007-10-17 17:06:05 +02001497 s->data_ctx.stats.px_st = DATA_ST_PX_END;
1498 /* fall through */
1499
1500 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001501 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001502 chunk_printf(&msg, "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001503
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001504 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001505 return 0;
1506 }
Willy Tarreau91861262007-10-17 17:06:05 +02001507
1508 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
1509 /* fall through */
1510
1511 case DATA_ST_PX_FIN:
1512 return 1;
1513
1514 default:
1515 /* unknown state, we should put an abort() here ! */
1516 return 1;
1517 }
1518}
1519
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001520
1521/* This function is called to send output to the response buffer.
1522 * It dumps the sessions states onto the output buffer <rep>.
1523 * Expects to be called with client socket shut down on input.
1524 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001525 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001526 */
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001527int stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001528{
1529 struct chunk msg;
1530
1531 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
1532 /* If we're forced to shut down, we might have to remove our
1533 * reference to the last session being dumped.
1534 */
1535 if (s->data_state == DATA_ST_LIST) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001536 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001537 LIST_DEL(&s->data_ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001538 LIST_INIT(&s->data_ctx.sess.bref.users);
1539 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001540 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001541 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001542 }
1543
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001544 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001545
1546 switch (s->data_state) {
1547 case DATA_ST_INIT:
1548 /* the function had not been called yet, let's prepare the
1549 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001550 * pointer to the first in the global list. When a target
1551 * session is being destroyed, it is responsible for updating
1552 * this pointer. We know we have reached the end when this
1553 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001554 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001555 LIST_INIT(&s->data_ctx.sess.bref.users);
1556 s->data_ctx.sess.bref.ref = sessions.n;
1557 s->data_state = DATA_ST_LIST;
1558 /* fall through */
1559
1560 case DATA_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001561 /* first, let's detach the back-ref from a possible previous session */
1562 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
1563 LIST_DEL(&s->data_ctx.sess.bref.users);
1564 LIST_INIT(&s->data_ctx.sess.bref.users);
1565 }
1566
1567 /* and start from where we stopped */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001568 while (s->data_ctx.sess.bref.ref != &sessions) {
1569 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1570 struct session *curr_sess;
1571
1572 curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list);
1573
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001574 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001575 "%p: proto=%s",
1576 curr_sess,
1577 curr_sess->listener->proto->name);
1578
1579 switch (curr_sess->listener->proto->sock_family) {
1580 case AF_INET:
1581 inet_ntop(AF_INET,
1582 (const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr,
1583 pn, sizeof(pn));
1584
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001585 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001586 " src=%s:%d fe=%s be=%s srv=%s",
1587 pn,
1588 ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port),
1589 curr_sess->fe->id,
1590 curr_sess->be->id,
1591 curr_sess->srv ? curr_sess->srv->id : "<none>"
1592 );
1593 break;
1594 case AF_INET6:
1595 inet_ntop(AF_INET6,
1596 (const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr,
1597 pn, sizeof(pn));
1598
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001599 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001600 " src=%s:%d fe=%s be=%s srv=%s",
1601 pn,
1602 ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port),
1603 curr_sess->fe->id,
1604 curr_sess->be->id,
1605 curr_sess->srv ? curr_sess->srv->id : "<none>"
1606 );
1607
1608 break;
1609 case AF_UNIX:
1610 /* no more information to print right now */
1611 break;
1612 }
1613
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001614 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001615 " as=%d ts=%02x age=%s calls=%d",
Willy Tarreau8a5c6262008-12-08 00:16:21 +01001616 curr_sess->ana_state, curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01001617 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
1618 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001619
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001620 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001621 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
1622 curr_sess->req->flags,
1623 curr_sess->req->l,
1624 curr_sess->req->analysers,
1625 curr_sess->req->rex ?
1626 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
1627 TICKS_TO_MS(1000)) : "");
1628
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001629 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001630 ",wx=%s",
1631 curr_sess->req->wex ?
1632 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
1633 TICKS_TO_MS(1000)) : "");
1634
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001635 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001636 ",ax=%s]",
1637 curr_sess->req->analyse_exp ?
1638 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
1639 TICKS_TO_MS(1000)) : "");
1640
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001641 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001642 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
1643 curr_sess->rep->flags,
1644 curr_sess->rep->l,
1645 curr_sess->rep->analysers,
1646 curr_sess->rep->rex ?
1647 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
1648 TICKS_TO_MS(1000)) : "");
1649
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001650 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001651 ",wx=%s",
1652 curr_sess->rep->wex ?
1653 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
1654 TICKS_TO_MS(1000)) : "");
1655
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001656 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001657 ",ax=%s]",
1658 curr_sess->rep->analyse_exp ?
1659 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
1660 TICKS_TO_MS(1000)) : "");
1661
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001662 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001663 " s0=[%d,%1xh,fd=%d,ex=%s]",
1664 curr_sess->si[0].state,
1665 curr_sess->si[0].flags,
1666 curr_sess->si[0].fd,
1667 curr_sess->si[0].exp ?
1668 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
1669 TICKS_TO_MS(1000)) : "");
1670
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001671 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001672 " s1=[%d,%1xh,fd=%d,ex=%s]",
1673 curr_sess->si[1].state,
1674 curr_sess->si[1].flags,
1675 curr_sess->si[1].fd,
1676 curr_sess->si[1].exp ?
1677 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
1678 TICKS_TO_MS(1000)) : "");
1679
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001680 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001681 " exp=%s",
1682 curr_sess->task->expire ?
1683 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
1684 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01001685 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001686 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001687
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001688 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001689
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001690 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001691 /* let's try again later from this session. We add ourselves into
1692 * this session's users so that it can remove us upon termination.
1693 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001694 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001695 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001696 }
1697
1698 s->data_ctx.sess.bref.ref = curr_sess->list.n;
1699 }
1700 s->data_state = DATA_ST_FIN;
1701 /* fall through */
1702
1703 default:
1704 s->data_state = DATA_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001705 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001706 }
1707}
1708
Willy Tarreau74808cb2009-03-04 15:53:18 +01001709/* print a line of error buffer (limited to 70 bytes) to <out>. The format is :
1710 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
1711 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
1712 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
1713 * lines are respected within the limit of 70 output chars. Lines that are
1714 * continuation of a previous truncated line begin with "+" instead of " "
1715 * after the offset. The new pointer is returned.
1716 */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001717static int dump_error_line(struct chunk *out, struct error_snapshot *err,
1718 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01001719{
1720 int end;
1721 unsigned char c;
1722
1723 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001724 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01001725 return ptr;
1726
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001727 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01001728
Willy Tarreau61b34732009-10-03 23:49:35 +02001729 while (ptr < err->len && ptr < sizeof(err->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001730 c = err->buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01001731 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001732 if (out->len > end - 2)
1733 break;
1734 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01001735 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001736 if (out->len > end - 3)
1737 break;
1738 out->str[out->len++] = '\\';
1739 switch (c) {
1740 case '\t': c = 't'; break;
1741 case '\n': c = 'n'; break;
1742 case '\r': c = 'r'; break;
1743 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01001744 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001745 }
1746 out->str[out->len++] = c;
1747 } else {
1748 if (out->len > end - 5)
1749 break;
1750 out->str[out->len++] = '\\';
1751 out->str[out->len++] = 'x';
1752 out->str[out->len++] = hextab[(c >> 4) & 0xF];
1753 out->str[out->len++] = hextab[c & 0xF];
1754 }
1755 if (err->buf[ptr++] == '\n') {
1756 /* we had a line break, let's return now */
1757 out->str[out->len++] = '\n';
1758 *line = ptr;
1759 return ptr;
1760 }
1761 }
1762 /* we have an incomplete line, we return it as-is */
1763 out->str[out->len++] = '\n';
1764 return ptr;
1765}
1766
1767/* This function is called to send output to the response buffer.
1768 * It dumps the errors logged in proxies onto the output buffer <rep>.
1769 * Expects to be called with client socket shut down on input.
1770 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau61b34732009-10-03 23:49:35 +02001771 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau74808cb2009-03-04 15:53:18 +01001772 */
Willy Tarreau61b34732009-10-03 23:49:35 +02001773int stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau74808cb2009-03-04 15:53:18 +01001774{
1775 extern const char *monthname[12];
1776 struct chunk msg;
1777
Willy Tarreau61b34732009-10-03 23:49:35 +02001778 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW)))
1779 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001780
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001781 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01001782
1783 if (!s->data_ctx.errors.px) {
1784 /* the function had not been called yet, let's prepare the
1785 * buffer for a response.
1786 */
Willy Tarreau74808cb2009-03-04 15:53:18 +01001787 s->data_ctx.errors.px = proxy;
1788 s->data_ctx.errors.buf = 0;
1789 s->data_ctx.errors.bol = 0;
1790 s->data_ctx.errors.ptr = -1;
1791 }
1792
1793 /* we have two inner loops here, one for the proxy, the other one for
1794 * the buffer.
1795 */
1796 while (s->data_ctx.errors.px) {
1797 struct error_snapshot *es;
1798
1799 if (s->data_ctx.errors.buf == 0)
1800 es = &s->data_ctx.errors.px->invalid_req;
1801 else
1802 es = &s->data_ctx.errors.px->invalid_rep;
1803
1804 if (!es->when.tv_sec)
1805 goto next;
1806
1807 if (s->data_ctx.errors.iid >= 0 &&
1808 s->data_ctx.errors.px->uuid != s->data_ctx.errors.iid &&
1809 es->oe->uuid != s->data_ctx.errors.iid)
1810 goto next;
1811
1812 if (s->data_ctx.errors.ptr < 0) {
1813 /* just print headers now */
1814
1815 char pn[INET6_ADDRSTRLEN];
1816 struct tm tm;
1817
1818 get_localtime(es->when.tv_sec, &tm);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001819 chunk_printf(&msg, "\n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01001820 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001821 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01001822
1823
1824 if (es->src.ss_family == AF_INET)
1825 inet_ntop(AF_INET,
1826 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
1827 pn, sizeof(pn));
1828 else
1829 inet_ntop(AF_INET6,
1830 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
1831 pn, sizeof(pn));
1832
1833 switch (s->data_ctx.errors.buf) {
1834 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001835 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01001836 " frontend %s (#%d): invalid request\n"
1837 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
1838 " request length %d bytes, error at position %d:\n\n",
1839 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
1840 pn, es->sid, es->oe->id, es->oe->uuid,
1841 es->srv ? es->srv->id : "<NONE>",
1842 es->srv ? es->srv->puid : -1,
1843 es->len, es->pos);
1844 break;
1845 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001846 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01001847 " backend %s (#%d) : invalid response\n"
1848 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
1849 " response length %d bytes, error at position %d:\n\n",
1850 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
1851 pn, es->sid, es->oe->id, es->oe->uuid,
1852 es->srv ? es->srv->id : "<NONE>",
1853 es->srv ? es->srv->puid : -1,
1854 es->len, es->pos);
1855 break;
1856 }
1857
Willy Tarreau61b34732009-10-03 23:49:35 +02001858 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001859 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02001860 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001861 }
1862 s->data_ctx.errors.ptr = 0;
1863 s->data_ctx.errors.sid = es->sid;
1864 }
1865
1866 if (s->data_ctx.errors.sid != es->sid) {
1867 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001868 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01001869 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau61b34732009-10-03 23:49:35 +02001870 if (buffer_feed_chunk(rep, &msg) >= 0)
1871 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001872 goto next;
1873 }
1874
1875 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau61b34732009-10-03 23:49:35 +02001876 while (s->data_ctx.errors.ptr < es->len && s->data_ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001877 int newptr;
1878 int newline;
1879
1880 newline = s->data_ctx.errors.bol;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001881 newptr = dump_error_line(&msg, es, &newline, s->data_ctx.errors.ptr);
Willy Tarreau74808cb2009-03-04 15:53:18 +01001882 if (newptr == s->data_ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02001883 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001884
Willy Tarreau61b34732009-10-03 23:49:35 +02001885 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001886 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02001887 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001888 }
1889 s->data_ctx.errors.ptr = newptr;
1890 s->data_ctx.errors.bol = newline;
1891 };
1892 next:
1893 s->data_ctx.errors.bol = 0;
1894 s->data_ctx.errors.ptr = -1;
1895 s->data_ctx.errors.buf++;
1896 if (s->data_ctx.errors.buf > 1) {
1897 s->data_ctx.errors.buf = 0;
1898 s->data_ctx.errors.px = s->data_ctx.errors.px->next;
1899 }
1900 }
1901
1902 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02001903 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001904}
1905
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001906
Willy Tarreau10522fd2008-07-09 20:12:41 +02001907static struct cfg_kw_list cfg_kws = {{ },{
1908 { CFG_GLOBAL, "stats", stats_parse_global },
1909 { 0, NULL, NULL },
1910}};
1911
1912__attribute__((constructor))
1913static void __dumpstats_module_init(void)
1914{
1915 cfg_register_keywords(&cfg_kws);
1916}
1917
Willy Tarreau91861262007-10-17 17:06:05 +02001918/*
1919 * Local variables:
1920 * c-indent-level: 8
1921 * c-basic-offset: 8
1922 * End:
1923 */