blob: e8eef21f61f23e3eac3d54bf747d11df45e26c48 [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
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200235 * called from an applet running in a stream interface. The function returns 1
236 * if the request was understood, otherwise zero. It sets si->st0 to a value
237 * designating the function which will have to process the request.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200238 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200239int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200240{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200241 struct session *s = si->private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200242 char *args[MAX_STATS_ARGS + 1];
243 int arg;
244
245 while (isspace((unsigned char)*line))
246 line++;
247
248 arg = 0;
249 args[arg] = line;
250
251 while (*line && arg < MAX_STATS_ARGS) {
252 if (isspace((unsigned char)*line)) {
253 *line++ = '\0';
254
255 while (isspace((unsigned char)*line))
256 line++;
257
258 args[++arg] = line;
259 continue;
260 }
261
262 line++;
263 }
264
265 while (++arg <= MAX_STATS_ARGS)
266 args[arg] = line;
267
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200268 s->data_ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200269 if (strcmp(args[0], "show") == 0) {
270 if (strcmp(args[1], "stat") == 0) {
271 if (*args[2] && *args[3] && *args[4]) {
272 s->data_ctx.stats.flags |= STAT_BOUND;
273 s->data_ctx.stats.iid = atoi(args[2]);
274 s->data_ctx.stats.type = atoi(args[3]);
275 s->data_ctx.stats.sid = atoi(args[4]);
276 }
277
278 s->data_ctx.stats.flags |= STAT_SHOW_STAT;
279 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200280 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200281 si->st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200282 }
283 else if (strcmp(args[1], "info") == 0) {
284 s->data_ctx.stats.flags |= STAT_SHOW_INFO;
285 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200286 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200287 si->st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200288 }
289 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200290 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200291 si->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200292 }
293 else if (strcmp(args[1], "errors") == 0) {
294 if (*args[2])
295 s->data_ctx.errors.iid = atoi(args[2]);
296 else
297 s->data_ctx.errors.iid = -1;
298 s->data_ctx.errors.px = NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200299 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200300 si->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200301 }
302 else { /* neither "stat" nor "info" nor "sess" */
303 return 0;
304 }
305 }
306 else { /* not "show" */
307 return 0;
308 }
309 return 1;
310}
311
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200312/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200313 * used to processes I/O from/to the stats unix socket. The system relies on a
314 * state machine handling requests and various responses. We read a request,
315 * then we process it and send the response, and we possibly display a prompt.
316 * Then we can read again. The state is stored in si->st0 and is one of the
317 * STAT_CLI_* constants. si->st1 is used to indicate whether prompt is enabled
318 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200319 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200320void stats_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200321{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200322 struct session *s = si->private;
323 struct buffer *req = si->ob;
324 struct buffer *res = si->ib;
325 int reql;
326 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200327
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200328 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
329 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200330
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200331 while (1) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200332 if (si->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200333 /* Stats output not initialized yet */
334 memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats));
335 s->data_source = DATA_SRC_STATS;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200336 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200337 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200338 else if (si->st0 == STAT_CLI_END) {
339 /* Let's close for real now. We just close the request
340 * side, the conditions below will complete if needed.
341 */
342 si->shutw(si);
343 break;
344 }
345 else if (si->st0 == STAT_CLI_GETREQ) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200346 reql = buffer_si_peekline(si->ob, trash, sizeof(trash));
347 if (reql <= 0) { /* closed or EOL not found */
348 if (reql == 0)
349 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200350 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200351 continue;
352 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200353
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200354 /* seek for a possible semi-colon. If we find one, we
355 * replace it with an LF and skip only this part.
356 */
357 for (len = 0; len < reql; len++)
358 if (trash[len] == ';') {
359 trash[len] = '\n';
360 reql = len + 1;
361 break;
362 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200363
Willy Tarreau816fc222009-10-04 07:36:58 +0200364 /* now it is time to check that we have a full line,
365 * remove the trailing \n and possibly \r, then cut the
366 * line.
367 */
368 len = reql - 1;
369 if (trash[len] != '\n') {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200370 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200371 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200372 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200373
Willy Tarreau816fc222009-10-04 07:36:58 +0200374 if (len && trash[len-1] == '\r')
375 len--;
376
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200377 trash[len] = '\0';
378
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200379 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200380 if (len) {
381 if (strcmp(trash, "quit") == 0) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200382 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200383 continue;
384 }
385 else if (strcmp(trash, "prompt") == 0)
386 si->st1 = !si->st1;
387 else if (strcmp(trash, "help") == 0 ||
388 !stats_sock_parse_request(si, trash))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200389 si->st0 = STAT_CLI_O_HELP;
390 /* NB: stats_sock_parse_request() may have put
391 * another STAT_CLI_O_* into si->st0.
392 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200393 }
394 else if (!si->st1) {
395 /* if prompt is disabled, print help on empty lines,
396 * so that the user at least knows how to enable
397 * prompt and find help.
398 */
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200399 si->st0 = STAT_CLI_O_HELP;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200400 }
401
402 /* re-adjust req buffer */
403 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200404 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200405 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200406 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200407 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200408 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200409 continue;
410 }
411
412 switch (si->st0) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200413 case STAT_CLI_O_HELP:
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200414 if (buffer_feed(si->ib, stats_sock_usage.str, stats_sock_usage.len) < 0)
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200415 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200416 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200417 case STAT_CLI_O_INFO:
Willy Tarreau24955a12009-10-04 11:54:04 +0200418 if (stats_dump_raw_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200419 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200420 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200421 case STAT_CLI_O_SESS:
Willy Tarreau7e72a8f2009-10-03 23:55:14 +0200422 if (stats_dump_sess_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200423 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200424 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200425 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau61b34732009-10-03 23:49:35 +0200426 if (stats_dump_errors_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200427 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200428 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200429 default: /* abnormal state */
430 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200431 break;
432 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200433
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200434 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
435 if (si->st0 == STAT_CLI_PROMPT) {
436 if (buffer_feed(si->ib, si->st1 ? "\n> " : "\n", si->st1 ? 3 : 1) < 0)
437 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200438 }
439
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200440 /* If the output functions are still there, it means they require more room. */
441 if (si->st0 >= STAT_CLI_OUTPUT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200442 si->flags |= SI_FL_WAIT_ROOM;
443 break;
444 }
445
446 /* Now we close the output if one of the writers did so,
447 * or if we're not in interactive mode and the request
448 * buffer is empty. This still allows pipelined requests
449 * to be sent in non-interactive mode.
450 */
451 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->st1 && !req->send_max)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200452 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200453 continue;
454 }
455
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200456 /* switch state back to GETREQ to read next requests */
457 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200458 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200459 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200460
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200461 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200462 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
463 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
464 /* Other size has closed, let's abort if we have no more processing to do
465 * and nothing more to consume. This is comparable to a broken pipe, so
466 * we forward the close to the request side so that it flows upstream to
467 * the client.
468 */
469 si->shutw(si);
470 }
471
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200472 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200473 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
474 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
475 /* We have no more processing to do, and nothing more to send, and
476 * the client side has closed. So we'll forward this state downstream
477 * on the response buffer.
478 */
479 si->shutr(si);
480 res->flags |= BF_READ_NULL;
481 }
482
483 /* update all other flags and resync with the other side */
484 si->update(si);
485
486 /* we don't want to expire timeouts while we're processing requests */
487 si->ib->rex = TICK_ETERNITY;
488 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200489
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200490 out:
491 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
492 __FUNCTION__, __LINE__,
493 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
494
495 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
496 /* check that we have released everything then unregister */
497 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200498 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200499}
500
Willy Tarreau24955a12009-10-04 11:54:04 +0200501/* This function is called to send output to the response buffer.
502 * It dumps statistics onto the output buffer <rep> owned by session <s>.
503 * s->data_ctx must have been zeroed first, and the flags properly set.
504 * It returns 0 as long as it does not complete, non-zero upon completion.
505 * Some states are not used but it makes the code more similar to other
506 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200507 */
Willy Tarreau24955a12009-10-04 11:54:04 +0200508int stats_dump_raw_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3e76e722007-10-17 18:57:38 +0200509{
Willy Tarreau3e76e722007-10-17 18:57:38 +0200510 struct proxy *px;
511 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +0100512 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200513
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200514 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +0200515
516 switch (s->data_state) {
517 case DATA_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +0200518 /* the function had not been called yet */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200519 s->data_state = DATA_ST_HEAD;
520 /* fall through */
521
522 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100523 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200524 print_csv_header(&msg);
Willy Tarreau24955a12009-10-04 11:54:04 +0200525 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100526 return 0;
527 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200528
529 s->data_state = DATA_ST_INFO;
530 /* fall through */
531
532 case DATA_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +0100533 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100534 if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200535 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +0100536 "Name: " PRODUCT_NAME "\n"
537 "Version: " HAPROXY_VERSION "\n"
538 "Release_date: " HAPROXY_DATE "\n"
539 "Nbproc: %d\n"
540 "Process_num: %d\n"
541 "Pid: %d\n"
542 "Uptime: %dd %dh%02dm%02ds\n"
543 "Uptime_sec: %d\n"
544 "Memmax_MB: %d\n"
545 "Ulimit-n: %d\n"
546 "Maxsock: %d\n"
547 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100548 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100549 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100550 "PipesUsed: %d\n"
551 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100552 "Tasks: %d\n"
553 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200554 "node: %s\n"
555 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100556 "",
557 global.nbproc,
558 relative_pid,
559 pid,
560 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
561 up,
562 global.rlimit_memmax,
563 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100564 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100565 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200566 nb_tasks_cur, run_queue_cur,
567 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +0100568 );
Willy Tarreau24955a12009-10-04 11:54:04 +0200569 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100570 return 0;
571 }
572
Willy Tarreau3e76e722007-10-17 18:57:38 +0200573 s->data_ctx.stats.px = proxy;
574 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100575
576 s->data_ctx.stats.sv = NULL;
577 s->data_ctx.stats.sv_st = 0;
578
Willy Tarreau3e76e722007-10-17 18:57:38 +0200579 s->data_state = DATA_ST_LIST;
580 /* fall through */
581
582 case DATA_ST_LIST:
583 /* dump proxies */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100584 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100585 while (s->data_ctx.stats.px) {
586 px = s->data_ctx.stats.px;
587 /* skip the disabled proxies and non-networked ones */
588 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +0200589 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100590 if (stats_dump_proxy(s, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100591 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +0200592 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200593
Willy Tarreaua8efd362008-01-03 10:19:15 +0100594 s->data_ctx.stats.px = px->next;
595 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
596 }
597 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200598 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200599
600 s->data_state = DATA_ST_END;
601 /* fall through */
602
603 case DATA_ST_END:
604 s->data_state = DATA_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +0100605 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200606
607 case DATA_ST_FIN:
608 return 1;
609
610 default:
611 /* unknown state ! */
Willy Tarreau24955a12009-10-04 11:54:04 +0200612 s->data_state = DATA_ST_FIN;
613 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200614 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100615}
616
617
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200618/* This I/O handler runs as an applet embedded in a stream interface. It is
619 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
620 * si->st0 becomes non-zero once the transfer is finished. The handler
621 * automatically unregisters itself once transfer is complete.
622 */
623void http_stats_io_handler(struct stream_interface *si)
624{
625 struct session *s = si->private;
626 struct buffer *req = si->ob;
627 struct buffer *res = si->ib;
628
629 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
630 goto out;
631
632 /* check that the output is not closed */
633 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
634 si->st0 = 1;
635
636 if (!si->st0) {
637 if (stats_dump_http(s, res, s->be->uri_auth)) {
638 si->st0 = 1;
639 si->shutw(si);
640 } else {
641 /* buffer full */
642 si->flags |= SI_FL_WAIT_ROOM;
643 }
644 }
645
646 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
647 si->shutw(si);
648
649 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->st0) {
650 si->shutr(si);
651 res->flags |= BF_READ_NULL;
652 }
653
654 /* update all other flags and resync with the other side */
655 si->update(si);
656
657 /* we don't want to expire timeouts while we're processing requests */
658 si->ib->rex = TICK_ETERNITY;
659 si->ob->wex = TICK_ETERNITY;
660
661 out:
662 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
663 /* check that we have released everything then unregister */
664 stream_int_unregister_handler(si);
665 }
666}
667
668
Willy Tarreau3e76e722007-10-17 18:57:38 +0200669/*
670 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200671 * client socket shut down on input. It stops by itself by unsetting the
Willy Tarreau72b179a2008-08-28 16:01:32 +0200672 * BF_HIJACK flag from the buffer, which it uses to keep on being called
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200673 * when there is free space in the buffer, of simply by letting an empty buffer
674 * upon return.s->data_ctx must have been zeroed before the first call, and the
675 * flags set. It returns 0 if it had to stop writing data and an I/O is needed,
676 * 1 if the dump is finished and the session must be closed, or -1 in case of
677 * any error.
Willy Tarreau91861262007-10-17 17:06:05 +0200678 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100679int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200680{
Willy Tarreau91861262007-10-17 17:06:05 +0200681 struct proxy *px;
682 struct chunk msg;
683 unsigned int up;
684
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200685 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +0200686
687 switch (s->data_state) {
688 case DATA_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200689 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200690 "HTTP/1.0 200 OK\r\n"
691 "Cache-Control: no-cache\r\n"
692 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200693 "Content-Type: %s\r\n",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100694 (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +0200695
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100696 if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200697 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +0200698 uri->refresh);
699
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200700 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +0200701
702 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200703 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +0200704 return 0;
705
Willy Tarreau91861262007-10-17 17:06:05 +0200706 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
707 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
708 if (!(s->flags & SN_FINST_MASK))
709 s->flags |= SN_FINST_R;
710
711 if (s->txn.meth == HTTP_METH_HEAD) {
712 /* that's all we return in case of HEAD request */
713 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +0200714 return 1;
715 }
716
717 s->data_state = DATA_ST_HEAD; /* let's start producing data */
718 /* fall through */
719
720 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100721 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200722 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200723 chunk_printf(&msg,
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200724 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200725 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
726 "<style type=\"text/css\"><!--\n"
727 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +0200728 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +0200729 " font-size: 12px;"
730 " font-weight: normal;"
731 " color: black;"
732 " background: white;"
733 "}\n"
734 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +0200735 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +0200736 " align: center;"
737 "}\n"
738 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +0200739 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +0200740 " margin-bottom: 0.5em;"
741 "}\n"
742 "h2 {"
743 " font-family: helvetica, arial;"
744 " font-size: x-large;"
745 " font-weight: bold;"
746 " font-style: italic;"
747 " color: #6020a0;"
748 " margin-top: 0em;"
749 " margin-bottom: 0em;"
750 "}\n"
751 "h3 {"
752 " font-family: helvetica, arial;"
753 " font-size: 16px;"
754 " font-weight: bold;"
755 " color: #b00040;"
756 " background: #e8e8d0;"
757 " margin-top: 0em;"
758 " margin-bottom: 0em;"
759 "}\n"
760 "li {"
761 " margin-top: 0.25em;"
762 " margin-right: 2em;"
763 "}\n"
764 ".hr {margin-top: 0.25em;"
765 " border-color: black;"
766 " border-bottom-style: solid;"
767 "}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200768 ".titre {background: #20D0D0;color: #000000; font-weight: bold;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200769 ".total {background: #20D0D0;color: #ffff80;}\n"
770 ".frontend {background: #e8e8d0;}\n"
771 ".backend {background: #e8e8d0;}\n"
772 ".active0 {background: #ff9090;}\n"
773 ".active1 {background: #ffd020;}\n"
774 ".active2 {background: #ffffa0;}\n"
775 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100776 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
777 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
778 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200779 ".backup0 {background: #ff9090;}\n"
780 ".backup1 {background: #ff80ff;}\n"
781 ".backup2 {background: #c060ff;}\n"
782 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100783 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
784 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
785 ".backup6 {background: #e0e0e0;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +0200786 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Willy Tarreau91861262007-10-17 17:06:05 +0200787 "table.tbl { border-collapse: collapse; border-style: none;}\n"
788 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray;}\n"
789 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzki619caca2009-10-03 15:46:08 +0200790 "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 +0200791 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200792 "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 +0200793 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
794 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
795 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
796 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200797 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200798 (uri->flags&ST_SHNODE) ? " on " : "",
799 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200800 );
Willy Tarreau55bb8452007-10-17 18:44:57 +0200801 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200802 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200803 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200804 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200805 return 0;
806
807 s->data_state = DATA_ST_INFO;
808 /* fall through */
809
810 case DATA_ST_INFO:
811 up = (now.tv_sec - start_date.tv_sec);
812
813 /* WARNING! this has to fit the first packet too.
814 * We are around 3.5 kB, add adding entries will
815 * become tricky if we want to support 4kB buffers !
816 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100817 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200818 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200819 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
820 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200821 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200822 "<hr width=\"100%%\" class=\"hr\">\n"
823 "<h3>&gt; General process information</h3>\n"
824 "<table border=0 cols=4><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100825 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200826 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200827 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
828 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
829 "current conns = %d; current pipes = %d/%d<br>\n"
830 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200831 "</td><td align=\"center\" nowrap>\n"
832 "<table class=\"lgd\"><tr>\n"
833 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
834 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
835 "</tr><tr>\n"
836 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
837 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
838 "</tr><tr>\n"
839 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
840 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
841 "</tr><tr>\n"
842 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100843 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200844 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100845 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +0200846 "</td>"
847 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
848 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
849 "",
850 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200851 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
852 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
853 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +0200854 up / 86400, (up % 86400) / 3600,
855 (up % 3600) / 60, (up % 60),
856 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
857 global.rlimit_memmax ? " MB" : "",
858 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100859 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100860 actconn, pipes_used, pipes_used+pipes_free,
861 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +0200862 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100863
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100864 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200865 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200866 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
867 uri->uri_prefix,
868 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100869 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200870 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200871 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200872 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
873 uri->uri_prefix,
874 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100875 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200876
Willy Tarreau55bb8452007-10-17 18:44:57 +0200877 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100878 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200879 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200880 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
881 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100882 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200883 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200884 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200885 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200886 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
887 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100888 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200889 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200890 }
Willy Tarreau91861262007-10-17 17:06:05 +0200891
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200892 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200893 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
894 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100895 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
896 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200897
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200898 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +0200899 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
900 uri->uri_prefix,
901 (uri->refresh > 0) ? ";norefresh" : "");
902
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200903 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200904 "</td>"
905 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
906 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
907 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
908 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
909 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
910 "</ul>"
911 "</td>"
912 "</tr></table>\n"
913 ""
914 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100915
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200916 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200917 return 0;
918 }
Willy Tarreau91861262007-10-17 17:06:05 +0200919
Willy Tarreau91861262007-10-17 17:06:05 +0200920 s->data_ctx.stats.px = proxy;
921 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
922 s->data_state = DATA_ST_LIST;
923 /* fall through */
924
925 case DATA_ST_LIST:
926 /* dump proxies */
927 while (s->data_ctx.stats.px) {
928 px = s->data_ctx.stats.px;
929 /* skip the disabled proxies and non-networked ones */
930 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100931 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200932 return 0;
933
934 s->data_ctx.stats.px = px->next;
935 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
936 }
937 /* here, we just have reached the last proxy */
938
939 s->data_state = DATA_ST_END;
940 /* fall through */
941
942 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100943 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200944 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200945 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200946 return 0;
947 }
Willy Tarreau91861262007-10-17 17:06:05 +0200948
949 s->data_state = DATA_ST_FIN;
950 /* fall through */
951
952 case DATA_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +0200953 return 1;
954
955 default:
956 /* unknown state ! */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200957 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +0200958 return -1;
959 }
960}
961
962
963/*
964 * Dumps statistics for a proxy.
965 * Returns 0 if it had to stop dumping data because of lack of buffer space,
966 * ot non-zero if everything completed.
967 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100968int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200969{
970 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100971 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Willy Tarreau91861262007-10-17 17:06:05 +0200972 struct chunk msg;
973
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200974 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +0200975
976 switch (s->data_ctx.stats.px_st) {
977 case DATA_ST_PX_INIT:
978 /* we are on a new proxy */
979
980 if (uri && uri->scope) {
981 /* we have a limited scope, we have to check the proxy name */
982 struct stat_scope *scope;
983 int len;
984
985 len = strlen(px->id);
986 scope = uri->scope;
987
988 while (scope) {
989 /* match exact proxy name */
990 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
991 break;
992
993 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +0200994 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +0200995 break;
996 scope = scope->next;
997 }
998
999 /* proxy name not found : don't dump anything */
1000 if (scope == NULL)
1001 return 1;
1002 }
1003
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001004 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001005 (px->uuid != s->data_ctx.stats.iid))
1006 return 1;
1007
Willy Tarreau91861262007-10-17 17:06:05 +02001008 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
1009 /* fall through */
1010
1011 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001012 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001013 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001014 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001015 "<table class=\"tbl\" width=\"100%%\">\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001016 "<tr align=\"center\" class=\"titre\">"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001017 "<th class=\"pxname\" width=\"10%%\">%s</th>"
1018 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001019 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001020 "</table>\n"
1021 "<table cols=\"29\" class=\"tbl\" width=\"100%%\">\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001022 "<tr align=\"center\" class=\"titre\">"
1023 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001024 "<th colspan=3>Queue</th>"
1025 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001026 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001027 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001028 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001029 "</tr>\n"
1030 "<tr align=\"center\" class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001031 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001032 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001033 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001034 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001035 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001036 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001037 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1038 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001039 "</tr>",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001040 px->id,
1041 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001042
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001043 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001044 return 0;
1045 }
Willy Tarreau91861262007-10-17 17:06:05 +02001046
1047 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
1048 /* fall through */
1049
1050 case DATA_ST_PX_FE:
1051 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001052 if ((px->cap & PR_CAP_FE) &&
1053 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
1054 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001055 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001056 /* name, queue */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001057 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=3></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001058 /* sessions rate : current, max, limit */
1059 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
1060 /* sessions : current, max, limit, total, lbtot */
1061 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001062 "<td align=right>%s</td><td align=right></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001063 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001064 "<td align=right>%s</td><td align=right>%s</td>"
1065 "",
Willy Tarreaua3e49422009-05-10 19:19:41 +02001066 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
1067 U2H1(px->fe_sps_max), LIM2A2(px->fe_sps_lim, "-"),
1068 U2H3(px->feconn), U2H4(px->feconn_max), U2H5(px->maxconn),
1069 U2H6(px->cum_feconn), U2H7(px->bytes_in), U2H8(px->bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001070
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001071 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001072 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001073 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001074 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001075 "<td align=right>%s</td><td align=right></td><td align=right></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001076 /* warnings: retries, redispatches */
1077 "<td align=right></td><td align=right></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001078 /* server status : reflect frontend status */
Willy Tarreau91861262007-10-17 17:06:05 +02001079 "<td align=center>%s</td>"
1080 /* rest of server: nothing */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001081 "<td align=center colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02001082 "",
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001083 U2H0(px->denied_req), U2H1(px->denied_resp),
1084 U2H2(px->failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +02001085 px->state == PR_STRUN ? "OPEN" :
1086 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001087 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001088 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001089 /* pxid, name, queue cur, queue max, */
1090 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001091 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001092 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001093 /* bytes : in, out */
1094 "%lld,%lld,"
1095 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001096 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001097 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001098 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001099 /* warnings: retries, redispatches */
1100 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001101 /* server status : reflect frontend status */
1102 "%s,"
1103 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001104 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001105 /* pid, iid, sid, throttle, lbtot, tracked, type */
1106 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001107 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001108 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001109 /* check_status, check_code, check_duration */
1110 ",,,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001111 "\n",
1112 px->id,
1113 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
1114 px->bytes_in, px->bytes_out,
1115 px->denied_req, px->denied_resp,
1116 px->failed_req,
1117 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +01001118 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01001119 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001120 read_freq_ctr(&px->fe_sess_per_sec),
1121 px->fe_sps_lim, px->fe_sps_max);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001122 }
Willy Tarreau91861262007-10-17 17:06:05 +02001123
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001124 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001125 return 0;
1126 }
1127
1128 s->data_ctx.stats.sv = px->srv; /* may be NULL */
1129 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
1130 /* fall through */
1131
1132 case DATA_ST_PX_SV:
1133 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001134 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
1135
Willy Tarreau2ea81932007-11-30 12:04:38 +01001136 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 +02001137
1138 sv = s->data_ctx.stats.sv;
1139
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001140 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001141 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
1142 break;
1143
1144 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
1145 continue;
1146 }
1147
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001148 if (sv->tracked)
1149 svs = sv->tracked;
1150 else
1151 svs = sv;
1152
Willy Tarreau91861262007-10-17 17:06:05 +02001153 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001154 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01001155 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001156 else if (svs->state & SRV_RUNNING) {
1157 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02001158 sv_state = 3; /* UP */
1159 else
1160 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01001161
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001162 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01001163 sv_state += 2;
1164 }
Willy Tarreau91861262007-10-17 17:06:05 +02001165 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001166 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02001167 sv_state = 1; /* going up */
1168 else
1169 sv_state = 0; /* DOWN */
1170
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001171 if ((sv_state == 0) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02001172 /* do not report servers which are DOWN */
1173 s->data_ctx.stats.sv = sv->next;
1174 continue;
1175 }
1176
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001177 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001178 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
1179 "UP %d/%d &darr;", "UP",
1180 "NOLB %d/%d &darr;", "NOLB",
1181 "<i>no check</i>" };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001182 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001183 /* name */
1184 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001185 /* queue : current, max, limit */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001186 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001187 /* sessions rate : current, max, limit */
1188 "<td align=right>%s</td><td align=right>%s</td><td align=right></td>"
1189 /* sessions : current, max, limit, total, lbtot */
1190 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001191 "<td align=right>%s</td><td align=right>%s</td>"
1192 "",
1193 (sv->state & SRV_BACKUP) ? "backup" : "active",
1194 sv_state, sv->id,
1195 U2H0(sv->nbpend), U2H1(sv->nbpend_max), LIM2A2(sv->maxqueue, "-"),
Willy Tarreaua3e49422009-05-10 19:19:41 +02001196 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->sps_max),
1197 U2H5(sv->cur_sess), U2H6(sv->cur_sess_max), LIM2A7(sv->maxconn, "-"),
1198 U2H8(sv->cum_sess), U2H9(sv->cum_lbconn));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001199
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001200 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001201 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001202 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001203 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001204 "<td align=right></td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001205 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001206 "<td align=right></td><td align=right>%s</td><td align=right>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001207 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001208 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001209 "",
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001210 U2H0(sv->bytes_in), U2H1(sv->bytes_out),
1211 U2H2(sv->failed_secu),
1212 U2H3(sv->failed_conns), U2H4(sv->failed_resp),
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001213 sv->retries, sv->redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001214
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001215 /* status, lest check */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001216 chunk_printf(&msg, "<td nowrap>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001217
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001218 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001219 chunk_printf(&msg, "%s ",
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001220 human_time(now.tv_sec - sv->last_change, 1));
1221
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001222 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001223 srv_hlt_st[sv_state],
1224 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
1225 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
1226
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001227 chunk_printf(&msg, "</td><td title=\"%s\" nowrap> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001228 get_check_status_description(sv->check_status),
1229 tv_iszero(&sv->check_start)?"":"* ",
1230 get_check_status_info(sv->check_status));
1231
1232 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001233 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001234
1235 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001236 chunk_printf(&msg, " in %lums", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001237 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001238 chunk_printf(&msg, "</td><td>");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001239 }
Willy Tarreau91861262007-10-17 17:06:05 +02001240
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001241 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001242 /* weight */
1243 "</td><td>%d</td>"
1244 /* act, bck */
1245 "<td>%s</td><td>%s</td>"
1246 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001247 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02001248 (sv->state & SRV_BACKUP) ? "-" : "Y",
1249 (sv->state & SRV_BACKUP) ? "Y" : "-");
1250
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001251 /* check failures: unique, fatal, down time */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001252 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001253 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02001254 "<td align=right>%lld</td><td align=right>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001255 "<td nowrap align=right>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001256 "",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001257 svs->failed_checks, svs->down_trans,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001258 human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001259 else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001260 chunk_printf(&msg,
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001261 "<td nowrap colspan=3>via %s/%s</td>", svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001262 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001263 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001264 "<td colspan=3></td>");
1265
1266 /* throttle */
1267 if ((sv->state & SRV_WARMINGUP) &&
1268 now.tv_sec < sv->last_change + sv->slowstart &&
1269 now.tv_sec >= sv->last_change) {
1270 unsigned int ratio;
1271 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001272 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001273 "<td>%d %%</td></tr>\n", ratio);
1274 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001275 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001276 "<td>-</td></tr>\n");
1277 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02001278 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001279 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
1280 "UP %d/%d,", "UP,",
1281 "NOLB %d/%d,", "NOLB,",
1282 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001283 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001284 /* pxid, name */
1285 "%s,%s,"
1286 /* queue : current, max */
1287 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001288 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001289 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001290 /* bytes : in, out */
1291 "%lld,%lld,"
1292 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001293 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001294 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001295 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001296 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001297 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001298 "",
1299 px->id, sv->id,
1300 sv->nbpend, sv->nbpend_max,
Willy Tarreaudcd47712007-11-04 23:35:08 +01001301 sv->cur_sess, sv->cur_sess_max, LIM2A0(sv->maxconn, ""), sv->cum_sess,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001302 sv->bytes_in, sv->bytes_out,
1303 sv->failed_secu,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001304 sv->failed_conns, sv->failed_resp,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001305 sv->retries, sv->redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001306
Willy Tarreau55bb8452007-10-17 18:44:57 +02001307 /* status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001308 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001309 srv_hlt_st[sv_state],
1310 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
1311 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
Willy Tarreau91861262007-10-17 17:06:05 +02001312
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001313 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001314 /* weight, active, backup */
1315 "%d,%d,%d,"
1316 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001317 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001318 (sv->state & SRV_BACKUP) ? 0 : 1,
1319 (sv->state & SRV_BACKUP) ? 1 : 0);
1320
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001321 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001322 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001323 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02001324 "%lld,%lld,%d,%d,",
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001325 sv->failed_checks, sv->down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001326 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02001327 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001328 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001329 ",,,,");
1330
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001331 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001332 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01001333 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001334 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01001335 LIM2A0(sv->maxqueue, ""),
1336 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001337
1338 /* throttle */
1339 if ((sv->state & SRV_WARMINGUP) &&
1340 now.tv_sec < sv->last_change + sv->slowstart &&
1341 now.tv_sec >= sv->last_change) {
1342 unsigned int ratio;
1343 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001344 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001345 }
1346
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001347 /* sessions: lbtot */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001348 chunk_printf(&msg, ",%lld,", sv->cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001349
1350 /* tracked */
1351 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001352 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001353 sv->tracked->proxy->id, sv->tracked->id);
1354 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001355 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001356
Willy Tarreau7f062c42009-03-05 18:43:00 +01001357 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001358 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01001359
1360 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001361 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001362 read_freq_ctr(&sv->sess_per_sec),
1363 sv->sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01001364
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001365 if (sv->state & SRV_CHECKED) {
1366 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001367 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001368
1369 /* check_code */
1370 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001371 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001372 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001373 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001374
1375 /* check_duration */
1376 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001377 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001378 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001379 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001380
1381 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001382 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001383 }
1384
Willy Tarreau7f062c42009-03-05 18:43:00 +01001385 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001386 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001387 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001388 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001389 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001390 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02001391
1392 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
1393 /* fall through */
1394
1395 case DATA_ST_PX_BE:
1396 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001397 if ((px->cap & PR_CAP_BE) &&
1398 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
1399 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001400 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001401 /* name */
1402 "<tr align=center class=\"backend\"><td>Backend</td>"
1403 /* queue : current, max */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001404 "<td align=right>%s</td><td align=right>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001405 /* sessions rate : current, max, limit */
1406 "<td align=right>%s</td><td align=right>%s</td><td align=right></td>"
1407 "",
1408 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->nbpend_max),
1409 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_sps_max));
1410
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001411 chunk_printf(&msg,
Willy Tarreaua3e49422009-05-10 19:19:41 +02001412 /* sessions : current, max, limit, total, lbtot */
1413 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001414 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001415 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001416 "<td align=right>%s</td><td align=right>%s</td>"
1417 "",
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001418 U2H2(px->beconn), U2H3(px->beconn_max), U2H4(px->fullconn),
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001419 U2H6(px->cum_beconn), U2H7(px->cum_lbconn),
1420 U2H8(px->bytes_in), U2H9(px->bytes_out));
1421
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001422 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001423 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001424 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001425 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001426 "<td align=right></td><td align=right>%s</td><td align=right>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001427 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001428 "<td align=right>%lld</td><td align=right>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001429 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02001430 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001431 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02001432 * active and backups. */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001433 "<td align=center nowrap>%s %s</td><td align=center>&nbsp;</td><td align=center>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001434 "<td align=center>%d</td><td align=center>%d</td>"
1435 "",
1436 U2H0(px->denied_req), U2H1(px->denied_resp),
1437 U2H2(px->failed_conns), U2H3(px->failed_resp),
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001438 px->retries, px->redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001439 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01001440 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
1441 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01001442 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001443 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001444
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001445 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001446 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001447 "<td align=center>&nbsp;</td><td align=\"right\">%d</td>"
1448 "<td align=\"right\" nowrap>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001449 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001450 "</tr>",
1451 px->down_trans,
1452 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001453 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001454 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001455 /* pxid, name */
1456 "%s,BACKEND,"
1457 /* queue : current, max */
1458 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001459 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001460 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001461 /* bytes : in, out */
1462 "%lld,%lld,"
1463 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001464 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001465 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001466 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001467 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001468 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001469 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02001470 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001471 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02001472 * active and backups. */
1473 "%s,"
1474 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001475 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001476 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001477 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001478 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001479 /* rate, rate_lim, rate_max, */
1480 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001481 /* check_status, check_code, check_duration */
1482 ",,,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001483 "\n",
1484 px->id,
1485 px->nbpend /* or px->totpend ? */, px->nbpend_max,
1486 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
1487 px->bytes_in, px->bytes_out,
1488 px->denied_req, px->denied_resp,
1489 px->failed_conns, px->failed_resp,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001490 px->retries, px->redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01001491 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01001492 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001493 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001494 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01001495 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001496 relative_pid, px->uuid,
Willy Tarreau7f062c42009-03-05 18:43:00 +01001497 px->cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001498 read_freq_ctr(&px->be_sess_per_sec),
1499 px->be_sps_max);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001500 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001501 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001502 return 0;
1503 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001504
Willy Tarreau91861262007-10-17 17:06:05 +02001505 s->data_ctx.stats.px_st = DATA_ST_PX_END;
1506 /* fall through */
1507
1508 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001509 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001510 chunk_printf(&msg, "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001511
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001512 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001513 return 0;
1514 }
Willy Tarreau91861262007-10-17 17:06:05 +02001515
1516 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
1517 /* fall through */
1518
1519 case DATA_ST_PX_FIN:
1520 return 1;
1521
1522 default:
1523 /* unknown state, we should put an abort() here ! */
1524 return 1;
1525 }
1526}
1527
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001528
1529/* This function is called to send output to the response buffer.
1530 * It dumps the sessions states onto the output buffer <rep>.
1531 * Expects to be called with client socket shut down on input.
1532 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001533 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001534 */
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001535int stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001536{
1537 struct chunk msg;
1538
1539 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
1540 /* If we're forced to shut down, we might have to remove our
1541 * reference to the last session being dumped.
1542 */
1543 if (s->data_state == DATA_ST_LIST) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001544 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001545 LIST_DEL(&s->data_ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001546 LIST_INIT(&s->data_ctx.sess.bref.users);
1547 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001548 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001549 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001550 }
1551
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001552 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001553
1554 switch (s->data_state) {
1555 case DATA_ST_INIT:
1556 /* the function had not been called yet, let's prepare the
1557 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001558 * pointer to the first in the global list. When a target
1559 * session is being destroyed, it is responsible for updating
1560 * this pointer. We know we have reached the end when this
1561 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001562 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001563 LIST_INIT(&s->data_ctx.sess.bref.users);
1564 s->data_ctx.sess.bref.ref = sessions.n;
1565 s->data_state = DATA_ST_LIST;
1566 /* fall through */
1567
1568 case DATA_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001569 /* first, let's detach the back-ref from a possible previous session */
1570 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
1571 LIST_DEL(&s->data_ctx.sess.bref.users);
1572 LIST_INIT(&s->data_ctx.sess.bref.users);
1573 }
1574
1575 /* and start from where we stopped */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001576 while (s->data_ctx.sess.bref.ref != &sessions) {
1577 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1578 struct session *curr_sess;
1579
1580 curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list);
1581
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001582 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001583 "%p: proto=%s",
1584 curr_sess,
1585 curr_sess->listener->proto->name);
1586
1587 switch (curr_sess->listener->proto->sock_family) {
1588 case AF_INET:
1589 inet_ntop(AF_INET,
1590 (const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr,
1591 pn, sizeof(pn));
1592
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001593 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001594 " src=%s:%d fe=%s be=%s srv=%s",
1595 pn,
1596 ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port),
1597 curr_sess->fe->id,
1598 curr_sess->be->id,
1599 curr_sess->srv ? curr_sess->srv->id : "<none>"
1600 );
1601 break;
1602 case AF_INET6:
1603 inet_ntop(AF_INET6,
1604 (const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr,
1605 pn, sizeof(pn));
1606
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001607 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001608 " src=%s:%d fe=%s be=%s srv=%s",
1609 pn,
1610 ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port),
1611 curr_sess->fe->id,
1612 curr_sess->be->id,
1613 curr_sess->srv ? curr_sess->srv->id : "<none>"
1614 );
1615
1616 break;
1617 case AF_UNIX:
1618 /* no more information to print right now */
1619 break;
1620 }
1621
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001622 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02001623 " ts=%02x age=%s calls=%d",
1624 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01001625 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
1626 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001627
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001628 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001629 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
1630 curr_sess->req->flags,
1631 curr_sess->req->l,
1632 curr_sess->req->analysers,
1633 curr_sess->req->rex ?
1634 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
1635 TICKS_TO_MS(1000)) : "");
1636
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001637 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001638 ",wx=%s",
1639 curr_sess->req->wex ?
1640 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
1641 TICKS_TO_MS(1000)) : "");
1642
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001643 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001644 ",ax=%s]",
1645 curr_sess->req->analyse_exp ?
1646 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
1647 TICKS_TO_MS(1000)) : "");
1648
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001649 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001650 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
1651 curr_sess->rep->flags,
1652 curr_sess->rep->l,
1653 curr_sess->rep->analysers,
1654 curr_sess->rep->rex ?
1655 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
1656 TICKS_TO_MS(1000)) : "");
1657
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001658 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001659 ",wx=%s",
1660 curr_sess->rep->wex ?
1661 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
1662 TICKS_TO_MS(1000)) : "");
1663
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001664 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001665 ",ax=%s]",
1666 curr_sess->rep->analyse_exp ?
1667 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
1668 TICKS_TO_MS(1000)) : "");
1669
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001670 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001671 " s0=[%d,%1xh,fd=%d,ex=%s]",
1672 curr_sess->si[0].state,
1673 curr_sess->si[0].flags,
1674 curr_sess->si[0].fd,
1675 curr_sess->si[0].exp ?
1676 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
1677 TICKS_TO_MS(1000)) : "");
1678
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001679 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001680 " s1=[%d,%1xh,fd=%d,ex=%s]",
1681 curr_sess->si[1].state,
1682 curr_sess->si[1].flags,
1683 curr_sess->si[1].fd,
1684 curr_sess->si[1].exp ?
1685 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
1686 TICKS_TO_MS(1000)) : "");
1687
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001688 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001689 " exp=%s",
1690 curr_sess->task->expire ?
1691 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
1692 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01001693 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001694 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001695
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001696 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001697
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001698 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001699 /* let's try again later from this session. We add ourselves into
1700 * this session's users so that it can remove us upon termination.
1701 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001702 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001703 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001704 }
1705
1706 s->data_ctx.sess.bref.ref = curr_sess->list.n;
1707 }
1708 s->data_state = DATA_ST_FIN;
1709 /* fall through */
1710
1711 default:
1712 s->data_state = DATA_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001713 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001714 }
1715}
1716
Willy Tarreau74808cb2009-03-04 15:53:18 +01001717/* print a line of error buffer (limited to 70 bytes) to <out>. The format is :
1718 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
1719 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
1720 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
1721 * lines are respected within the limit of 70 output chars. Lines that are
1722 * continuation of a previous truncated line begin with "+" instead of " "
1723 * after the offset. The new pointer is returned.
1724 */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001725static int dump_error_line(struct chunk *out, struct error_snapshot *err,
1726 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01001727{
1728 int end;
1729 unsigned char c;
1730
1731 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001732 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01001733 return ptr;
1734
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001735 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01001736
Willy Tarreau61b34732009-10-03 23:49:35 +02001737 while (ptr < err->len && ptr < sizeof(err->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001738 c = err->buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01001739 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001740 if (out->len > end - 2)
1741 break;
1742 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01001743 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001744 if (out->len > end - 3)
1745 break;
1746 out->str[out->len++] = '\\';
1747 switch (c) {
1748 case '\t': c = 't'; break;
1749 case '\n': c = 'n'; break;
1750 case '\r': c = 'r'; break;
1751 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01001752 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001753 }
1754 out->str[out->len++] = c;
1755 } else {
1756 if (out->len > end - 5)
1757 break;
1758 out->str[out->len++] = '\\';
1759 out->str[out->len++] = 'x';
1760 out->str[out->len++] = hextab[(c >> 4) & 0xF];
1761 out->str[out->len++] = hextab[c & 0xF];
1762 }
1763 if (err->buf[ptr++] == '\n') {
1764 /* we had a line break, let's return now */
1765 out->str[out->len++] = '\n';
1766 *line = ptr;
1767 return ptr;
1768 }
1769 }
1770 /* we have an incomplete line, we return it as-is */
1771 out->str[out->len++] = '\n';
1772 return ptr;
1773}
1774
1775/* This function is called to send output to the response buffer.
1776 * It dumps the errors logged in proxies onto the output buffer <rep>.
1777 * Expects to be called with client socket shut down on input.
1778 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau61b34732009-10-03 23:49:35 +02001779 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau74808cb2009-03-04 15:53:18 +01001780 */
Willy Tarreau61b34732009-10-03 23:49:35 +02001781int stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau74808cb2009-03-04 15:53:18 +01001782{
1783 extern const char *monthname[12];
1784 struct chunk msg;
1785
Willy Tarreau61b34732009-10-03 23:49:35 +02001786 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW)))
1787 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001788
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001789 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01001790
1791 if (!s->data_ctx.errors.px) {
1792 /* the function had not been called yet, let's prepare the
1793 * buffer for a response.
1794 */
Willy Tarreau74808cb2009-03-04 15:53:18 +01001795 s->data_ctx.errors.px = proxy;
1796 s->data_ctx.errors.buf = 0;
1797 s->data_ctx.errors.bol = 0;
1798 s->data_ctx.errors.ptr = -1;
1799 }
1800
1801 /* we have two inner loops here, one for the proxy, the other one for
1802 * the buffer.
1803 */
1804 while (s->data_ctx.errors.px) {
1805 struct error_snapshot *es;
1806
1807 if (s->data_ctx.errors.buf == 0)
1808 es = &s->data_ctx.errors.px->invalid_req;
1809 else
1810 es = &s->data_ctx.errors.px->invalid_rep;
1811
1812 if (!es->when.tv_sec)
1813 goto next;
1814
1815 if (s->data_ctx.errors.iid >= 0 &&
1816 s->data_ctx.errors.px->uuid != s->data_ctx.errors.iid &&
1817 es->oe->uuid != s->data_ctx.errors.iid)
1818 goto next;
1819
1820 if (s->data_ctx.errors.ptr < 0) {
1821 /* just print headers now */
1822
1823 char pn[INET6_ADDRSTRLEN];
1824 struct tm tm;
1825
1826 get_localtime(es->when.tv_sec, &tm);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001827 chunk_printf(&msg, "\n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01001828 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001829 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01001830
1831
1832 if (es->src.ss_family == AF_INET)
1833 inet_ntop(AF_INET,
1834 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
1835 pn, sizeof(pn));
1836 else
1837 inet_ntop(AF_INET6,
1838 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
1839 pn, sizeof(pn));
1840
1841 switch (s->data_ctx.errors.buf) {
1842 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001843 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01001844 " frontend %s (#%d): invalid request\n"
1845 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
1846 " request length %d bytes, error at position %d:\n\n",
1847 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
1848 pn, es->sid, es->oe->id, es->oe->uuid,
1849 es->srv ? es->srv->id : "<NONE>",
1850 es->srv ? es->srv->puid : -1,
1851 es->len, es->pos);
1852 break;
1853 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001854 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01001855 " backend %s (#%d) : invalid response\n"
1856 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
1857 " response length %d bytes, error at position %d:\n\n",
1858 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
1859 pn, es->sid, es->oe->id, es->oe->uuid,
1860 es->srv ? es->srv->id : "<NONE>",
1861 es->srv ? es->srv->puid : -1,
1862 es->len, es->pos);
1863 break;
1864 }
1865
Willy Tarreau61b34732009-10-03 23:49:35 +02001866 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001867 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02001868 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001869 }
1870 s->data_ctx.errors.ptr = 0;
1871 s->data_ctx.errors.sid = es->sid;
1872 }
1873
1874 if (s->data_ctx.errors.sid != es->sid) {
1875 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001876 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01001877 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau61b34732009-10-03 23:49:35 +02001878 if (buffer_feed_chunk(rep, &msg) >= 0)
1879 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001880 goto next;
1881 }
1882
1883 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau61b34732009-10-03 23:49:35 +02001884 while (s->data_ctx.errors.ptr < es->len && s->data_ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001885 int newptr;
1886 int newline;
1887
1888 newline = s->data_ctx.errors.bol;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001889 newptr = dump_error_line(&msg, es, &newline, s->data_ctx.errors.ptr);
Willy Tarreau74808cb2009-03-04 15:53:18 +01001890 if (newptr == s->data_ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02001891 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001892
Willy Tarreau61b34732009-10-03 23:49:35 +02001893 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001894 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02001895 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001896 }
1897 s->data_ctx.errors.ptr = newptr;
1898 s->data_ctx.errors.bol = newline;
1899 };
1900 next:
1901 s->data_ctx.errors.bol = 0;
1902 s->data_ctx.errors.ptr = -1;
1903 s->data_ctx.errors.buf++;
1904 if (s->data_ctx.errors.buf > 1) {
1905 s->data_ctx.errors.buf = 0;
1906 s->data_ctx.errors.px = s->data_ctx.errors.px->next;
1907 }
1908 }
1909
1910 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02001911 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001912}
1913
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001914
Willy Tarreau10522fd2008-07-09 20:12:41 +02001915static struct cfg_kw_list cfg_kws = {{ },{
1916 { CFG_GLOBAL, "stats", stats_parse_global },
1917 { 0, NULL, NULL },
1918}};
1919
1920__attribute__((constructor))
1921static void __dumpstats_module_init(void)
1922{
1923 cfg_register_keywords(&cfg_kws);
1924}
1925
Willy Tarreau91861262007-10-17 17:06:05 +02001926/*
1927 * Local variables:
1928 * c-indent-level: 8
1929 * c-basic-offset: 8
1930 * End:
1931 */