blob: c861ee58a52b20e85ac80942f68c554357a7ec60 [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"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +020057 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020058 " 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 Tarreau38338fa2009-10-10 18:37:29 +020065 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +020066 " set weight : change a server's weight\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +010067 " set timeout : change a timeout setting\n"
Cyril Bontécd19e512010-01-31 22:34:03 +010068 " disable server : set a server in maintenance mode\n"
69 " enable server : re-enable a server that was previously in maintenance mode\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020070 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020071
Willy Tarreau6162db22009-10-10 17:13:00 +020072const char stats_permission_denied_msg[] =
73 "Permission denied\n"
74 "";
75
Willy Tarreaufbee7132007-10-18 13:53:22 +020076/* This function parses a "stats" statement in the "global" section. It returns
77 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
78 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
79 * zero included. The trailing '\n' must not be written. The function must be
80 * called with <args> pointing to the first word after "stats".
81 */
Willy Tarreau10522fd2008-07-09 20:12:41 +020082static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
83 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +020084{
Willy Tarreau10522fd2008-07-09 20:12:41 +020085 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +020086 if (!strcmp(args[0], "socket")) {
87 struct sockaddr_un su;
88 int cur_arg;
89
90 if (*args[1] == 0) {
91 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
92 return -1;
93 }
94
95 if (global.stats_sock.state != LI_NEW) {
96 snprintf(err, errlen, "'stats socket' already specified in global section");
97 return -1;
98 }
99
100 su.sun_family = AF_UNIX;
101 strncpy(su.sun_path, args[1], sizeof(su.sun_path));
102 su.sun_path[sizeof(su.sun_path) - 1] = 0;
103 memcpy(&global.stats_sock.addr, &su, sizeof(su)); // guaranteed to fit
104
Willy Tarreau89a63132009-08-16 17:41:45 +0200105 if (!global.stats_fe) {
106 if ((global.stats_fe = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) {
107 snprintf(err, errlen, "out of memory");
108 return -1;
109 }
110
111 LIST_INIT(&global.stats_fe->pendconns);
112 LIST_INIT(&global.stats_fe->acl);
113 LIST_INIT(&global.stats_fe->block_cond);
114 LIST_INIT(&global.stats_fe->redirect_rules);
115 LIST_INIT(&global.stats_fe->mon_fail_cond);
116 LIST_INIT(&global.stats_fe->switching_rules);
117 LIST_INIT(&global.stats_fe->tcp_req.inspect_rules);
118
119 /* Timeouts are defined as -1, so we cannot use the zeroed area
120 * as a default value.
121 */
122 proxy_reset_timeouts(global.stats_fe);
123
124 global.stats_fe->last_change = now.tv_sec;
125 global.stats_fe->id = strdup("GLOBAL");
126 global.stats_fe->cap = PR_CAP_FE;
127 }
128
Willy Tarreaufbee7132007-10-18 13:53:22 +0200129 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100130 global.stats_sock.options = LI_O_NONE;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200131 global.stats_sock.accept = uxst_event_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200132 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200133 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200134 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreau89a63132009-08-16 17:41:45 +0200135 global.stats_sock.private = global.stats_fe; /* must point to the frontend */
Willy Tarreau6162db22009-10-10 17:13:00 +0200136 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau89a63132009-08-16 17:41:45 +0200137
138 global.stats_fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
139 global.stats_sock.timeout = &global.stats_fe->timeout.client;
140
141 global.stats_sock.next = global.stats_fe->listen;
142 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200143
144 cur_arg = 2;
145 while (*args[cur_arg]) {
146 if (!strcmp(args[cur_arg], "uid")) {
147 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
148 cur_arg += 2;
149 }
150 else if (!strcmp(args[cur_arg], "gid")) {
151 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
152 cur_arg += 2;
153 }
154 else if (!strcmp(args[cur_arg], "mode")) {
155 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
156 cur_arg += 2;
157 }
158 else if (!strcmp(args[cur_arg], "user")) {
159 struct passwd *user;
160 user = getpwnam(args[cur_arg + 1]);
161 if (!user) {
162 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
163 args[cur_arg + 1]);
164 return -1;
165 }
166 global.stats_sock.perm.ux.uid = user->pw_uid;
167 cur_arg += 2;
168 }
169 else if (!strcmp(args[cur_arg], "group")) {
170 struct group *group;
171 group = getgrnam(args[cur_arg + 1]);
172 if (!group) {
173 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
174 args[cur_arg + 1]);
175 return -1;
176 }
177 global.stats_sock.perm.ux.gid = group->gr_gid;
178 cur_arg += 2;
179 }
Willy Tarreau6162db22009-10-10 17:13:00 +0200180 else if (!strcmp(args[cur_arg], "level")) {
181 if (!strcmp(args[cur_arg+1], "user"))
182 global.stats_sock.perm.ux.level = ACCESS_LVL_USER;
183 else if (!strcmp(args[cur_arg+1], "operator"))
184 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER;
185 else if (!strcmp(args[cur_arg+1], "admin"))
186 global.stats_sock.perm.ux.level = ACCESS_LVL_ADMIN;
187 else {
188 snprintf(err, errlen, "'stats socket level' only supports 'user', 'operator', and 'admin'");
189 return -1;
190 }
191 cur_arg += 2;
192 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200193 else {
Willy Tarreau6162db22009-10-10 17:13:00 +0200194 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', 'level', and 'mode'");
Willy Tarreaufbee7132007-10-18 13:53:22 +0200195 return -1;
196 }
197 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100198
Willy Tarreaufbee7132007-10-18 13:53:22 +0200199 uxst_add_listener(&global.stats_sock);
200 global.maxsock++;
201 }
202 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100203 unsigned timeout;
204 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
205
206 if (res) {
207 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
208 return -1;
209 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200210
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100211 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200212 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
213 return -1;
214 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200215 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200216 }
217 else if (!strcmp(args[0], "maxconn")) {
218 int maxconn = atol(args[1]);
219
220 if (maxconn <= 0) {
221 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
222 return -1;
223 }
224 global.maxsock -= global.stats_sock.maxconn;
225 global.stats_sock.maxconn = maxconn;
226 global.maxsock += global.stats_sock.maxconn;
227 }
228 else {
229 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
230 return -1;
231 }
232 return 0;
233}
234
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200235int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100236{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200237 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100238 "# pxname,svname,"
239 "qcur,qmax,"
240 "scur,smax,slim,stot,"
241 "bin,bout,"
242 "dreq,dresp,"
243 "ereq,econ,eresp,"
244 "wretr,wredis,"
245 "status,weight,act,bck,"
246 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200247 "pid,iid,sid,throttle,lbtot,tracked,type,"
248 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200249 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100250 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreaud9b587f2010-02-26 10:05:55 +0100251 "req_rate, req_rate_max, req_tot,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100252 "\n");
253}
254
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200255/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200256 * called from an applet running in a stream interface. The function returns 1
257 * if the request was understood, otherwise zero. It sets si->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200258 * designating the function which will have to process the request, which can
259 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200260 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200261int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200262{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200263 struct session *s = si->private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200264 char *args[MAX_STATS_ARGS + 1];
265 int arg;
266
267 while (isspace((unsigned char)*line))
268 line++;
269
270 arg = 0;
271 args[arg] = line;
272
273 while (*line && arg < MAX_STATS_ARGS) {
274 if (isspace((unsigned char)*line)) {
275 *line++ = '\0';
276
277 while (isspace((unsigned char)*line))
278 line++;
279
280 args[++arg] = line;
281 continue;
282 }
283
284 line++;
285 }
286
287 while (++arg <= MAX_STATS_ARGS)
288 args[arg] = line;
289
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200290 s->data_ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200291 if (strcmp(args[0], "show") == 0) {
292 if (strcmp(args[1], "stat") == 0) {
293 if (*args[2] && *args[3] && *args[4]) {
294 s->data_ctx.stats.flags |= STAT_BOUND;
295 s->data_ctx.stats.iid = atoi(args[2]);
296 s->data_ctx.stats.type = atoi(args[3]);
297 s->data_ctx.stats.sid = atoi(args[4]);
298 }
299
300 s->data_ctx.stats.flags |= STAT_SHOW_STAT;
301 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200302 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200303 si->st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200304 }
305 else if (strcmp(args[1], "info") == 0) {
306 s->data_ctx.stats.flags |= STAT_SHOW_INFO;
307 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200308 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200309 si->st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200310 }
311 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200312 s->data_state = DATA_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200313 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200314 s->data_ctx.cli.msg = stats_permission_denied_msg;
315 si->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200316 return 1;
317 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200318 si->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200319 }
320 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200321 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200322 s->data_ctx.cli.msg = stats_permission_denied_msg;
323 si->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200324 return 1;
325 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200326 if (*args[2])
327 s->data_ctx.errors.iid = atoi(args[2]);
328 else
329 s->data_ctx.errors.iid = -1;
330 s->data_ctx.errors.px = NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200331 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200332 si->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200333 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200334 else { /* neither "stat" nor "info" nor "sess" nor "errors"*/
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200335 return 0;
336 }
337 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200338 else if (strcmp(args[0], "clear") == 0) {
339 if (strcmp(args[1], "counters") == 0) {
340 struct proxy *px;
341 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200342 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200343 int clrall = 0;
344
345 if (strcmp(args[2], "all") == 0)
346 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200347
Willy Tarreau6162db22009-10-10 17:13:00 +0200348 /* check permissions */
349 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
350 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200351 s->data_ctx.cli.msg = stats_permission_denied_msg;
352 si->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200353 return 1;
354 }
355
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200356 for (px = proxy; px; px = px->next) {
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200357 if (clrall)
358 memset(&px->counters, 0, sizeof(px->counters));
359 else {
360 px->counters.feconn_max = 0;
361 px->counters.beconn_max = 0;
Willy Tarreaud9b587f2010-02-26 10:05:55 +0100362 px->counters.fe_rps_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200363 px->counters.fe_sps_max = 0;
364 px->counters.be_sps_max = 0;
365 px->counters.nbpend_max = 0;
366 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200367
368 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200369 if (clrall)
370 memset(&sv->counters, 0, sizeof(sv->counters));
371 else {
372 sv->counters.cur_sess_max = 0;
373 sv->counters.nbpend_max = 0;
374 sv->counters.sps_max = 0;
375 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200376
377 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200378 if (li->counters) {
379 if (clrall)
380 memset(li->counters, 0, sizeof(*li->counters));
381 else
382 li->counters->conn_max = 0;
383 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200384 }
385
386 return 1;
387 }
388 else {
389 return 0;
390 }
391 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200392 else if (strcmp(args[0], "get") == 0) {
393 if (strcmp(args[1], "weight") == 0) {
394 struct proxy *px;
395 struct server *sv;
396
397 /* split "backend/server" and make <line> point to server */
398 for (line = args[2]; *line; line++)
399 if (*line == '/') {
400 *line++ = '\0';
401 break;
402 }
403
404 if (!*line) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200405 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
406 si->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200407 return 1;
408 }
409
410 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200411 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
412 si->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200413 return 1;
414 }
415
416 /* return server's effective weight at the moment */
417 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
418 buffer_feed(si->ib, trash);
419 return 1;
420 }
421 else { /* not "get weight" */
422 return 0;
423 }
424 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200425 else if (strcmp(args[0], "set") == 0) {
426 if (strcmp(args[1], "weight") == 0) {
427 struct proxy *px;
428 struct server *sv;
429 int w;
430
431 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200432 s->data_ctx.cli.msg = stats_permission_denied_msg;
433 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200434 return 1;
435 }
436
437 /* split "backend/server" and make <line> point to server */
438 for (line = args[2]; *line; line++)
439 if (*line == '/') {
440 *line++ = '\0';
441 break;
442 }
443
444 if (!*line || !*args[3]) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200445 s->data_ctx.cli.msg = "Require 'backend/server' and 'weight' or 'weight%'.\n";
446 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200447 return 1;
448 }
449
450 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200451 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
452 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200453 return 1;
454 }
455
456 /* if the weight is terminated with '%', it is set relative to
457 * the initial weight, otherwise it is absolute.
458 */
459 w = atoi(args[3]);
460 if (strchr(args[3], '%') != NULL) {
461 if (w < 0 || w > 100) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200462 s->data_ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
463 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200464 return 1;
465 }
466 w = sv->iweight * w / 100;
467 }
468 else {
469 if (w < 0 || w > 256) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200470 s->data_ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
471 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200472 return 1;
473 }
474 }
475
476 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200477 s->data_ctx.cli.msg = "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
478 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200479 return 1;
480 }
481
482 sv->uweight = w;
483
484 if (px->lbprm.algo & BE_LB_PROP_DYN) {
485 /* we must take care of not pushing the server to full throttle during slow starts */
486 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
487 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
488 else
489 sv->eweight = BE_WEIGHT_SCALE;
490 sv->eweight *= sv->uweight;
491 } else {
492 sv->eweight = sv->uweight;
493 }
494
495 /* static LB algorithms are a bit harder to update */
496 if (px->lbprm.update_server_eweight)
497 px->lbprm.update_server_eweight(sv);
498 else if (sv->eweight)
499 px->lbprm.set_server_status_up(sv);
500 else
501 px->lbprm.set_server_status_down(sv);
502
503 return 1;
504 }
Willy Tarreau7aabd112010-01-26 10:59:06 +0100505 else if (strcmp(args[1], "timeout") == 0) {
506 if (strcmp(args[2], "cli") == 0) {
507 unsigned timeout;
508 const char *res;
509
510 if (!*args[3]) {
511 s->data_ctx.cli.msg = "Expects an integer value.\n";
512 si->st0 = STAT_CLI_PRINT;
513 return 1;
514 }
515
516 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
517 if (res || timeout < 1) {
518 s->data_ctx.cli.msg = "Invalid timeout value.\n";
519 si->st0 = STAT_CLI_PRINT;
520 return 1;
521 }
522
523 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
524 return 1;
525 }
526 else {
527 s->data_ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
528 si->st0 = STAT_CLI_PRINT;
529 return 1;
530 }
531 }
532 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +0200533 return 0;
534 }
535 }
Cyril Bontécd19e512010-01-31 22:34:03 +0100536 else if (strcmp(args[0], "enable") == 0) {
537 if (strcmp(args[1], "server") == 0) {
538 struct proxy *px;
539 struct server *sv;
540
541 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
542 s->data_ctx.cli.msg = stats_permission_denied_msg;
543 si->st0 = STAT_CLI_PRINT;
544 return 1;
545 }
546
547 /* split "backend/server" and make <line> point to server */
548 for (line = args[2]; *line; line++)
549 if (*line == '/') {
550 *line++ = '\0';
551 break;
552 }
553
554 if (!*line || !*args[2]) {
555 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
556 si->st0 = STAT_CLI_PRINT;
557 return 1;
558 }
559
560 if (!get_backend_server(args[2], line, &px, &sv)) {
561 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
562 si->st0 = STAT_CLI_PRINT;
563 return 1;
564 }
565
566 if (sv->state & SRV_MAINTAIN) {
567 /* The server is really in maintenance, we can change the server state */
568 if (sv->tracked) {
569 /* If this server tracks the status of another one,
570 * we must restore the good status.
571 */
572 if (sv->tracked->state & SRV_RUNNING) {
573 set_server_up(sv);
574 } else {
575 sv->state &= ~SRV_MAINTAIN;
576 set_server_down(sv);
577 }
578 } else {
579 set_server_up(sv);
580 }
581 }
582
583 return 1;
584 }
585 else { /* unknown "enable" parameter */
586 return 0;
587 }
588 }
589 else if (strcmp(args[0], "disable") == 0) {
590 if (strcmp(args[1], "server") == 0) {
591 struct proxy *px;
592 struct server *sv;
593
594 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
595 s->data_ctx.cli.msg = stats_permission_denied_msg;
596 si->st0 = STAT_CLI_PRINT;
597 return 1;
598 }
599
600 /* split "backend/server" and make <line> point to server */
601 for (line = args[2]; *line; line++)
602 if (*line == '/') {
603 *line++ = '\0';
604 break;
605 }
606
607 if (!*line || !*args[2]) {
608 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
609 si->st0 = STAT_CLI_PRINT;
610 return 1;
611 }
612
613 if (!get_backend_server(args[2], line, &px, &sv)) {
614 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
615 si->st0 = STAT_CLI_PRINT;
616 return 1;
617 }
618
619 if (! (sv->state & SRV_MAINTAIN)) {
620 /* Not already in maintenance, we can change the server state */
621 sv->state |= SRV_MAINTAIN;
622 set_server_down(sv);
623 }
624
625 return 1;
626 }
627 else { /* unknown "disable" parameter */
628 return 0;
629 }
630 }
631 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200632 return 0;
633 }
634 return 1;
635}
636
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200637/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200638 * used to processes I/O from/to the stats unix socket. The system relies on a
639 * state machine handling requests and various responses. We read a request,
640 * then we process it and send the response, and we possibly display a prompt.
641 * Then we can read again. The state is stored in si->st0 and is one of the
642 * STAT_CLI_* constants. si->st1 is used to indicate whether prompt is enabled
643 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200644 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200645void stats_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200646{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200647 struct session *s = si->private;
648 struct buffer *req = si->ob;
649 struct buffer *res = si->ib;
650 int reql;
651 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200652
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200653 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
654 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200655
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200656 while (1) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200657 if (si->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200658 /* Stats output not initialized yet */
659 memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats));
660 s->data_source = DATA_SRC_STATS;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200661 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200662 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200663 else if (si->st0 == STAT_CLI_END) {
664 /* Let's close for real now. We just close the request
665 * side, the conditions below will complete if needed.
666 */
667 si->shutw(si);
668 break;
669 }
670 else if (si->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +0200671 /* ensure we have some output room left in the event we
672 * would want to return some info right after parsing.
673 */
674 if (buffer_almost_full(si->ib))
675 break;
676
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200677 reql = buffer_si_peekline(si->ob, trash, sizeof(trash));
678 if (reql <= 0) { /* closed or EOL not found */
679 if (reql == 0)
680 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200681 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200682 continue;
683 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200684
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200685 /* seek for a possible semi-colon. If we find one, we
686 * replace it with an LF and skip only this part.
687 */
688 for (len = 0; len < reql; len++)
689 if (trash[len] == ';') {
690 trash[len] = '\n';
691 reql = len + 1;
692 break;
693 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200694
Willy Tarreau816fc222009-10-04 07:36:58 +0200695 /* now it is time to check that we have a full line,
696 * remove the trailing \n and possibly \r, then cut the
697 * line.
698 */
699 len = reql - 1;
700 if (trash[len] != '\n') {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200701 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200702 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200703 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200704
Willy Tarreau816fc222009-10-04 07:36:58 +0200705 if (len && trash[len-1] == '\r')
706 len--;
707
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200708 trash[len] = '\0';
709
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200710 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200711 if (len) {
712 if (strcmp(trash, "quit") == 0) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200713 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200714 continue;
715 }
716 else if (strcmp(trash, "prompt") == 0)
717 si->st1 = !si->st1;
718 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200719 !stats_sock_parse_request(si, trash)) {
720 s->data_ctx.cli.msg = stats_sock_usage_msg;
721 si->st0 = STAT_CLI_PRINT;
722 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200723 /* NB: stats_sock_parse_request() may have put
724 * another STAT_CLI_O_* into si->st0.
725 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200726 }
727 else if (!si->st1) {
728 /* if prompt is disabled, print help on empty lines,
729 * so that the user at least knows how to enable
730 * prompt and find help.
731 */
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200732 s->data_ctx.cli.msg = stats_sock_usage_msg;
733 si->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200734 }
735
736 /* re-adjust req buffer */
737 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200738 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200739 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200740 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200741 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200742 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200743 continue;
744 }
745
746 switch (si->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200747 case STAT_CLI_PRINT:
748 if (buffer_feed(si->ib, s->data_ctx.cli.msg) < 0)
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200749 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200750 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200751 case STAT_CLI_O_INFO:
Willy Tarreau24955a12009-10-04 11:54:04 +0200752 if (stats_dump_raw_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200753 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200754 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200755 case STAT_CLI_O_SESS:
Willy Tarreau7e72a8f2009-10-03 23:55:14 +0200756 if (stats_dump_sess_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200757 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200758 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200759 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau61b34732009-10-03 23:49:35 +0200760 if (stats_dump_errors_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200761 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200762 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200763 default: /* abnormal state */
764 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200765 break;
766 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200767
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200768 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
769 if (si->st0 == STAT_CLI_PROMPT) {
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200770 if (buffer_feed(si->ib, si->st1 ? "\n> " : "\n") < 0)
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200771 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200772 }
773
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200774 /* If the output functions are still there, it means they require more room. */
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200775 if (si->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200776 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200777
778 /* Now we close the output if one of the writers did so,
779 * or if we're not in interactive mode and the request
780 * buffer is empty. This still allows pipelined requests
781 * to be sent in non-interactive mode.
782 */
783 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->st1 && !req->send_max)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200784 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200785 continue;
786 }
787
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200788 /* switch state back to GETREQ to read next requests */
789 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200790 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200791 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200792
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200793 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200794 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
795 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
796 /* Other size has closed, let's abort if we have no more processing to do
797 * and nothing more to consume. This is comparable to a broken pipe, so
798 * we forward the close to the request side so that it flows upstream to
799 * the client.
800 */
801 si->shutw(si);
802 }
803
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200804 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200805 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
806 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
807 /* We have no more processing to do, and nothing more to send, and
808 * the client side has closed. So we'll forward this state downstream
809 * on the response buffer.
810 */
811 si->shutr(si);
812 res->flags |= BF_READ_NULL;
813 }
814
815 /* update all other flags and resync with the other side */
816 si->update(si);
817
818 /* we don't want to expire timeouts while we're processing requests */
819 si->ib->rex = TICK_ETERNITY;
820 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200821
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200822 out:
823 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
824 __FUNCTION__, __LINE__,
825 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
826
827 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
828 /* check that we have released everything then unregister */
829 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200830 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200831}
832
Willy Tarreau24955a12009-10-04 11:54:04 +0200833/* This function is called to send output to the response buffer.
834 * It dumps statistics onto the output buffer <rep> owned by session <s>.
835 * s->data_ctx must have been zeroed first, and the flags properly set.
836 * It returns 0 as long as it does not complete, non-zero upon completion.
837 * Some states are not used but it makes the code more similar to other
838 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200839 */
Willy Tarreau24955a12009-10-04 11:54:04 +0200840int stats_dump_raw_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3e76e722007-10-17 18:57:38 +0200841{
Willy Tarreau3e76e722007-10-17 18:57:38 +0200842 struct proxy *px;
843 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +0100844 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200845
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200846 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +0200847
848 switch (s->data_state) {
849 case DATA_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +0200850 /* the function had not been called yet */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200851 s->data_state = DATA_ST_HEAD;
852 /* fall through */
853
854 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100855 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200856 print_csv_header(&msg);
Willy Tarreau24955a12009-10-04 11:54:04 +0200857 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100858 return 0;
859 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200860
861 s->data_state = DATA_ST_INFO;
862 /* fall through */
863
864 case DATA_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +0100865 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100866 if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200867 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +0100868 "Name: " PRODUCT_NAME "\n"
869 "Version: " HAPROXY_VERSION "\n"
870 "Release_date: " HAPROXY_DATE "\n"
871 "Nbproc: %d\n"
872 "Process_num: %d\n"
873 "Pid: %d\n"
874 "Uptime: %dd %dh%02dm%02ds\n"
875 "Uptime_sec: %d\n"
876 "Memmax_MB: %d\n"
877 "Ulimit-n: %d\n"
878 "Maxsock: %d\n"
879 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100880 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100881 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100882 "PipesUsed: %d\n"
883 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100884 "Tasks: %d\n"
885 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200886 "node: %s\n"
887 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100888 "",
889 global.nbproc,
890 relative_pid,
891 pid,
892 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
893 up,
894 global.rlimit_memmax,
895 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100896 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100897 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200898 nb_tasks_cur, run_queue_cur,
899 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +0100900 );
Willy Tarreau24955a12009-10-04 11:54:04 +0200901 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100902 return 0;
903 }
904
Willy Tarreau3e76e722007-10-17 18:57:38 +0200905 s->data_ctx.stats.px = proxy;
906 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100907
908 s->data_ctx.stats.sv = NULL;
909 s->data_ctx.stats.sv_st = 0;
910
Willy Tarreau3e76e722007-10-17 18:57:38 +0200911 s->data_state = DATA_ST_LIST;
912 /* fall through */
913
914 case DATA_ST_LIST:
915 /* dump proxies */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100916 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100917 while (s->data_ctx.stats.px) {
918 px = s->data_ctx.stats.px;
919 /* skip the disabled proxies and non-networked ones */
920 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +0200921 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100922 if (stats_dump_proxy(s, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100923 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +0200924 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200925
Willy Tarreaua8efd362008-01-03 10:19:15 +0100926 s->data_ctx.stats.px = px->next;
927 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
928 }
929 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200930 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200931
932 s->data_state = DATA_ST_END;
933 /* fall through */
934
935 case DATA_ST_END:
936 s->data_state = DATA_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +0100937 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200938
939 case DATA_ST_FIN:
940 return 1;
941
942 default:
943 /* unknown state ! */
Willy Tarreau24955a12009-10-04 11:54:04 +0200944 s->data_state = DATA_ST_FIN;
945 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200946 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100947}
948
949
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200950/* This I/O handler runs as an applet embedded in a stream interface. It is
951 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
952 * si->st0 becomes non-zero once the transfer is finished. The handler
953 * automatically unregisters itself once transfer is complete.
954 */
955void http_stats_io_handler(struct stream_interface *si)
956{
957 struct session *s = si->private;
958 struct buffer *req = si->ob;
959 struct buffer *res = si->ib;
960
961 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
962 goto out;
963
964 /* check that the output is not closed */
965 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
966 si->st0 = 1;
967
968 if (!si->st0) {
969 if (stats_dump_http(s, res, s->be->uri_auth)) {
970 si->st0 = 1;
971 si->shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200972 }
973 }
974
975 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
976 si->shutw(si);
977
978 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->st0) {
979 si->shutr(si);
980 res->flags |= BF_READ_NULL;
981 }
982
983 /* update all other flags and resync with the other side */
984 si->update(si);
985
986 /* we don't want to expire timeouts while we're processing requests */
987 si->ib->rex = TICK_ETERNITY;
988 si->ob->wex = TICK_ETERNITY;
989
990 out:
991 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
992 /* check that we have released everything then unregister */
993 stream_int_unregister_handler(si);
994 }
995}
996
997
Willy Tarreau3e76e722007-10-17 18:57:38 +0200998/*
999 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +02001000 * client socket shut down on input. It stops by itself by unsetting the
Willy Tarreau72b179a2008-08-28 16:01:32 +02001001 * BF_HIJACK flag from the buffer, which it uses to keep on being called
Willy Tarreau1ae3a052008-08-16 10:56:30 +02001002 * when there is free space in the buffer, of simply by letting an empty buffer
1003 * upon return.s->data_ctx must have been zeroed before the first call, and the
1004 * flags set. It returns 0 if it had to stop writing data and an I/O is needed,
1005 * 1 if the dump is finished and the session must be closed, or -1 in case of
1006 * any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001007 */
Willy Tarreau0a464892008-12-07 18:30:00 +01001008int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001009{
Willy Tarreau91861262007-10-17 17:06:05 +02001010 struct proxy *px;
1011 struct chunk msg;
1012 unsigned int up;
1013
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001014 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001015
1016 switch (s->data_state) {
1017 case DATA_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001018 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001019 "HTTP/1.0 200 OK\r\n"
1020 "Cache-Control: no-cache\r\n"
1021 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001022 "Content-Type: %s\r\n",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001023 (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001024
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001025 if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001026 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001027 uri->refresh);
1028
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001029 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001030
1031 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001032 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001033 return 0;
1034
Willy Tarreau91861262007-10-17 17:06:05 +02001035 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1036 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1037 if (!(s->flags & SN_FINST_MASK))
1038 s->flags |= SN_FINST_R;
1039
1040 if (s->txn.meth == HTTP_METH_HEAD) {
1041 /* that's all we return in case of HEAD request */
1042 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001043 return 1;
1044 }
1045
1046 s->data_state = DATA_ST_HEAD; /* let's start producing data */
1047 /* fall through */
1048
1049 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001050 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001051 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001052 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001053 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1054 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001055 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001056 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1057 "<style type=\"text/css\"><!--\n"
1058 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001059 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001060 " font-size: 12px;"
1061 " font-weight: normal;"
1062 " color: black;"
1063 " background: white;"
1064 "}\n"
1065 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001066 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001067 "}\n"
1068 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001069 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001070 " margin-bottom: 0.5em;"
1071 "}\n"
1072 "h2 {"
1073 " font-family: helvetica, arial;"
1074 " font-size: x-large;"
1075 " font-weight: bold;"
1076 " font-style: italic;"
1077 " color: #6020a0;"
1078 " margin-top: 0em;"
1079 " margin-bottom: 0em;"
1080 "}\n"
1081 "h3 {"
1082 " font-family: helvetica, arial;"
1083 " font-size: 16px;"
1084 " font-weight: bold;"
1085 " color: #b00040;"
1086 " background: #e8e8d0;"
1087 " margin-top: 0em;"
1088 " margin-bottom: 0em;"
1089 "}\n"
1090 "li {"
1091 " margin-top: 0.25em;"
1092 " margin-right: 2em;"
1093 "}\n"
1094 ".hr {margin-top: 0.25em;"
1095 " border-color: black;"
1096 " border-bottom-style: solid;"
1097 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001098 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001099 ".total {background: #20D0D0;color: #ffff80;}\n"
1100 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001101 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001102 ".backend {background: #e8e8d0;}\n"
1103 ".active0 {background: #ff9090;}\n"
1104 ".active1 {background: #ffd020;}\n"
1105 ".active2 {background: #ffffa0;}\n"
1106 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001107 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1108 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1109 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001110 ".backup0 {background: #ff9090;}\n"
1111 ".backup1 {background: #ff80ff;}\n"
1112 ".backup2 {background: #c060ff;}\n"
1113 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001114 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1115 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1116 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001117 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001118 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001119 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001120 "a.px:link {color: #ffff40; text-decoration: none;}"
1121 "a.px:visited {color: #ffff40; text-decoration: none;}"
1122 "a.px:hover {color: #ffffff; text-decoration: none;}"
1123 "a.lfsb:link {color: #000000; text-decoration: none;}"
1124 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1125 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1126 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001127 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001128 "table.tbl td { text-align: right; border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray; white-space: nowrap;}\n"
1129 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001130 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001131 "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 +02001132 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001133 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001134 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001135 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1136 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1137 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
1138 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001139 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001140 (uri->flags&ST_SHNODE) ? " on " : "",
1141 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001142 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001143 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001144 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001145 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001146 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001147 return 0;
1148
1149 s->data_state = DATA_ST_INFO;
1150 /* fall through */
1151
1152 case DATA_ST_INFO:
1153 up = (now.tv_sec - start_date.tv_sec);
1154
1155 /* WARNING! this has to fit the first packet too.
1156 * We are around 3.5 kB, add adding entries will
1157 * become tricky if we want to support 4kB buffers !
1158 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001159 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001160 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001161 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1162 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001163 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001164 "<hr width=\"100%%\" class=\"hr\">\n"
1165 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001166 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001167 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001168 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001169 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1170 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
1171 "current conns = %d; current pipes = %d/%d<br>\n"
1172 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001173 "</td><td align=\"center\" nowrap>\n"
1174 "<table class=\"lgd\"><tr>\n"
1175 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1176 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1177 "</tr><tr>\n"
1178 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1179 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1180 "</tr><tr>\n"
1181 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1182 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1183 "</tr><tr>\n"
1184 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001185 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001186 "</tr><tr>\n"
1187 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001188 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001189 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001190 "</td>"
1191 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1192 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1193 "",
1194 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001195 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1196 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1197 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001198 up / 86400, (up % 86400) / 3600,
1199 (up % 3600) / 60, (up % 60),
1200 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1201 global.rlimit_memmax ? " MB" : "",
1202 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001203 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001204 actconn, pipes_used, pipes_used+pipes_free,
1205 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +02001206 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001207
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001208 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001209 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001210 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1211 uri->uri_prefix,
1212 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001213 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001214 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001215 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001216 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1217 uri->uri_prefix,
1218 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001219 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001220
Willy Tarreau55bb8452007-10-17 18:44:57 +02001221 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001222 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001223 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001224 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1225 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001226 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001227 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001228 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001229 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001230 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1231 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001232 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001233 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001234 }
Willy Tarreau91861262007-10-17 17:06:05 +02001235
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001236 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001237 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
1238 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001239 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
1240 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001241
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001242 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02001243 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
1244 uri->uri_prefix,
1245 (uri->refresh > 0) ? ";norefresh" : "");
1246
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001247 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001248 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001249 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1250 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
1251 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
1252 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
1253 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
1254 "</ul>"
1255 "</td>"
1256 "</tr></table>\n"
1257 ""
1258 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001259
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001260 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001261 return 0;
1262 }
Willy Tarreau91861262007-10-17 17:06:05 +02001263
Willy Tarreau91861262007-10-17 17:06:05 +02001264 s->data_ctx.stats.px = proxy;
1265 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1266 s->data_state = DATA_ST_LIST;
1267 /* fall through */
1268
1269 case DATA_ST_LIST:
1270 /* dump proxies */
1271 while (s->data_ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001272 if (buffer_almost_full(rep))
1273 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02001274 px = s->data_ctx.stats.px;
1275 /* skip the disabled proxies and non-networked ones */
1276 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001277 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001278 return 0;
1279
1280 s->data_ctx.stats.px = px->next;
1281 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1282 }
1283 /* here, we just have reached the last proxy */
1284
1285 s->data_state = DATA_ST_END;
1286 /* fall through */
1287
1288 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001289 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001290 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001291 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001292 return 0;
1293 }
Willy Tarreau91861262007-10-17 17:06:05 +02001294
1295 s->data_state = DATA_ST_FIN;
1296 /* fall through */
1297
1298 case DATA_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02001299 return 1;
1300
1301 default:
1302 /* unknown state ! */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001303 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001304 return -1;
1305 }
1306}
1307
1308
1309/*
1310 * Dumps statistics for a proxy.
1311 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1312 * ot non-zero if everything completed.
1313 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001314int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001315{
1316 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001317 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001318 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02001319 struct chunk msg;
1320
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001321 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001322
1323 switch (s->data_ctx.stats.px_st) {
1324 case DATA_ST_PX_INIT:
1325 /* we are on a new proxy */
1326
1327 if (uri && uri->scope) {
1328 /* we have a limited scope, we have to check the proxy name */
1329 struct stat_scope *scope;
1330 int len;
1331
1332 len = strlen(px->id);
1333 scope = uri->scope;
1334
1335 while (scope) {
1336 /* match exact proxy name */
1337 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1338 break;
1339
1340 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001341 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001342 break;
1343 scope = scope->next;
1344 }
1345
1346 /* proxy name not found : don't dump anything */
1347 if (scope == NULL)
1348 return 1;
1349 }
1350
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001351 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001352 (px->uuid != s->data_ctx.stats.iid))
1353 return 1;
1354
Willy Tarreau91861262007-10-17 17:06:05 +02001355 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
1356 /* fall through */
1357
1358 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001359 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001360 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001361 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001362 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001363 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001364 "<th class=\"pxname\" width=\"10%%\"");
1365
1366 if (uri->flags&ST_SHLGNDS) {
1367 /* cap, mode, id */
1368 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
1369 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1370 px->uuid);
1371
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001372 chunk_printf(&msg, "\"");
1373 }
1374
1375 chunk_printf(&msg,
1376 "><a name=\"%s\"></a>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001377 "<a class=px href=\"#%s\">%s</a></th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001378 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001379 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001380 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001381 "<table class=\"tbl\" width=\"100%%\">\n"
1382 "<tr class=\"titre\">"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001383 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001384 "<th colspan=3>Queue</th>"
1385 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001386 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001387 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001388 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001389 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001390 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001391 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001392 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001393 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001394 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001395 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001396 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001397 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1398 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001399 "</tr>",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001400 px->id, px->id, px->id,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001401 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001402
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001403 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001404 return 0;
1405 }
Willy Tarreau91861262007-10-17 17:06:05 +02001406
1407 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
1408 /* fall through */
1409
1410 case DATA_ST_PX_FE:
1411 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001412 if ((px->cap & PR_CAP_FE) &&
1413 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
1414 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001415 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001416 /* name, queue */
Krzysztof Piotr Oledzki516ed492009-10-22 22:48:09 +02001417 "<tr class=\"frontend\"><td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001418 "<a name=\"%s/Frontend\"></a>"
1419 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td><td colspan=3></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001420 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001421 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001422 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001423 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001424 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001425 "",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001426 px->id, px->id,
Willy Tarreaua3e49422009-05-10 19:19:41 +02001427 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001428 U2H1(px->counters.fe_sps_max), LIM2A2(px->fe_sps_lim, "-"),
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001429 U2H3(px->feconn), U2H4(px->counters.feconn_max), U2H5(px->maxconn));
1430
1431 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1432 if (px->mode == PR_MODE_HTTP) {
1433 int i;
1434
1435 chunk_printf(&msg, " title=\"rsp codes:");
1436
1437 for (i = 1; i < 6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01001438 chunk_printf(&msg, " %dxx=%lld,", i, px->counters.fe.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001439
Willy Tarreau24657792010-02-26 10:30:28 +01001440 chunk_printf(&msg, " other=%lld\"", px->counters.fe.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001441 }
1442
1443 chunk_printf(&msg,
1444 /* sessions: total, lbtot */
1445 ">%s</td><td></td>"
1446 /* bytes : in, out */
1447 "<td>%s</td><td>%s</td>"
1448 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001449 U2H6(px->counters.cum_feconn), U2H7(px->counters.bytes_in), U2H8(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001450
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001451 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001452 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001453 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001454 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001455 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001456 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001457 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001458 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001459 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001460 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001461 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02001462 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001463 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
1464 U2H2(px->counters.failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +02001465 px->state == PR_STRUN ? "OPEN" :
1466 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001467 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001468 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001469 /* pxid, name, queue cur, queue max, */
1470 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001471 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001472 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001473 /* bytes : in, out */
1474 "%lld,%lld,"
1475 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001476 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001477 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001478 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001479 /* warnings: retries, redispatches */
1480 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001481 /* server status : reflect frontend status */
1482 "%s,"
1483 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001484 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001485 /* pid, iid, sid, throttle, lbtot, tracked, type */
1486 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001487 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001488 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001489 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001490 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02001491 px->id,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001492 px->feconn, px->counters.feconn_max, px->maxconn, px->counters.cum_feconn,
1493 px->counters.bytes_in, px->counters.bytes_out,
1494 px->counters.denied_req, px->counters.denied_resp,
1495 px->counters.failed_req,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001496 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +01001497 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01001498 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001499 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001500 px->fe_sps_lim, px->counters.fe_sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001501
1502 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1503 if (px->mode == PR_MODE_HTTP) {
1504 int i;
1505
1506 for (i=1; i<6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01001507 chunk_printf(&msg, "%lld,", px->counters.fe.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001508
Willy Tarreau24657792010-02-26 10:30:28 +01001509 chunk_printf(&msg, "%lld,", px->counters.fe.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001510 } else {
1511 chunk_printf(&msg, ",,,,,,");
1512 }
1513
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001514 /* failed health analyses */
1515 chunk_printf(&msg, ",");
1516
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001517 /* requests : req_rate, req_rate_max, req_tot, */
1518 chunk_printf(&msg, "%u,%u,%lld,",
1519 read_freq_ctr(&px->fe_req_per_sec),
1520 px->counters.fe_rps_max, px->counters.cum_fe_req);
1521
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001522 /* finish with EOL */
1523 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001524 }
Willy Tarreau91861262007-10-17 17:06:05 +02001525
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001526 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001527 return 0;
1528 }
1529
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001530 s->data_ctx.stats.l = px->listen; /* may be NULL */
1531 s->data_ctx.stats.px_st = DATA_ST_PX_LI;
1532 /* fall through */
1533
1534 case DATA_ST_PX_LI:
1535 /* stats.l has been initialized above */
1536 for (; s->data_ctx.stats.l != NULL; s->data_ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001537 if (buffer_almost_full(rep))
1538 return 0;
1539
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001540 l = s->data_ctx.stats.l;
1541 if (!l->counters)
1542 continue;
1543
1544 if (s->data_ctx.stats.flags & STAT_BOUND) {
1545 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SO)))
1546 break;
1547
1548 if (s->data_ctx.stats.sid != -1 && l->luid != s->data_ctx.stats.sid)
1549 continue;
1550 }
1551
1552 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001553 chunk_printf(&msg, "<tr class=socket><td class=ac");
1554
1555 if (uri->flags&ST_SHLGNDS) {
1556 char str[INET6_ADDRSTRLEN], *fmt = NULL;
1557 int port;
1558
1559 chunk_printf(&msg, " title=\"IP: ");
1560
1561 port = (l->addr.ss_family == AF_INET6)
1562 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
1563 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
1564
1565 if (l->addr.ss_family == AF_INET) {
1566 if (inet_ntop(AF_INET,
1567 (const void *)&((struct sockaddr_in *)&l->addr)->sin_addr,
1568 str, sizeof(str)))
1569 fmt = "%s:%d";
1570 } else {
1571 if (inet_ntop(AF_INET6,
1572 (const void *)&((struct sockaddr_in6 *)(&l->addr))->sin6_addr,
1573 str, sizeof(str)))
1574 fmt = "[%s]:%d";
1575 }
1576
1577 if (fmt)
1578 chunk_printf(&msg, fmt, str, port);
1579 else
1580 chunk_printf(&msg, "(%s)", strerror(errno));
1581
1582 /* id */
1583 chunk_printf(&msg, ", id: %d", l->luid);
1584
1585 chunk_printf(&msg, "\"");
1586 }
1587
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001588 chunk_printf(&msg,
1589 /* name, queue */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001590 "><a name=\"%s/+%s\"></a>"
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01001591 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001592 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001593 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001594 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001595 "<td>%s</td><td>%s</td><td>%s</td>"
1596 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001597 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001598 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001599 "",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01001600 px->id, l->name, px->id, l->name, l->name,
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001601 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
1602 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
1603
1604 chunk_printf(&msg,
1605 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001606 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001607 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001608 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001609 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001610 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001611 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001612 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001613 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001614 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001615 "",
1616 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
1617 U2H2(l->counters->failed_req),
1618 (l->nbconn < l->maxconn) ? "OPEN" : "FULL");
1619 } else {
1620 chunk_printf(&msg,
1621 /* pxid, name, queue cur, queue max, */
1622 "%s,%s,,,"
1623 /* sessions: current, max, limit, total */
1624 "%d,%d,%d,%lld,"
1625 /* bytes: in, out */
1626 "%lld,%lld,"
1627 /* denied: req, resp */
1628 "%lld,%lld,"
1629 /* errors: request, connect, response */
1630 "%lld,,,"
1631 /* warnings: retries, redispatches */
1632 ",,"
1633 /* server status: reflect listener status */
1634 "%s,"
1635 /* rest of server: nothing */
1636 ",,,,,,,,"
1637 /* pid, iid, sid, throttle, lbtot, tracked, type */
1638 "%d,%d,%d,,,,%d,"
1639 /* rate, rate_lim, rate_max */
1640 ",,,"
1641 /* check_status, check_code, check_duration */
1642 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001643 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1644 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001645 /* failed health analyses */
1646 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001647 /* requests : req_rate, req_rate_max, req_tot, */
1648 ",,,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001649 "\n",
1650 px->id, l->name,
1651 l->nbconn, l->counters->conn_max,
1652 l->maxconn, l->counters->cum_conn,
1653 l->counters->bytes_in, l->counters->bytes_out,
1654 l->counters->denied_req, l->counters->denied_resp,
1655 l->counters->failed_req,
1656 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
1657 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
1658 }
1659
1660 if (buffer_feed_chunk(rep, &msg) >= 0)
1661 return 0;
1662 }
1663
Willy Tarreau91861262007-10-17 17:06:05 +02001664 s->data_ctx.stats.sv = px->srv; /* may be NULL */
1665 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
1666 /* fall through */
1667
1668 case DATA_ST_PX_SV:
1669 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001670 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001671 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 +02001672
Willy Tarreau4e33d862009-10-11 23:35:10 +02001673 if (buffer_almost_full(rep))
1674 return 0;
1675
Willy Tarreau91861262007-10-17 17:06:05 +02001676 sv = s->data_ctx.stats.sv;
1677
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001678 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001679 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
1680 break;
1681
1682 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
1683 continue;
1684 }
1685
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001686 if (sv->tracked)
1687 svs = sv->tracked;
1688 else
1689 svs = sv;
1690
Willy Tarreau91861262007-10-17 17:06:05 +02001691 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001692 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01001693 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001694 else if (svs->state & SRV_RUNNING) {
1695 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02001696 sv_state = 3; /* UP */
1697 else
1698 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01001699
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001700 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01001701 sv_state += 2;
1702 }
Willy Tarreau91861262007-10-17 17:06:05 +02001703 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001704 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02001705 sv_state = 1; /* going up */
1706 else
1707 sv_state = 0; /* DOWN */
1708
Cyril Bonté0dae5852010-02-03 00:26:28 +01001709 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02001710 /* do not report servers which are DOWN */
1711 s->data_ctx.stats.sv = sv->next;
1712 continue;
1713 }
1714
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001715 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001716 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
1717 "UP %d/%d &darr;", "UP",
1718 "NOLB %d/%d &darr;", "NOLB",
1719 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01001720 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001721 chunk_printf(&msg,
1722 /* name */
1723 "<tr class=\"maintain\"><td class=ac"
1724 );
1725 }
1726 else {
1727 chunk_printf(&msg,
1728 /* name */
1729 "<tr class=\"%s%d\"><td class=ac",
1730 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
1731 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001732
1733 if (uri->flags&ST_SHLGNDS) {
1734 char str[INET6_ADDRSTRLEN];
1735
1736 chunk_printf(&msg, " title=\"IP: ");
1737
1738 /* IP */
1739 if (inet_ntop(sv->addr.sin_family, &sv->addr.sin_addr, str, sizeof(str)))
1740 chunk_printf(&msg, "%s:%d", str, htons(sv->addr.sin_port));
1741 else
1742 chunk_printf(&msg, "(%s)", strerror(errno));
1743
1744 /* id */
1745 chunk_printf(&msg, ", id: %d", sv->puid);
1746
1747 /* cookie */
1748 if (sv->cookie) {
1749 struct chunk src;
1750
1751 chunk_printf(&msg, ", cookie: '");
1752
1753 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
1754 chunk_htmlencode(&msg, &src);
1755
1756 chunk_printf(&msg, "'");
1757 }
1758
1759 chunk_printf(&msg, "\"");
1760 }
1761
1762 chunk_printf(&msg,
1763 "><a name=\"%s/%s\"></a>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001764 "<a class=lfsb href=\"#%s/%s\">%s</a></td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001765 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001766 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001767 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001768 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001769 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001770 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001771 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001772 "",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001773 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001774 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
1775 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001776 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
1777
1778 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1779 if (px->mode == PR_MODE_HTTP) {
1780 int i;
1781
1782 chunk_printf(&msg, " title=\"rsp codes:");
1783
1784 for (i = 1; i < 6; i++)
1785 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
1786
1787 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
1788 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001789
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001790 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001791 /* sessions: total, lbtot */
1792 ">%s</td><td>%s</td>",
1793 U2H0(sv->counters.cum_sess), U2H1(sv->counters.cum_lbconn));
1794
1795 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001796 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001797 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001798 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001799 "<td></td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001800 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001801 "<td></td><td>%s</td><td>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001802 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001803 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001804 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001805 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
1806 U2H2(sv->counters.failed_secu),
1807 U2H3(sv->counters.failed_conns), U2H4(sv->counters.failed_resp),
1808 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001809
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001810 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001811 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001812
Cyril Bontécd19e512010-01-31 22:34:03 +01001813 if (sv->state & SRV_MAINTAIN) {
1814 chunk_printf(&msg, "%s ",
1815 human_time(now.tv_sec - sv->last_change, 1));
1816 chunk_printf(&msg, "MAINT");
1817 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01001818 else if (svs != sv && svs->state & SRV_MAINTAIN) {
1819 chunk_printf(&msg, "%s ",
1820 human_time(now.tv_sec - svs->last_change, 1));
1821 chunk_printf(&msg, "MAINT(via)");
1822 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001823 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001824 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01001825 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001826
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001827 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001828 srv_hlt_st[sv_state],
1829 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
1830 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001831 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001832
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001833 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001834 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001835 get_check_status_description(sv->check_status));
1836
1837 if (*sv->check_desc) {
1838 struct chunk src;
1839
1840 chunk_printf(&msg, ": ");
1841
1842 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
1843 chunk_htmlencode(&msg, &src);
1844 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001845
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001846 chunk_printf(&msg, "\"> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001847 tv_iszero(&sv->check_start)?"":"* ",
1848 get_check_status_info(sv->check_status));
1849
1850 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001851 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001852
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001853 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001854 chunk_printf(&msg, " in %lums", sv->check_duration);
1855 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001856 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02001857
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001858 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001859 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001860 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001861 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001862 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001863 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001864 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02001865 (sv->state & SRV_BACKUP) ? "-" : "Y",
1866 (sv->state & SRV_BACKUP) ? "Y" : "-");
1867
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001868 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01001869 if (sv->state & SRV_CHECKED) {
1870 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\">%lld",
1871 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
1872
1873 if (svs->observe)
1874 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
1875
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001876 chunk_printf(&msg,
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01001877 "</td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001878 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001879 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001880 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01001881 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001882 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01001883 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
1884 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001885 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001886 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001887 "<td colspan=3></td>");
1888
1889 /* throttle */
1890 if ((sv->state & SRV_WARMINGUP) &&
1891 now.tv_sec < sv->last_change + sv->slowstart &&
1892 now.tv_sec >= sv->last_change) {
1893 unsigned int ratio;
1894 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001895 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001896 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001897 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001898 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001899 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001900 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02001901 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001902 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
1903 "UP %d/%d,", "UP,",
1904 "NOLB %d/%d,", "NOLB,",
1905 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001906 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001907 /* pxid, name */
1908 "%s,%s,"
1909 /* queue : current, max */
1910 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001911 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001912 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001913 /* bytes : in, out */
1914 "%lld,%lld,"
1915 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001916 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001917 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001918 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001919 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001920 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001921 "",
1922 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001923 sv->nbpend, sv->counters.nbpend_max,
1924 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001925 sv->counters.bytes_in, sv->counters.bytes_out,
1926 sv->counters.failed_secu,
1927 sv->counters.failed_conns, sv->counters.failed_resp,
1928 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001929
Willy Tarreau55bb8452007-10-17 18:44:57 +02001930 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01001931 if (sv->state & SRV_MAINTAIN) {
1932 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01001933 }
1934 else if (svs != sv && svs->state & SRV_MAINTAIN) {
1935 chunk_printf(&msg, "MAINT(via),");
1936 }
1937 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01001938 chunk_printf(&msg,
1939 srv_hlt_st[sv_state],
1940 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
1941 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
1942 }
Willy Tarreau91861262007-10-17 17:06:05 +02001943
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001944 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001945 /* weight, active, backup */
1946 "%d,%d,%d,"
1947 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001948 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001949 (sv->state & SRV_BACKUP) ? 0 : 1,
1950 (sv->state & SRV_BACKUP) ? 1 : 0);
1951
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001952 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001953 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001954 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02001955 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001956 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001957 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02001958 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001959 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001960 ",,,,");
1961
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001962 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001963 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01001964 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001965 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01001966 LIM2A0(sv->maxqueue, ""),
1967 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001968
1969 /* throttle */
1970 if ((sv->state & SRV_WARMINGUP) &&
1971 now.tv_sec < sv->last_change + sv->slowstart &&
1972 now.tv_sec >= sv->last_change) {
1973 unsigned int ratio;
1974 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001975 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001976 }
1977
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001978 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001979 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001980
1981 /* tracked */
1982 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001983 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001984 sv->tracked->proxy->id, sv->tracked->id);
1985 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001986 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001987
Willy Tarreau7f062c42009-03-05 18:43:00 +01001988 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001989 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01001990
1991 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001992 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001993 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001994 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01001995
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001996 if (sv->state & SRV_CHECKED) {
1997 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001998 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001999
2000 /* check_code */
2001 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002002 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002003 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002004 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002005
2006 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002007 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002008 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002009 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002010 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002011
2012 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002013 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002014 }
2015
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002016 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2017 if (px->mode == PR_MODE_HTTP) {
2018 int i;
2019
2020 for (i=1; i<6; i++)
2021 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2022
2023 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2024 } else {
2025 chunk_printf(&msg, ",,,,,,");
2026 }
2027
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002028 /* failed health analyses */
2029 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2030
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002031 /* requests : req_rate, req_rate_max, req_tot, */
2032 chunk_printf(&msg, ",,,");
2033
Willy Tarreau7f062c42009-03-05 18:43:00 +01002034 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002035 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002036 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002037 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002038 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002039 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002040
2041 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
2042 /* fall through */
2043
2044 case DATA_ST_PX_BE:
2045 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002046 if ((px->cap & PR_CAP_BE) &&
2047 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2048 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002049 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002050 /* name */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002051 "<tr class=\"backend\"><td class=ac");
2052
2053 if (uri->flags&ST_SHLGNDS) {
2054 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002055 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002056 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002057
2058 /* cookie */
2059 if (px->cookie_name) {
2060 struct chunk src;
2061
2062 chunk_printf(&msg, ", cookie: '");
2063
2064 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2065 chunk_htmlencode(&msg, &src);
2066
2067 chunk_printf(&msg, "'");
2068 }
2069
2070 chunk_printf(&msg, "\"");
2071
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002072 }
2073
2074 chunk_printf(&msg,
2075 "><a name=\"%s/Backend\"></a>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002076 "<a class=lfsb href=\"#%s/Backend\">Backend</a></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002077 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002078 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002079 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002080 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002081 "",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002082 px->id, px->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002083 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->counters.nbpend_max),
2084 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->counters.be_sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02002085
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002086 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002087 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002088 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002089 "<td"
2090 "",
2091 U2H2(px->beconn), U2H3(px->counters.beconn_max), U2H4(px->fullconn));
2092
2093 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2094 if (px->mode == PR_MODE_HTTP) {
2095 int i;
2096
2097 chunk_printf(&msg, " title=\"rsp codes:");
2098
2099 for (i = 1; i < 6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01002100 chunk_printf(&msg, " %dxx=%lld", i, px->counters.be.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002101
Willy Tarreau24657792010-02-26 10:30:28 +01002102 chunk_printf(&msg, " other=%lld\"", px->counters.be.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002103 }
2104
2105 chunk_printf(&msg,
2106 /* sessions: total, lbtot */
2107 ">%s</td><td>%s</td>"
2108 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002109 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002110 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002111 U2H6(px->counters.cum_beconn), U2H7(px->counters.cum_lbconn),
2112 U2H8(px->counters.bytes_in), U2H9(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002113
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002114 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002115 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002116 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002117 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002118 "<td></td><td>%s</td><td>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002119 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002120 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002121 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02002122 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002123 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02002124 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002125 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2126 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002127 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002128 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
2129 U2H2(px->counters.failed_conns), U2H3(px->counters.failed_resp),
2130 px->counters.retries, px->counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002131 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01002132 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2133 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01002134 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002135 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002136
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002137 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002138 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002139 "<td class=ac>&nbsp;</td><td>%d</td>"
2140 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002141 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002142 "</tr>",
2143 px->down_trans,
2144 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002145 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002146 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002147 /* pxid, name */
2148 "%s,BACKEND,"
2149 /* queue : current, max */
2150 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002151 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002152 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002153 /* bytes : in, out */
2154 "%lld,%lld,"
2155 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002156 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002157 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002158 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002159 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002160 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002161 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02002162 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002163 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02002164 * active and backups. */
2165 "%s,"
2166 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002167 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002168 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002169 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002170 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002171 /* rate, rate_lim, rate_max, */
2172 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002173 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002174 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002175 px->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002176 px->nbpend /* or px->totpend ? */, px->counters.nbpend_max,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002177 px->beconn, px->counters.beconn_max, px->fullconn, px->counters.cum_beconn,
2178 px->counters.bytes_in, px->counters.bytes_out,
2179 px->counters.denied_req, px->counters.denied_resp,
2180 px->counters.failed_conns, px->counters.failed_resp,
2181 px->counters.retries, px->counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01002182 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01002183 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002184 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002185 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01002186 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002187 relative_pid, px->uuid,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002188 px->counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002189 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002190 px->counters.be_sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002191
2192 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2193 if (px->mode == PR_MODE_HTTP) {
2194 int i;
2195
2196 for (i=1; i<6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01002197 chunk_printf(&msg, "%lld,", px->counters.be.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002198
Willy Tarreau24657792010-02-26 10:30:28 +01002199 chunk_printf(&msg, "%lld,", px->counters.be.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002200 } else {
2201 chunk_printf(&msg, ",,,,,,");
2202 }
2203
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002204 /* failed health analyses */
2205 chunk_printf(&msg, ",");
2206
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002207 /* requests : req_rate, req_rate_max, req_tot, */
2208 chunk_printf(&msg, ",,,");
2209
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002210 /* finish with EOL */
2211 chunk_printf(&msg, "\n");
2212
Willy Tarreau55bb8452007-10-17 18:44:57 +02002213 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002214 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002215 return 0;
2216 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002217
Willy Tarreau91861262007-10-17 17:06:05 +02002218 s->data_ctx.stats.px_st = DATA_ST_PX_END;
2219 /* fall through */
2220
2221 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002222 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002223 chunk_printf(&msg, "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02002224
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002225 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002226 return 0;
2227 }
Willy Tarreau91861262007-10-17 17:06:05 +02002228
2229 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
2230 /* fall through */
2231
2232 case DATA_ST_PX_FIN:
2233 return 1;
2234
2235 default:
2236 /* unknown state, we should put an abort() here ! */
2237 return 1;
2238 }
2239}
2240
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002241
2242/* This function is called to send output to the response buffer.
2243 * It dumps the sessions states onto the output buffer <rep>.
2244 * Expects to be called with client socket shut down on input.
2245 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002246 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002247 */
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002248int stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002249{
2250 struct chunk msg;
2251
2252 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
2253 /* If we're forced to shut down, we might have to remove our
2254 * reference to the last session being dumped.
2255 */
2256 if (s->data_state == DATA_ST_LIST) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002257 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002258 LIST_DEL(&s->data_ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002259 LIST_INIT(&s->data_ctx.sess.bref.users);
2260 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002261 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002262 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002263 }
2264
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002265 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002266
2267 switch (s->data_state) {
2268 case DATA_ST_INIT:
2269 /* the function had not been called yet, let's prepare the
2270 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002271 * pointer to the first in the global list. When a target
2272 * session is being destroyed, it is responsible for updating
2273 * this pointer. We know we have reached the end when this
2274 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002275 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002276 LIST_INIT(&s->data_ctx.sess.bref.users);
2277 s->data_ctx.sess.bref.ref = sessions.n;
2278 s->data_state = DATA_ST_LIST;
2279 /* fall through */
2280
2281 case DATA_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002282 /* first, let's detach the back-ref from a possible previous session */
2283 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
2284 LIST_DEL(&s->data_ctx.sess.bref.users);
2285 LIST_INIT(&s->data_ctx.sess.bref.users);
2286 }
2287
2288 /* and start from where we stopped */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002289 while (s->data_ctx.sess.bref.ref != &sessions) {
2290 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
2291 struct session *curr_sess;
2292
2293 curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list);
2294
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002295 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002296 "%p: proto=%s",
2297 curr_sess,
2298 curr_sess->listener->proto->name);
2299
2300 switch (curr_sess->listener->proto->sock_family) {
2301 case AF_INET:
2302 inet_ntop(AF_INET,
2303 (const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr,
2304 pn, sizeof(pn));
2305
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002306 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002307 " src=%s:%d fe=%s be=%s srv=%s",
2308 pn,
2309 ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port),
2310 curr_sess->fe->id,
2311 curr_sess->be->id,
2312 curr_sess->srv ? curr_sess->srv->id : "<none>"
2313 );
2314 break;
2315 case AF_INET6:
2316 inet_ntop(AF_INET6,
2317 (const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr,
2318 pn, sizeof(pn));
2319
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002320 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002321 " src=%s:%d fe=%s be=%s srv=%s",
2322 pn,
2323 ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port),
2324 curr_sess->fe->id,
2325 curr_sess->be->id,
2326 curr_sess->srv ? curr_sess->srv->id : "<none>"
2327 );
2328
2329 break;
2330 case AF_UNIX:
2331 /* no more information to print right now */
2332 break;
2333 }
2334
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002335 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02002336 " ts=%02x age=%s calls=%d",
2337 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01002338 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
2339 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002340
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002341 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002342 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
2343 curr_sess->req->flags,
2344 curr_sess->req->l,
2345 curr_sess->req->analysers,
2346 curr_sess->req->rex ?
2347 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
2348 TICKS_TO_MS(1000)) : "");
2349
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002350 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002351 ",wx=%s",
2352 curr_sess->req->wex ?
2353 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
2354 TICKS_TO_MS(1000)) : "");
2355
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002356 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002357 ",ax=%s]",
2358 curr_sess->req->analyse_exp ?
2359 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
2360 TICKS_TO_MS(1000)) : "");
2361
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002362 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002363 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
2364 curr_sess->rep->flags,
2365 curr_sess->rep->l,
2366 curr_sess->rep->analysers,
2367 curr_sess->rep->rex ?
2368 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
2369 TICKS_TO_MS(1000)) : "");
2370
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002371 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002372 ",wx=%s",
2373 curr_sess->rep->wex ?
2374 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
2375 TICKS_TO_MS(1000)) : "");
2376
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002377 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002378 ",ax=%s]",
2379 curr_sess->rep->analyse_exp ?
2380 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
2381 TICKS_TO_MS(1000)) : "");
2382
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002383 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002384 " s0=[%d,%1xh,fd=%d,ex=%s]",
2385 curr_sess->si[0].state,
2386 curr_sess->si[0].flags,
2387 curr_sess->si[0].fd,
2388 curr_sess->si[0].exp ?
2389 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
2390 TICKS_TO_MS(1000)) : "");
2391
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002392 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002393 " s1=[%d,%1xh,fd=%d,ex=%s]",
2394 curr_sess->si[1].state,
2395 curr_sess->si[1].flags,
2396 curr_sess->si[1].fd,
2397 curr_sess->si[1].exp ?
2398 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
2399 TICKS_TO_MS(1000)) : "");
2400
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002401 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002402 " exp=%s",
2403 curr_sess->task->expire ?
2404 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
2405 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01002406 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002407 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002408
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002409 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002410
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002411 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002412 /* let's try again later from this session. We add ourselves into
2413 * this session's users so that it can remove us upon termination.
2414 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002415 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002416 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002417 }
2418
2419 s->data_ctx.sess.bref.ref = curr_sess->list.n;
2420 }
2421 s->data_state = DATA_ST_FIN;
2422 /* fall through */
2423
2424 default:
2425 s->data_state = DATA_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002426 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002427 }
2428}
2429
Willy Tarreau74808cb2009-03-04 15:53:18 +01002430/* print a line of error buffer (limited to 70 bytes) to <out>. The format is :
2431 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
2432 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
2433 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
2434 * lines are respected within the limit of 70 output chars. Lines that are
2435 * continuation of a previous truncated line begin with "+" instead of " "
2436 * after the offset. The new pointer is returned.
2437 */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002438static int dump_error_line(struct chunk *out, struct error_snapshot *err,
2439 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01002440{
2441 int end;
2442 unsigned char c;
2443
2444 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002445 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01002446 return ptr;
2447
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002448 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01002449
Willy Tarreau61b34732009-10-03 23:49:35 +02002450 while (ptr < err->len && ptr < sizeof(err->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002451 c = err->buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01002452 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002453 if (out->len > end - 2)
2454 break;
2455 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01002456 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002457 if (out->len > end - 3)
2458 break;
2459 out->str[out->len++] = '\\';
2460 switch (c) {
2461 case '\t': c = 't'; break;
2462 case '\n': c = 'n'; break;
2463 case '\r': c = 'r'; break;
2464 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01002465 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002466 }
2467 out->str[out->len++] = c;
2468 } else {
2469 if (out->len > end - 5)
2470 break;
2471 out->str[out->len++] = '\\';
2472 out->str[out->len++] = 'x';
2473 out->str[out->len++] = hextab[(c >> 4) & 0xF];
2474 out->str[out->len++] = hextab[c & 0xF];
2475 }
2476 if (err->buf[ptr++] == '\n') {
2477 /* we had a line break, let's return now */
2478 out->str[out->len++] = '\n';
2479 *line = ptr;
2480 return ptr;
2481 }
2482 }
2483 /* we have an incomplete line, we return it as-is */
2484 out->str[out->len++] = '\n';
2485 return ptr;
2486}
2487
2488/* This function is called to send output to the response buffer.
2489 * It dumps the errors logged in proxies onto the output buffer <rep>.
2490 * Expects to be called with client socket shut down on input.
2491 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau61b34732009-10-03 23:49:35 +02002492 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau74808cb2009-03-04 15:53:18 +01002493 */
Willy Tarreau61b34732009-10-03 23:49:35 +02002494int stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau74808cb2009-03-04 15:53:18 +01002495{
2496 extern const char *monthname[12];
2497 struct chunk msg;
2498
Willy Tarreau61b34732009-10-03 23:49:35 +02002499 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW)))
2500 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002501
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002502 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01002503
2504 if (!s->data_ctx.errors.px) {
2505 /* the function had not been called yet, let's prepare the
2506 * buffer for a response.
2507 */
Willy Tarreau74808cb2009-03-04 15:53:18 +01002508 s->data_ctx.errors.px = proxy;
2509 s->data_ctx.errors.buf = 0;
2510 s->data_ctx.errors.bol = 0;
2511 s->data_ctx.errors.ptr = -1;
2512 }
2513
2514 /* we have two inner loops here, one for the proxy, the other one for
2515 * the buffer.
2516 */
2517 while (s->data_ctx.errors.px) {
2518 struct error_snapshot *es;
2519
2520 if (s->data_ctx.errors.buf == 0)
2521 es = &s->data_ctx.errors.px->invalid_req;
2522 else
2523 es = &s->data_ctx.errors.px->invalid_rep;
2524
2525 if (!es->when.tv_sec)
2526 goto next;
2527
2528 if (s->data_ctx.errors.iid >= 0 &&
2529 s->data_ctx.errors.px->uuid != s->data_ctx.errors.iid &&
2530 es->oe->uuid != s->data_ctx.errors.iid)
2531 goto next;
2532
2533 if (s->data_ctx.errors.ptr < 0) {
2534 /* just print headers now */
2535
2536 char pn[INET6_ADDRSTRLEN];
2537 struct tm tm;
2538
2539 get_localtime(es->when.tv_sec, &tm);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002540 chunk_printf(&msg, "\n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01002541 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002542 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01002543
2544
2545 if (es->src.ss_family == AF_INET)
2546 inet_ntop(AF_INET,
2547 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
2548 pn, sizeof(pn));
2549 else
2550 inet_ntop(AF_INET6,
2551 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
2552 pn, sizeof(pn));
2553
2554 switch (s->data_ctx.errors.buf) {
2555 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002556 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002557 " frontend %s (#%d): invalid request\n"
2558 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
2559 " request length %d bytes, error at position %d:\n\n",
2560 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
2561 pn, es->sid, es->oe->id, es->oe->uuid,
2562 es->srv ? es->srv->id : "<NONE>",
2563 es->srv ? es->srv->puid : -1,
2564 es->len, es->pos);
2565 break;
2566 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002567 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002568 " backend %s (#%d) : invalid response\n"
2569 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
2570 " response length %d bytes, error at position %d:\n\n",
2571 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
2572 pn, es->sid, es->oe->id, es->oe->uuid,
2573 es->srv ? es->srv->id : "<NONE>",
2574 es->srv ? es->srv->puid : -1,
2575 es->len, es->pos);
2576 break;
2577 }
2578
Willy Tarreau61b34732009-10-03 23:49:35 +02002579 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002580 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02002581 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002582 }
2583 s->data_ctx.errors.ptr = 0;
2584 s->data_ctx.errors.sid = es->sid;
2585 }
2586
2587 if (s->data_ctx.errors.sid != es->sid) {
2588 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002589 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002590 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau61b34732009-10-03 23:49:35 +02002591 if (buffer_feed_chunk(rep, &msg) >= 0)
2592 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002593 goto next;
2594 }
2595
2596 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau61b34732009-10-03 23:49:35 +02002597 while (s->data_ctx.errors.ptr < es->len && s->data_ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002598 int newptr;
2599 int newline;
2600
2601 newline = s->data_ctx.errors.bol;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002602 newptr = dump_error_line(&msg, es, &newline, s->data_ctx.errors.ptr);
Willy Tarreau74808cb2009-03-04 15:53:18 +01002603 if (newptr == s->data_ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02002604 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002605
Willy Tarreau61b34732009-10-03 23:49:35 +02002606 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002607 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02002608 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002609 }
2610 s->data_ctx.errors.ptr = newptr;
2611 s->data_ctx.errors.bol = newline;
2612 };
2613 next:
2614 s->data_ctx.errors.bol = 0;
2615 s->data_ctx.errors.ptr = -1;
2616 s->data_ctx.errors.buf++;
2617 if (s->data_ctx.errors.buf > 1) {
2618 s->data_ctx.errors.buf = 0;
2619 s->data_ctx.errors.px = s->data_ctx.errors.px->next;
2620 }
2621 }
2622
2623 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02002624 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002625}
2626
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002627
Willy Tarreau10522fd2008-07-09 20:12:41 +02002628static struct cfg_kw_list cfg_kws = {{ },{
2629 { CFG_GLOBAL, "stats", stats_parse_global },
2630 { 0, NULL, NULL },
2631}};
2632
2633__attribute__((constructor))
2634static void __dumpstats_module_init(void)
2635{
2636 cfg_register_keywords(&cfg_kws);
2637}
2638
Willy Tarreau91861262007-10-17 17:06:05 +02002639/*
2640 * Local variables:
2641 * c-indent-level: 8
2642 * c-basic-offset: 8
2643 * End:
2644 */