blob: d3d036b024e4a12ff6800666a62e4009fc451611 [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"
Willy Tarreaue0454092010-02-26 12:29:07 +01001138 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001139 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001140 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001141 (uri->flags&ST_SHNODE) ? " on " : "",
1142 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001143 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001144 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001145 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001146 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001147 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001148 return 0;
1149
1150 s->data_state = DATA_ST_INFO;
1151 /* fall through */
1152
1153 case DATA_ST_INFO:
1154 up = (now.tv_sec - start_date.tv_sec);
1155
1156 /* WARNING! this has to fit the first packet too.
1157 * We are around 3.5 kB, add adding entries will
1158 * become tricky if we want to support 4kB buffers !
1159 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001160 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001161 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001162 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1163 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001164 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001165 "<hr width=\"100%%\" class=\"hr\">\n"
1166 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001167 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001168 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001169 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001170 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1171 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
1172 "current conns = %d; current pipes = %d/%d<br>\n"
1173 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001174 "</td><td align=\"center\" nowrap>\n"
1175 "<table class=\"lgd\"><tr>\n"
1176 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1177 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1178 "</tr><tr>\n"
1179 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1180 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1181 "</tr><tr>\n"
1182 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1183 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1184 "</tr><tr>\n"
1185 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001186 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001187 "</tr><tr>\n"
1188 "<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 +02001189 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001190 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001191 "</td>"
1192 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1193 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1194 "",
1195 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001196 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1197 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1198 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001199 up / 86400, (up % 86400) / 3600,
1200 (up % 3600) / 60, (up % 60),
1201 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1202 global.rlimit_memmax ? " MB" : "",
1203 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001204 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001205 actconn, pipes_used, pipes_used+pipes_free,
1206 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +02001207 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001208
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001209 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001210 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001211 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1212 uri->uri_prefix,
1213 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001214 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001215 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001216 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001217 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1218 uri->uri_prefix,
1219 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001220 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001221
Willy Tarreau55bb8452007-10-17 18:44:57 +02001222 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001223 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001224 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001225 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1226 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001227 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001228 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001229 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001230 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001231 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1232 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001233 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001234 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001235 }
Willy Tarreau91861262007-10-17 17:06:05 +02001236
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001237 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001238 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
1239 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001240 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
1241 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001242
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001243 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02001244 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
1245 uri->uri_prefix,
1246 (uri->refresh > 0) ? ";norefresh" : "");
1247
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001248 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001249 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001250 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1251 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
1252 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
1253 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
1254 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
1255 "</ul>"
1256 "</td>"
1257 "</tr></table>\n"
1258 ""
1259 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001260
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001261 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001262 return 0;
1263 }
Willy Tarreau91861262007-10-17 17:06:05 +02001264
Willy Tarreau91861262007-10-17 17:06:05 +02001265 s->data_ctx.stats.px = proxy;
1266 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1267 s->data_state = DATA_ST_LIST;
1268 /* fall through */
1269
1270 case DATA_ST_LIST:
1271 /* dump proxies */
1272 while (s->data_ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001273 if (buffer_almost_full(rep))
1274 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02001275 px = s->data_ctx.stats.px;
1276 /* skip the disabled proxies and non-networked ones */
1277 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001278 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001279 return 0;
1280
1281 s->data_ctx.stats.px = px->next;
1282 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1283 }
1284 /* here, we just have reached the last proxy */
1285
1286 s->data_state = DATA_ST_END;
1287 /* fall through */
1288
1289 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001290 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001291 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001292 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001293 return 0;
1294 }
Willy Tarreau91861262007-10-17 17:06:05 +02001295
1296 s->data_state = DATA_ST_FIN;
1297 /* fall through */
1298
1299 case DATA_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02001300 return 1;
1301
1302 default:
1303 /* unknown state ! */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001304 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001305 return -1;
1306 }
1307}
1308
1309
1310/*
1311 * Dumps statistics for a proxy.
1312 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1313 * ot non-zero if everything completed.
1314 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001315int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001316{
1317 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001318 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001319 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02001320 struct chunk msg;
1321
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001322 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001323
1324 switch (s->data_ctx.stats.px_st) {
1325 case DATA_ST_PX_INIT:
1326 /* we are on a new proxy */
1327
1328 if (uri && uri->scope) {
1329 /* we have a limited scope, we have to check the proxy name */
1330 struct stat_scope *scope;
1331 int len;
1332
1333 len = strlen(px->id);
1334 scope = uri->scope;
1335
1336 while (scope) {
1337 /* match exact proxy name */
1338 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1339 break;
1340
1341 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001342 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001343 break;
1344 scope = scope->next;
1345 }
1346
1347 /* proxy name not found : don't dump anything */
1348 if (scope == NULL)
1349 return 1;
1350 }
1351
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001352 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001353 (px->uuid != s->data_ctx.stats.iid))
1354 return 1;
1355
Willy Tarreau91861262007-10-17 17:06:05 +02001356 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
1357 /* fall through */
1358
1359 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001360 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001361 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001362 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001363 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001364 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001365 "<th class=\"pxname\" width=\"10%%\"");
1366
1367 if (uri->flags&ST_SHLGNDS) {
1368 /* cap, mode, id */
1369 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
1370 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1371 px->uuid);
1372
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001373 chunk_printf(&msg, "\"");
1374 }
1375
1376 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001377 ">%s<a name=\"%s\"></a>"
1378 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001379 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001380 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001381 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001382 "<table class=\"tbl\" width=\"100%%\">\n"
1383 "<tr class=\"titre\">"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001384 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001385 "<th colspan=3>Queue</th>"
1386 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001387 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001388 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001389 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001390 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001391 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001392 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001393 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001394 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001395 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001396 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001397 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001398 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1399 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001400 "</tr>",
Willy Tarreaue0454092010-02-26 12:29:07 +01001401 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001402 px->id, px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01001403 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001404 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001405
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001406 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001407 return 0;
1408 }
Willy Tarreau91861262007-10-17 17:06:05 +02001409
1410 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
1411 /* fall through */
1412
1413 case DATA_ST_PX_FE:
1414 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001415 if ((px->cap & PR_CAP_FE) &&
1416 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
1417 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001418 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001419 /* name, queue */
Krzysztof Piotr Oledzki516ed492009-10-22 22:48:09 +02001420 "<tr class=\"frontend\"><td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001421 "<a name=\"%s/Frontend\"></a>"
1422 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td><td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01001423 "",
1424 px->id, px->id);
1425
1426 if (px->mode == PR_MODE_HTTP) {
1427 chunk_printf(&msg,
1428 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01001429 "<td title=\"Cur: %u req/s\"><u>%s</u></td><td title=\"Max: %u req/s\"><u>%s</u></td><td>%s</td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01001430 "",
1431 read_freq_ctr(&px->fe_req_per_sec),
1432 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
1433 px->counters.fe_rps_max,
1434 U2H2(px->counters.fe_sps_max),
1435 LIM2A2(px->fe_sps_lim, "-"));
1436 } else {
1437 chunk_printf(&msg,
1438 /* sessions rate : current, max, limit */
1439 "<td>%s</td><td>%s</td><td>%s</td>"
1440 "",
1441 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
1442 U2H1(px->counters.fe_sps_max), LIM2A2(px->fe_sps_lim, "-"));
1443 }
1444
1445 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001446 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001447 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001448 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001449 "",
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001450 U2H3(px->feconn), U2H4(px->counters.feconn_max), U2H5(px->maxconn));
1451
1452 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1453 if (px->mode == PR_MODE_HTTP) {
1454 int i;
1455
Willy Tarreaub44939a2010-02-26 11:35:39 +01001456 chunk_printf(&msg, " title=\"%lld requests:", px->counters.cum_fe_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001457
1458 for (i = 1; i < 6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01001459 chunk_printf(&msg, " %dxx=%lld,", i, px->counters.fe.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001460
Willy Tarreau24657792010-02-26 10:30:28 +01001461 chunk_printf(&msg, " other=%lld\"", px->counters.fe.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001462 }
1463
1464 chunk_printf(&msg,
1465 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01001466 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001467 /* bytes : in, out */
1468 "<td>%s</td><td>%s</td>"
1469 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001470 (px->mode == PR_MODE_HTTP)?"<u>":"",
1471 U2H6(px->counters.cum_feconn),
1472 (px->mode == PR_MODE_HTTP)?"</u>":"",
1473 U2H7(px->counters.bytes_in), U2H8(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001474
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001475 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001476 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001477 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001478 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001479 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001480 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001481 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001482 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001483 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001484 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001485 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02001486 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001487 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
1488 U2H2(px->counters.failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +02001489 px->state == PR_STRUN ? "OPEN" :
1490 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001491 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001492 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001493 /* pxid, name, queue cur, queue max, */
1494 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001495 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001496 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001497 /* bytes : in, out */
1498 "%lld,%lld,"
1499 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001500 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001501 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001502 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001503 /* warnings: retries, redispatches */
1504 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001505 /* server status : reflect frontend status */
1506 "%s,"
1507 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001508 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001509 /* pid, iid, sid, throttle, lbtot, tracked, type */
1510 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001511 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001512 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001513 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001514 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02001515 px->id,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001516 px->feconn, px->counters.feconn_max, px->maxconn, px->counters.cum_feconn,
1517 px->counters.bytes_in, px->counters.bytes_out,
1518 px->counters.denied_req, px->counters.denied_resp,
1519 px->counters.failed_req,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001520 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +01001521 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01001522 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001523 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001524 px->fe_sps_lim, px->counters.fe_sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001525
1526 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1527 if (px->mode == PR_MODE_HTTP) {
1528 int i;
1529
1530 for (i=1; i<6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01001531 chunk_printf(&msg, "%lld,", px->counters.fe.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001532
Willy Tarreau24657792010-02-26 10:30:28 +01001533 chunk_printf(&msg, "%lld,", px->counters.fe.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001534 } else {
1535 chunk_printf(&msg, ",,,,,,");
1536 }
1537
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001538 /* failed health analyses */
1539 chunk_printf(&msg, ",");
1540
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001541 /* requests : req_rate, req_rate_max, req_tot, */
1542 chunk_printf(&msg, "%u,%u,%lld,",
1543 read_freq_ctr(&px->fe_req_per_sec),
1544 px->counters.fe_rps_max, px->counters.cum_fe_req);
1545
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001546 /* finish with EOL */
1547 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001548 }
Willy Tarreau91861262007-10-17 17:06:05 +02001549
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001550 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001551 return 0;
1552 }
1553
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001554 s->data_ctx.stats.l = px->listen; /* may be NULL */
1555 s->data_ctx.stats.px_st = DATA_ST_PX_LI;
1556 /* fall through */
1557
1558 case DATA_ST_PX_LI:
1559 /* stats.l has been initialized above */
1560 for (; s->data_ctx.stats.l != NULL; s->data_ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001561 if (buffer_almost_full(rep))
1562 return 0;
1563
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001564 l = s->data_ctx.stats.l;
1565 if (!l->counters)
1566 continue;
1567
1568 if (s->data_ctx.stats.flags & STAT_BOUND) {
1569 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SO)))
1570 break;
1571
1572 if (s->data_ctx.stats.sid != -1 && l->luid != s->data_ctx.stats.sid)
1573 continue;
1574 }
1575
1576 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001577 chunk_printf(&msg, "<tr class=socket><td class=ac");
1578
1579 if (uri->flags&ST_SHLGNDS) {
1580 char str[INET6_ADDRSTRLEN], *fmt = NULL;
1581 int port;
1582
1583 chunk_printf(&msg, " title=\"IP: ");
1584
1585 port = (l->addr.ss_family == AF_INET6)
1586 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
1587 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
1588
1589 if (l->addr.ss_family == AF_INET) {
1590 if (inet_ntop(AF_INET,
1591 (const void *)&((struct sockaddr_in *)&l->addr)->sin_addr,
1592 str, sizeof(str)))
1593 fmt = "%s:%d";
1594 } else {
1595 if (inet_ntop(AF_INET6,
1596 (const void *)&((struct sockaddr_in6 *)(&l->addr))->sin6_addr,
1597 str, sizeof(str)))
1598 fmt = "[%s]:%d";
1599 }
1600
1601 if (fmt)
1602 chunk_printf(&msg, fmt, str, port);
1603 else
1604 chunk_printf(&msg, "(%s)", strerror(errno));
1605
1606 /* id */
1607 chunk_printf(&msg, ", id: %d", l->luid);
1608
1609 chunk_printf(&msg, "\"");
1610 }
1611
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001612 chunk_printf(&msg,
1613 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01001614 ">%s<a name=\"%s/+%s\"></a>"
1615 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001616 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001617 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001618 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001619 "<td>%s</td><td>%s</td><td>%s</td>"
1620 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001621 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001622 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001623 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001624 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01001625 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01001626 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001627 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
1628 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
1629
1630 chunk_printf(&msg,
1631 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001632 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001633 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001634 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001635 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001636 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001637 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001638 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001639 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001640 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001641 "",
1642 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
1643 U2H2(l->counters->failed_req),
1644 (l->nbconn < l->maxconn) ? "OPEN" : "FULL");
1645 } else {
1646 chunk_printf(&msg,
1647 /* pxid, name, queue cur, queue max, */
1648 "%s,%s,,,"
1649 /* sessions: current, max, limit, total */
1650 "%d,%d,%d,%lld,"
1651 /* bytes: in, out */
1652 "%lld,%lld,"
1653 /* denied: req, resp */
1654 "%lld,%lld,"
1655 /* errors: request, connect, response */
1656 "%lld,,,"
1657 /* warnings: retries, redispatches */
1658 ",,"
1659 /* server status: reflect listener status */
1660 "%s,"
1661 /* rest of server: nothing */
1662 ",,,,,,,,"
1663 /* pid, iid, sid, throttle, lbtot, tracked, type */
1664 "%d,%d,%d,,,,%d,"
1665 /* rate, rate_lim, rate_max */
1666 ",,,"
1667 /* check_status, check_code, check_duration */
1668 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001669 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1670 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001671 /* failed health analyses */
1672 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001673 /* requests : req_rate, req_rate_max, req_tot, */
1674 ",,,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001675 "\n",
1676 px->id, l->name,
1677 l->nbconn, l->counters->conn_max,
1678 l->maxconn, l->counters->cum_conn,
1679 l->counters->bytes_in, l->counters->bytes_out,
1680 l->counters->denied_req, l->counters->denied_resp,
1681 l->counters->failed_req,
1682 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
1683 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
1684 }
1685
1686 if (buffer_feed_chunk(rep, &msg) >= 0)
1687 return 0;
1688 }
1689
Willy Tarreau91861262007-10-17 17:06:05 +02001690 s->data_ctx.stats.sv = px->srv; /* may be NULL */
1691 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
1692 /* fall through */
1693
1694 case DATA_ST_PX_SV:
1695 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001696 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001697 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 +02001698
Willy Tarreau4e33d862009-10-11 23:35:10 +02001699 if (buffer_almost_full(rep))
1700 return 0;
1701
Willy Tarreau91861262007-10-17 17:06:05 +02001702 sv = s->data_ctx.stats.sv;
1703
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001704 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001705 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
1706 break;
1707
1708 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
1709 continue;
1710 }
1711
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001712 if (sv->tracked)
1713 svs = sv->tracked;
1714 else
1715 svs = sv;
1716
Willy Tarreau91861262007-10-17 17:06:05 +02001717 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001718 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01001719 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001720 else if (svs->state & SRV_RUNNING) {
1721 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02001722 sv_state = 3; /* UP */
1723 else
1724 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01001725
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001726 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01001727 sv_state += 2;
1728 }
Willy Tarreau91861262007-10-17 17:06:05 +02001729 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001730 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02001731 sv_state = 1; /* going up */
1732 else
1733 sv_state = 0; /* DOWN */
1734
Cyril Bonté0dae5852010-02-03 00:26:28 +01001735 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02001736 /* do not report servers which are DOWN */
1737 s->data_ctx.stats.sv = sv->next;
1738 continue;
1739 }
1740
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001741 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001742 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
1743 "UP %d/%d &darr;", "UP",
1744 "NOLB %d/%d &darr;", "NOLB",
1745 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01001746 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001747 chunk_printf(&msg,
1748 /* name */
1749 "<tr class=\"maintain\"><td class=ac"
1750 );
1751 }
1752 else {
1753 chunk_printf(&msg,
1754 /* name */
1755 "<tr class=\"%s%d\"><td class=ac",
1756 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
1757 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001758
1759 if (uri->flags&ST_SHLGNDS) {
1760 char str[INET6_ADDRSTRLEN];
1761
1762 chunk_printf(&msg, " title=\"IP: ");
1763
1764 /* IP */
1765 if (inet_ntop(sv->addr.sin_family, &sv->addr.sin_addr, str, sizeof(str)))
1766 chunk_printf(&msg, "%s:%d", str, htons(sv->addr.sin_port));
1767 else
1768 chunk_printf(&msg, "(%s)", strerror(errno));
1769
1770 /* id */
1771 chunk_printf(&msg, ", id: %d", sv->puid);
1772
1773 /* cookie */
1774 if (sv->cookie) {
1775 struct chunk src;
1776
1777 chunk_printf(&msg, ", cookie: '");
1778
1779 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
1780 chunk_htmlencode(&msg, &src);
1781
1782 chunk_printf(&msg, "'");
1783 }
1784
1785 chunk_printf(&msg, "\"");
1786 }
1787
1788 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001789 ">%s<a name=\"%s/%s\"></a>"
1790 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001791 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001792 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001793 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001794 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001795 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001796 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001797 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001798 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001799 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001800 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01001801 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001802 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
1803 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001804 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
1805
1806 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1807 if (px->mode == PR_MODE_HTTP) {
1808 int i;
1809
1810 chunk_printf(&msg, " title=\"rsp codes:");
1811
1812 for (i = 1; i < 6; i++)
1813 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
1814
1815 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
1816 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001817
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001818 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001819 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01001820 ">%s%s%s</td><td>%s</td>",
1821 (px->mode == PR_MODE_HTTP)?"<u>":"",
1822 U2H0(sv->counters.cum_sess),
1823 (px->mode == PR_MODE_HTTP)?"</u>":"",
1824 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001825
1826 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001827 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001828 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001829 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001830 "<td></td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001831 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001832 "<td></td><td>%s</td><td>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001833 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001834 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001835 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001836 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
1837 U2H2(sv->counters.failed_secu),
1838 U2H3(sv->counters.failed_conns), U2H4(sv->counters.failed_resp),
1839 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001840
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001841 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001842 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001843
Cyril Bontécd19e512010-01-31 22:34:03 +01001844 if (sv->state & SRV_MAINTAIN) {
1845 chunk_printf(&msg, "%s ",
1846 human_time(now.tv_sec - sv->last_change, 1));
1847 chunk_printf(&msg, "MAINT");
1848 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01001849 else if (svs != sv && svs->state & SRV_MAINTAIN) {
1850 chunk_printf(&msg, "%s ",
1851 human_time(now.tv_sec - svs->last_change, 1));
1852 chunk_printf(&msg, "MAINT(via)");
1853 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001854 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001855 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01001856 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001857
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001858 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001859 srv_hlt_st[sv_state],
1860 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
1861 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001862 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001863
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001864 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001865 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001866 get_check_status_description(sv->check_status));
1867
1868 if (*sv->check_desc) {
1869 struct chunk src;
1870
1871 chunk_printf(&msg, ": ");
1872
1873 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
1874 chunk_htmlencode(&msg, &src);
1875 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001876
Willy Tarreaue0454092010-02-26 12:29:07 +01001877 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001878 tv_iszero(&sv->check_start)?"":"* ",
1879 get_check_status_info(sv->check_status));
1880
1881 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001882 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001883
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001884 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01001885 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001886 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001887 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02001888
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001889 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001890 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001891 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001892 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001893 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001894 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001895 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02001896 (sv->state & SRV_BACKUP) ? "-" : "Y",
1897 (sv->state & SRV_BACKUP) ? "Y" : "-");
1898
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001899 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01001900 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01001901 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01001902 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
1903
1904 if (svs->observe)
1905 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
1906
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001907 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001908 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001909 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001910 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001911 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01001912 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001913 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01001914 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
1915 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001916 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001917 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001918 "<td colspan=3></td>");
1919
1920 /* throttle */
1921 if ((sv->state & SRV_WARMINGUP) &&
1922 now.tv_sec < sv->last_change + sv->slowstart &&
1923 now.tv_sec >= sv->last_change) {
1924 unsigned int ratio;
1925 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001926 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001927 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001928 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001929 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001930 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001931 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02001932 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001933 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
1934 "UP %d/%d,", "UP,",
1935 "NOLB %d/%d,", "NOLB,",
1936 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001937 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001938 /* pxid, name */
1939 "%s,%s,"
1940 /* queue : current, max */
1941 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001942 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001943 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001944 /* bytes : in, out */
1945 "%lld,%lld,"
1946 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001947 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001948 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001949 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001950 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001951 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001952 "",
1953 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001954 sv->nbpend, sv->counters.nbpend_max,
1955 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001956 sv->counters.bytes_in, sv->counters.bytes_out,
1957 sv->counters.failed_secu,
1958 sv->counters.failed_conns, sv->counters.failed_resp,
1959 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001960
Willy Tarreau55bb8452007-10-17 18:44:57 +02001961 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01001962 if (sv->state & SRV_MAINTAIN) {
1963 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01001964 }
1965 else if (svs != sv && svs->state & SRV_MAINTAIN) {
1966 chunk_printf(&msg, "MAINT(via),");
1967 }
1968 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01001969 chunk_printf(&msg,
1970 srv_hlt_st[sv_state],
1971 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
1972 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
1973 }
Willy Tarreau91861262007-10-17 17:06:05 +02001974
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001975 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001976 /* weight, active, backup */
1977 "%d,%d,%d,"
1978 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001979 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001980 (sv->state & SRV_BACKUP) ? 0 : 1,
1981 (sv->state & SRV_BACKUP) ? 1 : 0);
1982
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001983 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001984 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001985 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02001986 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001987 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001988 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02001989 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001990 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001991 ",,,,");
1992
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001993 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001994 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01001995 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001996 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01001997 LIM2A0(sv->maxqueue, ""),
1998 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001999
2000 /* throttle */
2001 if ((sv->state & SRV_WARMINGUP) &&
2002 now.tv_sec < sv->last_change + sv->slowstart &&
2003 now.tv_sec >= sv->last_change) {
2004 unsigned int ratio;
2005 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002006 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002007 }
2008
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002009 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002010 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002011
2012 /* tracked */
2013 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002014 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002015 sv->tracked->proxy->id, sv->tracked->id);
2016 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002017 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002018
Willy Tarreau7f062c42009-03-05 18:43:00 +01002019 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002020 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002021
2022 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002023 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002024 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002025 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002026
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002027 if (sv->state & SRV_CHECKED) {
2028 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002029 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002030
2031 /* check_code */
2032 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002033 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002034 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002035 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002036
2037 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002038 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002039 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002040 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002041 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002042
2043 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002044 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002045 }
2046
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002047 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2048 if (px->mode == PR_MODE_HTTP) {
2049 int i;
2050
2051 for (i=1; i<6; i++)
2052 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2053
2054 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2055 } else {
2056 chunk_printf(&msg, ",,,,,,");
2057 }
2058
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002059 /* failed health analyses */
2060 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2061
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002062 /* requests : req_rate, req_rate_max, req_tot, */
2063 chunk_printf(&msg, ",,,");
2064
Willy Tarreau7f062c42009-03-05 18:43:00 +01002065 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002066 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002067 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002068 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002069 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002070 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002071
2072 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
2073 /* fall through */
2074
2075 case DATA_ST_PX_BE:
2076 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002077 if ((px->cap & PR_CAP_BE) &&
2078 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2079 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002080 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002081 /* name */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002082 "<tr class=\"backend\"><td class=ac");
2083
2084 if (uri->flags&ST_SHLGNDS) {
2085 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002086 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002087 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002088
2089 /* cookie */
2090 if (px->cookie_name) {
2091 struct chunk src;
2092
2093 chunk_printf(&msg, ", cookie: '");
2094
2095 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2096 chunk_htmlencode(&msg, &src);
2097
2098 chunk_printf(&msg, "'");
2099 }
2100
2101 chunk_printf(&msg, "\"");
2102
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002103 }
2104
2105 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002106 ">%s<a name=\"%s/Backend\"></a>"
2107 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002108 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002109 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002110 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002111 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002112 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002113 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002114 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002115 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002116 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->counters.nbpend_max),
2117 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->counters.be_sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02002118
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002119 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002120 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002121 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002122 "<td"
2123 "",
2124 U2H2(px->beconn), U2H3(px->counters.beconn_max), U2H4(px->fullconn));
2125
2126 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2127 if (px->mode == PR_MODE_HTTP) {
2128 int i;
2129
2130 chunk_printf(&msg, " title=\"rsp codes:");
2131
2132 for (i = 1; i < 6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01002133 chunk_printf(&msg, " %dxx=%lld", i, px->counters.be.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002134
Willy Tarreau24657792010-02-26 10:30:28 +01002135 chunk_printf(&msg, " other=%lld\"", px->counters.be.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002136 }
2137
2138 chunk_printf(&msg,
2139 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002140 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002141 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002142 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002143 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002144 (px->mode == PR_MODE_HTTP)?"<u>":"",
2145 U2H6(px->counters.cum_beconn),
2146 (px->mode == PR_MODE_HTTP)?"</u>":"",
2147 U2H7(px->counters.cum_lbconn),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002148 U2H8(px->counters.bytes_in), U2H9(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002149
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002150 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002151 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002152 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002153 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002154 "<td></td><td>%s</td><td>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002155 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002156 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002157 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02002158 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002159 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02002160 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002161 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2162 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002163 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002164 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
2165 U2H2(px->counters.failed_conns), U2H3(px->counters.failed_resp),
2166 px->counters.retries, px->counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002167 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01002168 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2169 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01002170 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002171 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002172
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002173 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002174 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002175 "<td class=ac>&nbsp;</td><td>%d</td>"
2176 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002177 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002178 "</tr>",
2179 px->down_trans,
2180 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002181 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002182 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002183 /* pxid, name */
2184 "%s,BACKEND,"
2185 /* queue : current, max */
2186 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002187 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002188 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002189 /* bytes : in, out */
2190 "%lld,%lld,"
2191 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002192 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002193 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002194 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002195 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002196 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002197 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02002198 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002199 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02002200 * active and backups. */
2201 "%s,"
2202 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002203 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002204 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002205 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002206 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002207 /* rate, rate_lim, rate_max, */
2208 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002209 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002210 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002211 px->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002212 px->nbpend /* or px->totpend ? */, px->counters.nbpend_max,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002213 px->beconn, px->counters.beconn_max, px->fullconn, px->counters.cum_beconn,
2214 px->counters.bytes_in, px->counters.bytes_out,
2215 px->counters.denied_req, px->counters.denied_resp,
2216 px->counters.failed_conns, px->counters.failed_resp,
2217 px->counters.retries, px->counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01002218 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01002219 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002220 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002221 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01002222 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002223 relative_pid, px->uuid,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002224 px->counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002225 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002226 px->counters.be_sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002227
2228 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2229 if (px->mode == PR_MODE_HTTP) {
2230 int i;
2231
2232 for (i=1; i<6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01002233 chunk_printf(&msg, "%lld,", px->counters.be.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002234
Willy Tarreau24657792010-02-26 10:30:28 +01002235 chunk_printf(&msg, "%lld,", px->counters.be.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002236 } else {
2237 chunk_printf(&msg, ",,,,,,");
2238 }
2239
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002240 /* failed health analyses */
2241 chunk_printf(&msg, ",");
2242
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002243 /* requests : req_rate, req_rate_max, req_tot, */
2244 chunk_printf(&msg, ",,,");
2245
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002246 /* finish with EOL */
2247 chunk_printf(&msg, "\n");
2248
Willy Tarreau55bb8452007-10-17 18:44:57 +02002249 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002250 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002251 return 0;
2252 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002253
Willy Tarreau91861262007-10-17 17:06:05 +02002254 s->data_ctx.stats.px_st = DATA_ST_PX_END;
2255 /* fall through */
2256
2257 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002258 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002259 chunk_printf(&msg, "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02002260
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002261 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002262 return 0;
2263 }
Willy Tarreau91861262007-10-17 17:06:05 +02002264
2265 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
2266 /* fall through */
2267
2268 case DATA_ST_PX_FIN:
2269 return 1;
2270
2271 default:
2272 /* unknown state, we should put an abort() here ! */
2273 return 1;
2274 }
2275}
2276
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002277
2278/* This function is called to send output to the response buffer.
2279 * It dumps the sessions states onto the output buffer <rep>.
2280 * Expects to be called with client socket shut down on input.
2281 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002282 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002283 */
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002284int stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002285{
2286 struct chunk msg;
2287
2288 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
2289 /* If we're forced to shut down, we might have to remove our
2290 * reference to the last session being dumped.
2291 */
2292 if (s->data_state == DATA_ST_LIST) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002293 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002294 LIST_DEL(&s->data_ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002295 LIST_INIT(&s->data_ctx.sess.bref.users);
2296 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002297 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002298 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002299 }
2300
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002301 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002302
2303 switch (s->data_state) {
2304 case DATA_ST_INIT:
2305 /* the function had not been called yet, let's prepare the
2306 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002307 * pointer to the first in the global list. When a target
2308 * session is being destroyed, it is responsible for updating
2309 * this pointer. We know we have reached the end when this
2310 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002311 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002312 LIST_INIT(&s->data_ctx.sess.bref.users);
2313 s->data_ctx.sess.bref.ref = sessions.n;
2314 s->data_state = DATA_ST_LIST;
2315 /* fall through */
2316
2317 case DATA_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002318 /* first, let's detach the back-ref from a possible previous session */
2319 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
2320 LIST_DEL(&s->data_ctx.sess.bref.users);
2321 LIST_INIT(&s->data_ctx.sess.bref.users);
2322 }
2323
2324 /* and start from where we stopped */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002325 while (s->data_ctx.sess.bref.ref != &sessions) {
2326 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
2327 struct session *curr_sess;
2328
2329 curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list);
2330
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002331 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002332 "%p: proto=%s",
2333 curr_sess,
2334 curr_sess->listener->proto->name);
2335
2336 switch (curr_sess->listener->proto->sock_family) {
2337 case AF_INET:
2338 inet_ntop(AF_INET,
2339 (const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr,
2340 pn, sizeof(pn));
2341
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002342 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002343 " src=%s:%d fe=%s be=%s srv=%s",
2344 pn,
2345 ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port),
2346 curr_sess->fe->id,
2347 curr_sess->be->id,
2348 curr_sess->srv ? curr_sess->srv->id : "<none>"
2349 );
2350 break;
2351 case AF_INET6:
2352 inet_ntop(AF_INET6,
2353 (const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr,
2354 pn, sizeof(pn));
2355
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002356 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002357 " src=%s:%d fe=%s be=%s srv=%s",
2358 pn,
2359 ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port),
2360 curr_sess->fe->id,
2361 curr_sess->be->id,
2362 curr_sess->srv ? curr_sess->srv->id : "<none>"
2363 );
2364
2365 break;
2366 case AF_UNIX:
2367 /* no more information to print right now */
2368 break;
2369 }
2370
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002371 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02002372 " ts=%02x age=%s calls=%d",
2373 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01002374 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
2375 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002376
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002377 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002378 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
2379 curr_sess->req->flags,
2380 curr_sess->req->l,
2381 curr_sess->req->analysers,
2382 curr_sess->req->rex ?
2383 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
2384 TICKS_TO_MS(1000)) : "");
2385
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002386 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002387 ",wx=%s",
2388 curr_sess->req->wex ?
2389 human_time(TICKS_TO_MS(curr_sess->req->wex - 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 ",ax=%s]",
2394 curr_sess->req->analyse_exp ?
2395 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
2396 TICKS_TO_MS(1000)) : "");
2397
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002398 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002399 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
2400 curr_sess->rep->flags,
2401 curr_sess->rep->l,
2402 curr_sess->rep->analysers,
2403 curr_sess->rep->rex ?
2404 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
2405 TICKS_TO_MS(1000)) : "");
2406
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002407 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002408 ",wx=%s",
2409 curr_sess->rep->wex ?
2410 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
2411 TICKS_TO_MS(1000)) : "");
2412
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002413 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002414 ",ax=%s]",
2415 curr_sess->rep->analyse_exp ?
2416 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
2417 TICKS_TO_MS(1000)) : "");
2418
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002419 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002420 " s0=[%d,%1xh,fd=%d,ex=%s]",
2421 curr_sess->si[0].state,
2422 curr_sess->si[0].flags,
2423 curr_sess->si[0].fd,
2424 curr_sess->si[0].exp ?
2425 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
2426 TICKS_TO_MS(1000)) : "");
2427
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002428 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002429 " s1=[%d,%1xh,fd=%d,ex=%s]",
2430 curr_sess->si[1].state,
2431 curr_sess->si[1].flags,
2432 curr_sess->si[1].fd,
2433 curr_sess->si[1].exp ?
2434 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
2435 TICKS_TO_MS(1000)) : "");
2436
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002437 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002438 " exp=%s",
2439 curr_sess->task->expire ?
2440 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
2441 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01002442 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002443 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002444
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002445 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002446
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002447 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002448 /* let's try again later from this session. We add ourselves into
2449 * this session's users so that it can remove us upon termination.
2450 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002451 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002452 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002453 }
2454
2455 s->data_ctx.sess.bref.ref = curr_sess->list.n;
2456 }
2457 s->data_state = DATA_ST_FIN;
2458 /* fall through */
2459
2460 default:
2461 s->data_state = DATA_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002462 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002463 }
2464}
2465
Willy Tarreau74808cb2009-03-04 15:53:18 +01002466/* print a line of error buffer (limited to 70 bytes) to <out>. The format is :
2467 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
2468 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
2469 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
2470 * lines are respected within the limit of 70 output chars. Lines that are
2471 * continuation of a previous truncated line begin with "+" instead of " "
2472 * after the offset. The new pointer is returned.
2473 */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002474static int dump_error_line(struct chunk *out, struct error_snapshot *err,
2475 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01002476{
2477 int end;
2478 unsigned char c;
2479
2480 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002481 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01002482 return ptr;
2483
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002484 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01002485
Willy Tarreau61b34732009-10-03 23:49:35 +02002486 while (ptr < err->len && ptr < sizeof(err->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002487 c = err->buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01002488 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002489 if (out->len > end - 2)
2490 break;
2491 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01002492 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002493 if (out->len > end - 3)
2494 break;
2495 out->str[out->len++] = '\\';
2496 switch (c) {
2497 case '\t': c = 't'; break;
2498 case '\n': c = 'n'; break;
2499 case '\r': c = 'r'; break;
2500 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01002501 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002502 }
2503 out->str[out->len++] = c;
2504 } else {
2505 if (out->len > end - 5)
2506 break;
2507 out->str[out->len++] = '\\';
2508 out->str[out->len++] = 'x';
2509 out->str[out->len++] = hextab[(c >> 4) & 0xF];
2510 out->str[out->len++] = hextab[c & 0xF];
2511 }
2512 if (err->buf[ptr++] == '\n') {
2513 /* we had a line break, let's return now */
2514 out->str[out->len++] = '\n';
2515 *line = ptr;
2516 return ptr;
2517 }
2518 }
2519 /* we have an incomplete line, we return it as-is */
2520 out->str[out->len++] = '\n';
2521 return ptr;
2522}
2523
2524/* This function is called to send output to the response buffer.
2525 * It dumps the errors logged in proxies onto the output buffer <rep>.
2526 * Expects to be called with client socket shut down on input.
2527 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau61b34732009-10-03 23:49:35 +02002528 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau74808cb2009-03-04 15:53:18 +01002529 */
Willy Tarreau61b34732009-10-03 23:49:35 +02002530int stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau74808cb2009-03-04 15:53:18 +01002531{
2532 extern const char *monthname[12];
2533 struct chunk msg;
2534
Willy Tarreau61b34732009-10-03 23:49:35 +02002535 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW)))
2536 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002537
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002538 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01002539
2540 if (!s->data_ctx.errors.px) {
2541 /* the function had not been called yet, let's prepare the
2542 * buffer for a response.
2543 */
Willy Tarreau74808cb2009-03-04 15:53:18 +01002544 s->data_ctx.errors.px = proxy;
2545 s->data_ctx.errors.buf = 0;
2546 s->data_ctx.errors.bol = 0;
2547 s->data_ctx.errors.ptr = -1;
2548 }
2549
2550 /* we have two inner loops here, one for the proxy, the other one for
2551 * the buffer.
2552 */
2553 while (s->data_ctx.errors.px) {
2554 struct error_snapshot *es;
2555
2556 if (s->data_ctx.errors.buf == 0)
2557 es = &s->data_ctx.errors.px->invalid_req;
2558 else
2559 es = &s->data_ctx.errors.px->invalid_rep;
2560
2561 if (!es->when.tv_sec)
2562 goto next;
2563
2564 if (s->data_ctx.errors.iid >= 0 &&
2565 s->data_ctx.errors.px->uuid != s->data_ctx.errors.iid &&
2566 es->oe->uuid != s->data_ctx.errors.iid)
2567 goto next;
2568
2569 if (s->data_ctx.errors.ptr < 0) {
2570 /* just print headers now */
2571
2572 char pn[INET6_ADDRSTRLEN];
2573 struct tm tm;
2574
2575 get_localtime(es->when.tv_sec, &tm);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002576 chunk_printf(&msg, "\n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01002577 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002578 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01002579
2580
2581 if (es->src.ss_family == AF_INET)
2582 inet_ntop(AF_INET,
2583 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
2584 pn, sizeof(pn));
2585 else
2586 inet_ntop(AF_INET6,
2587 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
2588 pn, sizeof(pn));
2589
2590 switch (s->data_ctx.errors.buf) {
2591 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002592 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002593 " frontend %s (#%d): invalid request\n"
2594 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
2595 " request length %d bytes, error at position %d:\n\n",
2596 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
2597 pn, es->sid, es->oe->id, es->oe->uuid,
2598 es->srv ? es->srv->id : "<NONE>",
2599 es->srv ? es->srv->puid : -1,
2600 es->len, es->pos);
2601 break;
2602 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002603 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002604 " backend %s (#%d) : invalid response\n"
2605 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
2606 " response length %d bytes, error at position %d:\n\n",
2607 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
2608 pn, es->sid, es->oe->id, es->oe->uuid,
2609 es->srv ? es->srv->id : "<NONE>",
2610 es->srv ? es->srv->puid : -1,
2611 es->len, es->pos);
2612 break;
2613 }
2614
Willy Tarreau61b34732009-10-03 23:49:35 +02002615 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002616 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02002617 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002618 }
2619 s->data_ctx.errors.ptr = 0;
2620 s->data_ctx.errors.sid = es->sid;
2621 }
2622
2623 if (s->data_ctx.errors.sid != es->sid) {
2624 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002625 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002626 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau61b34732009-10-03 23:49:35 +02002627 if (buffer_feed_chunk(rep, &msg) >= 0)
2628 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002629 goto next;
2630 }
2631
2632 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau61b34732009-10-03 23:49:35 +02002633 while (s->data_ctx.errors.ptr < es->len && s->data_ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002634 int newptr;
2635 int newline;
2636
2637 newline = s->data_ctx.errors.bol;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002638 newptr = dump_error_line(&msg, es, &newline, s->data_ctx.errors.ptr);
Willy Tarreau74808cb2009-03-04 15:53:18 +01002639 if (newptr == s->data_ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02002640 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002641
Willy Tarreau61b34732009-10-03 23:49:35 +02002642 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002643 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02002644 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002645 }
2646 s->data_ctx.errors.ptr = newptr;
2647 s->data_ctx.errors.bol = newline;
2648 };
2649 next:
2650 s->data_ctx.errors.bol = 0;
2651 s->data_ctx.errors.ptr = -1;
2652 s->data_ctx.errors.buf++;
2653 if (s->data_ctx.errors.buf > 1) {
2654 s->data_ctx.errors.buf = 0;
2655 s->data_ctx.errors.px = s->data_ctx.errors.px->next;
2656 }
2657 }
2658
2659 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02002660 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002661}
2662
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002663
Willy Tarreau10522fd2008-07-09 20:12:41 +02002664static struct cfg_kw_list cfg_kws = {{ },{
2665 { CFG_GLOBAL, "stats", stats_parse_global },
2666 { 0, NULL, NULL },
2667}};
2668
2669__attribute__((constructor))
2670static void __dumpstats_module_init(void)
2671{
2672 cfg_register_keywords(&cfg_kws);
2673}
2674
Willy Tarreau91861262007-10-17 17:06:05 +02002675/*
2676 * Local variables:
2677 * c-indent-level: 8
2678 * c-basic-offset: 8
2679 * End:
2680 */