blob: 1317cc080d2941c85c8db09231ada73b30c5c60c [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[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020056 "Unknown command. Please enter one of the following commands only :\n"
57 " clear counters : clear statistics counters\n"
58 " help : this message\n"
59 " prompt : toggle interactive mode with prompt\n"
60 " quit : disconnect\n"
61 " show info : report information about the running process\n"
62 " show stat : report counters for each proxy and server\n"
63 " show errors : report last request and response errors for each proxy\n"
64 " show sess : report the list of current sessions\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020065 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020066
67const struct chunk stats_sock_usage = {
68 .str = (char *)&stats_sock_usage_msg,
69 .len = sizeof(stats_sock_usage_msg)-1
70};
71
Willy Tarreaufbee7132007-10-18 13:53:22 +020072/* This function parses a "stats" statement in the "global" section. It returns
73 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
74 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
75 * zero included. The trailing '\n' must not be written. The function must be
76 * called with <args> pointing to the first word after "stats".
77 */
Willy Tarreau10522fd2008-07-09 20:12:41 +020078static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
79 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +020080{
Willy Tarreau10522fd2008-07-09 20:12:41 +020081 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +020082 if (!strcmp(args[0], "socket")) {
83 struct sockaddr_un su;
84 int cur_arg;
85
86 if (*args[1] == 0) {
87 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
88 return -1;
89 }
90
91 if (global.stats_sock.state != LI_NEW) {
92 snprintf(err, errlen, "'stats socket' already specified in global section");
93 return -1;
94 }
95
96 su.sun_family = AF_UNIX;
97 strncpy(su.sun_path, args[1], sizeof(su.sun_path));
98 su.sun_path[sizeof(su.sun_path) - 1] = 0;
99 memcpy(&global.stats_sock.addr, &su, sizeof(su)); // guaranteed to fit
100
Willy Tarreau89a63132009-08-16 17:41:45 +0200101 if (!global.stats_fe) {
102 if ((global.stats_fe = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) {
103 snprintf(err, errlen, "out of memory");
104 return -1;
105 }
106
107 LIST_INIT(&global.stats_fe->pendconns);
108 LIST_INIT(&global.stats_fe->acl);
109 LIST_INIT(&global.stats_fe->block_cond);
110 LIST_INIT(&global.stats_fe->redirect_rules);
111 LIST_INIT(&global.stats_fe->mon_fail_cond);
112 LIST_INIT(&global.stats_fe->switching_rules);
113 LIST_INIT(&global.stats_fe->tcp_req.inspect_rules);
114
115 /* Timeouts are defined as -1, so we cannot use the zeroed area
116 * as a default value.
117 */
118 proxy_reset_timeouts(global.stats_fe);
119
120 global.stats_fe->last_change = now.tv_sec;
121 global.stats_fe->id = strdup("GLOBAL");
122 global.stats_fe->cap = PR_CAP_FE;
123 }
124
Willy Tarreaufbee7132007-10-18 13:53:22 +0200125 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100126 global.stats_sock.options = LI_O_NONE;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200127 global.stats_sock.accept = uxst_event_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200128 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200129 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200130 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreau89a63132009-08-16 17:41:45 +0200131 global.stats_sock.private = global.stats_fe; /* must point to the frontend */
132
133 global.stats_fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
134 global.stats_sock.timeout = &global.stats_fe->timeout.client;
135
136 global.stats_sock.next = global.stats_fe->listen;
137 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200138
139 cur_arg = 2;
140 while (*args[cur_arg]) {
141 if (!strcmp(args[cur_arg], "uid")) {
142 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
143 cur_arg += 2;
144 }
145 else if (!strcmp(args[cur_arg], "gid")) {
146 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
147 cur_arg += 2;
148 }
149 else if (!strcmp(args[cur_arg], "mode")) {
150 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
151 cur_arg += 2;
152 }
153 else if (!strcmp(args[cur_arg], "user")) {
154 struct passwd *user;
155 user = getpwnam(args[cur_arg + 1]);
156 if (!user) {
157 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
158 args[cur_arg + 1]);
159 return -1;
160 }
161 global.stats_sock.perm.ux.uid = user->pw_uid;
162 cur_arg += 2;
163 }
164 else if (!strcmp(args[cur_arg], "group")) {
165 struct group *group;
166 group = getgrnam(args[cur_arg + 1]);
167 if (!group) {
168 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
169 args[cur_arg + 1]);
170 return -1;
171 }
172 global.stats_sock.perm.ux.gid = group->gr_gid;
173 cur_arg += 2;
174 }
175 else {
176 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', and 'mode'");
177 return -1;
178 }
179 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100180
Willy Tarreaufbee7132007-10-18 13:53:22 +0200181 uxst_add_listener(&global.stats_sock);
182 global.maxsock++;
183 }
184 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100185 unsigned timeout;
186 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
187
188 if (res) {
189 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
190 return -1;
191 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200192
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100193 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200194 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
195 return -1;
196 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200197 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200198 }
199 else if (!strcmp(args[0], "maxconn")) {
200 int maxconn = atol(args[1]);
201
202 if (maxconn <= 0) {
203 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
204 return -1;
205 }
206 global.maxsock -= global.stats_sock.maxconn;
207 global.stats_sock.maxconn = maxconn;
208 global.maxsock += global.stats_sock.maxconn;
209 }
210 else {
211 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
212 return -1;
213 }
214 return 0;
215}
216
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200217int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100218{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200219 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100220 "# pxname,svname,"
221 "qcur,qmax,"
222 "scur,smax,slim,stot,"
223 "bin,bout,"
224 "dreq,dresp,"
225 "ereq,econ,eresp,"
226 "wretr,wredis,"
227 "status,weight,act,bck,"
228 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200229 "pid,iid,sid,throttle,lbtot,tracked,type,"
230 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200231 "check_status,check_code,check_duration,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100232 "\n");
233}
234
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200235/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200236 * called from an applet running in a stream interface. The function returns 1
237 * if the request was understood, otherwise zero. It sets si->st0 to a value
238 * designating the function which will have to process the request.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200239 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200240int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200241{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200242 struct session *s = si->private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200243 char *args[MAX_STATS_ARGS + 1];
244 int arg;
245
246 while (isspace((unsigned char)*line))
247 line++;
248
249 arg = 0;
250 args[arg] = line;
251
252 while (*line && arg < MAX_STATS_ARGS) {
253 if (isspace((unsigned char)*line)) {
254 *line++ = '\0';
255
256 while (isspace((unsigned char)*line))
257 line++;
258
259 args[++arg] = line;
260 continue;
261 }
262
263 line++;
264 }
265
266 while (++arg <= MAX_STATS_ARGS)
267 args[arg] = line;
268
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200269 s->data_ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200270 if (strcmp(args[0], "show") == 0) {
271 if (strcmp(args[1], "stat") == 0) {
272 if (*args[2] && *args[3] && *args[4]) {
273 s->data_ctx.stats.flags |= STAT_BOUND;
274 s->data_ctx.stats.iid = atoi(args[2]);
275 s->data_ctx.stats.type = atoi(args[3]);
276 s->data_ctx.stats.sid = atoi(args[4]);
277 }
278
279 s->data_ctx.stats.flags |= STAT_SHOW_STAT;
280 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200281 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200282 si->st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200283 }
284 else if (strcmp(args[1], "info") == 0) {
285 s->data_ctx.stats.flags |= STAT_SHOW_INFO;
286 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200287 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200288 si->st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200289 }
290 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200291 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200292 si->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200293 }
294 else if (strcmp(args[1], "errors") == 0) {
295 if (*args[2])
296 s->data_ctx.errors.iid = atoi(args[2]);
297 else
298 s->data_ctx.errors.iid = -1;
299 s->data_ctx.errors.px = NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200300 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200301 si->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200302 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200303 else { /* neither "stat" nor "info" nor "sess" nor "errors"*/
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200304 return 0;
305 }
306 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200307 else if (strcmp(args[0], "clear") == 0) {
308 if (strcmp(args[1], "counters") == 0) {
309 struct proxy *px;
310 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200311 struct listener *li;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200312
313 for (px = proxy; px; px = px->next) {
314 memset(&px->counters, 0, sizeof(px->counters));
315
316 for (sv = px->srv; sv; sv = sv->next)
317 memset(&sv->counters, 0, sizeof(sv->counters));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200318
319 for (li = px->listen; li; li = li->next)
320 memset(li->counters, 0, sizeof(*li->counters));
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200321 }
322
323 return 1;
324 }
325 else {
326 return 0;
327 }
328 }
329 else { /* not "show" nor "clear"*/
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200330 return 0;
331 }
332 return 1;
333}
334
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200335/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200336 * used to processes I/O from/to the stats unix socket. The system relies on a
337 * state machine handling requests and various responses. We read a request,
338 * then we process it and send the response, and we possibly display a prompt.
339 * Then we can read again. The state is stored in si->st0 and is one of the
340 * STAT_CLI_* constants. si->st1 is used to indicate whether prompt is enabled
341 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200342 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200343void stats_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200344{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200345 struct session *s = si->private;
346 struct buffer *req = si->ob;
347 struct buffer *res = si->ib;
348 int reql;
349 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200350
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200351 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
352 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200353
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200354 while (1) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200355 if (si->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200356 /* Stats output not initialized yet */
357 memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats));
358 s->data_source = DATA_SRC_STATS;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200359 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200360 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200361 else if (si->st0 == STAT_CLI_END) {
362 /* Let's close for real now. We just close the request
363 * side, the conditions below will complete if needed.
364 */
365 si->shutw(si);
366 break;
367 }
368 else if (si->st0 == STAT_CLI_GETREQ) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200369 reql = buffer_si_peekline(si->ob, trash, sizeof(trash));
370 if (reql <= 0) { /* closed or EOL not found */
371 if (reql == 0)
372 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200373 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200374 continue;
375 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200376
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200377 /* seek for a possible semi-colon. If we find one, we
378 * replace it with an LF and skip only this part.
379 */
380 for (len = 0; len < reql; len++)
381 if (trash[len] == ';') {
382 trash[len] = '\n';
383 reql = len + 1;
384 break;
385 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200386
Willy Tarreau816fc222009-10-04 07:36:58 +0200387 /* now it is time to check that we have a full line,
388 * remove the trailing \n and possibly \r, then cut the
389 * line.
390 */
391 len = reql - 1;
392 if (trash[len] != '\n') {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200393 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200394 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200395 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200396
Willy Tarreau816fc222009-10-04 07:36:58 +0200397 if (len && trash[len-1] == '\r')
398 len--;
399
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200400 trash[len] = '\0';
401
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200402 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200403 if (len) {
404 if (strcmp(trash, "quit") == 0) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200405 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200406 continue;
407 }
408 else if (strcmp(trash, "prompt") == 0)
409 si->st1 = !si->st1;
410 else if (strcmp(trash, "help") == 0 ||
411 !stats_sock_parse_request(si, trash))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200412 si->st0 = STAT_CLI_O_HELP;
413 /* NB: stats_sock_parse_request() may have put
414 * another STAT_CLI_O_* into si->st0.
415 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200416 }
417 else if (!si->st1) {
418 /* if prompt is disabled, print help on empty lines,
419 * so that the user at least knows how to enable
420 * prompt and find help.
421 */
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200422 si->st0 = STAT_CLI_O_HELP;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200423 }
424
425 /* re-adjust req buffer */
426 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200427 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200428 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200429 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200430 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200431 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200432 continue;
433 }
434
435 switch (si->st0) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200436 case STAT_CLI_O_HELP:
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200437 if (buffer_feed(si->ib, stats_sock_usage.str, stats_sock_usage.len) < 0)
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200438 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200439 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200440 case STAT_CLI_O_INFO:
Willy Tarreau24955a12009-10-04 11:54:04 +0200441 if (stats_dump_raw_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200442 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200443 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200444 case STAT_CLI_O_SESS:
Willy Tarreau7e72a8f2009-10-03 23:55:14 +0200445 if (stats_dump_sess_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200446 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200447 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200448 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau61b34732009-10-03 23:49:35 +0200449 if (stats_dump_errors_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200450 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200451 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200452 default: /* abnormal state */
453 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200454 break;
455 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200456
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200457 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
458 if (si->st0 == STAT_CLI_PROMPT) {
459 if (buffer_feed(si->ib, si->st1 ? "\n> " : "\n", si->st1 ? 3 : 1) < 0)
460 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200461 }
462
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200463 /* If the output functions are still there, it means they require more room. */
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200464 if (si->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200465 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200466
467 /* Now we close the output if one of the writers did so,
468 * or if we're not in interactive mode and the request
469 * buffer is empty. This still allows pipelined requests
470 * to be sent in non-interactive mode.
471 */
472 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->st1 && !req->send_max)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200473 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200474 continue;
475 }
476
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200477 /* switch state back to GETREQ to read next requests */
478 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200479 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200480 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200481
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200482 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200483 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
484 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
485 /* Other size has closed, let's abort if we have no more processing to do
486 * and nothing more to consume. This is comparable to a broken pipe, so
487 * we forward the close to the request side so that it flows upstream to
488 * the client.
489 */
490 si->shutw(si);
491 }
492
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200493 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200494 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
495 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
496 /* We have no more processing to do, and nothing more to send, and
497 * the client side has closed. So we'll forward this state downstream
498 * on the response buffer.
499 */
500 si->shutr(si);
501 res->flags |= BF_READ_NULL;
502 }
503
504 /* update all other flags and resync with the other side */
505 si->update(si);
506
507 /* we don't want to expire timeouts while we're processing requests */
508 si->ib->rex = TICK_ETERNITY;
509 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200510
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200511 out:
512 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
513 __FUNCTION__, __LINE__,
514 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
515
516 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
517 /* check that we have released everything then unregister */
518 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200519 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200520}
521
Willy Tarreau24955a12009-10-04 11:54:04 +0200522/* This function is called to send output to the response buffer.
523 * It dumps statistics onto the output buffer <rep> owned by session <s>.
524 * s->data_ctx must have been zeroed first, and the flags properly set.
525 * It returns 0 as long as it does not complete, non-zero upon completion.
526 * Some states are not used but it makes the code more similar to other
527 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200528 */
Willy Tarreau24955a12009-10-04 11:54:04 +0200529int stats_dump_raw_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3e76e722007-10-17 18:57:38 +0200530{
Willy Tarreau3e76e722007-10-17 18:57:38 +0200531 struct proxy *px;
532 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +0100533 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200534
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200535 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +0200536
537 switch (s->data_state) {
538 case DATA_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +0200539 /* the function had not been called yet */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200540 s->data_state = DATA_ST_HEAD;
541 /* fall through */
542
543 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100544 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200545 print_csv_header(&msg);
Willy Tarreau24955a12009-10-04 11:54:04 +0200546 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100547 return 0;
548 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200549
550 s->data_state = DATA_ST_INFO;
551 /* fall through */
552
553 case DATA_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +0100554 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100555 if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200556 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +0100557 "Name: " PRODUCT_NAME "\n"
558 "Version: " HAPROXY_VERSION "\n"
559 "Release_date: " HAPROXY_DATE "\n"
560 "Nbproc: %d\n"
561 "Process_num: %d\n"
562 "Pid: %d\n"
563 "Uptime: %dd %dh%02dm%02ds\n"
564 "Uptime_sec: %d\n"
565 "Memmax_MB: %d\n"
566 "Ulimit-n: %d\n"
567 "Maxsock: %d\n"
568 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100569 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100570 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100571 "PipesUsed: %d\n"
572 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100573 "Tasks: %d\n"
574 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200575 "node: %s\n"
576 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100577 "",
578 global.nbproc,
579 relative_pid,
580 pid,
581 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
582 up,
583 global.rlimit_memmax,
584 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100585 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100586 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200587 nb_tasks_cur, run_queue_cur,
588 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +0100589 );
Willy Tarreau24955a12009-10-04 11:54:04 +0200590 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100591 return 0;
592 }
593
Willy Tarreau3e76e722007-10-17 18:57:38 +0200594 s->data_ctx.stats.px = proxy;
595 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100596
597 s->data_ctx.stats.sv = NULL;
598 s->data_ctx.stats.sv_st = 0;
599
Willy Tarreau3e76e722007-10-17 18:57:38 +0200600 s->data_state = DATA_ST_LIST;
601 /* fall through */
602
603 case DATA_ST_LIST:
604 /* dump proxies */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100605 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100606 while (s->data_ctx.stats.px) {
607 px = s->data_ctx.stats.px;
608 /* skip the disabled proxies and non-networked ones */
609 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +0200610 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100611 if (stats_dump_proxy(s, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100612 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +0200613 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200614
Willy Tarreaua8efd362008-01-03 10:19:15 +0100615 s->data_ctx.stats.px = px->next;
616 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
617 }
618 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200619 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200620
621 s->data_state = DATA_ST_END;
622 /* fall through */
623
624 case DATA_ST_END:
625 s->data_state = DATA_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +0100626 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200627
628 case DATA_ST_FIN:
629 return 1;
630
631 default:
632 /* unknown state ! */
Willy Tarreau24955a12009-10-04 11:54:04 +0200633 s->data_state = DATA_ST_FIN;
634 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200635 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100636}
637
638
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200639/* This I/O handler runs as an applet embedded in a stream interface. It is
640 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
641 * si->st0 becomes non-zero once the transfer is finished. The handler
642 * automatically unregisters itself once transfer is complete.
643 */
644void http_stats_io_handler(struct stream_interface *si)
645{
646 struct session *s = si->private;
647 struct buffer *req = si->ob;
648 struct buffer *res = si->ib;
649
650 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
651 goto out;
652
653 /* check that the output is not closed */
654 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
655 si->st0 = 1;
656
657 if (!si->st0) {
658 if (stats_dump_http(s, res, s->be->uri_auth)) {
659 si->st0 = 1;
660 si->shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200661 }
662 }
663
664 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
665 si->shutw(si);
666
667 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->st0) {
668 si->shutr(si);
669 res->flags |= BF_READ_NULL;
670 }
671
672 /* update all other flags and resync with the other side */
673 si->update(si);
674
675 /* we don't want to expire timeouts while we're processing requests */
676 si->ib->rex = TICK_ETERNITY;
677 si->ob->wex = TICK_ETERNITY;
678
679 out:
680 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
681 /* check that we have released everything then unregister */
682 stream_int_unregister_handler(si);
683 }
684}
685
686
Willy Tarreau3e76e722007-10-17 18:57:38 +0200687/*
688 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200689 * client socket shut down on input. It stops by itself by unsetting the
Willy Tarreau72b179a2008-08-28 16:01:32 +0200690 * BF_HIJACK flag from the buffer, which it uses to keep on being called
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200691 * when there is free space in the buffer, of simply by letting an empty buffer
692 * upon return.s->data_ctx must have been zeroed before the first call, and the
693 * flags set. It returns 0 if it had to stop writing data and an I/O is needed,
694 * 1 if the dump is finished and the session must be closed, or -1 in case of
695 * any error.
Willy Tarreau91861262007-10-17 17:06:05 +0200696 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100697int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200698{
Willy Tarreau91861262007-10-17 17:06:05 +0200699 struct proxy *px;
700 struct chunk msg;
701 unsigned int up;
702
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200703 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +0200704
705 switch (s->data_state) {
706 case DATA_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200707 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200708 "HTTP/1.0 200 OK\r\n"
709 "Cache-Control: no-cache\r\n"
710 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200711 "Content-Type: %s\r\n",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100712 (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +0200713
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100714 if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200715 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +0200716 uri->refresh);
717
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200718 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +0200719
720 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200721 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +0200722 return 0;
723
Willy Tarreau91861262007-10-17 17:06:05 +0200724 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
725 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
726 if (!(s->flags & SN_FINST_MASK))
727 s->flags |= SN_FINST_R;
728
729 if (s->txn.meth == HTTP_METH_HEAD) {
730 /* that's all we return in case of HEAD request */
731 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +0200732 return 1;
733 }
734
735 s->data_state = DATA_ST_HEAD; /* let's start producing data */
736 /* fall through */
737
738 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100739 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200740 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200741 chunk_printf(&msg,
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200742 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200743 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
744 "<style type=\"text/css\"><!--\n"
745 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +0200746 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +0200747 " font-size: 12px;"
748 " font-weight: normal;"
749 " color: black;"
750 " background: white;"
751 "}\n"
752 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +0200753 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +0200754 " align: center;"
755 "}\n"
756 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +0200757 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +0200758 " margin-bottom: 0.5em;"
759 "}\n"
760 "h2 {"
761 " font-family: helvetica, arial;"
762 " font-size: x-large;"
763 " font-weight: bold;"
764 " font-style: italic;"
765 " color: #6020a0;"
766 " margin-top: 0em;"
767 " margin-bottom: 0em;"
768 "}\n"
769 "h3 {"
770 " font-family: helvetica, arial;"
771 " font-size: 16px;"
772 " font-weight: bold;"
773 " color: #b00040;"
774 " background: #e8e8d0;"
775 " margin-top: 0em;"
776 " margin-bottom: 0em;"
777 "}\n"
778 "li {"
779 " margin-top: 0.25em;"
780 " margin-right: 2em;"
781 "}\n"
782 ".hr {margin-top: 0.25em;"
783 " border-color: black;"
784 " border-bottom-style: solid;"
785 "}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200786 ".titre {background: #20D0D0;color: #000000; font-weight: bold;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200787 ".total {background: #20D0D0;color: #ffff80;}\n"
788 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200789 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200790 ".backend {background: #e8e8d0;}\n"
791 ".active0 {background: #ff9090;}\n"
792 ".active1 {background: #ffd020;}\n"
793 ".active2 {background: #ffffa0;}\n"
794 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100795 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
796 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
797 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200798 ".backup0 {background: #ff9090;}\n"
799 ".backup1 {background: #ff80ff;}\n"
800 ".backup2 {background: #c060ff;}\n"
801 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100802 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
803 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
804 ".backup6 {background: #e0e0e0;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +0200805 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Willy Tarreau91861262007-10-17 17:06:05 +0200806 "table.tbl { border-collapse: collapse; border-style: none;}\n"
807 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray;}\n"
808 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzki619caca2009-10-03 15:46:08 +0200809 "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 +0200810 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200811 "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 +0200812 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
813 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
814 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
815 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200816 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200817 (uri->flags&ST_SHNODE) ? " on " : "",
818 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200819 );
Willy Tarreau55bb8452007-10-17 18:44:57 +0200820 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200821 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200822 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200823 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200824 return 0;
825
826 s->data_state = DATA_ST_INFO;
827 /* fall through */
828
829 case DATA_ST_INFO:
830 up = (now.tv_sec - start_date.tv_sec);
831
832 /* WARNING! this has to fit the first packet too.
833 * We are around 3.5 kB, add adding entries will
834 * become tricky if we want to support 4kB buffers !
835 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100836 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200837 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200838 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
839 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200840 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200841 "<hr width=\"100%%\" class=\"hr\">\n"
842 "<h3>&gt; General process information</h3>\n"
843 "<table border=0 cols=4><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100844 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200845 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200846 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
847 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
848 "current conns = %d; current pipes = %d/%d<br>\n"
849 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200850 "</td><td align=\"center\" nowrap>\n"
851 "<table class=\"lgd\"><tr>\n"
852 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
853 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
854 "</tr><tr>\n"
855 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
856 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
857 "</tr><tr>\n"
858 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
859 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
860 "</tr><tr>\n"
861 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100862 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200863 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100864 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +0200865 "</td>"
866 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
867 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
868 "",
869 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200870 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
871 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
872 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +0200873 up / 86400, (up % 86400) / 3600,
874 (up % 3600) / 60, (up % 60),
875 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
876 global.rlimit_memmax ? " MB" : "",
877 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100878 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100879 actconn, pipes_used, pipes_used+pipes_free,
880 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +0200881 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100882
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100883 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200884 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200885 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
886 uri->uri_prefix,
887 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100888 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200889 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200890 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200891 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
892 uri->uri_prefix,
893 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100894 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200895
Willy Tarreau55bb8452007-10-17 18:44:57 +0200896 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100897 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200898 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200899 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
900 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100901 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200902 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200903 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200904 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200905 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
906 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100907 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200908 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200909 }
Willy Tarreau91861262007-10-17 17:06:05 +0200910
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200911 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200912 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
913 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100914 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
915 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200916
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200917 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +0200918 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
919 uri->uri_prefix,
920 (uri->refresh > 0) ? ";norefresh" : "");
921
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200922 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200923 "</td>"
924 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
925 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
926 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
927 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
928 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
929 "</ul>"
930 "</td>"
931 "</tr></table>\n"
932 ""
933 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100934
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200935 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200936 return 0;
937 }
Willy Tarreau91861262007-10-17 17:06:05 +0200938
Willy Tarreau91861262007-10-17 17:06:05 +0200939 s->data_ctx.stats.px = proxy;
940 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
941 s->data_state = DATA_ST_LIST;
942 /* fall through */
943
944 case DATA_ST_LIST:
945 /* dump proxies */
946 while (s->data_ctx.stats.px) {
947 px = s->data_ctx.stats.px;
948 /* skip the disabled proxies and non-networked ones */
949 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100950 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200951 return 0;
952
953 s->data_ctx.stats.px = px->next;
954 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
955 }
956 /* here, we just have reached the last proxy */
957
958 s->data_state = DATA_ST_END;
959 /* fall through */
960
961 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100962 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200963 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200964 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200965 return 0;
966 }
Willy Tarreau91861262007-10-17 17:06:05 +0200967
968 s->data_state = DATA_ST_FIN;
969 /* fall through */
970
971 case DATA_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +0200972 return 1;
973
974 default:
975 /* unknown state ! */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200976 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +0200977 return -1;
978 }
979}
980
981
982/*
983 * Dumps statistics for a proxy.
984 * Returns 0 if it had to stop dumping data because of lack of buffer space,
985 * ot non-zero if everything completed.
986 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100987int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200988{
989 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100990 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200991 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +0200992 struct chunk msg;
993
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200994 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +0200995
996 switch (s->data_ctx.stats.px_st) {
997 case DATA_ST_PX_INIT:
998 /* we are on a new proxy */
999
1000 if (uri && uri->scope) {
1001 /* we have a limited scope, we have to check the proxy name */
1002 struct stat_scope *scope;
1003 int len;
1004
1005 len = strlen(px->id);
1006 scope = uri->scope;
1007
1008 while (scope) {
1009 /* match exact proxy name */
1010 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1011 break;
1012
1013 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001014 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001015 break;
1016 scope = scope->next;
1017 }
1018
1019 /* proxy name not found : don't dump anything */
1020 if (scope == NULL)
1021 return 1;
1022 }
1023
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001024 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001025 (px->uuid != s->data_ctx.stats.iid))
1026 return 1;
1027
Willy Tarreau91861262007-10-17 17:06:05 +02001028 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
1029 /* fall through */
1030
1031 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001032 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001033 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001034 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001035 "<table class=\"tbl\" width=\"100%%\">\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001036 "<tr align=\"center\" class=\"titre\">"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001037 "<th class=\"pxname\" width=\"10%%\">%s</th>"
1038 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001039 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001040 "</table>\n"
1041 "<table cols=\"29\" class=\"tbl\" width=\"100%%\">\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001042 "<tr align=\"center\" class=\"titre\">"
1043 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001044 "<th colspan=3>Queue</th>"
1045 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001046 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001047 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001048 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001049 "</tr>\n"
1050 "<tr align=\"center\" class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001051 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001052 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001053 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001054 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001055 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001056 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001057 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1058 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001059 "</tr>",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001060 px->id,
1061 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001062
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001063 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001064 return 0;
1065 }
Willy Tarreau91861262007-10-17 17:06:05 +02001066
1067 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
1068 /* fall through */
1069
1070 case DATA_ST_PX_FE:
1071 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001072 if ((px->cap & PR_CAP_FE) &&
1073 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
1074 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001075 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001076 /* name, queue */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001077 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=3></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001078 /* sessions rate : current, max, limit */
1079 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
1080 /* sessions : current, max, limit, total, lbtot */
1081 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001082 "<td align=right>%s</td><td align=right></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001083 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001084 "<td align=right>%s</td><td align=right>%s</td>"
1085 "",
Willy Tarreaua3e49422009-05-10 19:19:41 +02001086 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001087 U2H1(px->counters.fe_sps_max), LIM2A2(px->fe_sps_lim, "-"),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001088 U2H3(px->feconn), U2H4(px->counters.feconn_max), U2H5(px->maxconn),
1089 U2H6(px->counters.cum_feconn), U2H7(px->counters.bytes_in), U2H8(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001090
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001091 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001092 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001093 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001094 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001095 "<td align=right>%s</td><td align=right></td><td align=right></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001096 /* warnings: retries, redispatches */
1097 "<td align=right></td><td align=right></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001098 /* server status : reflect frontend status */
Willy Tarreau91861262007-10-17 17:06:05 +02001099 "<td align=center>%s</td>"
1100 /* rest of server: nothing */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001101 "<td align=center colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02001102 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001103 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
1104 U2H2(px->counters.failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +02001105 px->state == PR_STRUN ? "OPEN" :
1106 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001107 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001108 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001109 /* pxid, name, queue cur, queue max, */
1110 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001111 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001112 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001113 /* bytes : in, out */
1114 "%lld,%lld,"
1115 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001116 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001117 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001118 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001119 /* warnings: retries, redispatches */
1120 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001121 /* server status : reflect frontend status */
1122 "%s,"
1123 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001124 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001125 /* pid, iid, sid, throttle, lbtot, tracked, type */
1126 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001127 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001128 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001129 /* check_status, check_code, check_duration */
1130 ",,,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001131 "\n",
1132 px->id,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001133 px->feconn, px->counters.feconn_max, px->maxconn, px->counters.cum_feconn,
1134 px->counters.bytes_in, px->counters.bytes_out,
1135 px->counters.denied_req, px->counters.denied_resp,
1136 px->counters.failed_req,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001137 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +01001138 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01001139 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001140 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001141 px->fe_sps_lim, px->counters.fe_sps_max);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001142 }
Willy Tarreau91861262007-10-17 17:06:05 +02001143
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001144 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001145 return 0;
1146 }
1147
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001148 s->data_ctx.stats.l = px->listen; /* may be NULL */
1149 s->data_ctx.stats.px_st = DATA_ST_PX_LI;
1150 /* fall through */
1151
1152 case DATA_ST_PX_LI:
1153 /* stats.l has been initialized above */
1154 for (; s->data_ctx.stats.l != NULL; s->data_ctx.stats.l = l->next) {
1155 l = s->data_ctx.stats.l;
1156 if (!l->counters)
1157 continue;
1158
1159 if (s->data_ctx.stats.flags & STAT_BOUND) {
1160 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SO)))
1161 break;
1162
1163 if (s->data_ctx.stats.sid != -1 && l->luid != s->data_ctx.stats.sid)
1164 continue;
1165 }
1166
1167 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
1168 chunk_printf(&msg,
1169 /* name, queue */
1170 "<tr align=center class=\"socket\"><td>%s</td><td colspan=3></td>"
1171 /* sessions rate: current, max, limit */
1172 "<td align=right colspan=3>&nbsp;</td>"
1173 /* sessions: current, max, limit, total, lbtot */
1174 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
1175 "<td align=right>%s</td><td align=right>&nbsp;</td>"
1176 /* bytes: in, out */
1177 "<td align=right>%s</td><td align=right>%s</td>"
1178 "",
1179 l->name,
1180 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
1181 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
1182
1183 chunk_printf(&msg,
1184 /* denied: req, resp */
1185 "<td align=right>%s</td><td align=right>%s</td>"
1186 /* errors: request, connect, response */
1187 "<td align=right>%s</td><td align=right></td><td align=right></td>"
1188 /* warnings: retries, redispatches */
1189 "<td align=right></td><td align=right></td>"
1190 /* server status: reflect listener status */
1191 "<td align=center>%s</td>"
1192 /* rest of server: nothing */
1193 "<td align=center colspan=8></td></tr>"
1194 "",
1195 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
1196 U2H2(l->counters->failed_req),
1197 (l->nbconn < l->maxconn) ? "OPEN" : "FULL");
1198 } else {
1199 chunk_printf(&msg,
1200 /* pxid, name, queue cur, queue max, */
1201 "%s,%s,,,"
1202 /* sessions: current, max, limit, total */
1203 "%d,%d,%d,%lld,"
1204 /* bytes: in, out */
1205 "%lld,%lld,"
1206 /* denied: req, resp */
1207 "%lld,%lld,"
1208 /* errors: request, connect, response */
1209 "%lld,,,"
1210 /* warnings: retries, redispatches */
1211 ",,"
1212 /* server status: reflect listener status */
1213 "%s,"
1214 /* rest of server: nothing */
1215 ",,,,,,,,"
1216 /* pid, iid, sid, throttle, lbtot, tracked, type */
1217 "%d,%d,%d,,,,%d,"
1218 /* rate, rate_lim, rate_max */
1219 ",,,"
1220 /* check_status, check_code, check_duration */
1221 ",,,"
1222 "\n",
1223 px->id, l->name,
1224 l->nbconn, l->counters->conn_max,
1225 l->maxconn, l->counters->cum_conn,
1226 l->counters->bytes_in, l->counters->bytes_out,
1227 l->counters->denied_req, l->counters->denied_resp,
1228 l->counters->failed_req,
1229 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
1230 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
1231 }
1232
1233 if (buffer_feed_chunk(rep, &msg) >= 0)
1234 return 0;
1235 }
1236
Willy Tarreau91861262007-10-17 17:06:05 +02001237 s->data_ctx.stats.sv = px->srv; /* may be NULL */
1238 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
1239 /* fall through */
1240
1241 case DATA_ST_PX_SV:
1242 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001243 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
1244
Willy Tarreau2ea81932007-11-30 12:04:38 +01001245 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 +02001246
1247 sv = s->data_ctx.stats.sv;
1248
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001249 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001250 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
1251 break;
1252
1253 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
1254 continue;
1255 }
1256
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001257 if (sv->tracked)
1258 svs = sv->tracked;
1259 else
1260 svs = sv;
1261
Willy Tarreau91861262007-10-17 17:06:05 +02001262 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001263 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01001264 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001265 else if (svs->state & SRV_RUNNING) {
1266 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02001267 sv_state = 3; /* UP */
1268 else
1269 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01001270
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001271 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01001272 sv_state += 2;
1273 }
Willy Tarreau91861262007-10-17 17:06:05 +02001274 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001275 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02001276 sv_state = 1; /* going up */
1277 else
1278 sv_state = 0; /* DOWN */
1279
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001280 if ((sv_state == 0) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02001281 /* do not report servers which are DOWN */
1282 s->data_ctx.stats.sv = sv->next;
1283 continue;
1284 }
1285
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001286 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001287 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
1288 "UP %d/%d &darr;", "UP",
1289 "NOLB %d/%d &darr;", "NOLB",
1290 "<i>no check</i>" };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001291 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001292 /* name */
1293 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001294 /* queue : current, max, limit */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001295 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001296 /* sessions rate : current, max, limit */
1297 "<td align=right>%s</td><td align=right>%s</td><td align=right></td>"
1298 /* sessions : current, max, limit, total, lbtot */
1299 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001300 "<td align=right>%s</td><td align=right>%s</td>"
1301 "",
1302 (sv->state & SRV_BACKUP) ? "backup" : "active",
1303 sv_state, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001304 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
1305 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
1306 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001307 U2H8(sv->counters.cum_sess), U2H9(sv->counters.cum_lbconn));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001308
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001309 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001310 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001311 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001312 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001313 "<td align=right></td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001314 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001315 "<td align=right></td><td align=right>%s</td><td align=right>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001316 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001317 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001318 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001319 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
1320 U2H2(sv->counters.failed_secu),
1321 U2H3(sv->counters.failed_conns), U2H4(sv->counters.failed_resp),
1322 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001323
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001324 /* status, lest check */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001325 chunk_printf(&msg, "<td nowrap>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001326
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001327 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001328 chunk_printf(&msg, "%s ",
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001329 human_time(now.tv_sec - sv->last_change, 1));
1330
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001331 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001332 srv_hlt_st[sv_state],
1333 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
1334 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
1335
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001336 chunk_printf(&msg, "</td><td title=\"%s\" nowrap> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001337 get_check_status_description(sv->check_status),
1338 tv_iszero(&sv->check_start)?"":"* ",
1339 get_check_status_info(sv->check_status));
1340
1341 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001342 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001343
1344 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001345 chunk_printf(&msg, " in %lums", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001346 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001347 chunk_printf(&msg, "</td><td>");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001348 }
Willy Tarreau91861262007-10-17 17:06:05 +02001349
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001350 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001351 /* weight */
1352 "</td><td>%d</td>"
1353 /* act, bck */
1354 "<td>%s</td><td>%s</td>"
1355 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001356 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02001357 (sv->state & SRV_BACKUP) ? "-" : "Y",
1358 (sv->state & SRV_BACKUP) ? "Y" : "-");
1359
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001360 /* check failures: unique, fatal, down time */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001361 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001362 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02001363 "<td align=right>%lld</td><td align=right>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001364 "<td nowrap align=right>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001365 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001366 svs->counters.failed_checks, svs->counters.down_trans,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001367 human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001368 else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001369 chunk_printf(&msg,
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001370 "<td nowrap colspan=3>via %s/%s</td>", svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001371 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001372 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001373 "<td colspan=3></td>");
1374
1375 /* throttle */
1376 if ((sv->state & SRV_WARMINGUP) &&
1377 now.tv_sec < sv->last_change + sv->slowstart &&
1378 now.tv_sec >= sv->last_change) {
1379 unsigned int ratio;
1380 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001381 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001382 "<td>%d %%</td></tr>\n", ratio);
1383 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001384 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001385 "<td>-</td></tr>\n");
1386 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02001387 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001388 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
1389 "UP %d/%d,", "UP,",
1390 "NOLB %d/%d,", "NOLB,",
1391 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001392 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001393 /* pxid, name */
1394 "%s,%s,"
1395 /* queue : current, max */
1396 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001397 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001398 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001399 /* bytes : in, out */
1400 "%lld,%lld,"
1401 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001402 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001403 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001404 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001405 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001406 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001407 "",
1408 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001409 sv->nbpend, sv->counters.nbpend_max,
1410 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001411 sv->counters.bytes_in, sv->counters.bytes_out,
1412 sv->counters.failed_secu,
1413 sv->counters.failed_conns, sv->counters.failed_resp,
1414 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001415
Willy Tarreau55bb8452007-10-17 18:44:57 +02001416 /* status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001417 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001418 srv_hlt_st[sv_state],
1419 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
1420 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
Willy Tarreau91861262007-10-17 17:06:05 +02001421
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001422 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001423 /* weight, active, backup */
1424 "%d,%d,%d,"
1425 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001426 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001427 (sv->state & SRV_BACKUP) ? 0 : 1,
1428 (sv->state & SRV_BACKUP) ? 1 : 0);
1429
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001430 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001431 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001432 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02001433 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001434 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001435 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02001436 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001437 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001438 ",,,,");
1439
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001440 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001441 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01001442 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001443 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01001444 LIM2A0(sv->maxqueue, ""),
1445 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001446
1447 /* throttle */
1448 if ((sv->state & SRV_WARMINGUP) &&
1449 now.tv_sec < sv->last_change + sv->slowstart &&
1450 now.tv_sec >= sv->last_change) {
1451 unsigned int ratio;
1452 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001453 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001454 }
1455
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001456 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001457 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001458
1459 /* tracked */
1460 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001461 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001462 sv->tracked->proxy->id, sv->tracked->id);
1463 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001464 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001465
Willy Tarreau7f062c42009-03-05 18:43:00 +01001466 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001467 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01001468
1469 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001470 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001471 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001472 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01001473
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001474 if (sv->state & SRV_CHECKED) {
1475 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001476 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001477
1478 /* check_code */
1479 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001480 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001481 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001482 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001483
1484 /* check_duration */
1485 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001486 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001487 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001488 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001489
1490 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001491 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001492 }
1493
Willy Tarreau7f062c42009-03-05 18:43:00 +01001494 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001495 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001496 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001497 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001498 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001499 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02001500
1501 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
1502 /* fall through */
1503
1504 case DATA_ST_PX_BE:
1505 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001506 if ((px->cap & PR_CAP_BE) &&
1507 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
1508 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001509 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001510 /* name */
1511 "<tr align=center class=\"backend\"><td>Backend</td>"
1512 /* queue : current, max */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001513 "<td align=right>%s</td><td align=right>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001514 /* sessions rate : current, max, limit */
1515 "<td align=right>%s</td><td align=right>%s</td><td align=right></td>"
1516 "",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001517 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->counters.nbpend_max),
1518 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->counters.be_sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02001519
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001520 chunk_printf(&msg,
Willy Tarreaua3e49422009-05-10 19:19:41 +02001521 /* sessions : current, max, limit, total, lbtot */
1522 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001523 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001524 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001525 "<td align=right>%s</td><td align=right>%s</td>"
1526 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001527 U2H2(px->beconn), U2H3(px->counters.beconn_max), U2H4(px->fullconn),
1528 U2H6(px->counters.cum_beconn), U2H7(px->counters.cum_lbconn),
1529 U2H8(px->counters.bytes_in), U2H9(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001530
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001531 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001532 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001533 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001534 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001535 "<td align=right></td><td align=right>%s</td><td align=right>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001536 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001537 "<td align=right>%lld</td><td align=right>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001538 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02001539 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001540 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02001541 * active and backups. */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001542 "<td align=center nowrap>%s %s</td><td align=center>&nbsp;</td><td align=center>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001543 "<td align=center>%d</td><td align=center>%d</td>"
1544 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001545 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
1546 U2H2(px->counters.failed_conns), U2H3(px->counters.failed_resp),
1547 px->counters.retries, px->counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001548 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01001549 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
1550 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01001551 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001552 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001553
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001554 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001555 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001556 "<td align=center>&nbsp;</td><td align=\"right\">%d</td>"
1557 "<td align=\"right\" nowrap>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001558 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001559 "</tr>",
1560 px->down_trans,
1561 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001562 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001563 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001564 /* pxid, name */
1565 "%s,BACKEND,"
1566 /* queue : current, max */
1567 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001568 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001569 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001570 /* bytes : in, out */
1571 "%lld,%lld,"
1572 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001573 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001574 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001575 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001576 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001577 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001578 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02001579 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001580 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02001581 * active and backups. */
1582 "%s,"
1583 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001584 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001585 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001586 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001587 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001588 /* rate, rate_lim, rate_max, */
1589 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001590 /* check_status, check_code, check_duration */
1591 ",,,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001592 "\n",
1593 px->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001594 px->nbpend /* or px->totpend ? */, px->counters.nbpend_max,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001595 px->beconn, px->counters.beconn_max, px->fullconn, px->counters.cum_beconn,
1596 px->counters.bytes_in, px->counters.bytes_out,
1597 px->counters.denied_req, px->counters.denied_resp,
1598 px->counters.failed_conns, px->counters.failed_resp,
1599 px->counters.retries, px->counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01001600 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01001601 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001602 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001603 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01001604 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001605 relative_pid, px->uuid,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001606 px->counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001607 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001608 px->counters.be_sps_max);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001609 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001610 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001611 return 0;
1612 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001613
Willy Tarreau91861262007-10-17 17:06:05 +02001614 s->data_ctx.stats.px_st = DATA_ST_PX_END;
1615 /* fall through */
1616
1617 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001618 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001619 chunk_printf(&msg, "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001620
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001621 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001622 return 0;
1623 }
Willy Tarreau91861262007-10-17 17:06:05 +02001624
1625 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
1626 /* fall through */
1627
1628 case DATA_ST_PX_FIN:
1629 return 1;
1630
1631 default:
1632 /* unknown state, we should put an abort() here ! */
1633 return 1;
1634 }
1635}
1636
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001637
1638/* This function is called to send output to the response buffer.
1639 * It dumps the sessions states onto the output buffer <rep>.
1640 * Expects to be called with client socket shut down on input.
1641 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001642 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001643 */
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001644int stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001645{
1646 struct chunk msg;
1647
1648 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
1649 /* If we're forced to shut down, we might have to remove our
1650 * reference to the last session being dumped.
1651 */
1652 if (s->data_state == DATA_ST_LIST) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001653 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001654 LIST_DEL(&s->data_ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001655 LIST_INIT(&s->data_ctx.sess.bref.users);
1656 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001657 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001658 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001659 }
1660
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001661 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001662
1663 switch (s->data_state) {
1664 case DATA_ST_INIT:
1665 /* the function had not been called yet, let's prepare the
1666 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001667 * pointer to the first in the global list. When a target
1668 * session is being destroyed, it is responsible for updating
1669 * this pointer. We know we have reached the end when this
1670 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001671 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001672 LIST_INIT(&s->data_ctx.sess.bref.users);
1673 s->data_ctx.sess.bref.ref = sessions.n;
1674 s->data_state = DATA_ST_LIST;
1675 /* fall through */
1676
1677 case DATA_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001678 /* first, let's detach the back-ref from a possible previous session */
1679 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
1680 LIST_DEL(&s->data_ctx.sess.bref.users);
1681 LIST_INIT(&s->data_ctx.sess.bref.users);
1682 }
1683
1684 /* and start from where we stopped */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001685 while (s->data_ctx.sess.bref.ref != &sessions) {
1686 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1687 struct session *curr_sess;
1688
1689 curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list);
1690
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001691 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001692 "%p: proto=%s",
1693 curr_sess,
1694 curr_sess->listener->proto->name);
1695
1696 switch (curr_sess->listener->proto->sock_family) {
1697 case AF_INET:
1698 inet_ntop(AF_INET,
1699 (const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr,
1700 pn, sizeof(pn));
1701
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001702 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001703 " src=%s:%d fe=%s be=%s srv=%s",
1704 pn,
1705 ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port),
1706 curr_sess->fe->id,
1707 curr_sess->be->id,
1708 curr_sess->srv ? curr_sess->srv->id : "<none>"
1709 );
1710 break;
1711 case AF_INET6:
1712 inet_ntop(AF_INET6,
1713 (const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr,
1714 pn, sizeof(pn));
1715
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001716 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001717 " src=%s:%d fe=%s be=%s srv=%s",
1718 pn,
1719 ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port),
1720 curr_sess->fe->id,
1721 curr_sess->be->id,
1722 curr_sess->srv ? curr_sess->srv->id : "<none>"
1723 );
1724
1725 break;
1726 case AF_UNIX:
1727 /* no more information to print right now */
1728 break;
1729 }
1730
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001731 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02001732 " ts=%02x age=%s calls=%d",
1733 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01001734 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
1735 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001736
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001737 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001738 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
1739 curr_sess->req->flags,
1740 curr_sess->req->l,
1741 curr_sess->req->analysers,
1742 curr_sess->req->rex ?
1743 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
1744 TICKS_TO_MS(1000)) : "");
1745
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001746 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001747 ",wx=%s",
1748 curr_sess->req->wex ?
1749 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
1750 TICKS_TO_MS(1000)) : "");
1751
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001752 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001753 ",ax=%s]",
1754 curr_sess->req->analyse_exp ?
1755 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
1756 TICKS_TO_MS(1000)) : "");
1757
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001758 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001759 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
1760 curr_sess->rep->flags,
1761 curr_sess->rep->l,
1762 curr_sess->rep->analysers,
1763 curr_sess->rep->rex ?
1764 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
1765 TICKS_TO_MS(1000)) : "");
1766
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001767 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001768 ",wx=%s",
1769 curr_sess->rep->wex ?
1770 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
1771 TICKS_TO_MS(1000)) : "");
1772
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001773 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001774 ",ax=%s]",
1775 curr_sess->rep->analyse_exp ?
1776 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
1777 TICKS_TO_MS(1000)) : "");
1778
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001779 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001780 " s0=[%d,%1xh,fd=%d,ex=%s]",
1781 curr_sess->si[0].state,
1782 curr_sess->si[0].flags,
1783 curr_sess->si[0].fd,
1784 curr_sess->si[0].exp ?
1785 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
1786 TICKS_TO_MS(1000)) : "");
1787
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001788 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001789 " s1=[%d,%1xh,fd=%d,ex=%s]",
1790 curr_sess->si[1].state,
1791 curr_sess->si[1].flags,
1792 curr_sess->si[1].fd,
1793 curr_sess->si[1].exp ?
1794 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
1795 TICKS_TO_MS(1000)) : "");
1796
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001797 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001798 " exp=%s",
1799 curr_sess->task->expire ?
1800 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
1801 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01001802 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001803 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001804
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001805 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001806
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001807 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001808 /* let's try again later from this session. We add ourselves into
1809 * this session's users so that it can remove us upon termination.
1810 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001811 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001812 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001813 }
1814
1815 s->data_ctx.sess.bref.ref = curr_sess->list.n;
1816 }
1817 s->data_state = DATA_ST_FIN;
1818 /* fall through */
1819
1820 default:
1821 s->data_state = DATA_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001822 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001823 }
1824}
1825
Willy Tarreau74808cb2009-03-04 15:53:18 +01001826/* print a line of error buffer (limited to 70 bytes) to <out>. The format is :
1827 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
1828 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
1829 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
1830 * lines are respected within the limit of 70 output chars. Lines that are
1831 * continuation of a previous truncated line begin with "+" instead of " "
1832 * after the offset. The new pointer is returned.
1833 */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001834static int dump_error_line(struct chunk *out, struct error_snapshot *err,
1835 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01001836{
1837 int end;
1838 unsigned char c;
1839
1840 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001841 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01001842 return ptr;
1843
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001844 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01001845
Willy Tarreau61b34732009-10-03 23:49:35 +02001846 while (ptr < err->len && ptr < sizeof(err->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001847 c = err->buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01001848 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001849 if (out->len > end - 2)
1850 break;
1851 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01001852 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001853 if (out->len > end - 3)
1854 break;
1855 out->str[out->len++] = '\\';
1856 switch (c) {
1857 case '\t': c = 't'; break;
1858 case '\n': c = 'n'; break;
1859 case '\r': c = 'r'; break;
1860 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01001861 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001862 }
1863 out->str[out->len++] = c;
1864 } else {
1865 if (out->len > end - 5)
1866 break;
1867 out->str[out->len++] = '\\';
1868 out->str[out->len++] = 'x';
1869 out->str[out->len++] = hextab[(c >> 4) & 0xF];
1870 out->str[out->len++] = hextab[c & 0xF];
1871 }
1872 if (err->buf[ptr++] == '\n') {
1873 /* we had a line break, let's return now */
1874 out->str[out->len++] = '\n';
1875 *line = ptr;
1876 return ptr;
1877 }
1878 }
1879 /* we have an incomplete line, we return it as-is */
1880 out->str[out->len++] = '\n';
1881 return ptr;
1882}
1883
1884/* This function is called to send output to the response buffer.
1885 * It dumps the errors logged in proxies onto the output buffer <rep>.
1886 * Expects to be called with client socket shut down on input.
1887 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau61b34732009-10-03 23:49:35 +02001888 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau74808cb2009-03-04 15:53:18 +01001889 */
Willy Tarreau61b34732009-10-03 23:49:35 +02001890int stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau74808cb2009-03-04 15:53:18 +01001891{
1892 extern const char *monthname[12];
1893 struct chunk msg;
1894
Willy Tarreau61b34732009-10-03 23:49:35 +02001895 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW)))
1896 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001897
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001898 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01001899
1900 if (!s->data_ctx.errors.px) {
1901 /* the function had not been called yet, let's prepare the
1902 * buffer for a response.
1903 */
Willy Tarreau74808cb2009-03-04 15:53:18 +01001904 s->data_ctx.errors.px = proxy;
1905 s->data_ctx.errors.buf = 0;
1906 s->data_ctx.errors.bol = 0;
1907 s->data_ctx.errors.ptr = -1;
1908 }
1909
1910 /* we have two inner loops here, one for the proxy, the other one for
1911 * the buffer.
1912 */
1913 while (s->data_ctx.errors.px) {
1914 struct error_snapshot *es;
1915
1916 if (s->data_ctx.errors.buf == 0)
1917 es = &s->data_ctx.errors.px->invalid_req;
1918 else
1919 es = &s->data_ctx.errors.px->invalid_rep;
1920
1921 if (!es->when.tv_sec)
1922 goto next;
1923
1924 if (s->data_ctx.errors.iid >= 0 &&
1925 s->data_ctx.errors.px->uuid != s->data_ctx.errors.iid &&
1926 es->oe->uuid != s->data_ctx.errors.iid)
1927 goto next;
1928
1929 if (s->data_ctx.errors.ptr < 0) {
1930 /* just print headers now */
1931
1932 char pn[INET6_ADDRSTRLEN];
1933 struct tm tm;
1934
1935 get_localtime(es->when.tv_sec, &tm);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001936 chunk_printf(&msg, "\n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01001937 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001938 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01001939
1940
1941 if (es->src.ss_family == AF_INET)
1942 inet_ntop(AF_INET,
1943 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
1944 pn, sizeof(pn));
1945 else
1946 inet_ntop(AF_INET6,
1947 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
1948 pn, sizeof(pn));
1949
1950 switch (s->data_ctx.errors.buf) {
1951 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001952 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01001953 " frontend %s (#%d): invalid request\n"
1954 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
1955 " request length %d bytes, error at position %d:\n\n",
1956 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
1957 pn, es->sid, es->oe->id, es->oe->uuid,
1958 es->srv ? es->srv->id : "<NONE>",
1959 es->srv ? es->srv->puid : -1,
1960 es->len, es->pos);
1961 break;
1962 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001963 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01001964 " backend %s (#%d) : invalid response\n"
1965 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
1966 " response length %d bytes, error at position %d:\n\n",
1967 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
1968 pn, es->sid, es->oe->id, es->oe->uuid,
1969 es->srv ? es->srv->id : "<NONE>",
1970 es->srv ? es->srv->puid : -1,
1971 es->len, es->pos);
1972 break;
1973 }
1974
Willy Tarreau61b34732009-10-03 23:49:35 +02001975 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001976 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02001977 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001978 }
1979 s->data_ctx.errors.ptr = 0;
1980 s->data_ctx.errors.sid = es->sid;
1981 }
1982
1983 if (s->data_ctx.errors.sid != es->sid) {
1984 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001985 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01001986 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau61b34732009-10-03 23:49:35 +02001987 if (buffer_feed_chunk(rep, &msg) >= 0)
1988 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001989 goto next;
1990 }
1991
1992 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau61b34732009-10-03 23:49:35 +02001993 while (s->data_ctx.errors.ptr < es->len && s->data_ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001994 int newptr;
1995 int newline;
1996
1997 newline = s->data_ctx.errors.bol;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001998 newptr = dump_error_line(&msg, es, &newline, s->data_ctx.errors.ptr);
Willy Tarreau74808cb2009-03-04 15:53:18 +01001999 if (newptr == s->data_ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02002000 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002001
Willy Tarreau61b34732009-10-03 23:49:35 +02002002 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002003 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02002004 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002005 }
2006 s->data_ctx.errors.ptr = newptr;
2007 s->data_ctx.errors.bol = newline;
2008 };
2009 next:
2010 s->data_ctx.errors.bol = 0;
2011 s->data_ctx.errors.ptr = -1;
2012 s->data_ctx.errors.buf++;
2013 if (s->data_ctx.errors.buf > 1) {
2014 s->data_ctx.errors.buf = 0;
2015 s->data_ctx.errors.px = s->data_ctx.errors.px->next;
2016 }
2017 }
2018
2019 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02002020 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002021}
2022
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002023
Willy Tarreau10522fd2008-07-09 20:12:41 +02002024static struct cfg_kw_list cfg_kws = {{ },{
2025 { CFG_GLOBAL, "stats", stats_parse_global },
2026 { 0, NULL, NULL },
2027}};
2028
2029__attribute__((constructor))
2030static void __dumpstats_module_init(void)
2031{
2032 cfg_register_keywords(&cfg_kws);
2033}
2034
Willy Tarreau91861262007-10-17 17:06:05 +02002035/*
2036 * Local variables:
2037 * c-indent-level: 8
2038 * c-basic-offset: 8
2039 * End:
2040 */