blob: b2967fc41086712cf51d3b4d7f62216b8896b8e0 [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 Tarreauae526782010-03-04 20:34:23 +0100251 "req_rate,req_rate_max,req_tot,"
252 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100253 "\n");
254}
255
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200256/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200257 * called from an applet running in a stream interface. The function returns 1
258 * if the request was understood, otherwise zero. It sets si->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200259 * designating the function which will have to process the request, which can
260 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200261 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200262int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200263{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200264 struct session *s = si->private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200265 char *args[MAX_STATS_ARGS + 1];
266 int arg;
267
268 while (isspace((unsigned char)*line))
269 line++;
270
271 arg = 0;
272 args[arg] = line;
273
274 while (*line && arg < MAX_STATS_ARGS) {
275 if (isspace((unsigned char)*line)) {
276 *line++ = '\0';
277
278 while (isspace((unsigned char)*line))
279 line++;
280
281 args[++arg] = line;
282 continue;
283 }
284
285 line++;
286 }
287
288 while (++arg <= MAX_STATS_ARGS)
289 args[arg] = line;
290
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200291 s->data_ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200292 if (strcmp(args[0], "show") == 0) {
293 if (strcmp(args[1], "stat") == 0) {
294 if (*args[2] && *args[3] && *args[4]) {
295 s->data_ctx.stats.flags |= STAT_BOUND;
296 s->data_ctx.stats.iid = atoi(args[2]);
297 s->data_ctx.stats.type = atoi(args[3]);
298 s->data_ctx.stats.sid = atoi(args[4]);
299 }
300
301 s->data_ctx.stats.flags |= STAT_SHOW_STAT;
302 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200303 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200304 si->st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200305 }
306 else if (strcmp(args[1], "info") == 0) {
307 s->data_ctx.stats.flags |= STAT_SHOW_INFO;
308 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200309 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200310 si->st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200311 }
312 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200313 s->data_state = DATA_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200314 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200315 s->data_ctx.cli.msg = stats_permission_denied_msg;
316 si->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200317 return 1;
318 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200319 si->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200320 }
321 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200322 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200323 s->data_ctx.cli.msg = stats_permission_denied_msg;
324 si->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200325 return 1;
326 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200327 if (*args[2])
328 s->data_ctx.errors.iid = atoi(args[2]);
329 else
330 s->data_ctx.errors.iid = -1;
331 s->data_ctx.errors.px = NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200332 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200333 si->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200334 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200335 else { /* neither "stat" nor "info" nor "sess" nor "errors"*/
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200336 return 0;
337 }
338 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200339 else if (strcmp(args[0], "clear") == 0) {
340 if (strcmp(args[1], "counters") == 0) {
341 struct proxy *px;
342 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200343 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200344 int clrall = 0;
345
346 if (strcmp(args[2], "all") == 0)
347 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200348
Willy Tarreau6162db22009-10-10 17:13:00 +0200349 /* check permissions */
350 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
351 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200352 s->data_ctx.cli.msg = stats_permission_denied_msg;
353 si->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200354 return 1;
355 }
356
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200357 for (px = proxy; px; px = px->next) {
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200358 if (clrall)
359 memset(&px->counters, 0, sizeof(px->counters));
360 else {
361 px->counters.feconn_max = 0;
362 px->counters.beconn_max = 0;
Willy Tarreaud9b587f2010-02-26 10:05:55 +0100363 px->counters.fe_rps_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200364 px->counters.fe_sps_max = 0;
365 px->counters.be_sps_max = 0;
366 px->counters.nbpend_max = 0;
367 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200368
369 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200370 if (clrall)
371 memset(&sv->counters, 0, sizeof(sv->counters));
372 else {
373 sv->counters.cur_sess_max = 0;
374 sv->counters.nbpend_max = 0;
375 sv->counters.sps_max = 0;
376 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200377
378 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200379 if (li->counters) {
380 if (clrall)
381 memset(li->counters, 0, sizeof(*li->counters));
382 else
383 li->counters->conn_max = 0;
384 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200385 }
386
387 return 1;
388 }
389 else {
390 return 0;
391 }
392 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200393 else if (strcmp(args[0], "get") == 0) {
394 if (strcmp(args[1], "weight") == 0) {
395 struct proxy *px;
396 struct server *sv;
397
398 /* split "backend/server" and make <line> point to server */
399 for (line = args[2]; *line; line++)
400 if (*line == '/') {
401 *line++ = '\0';
402 break;
403 }
404
405 if (!*line) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200406 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
407 si->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200408 return 1;
409 }
410
411 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200412 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
413 si->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200414 return 1;
415 }
416
417 /* return server's effective weight at the moment */
418 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
419 buffer_feed(si->ib, trash);
420 return 1;
421 }
422 else { /* not "get weight" */
423 return 0;
424 }
425 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200426 else if (strcmp(args[0], "set") == 0) {
427 if (strcmp(args[1], "weight") == 0) {
428 struct proxy *px;
429 struct server *sv;
430 int w;
431
432 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200433 s->data_ctx.cli.msg = stats_permission_denied_msg;
434 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200435 return 1;
436 }
437
438 /* split "backend/server" and make <line> point to server */
439 for (line = args[2]; *line; line++)
440 if (*line == '/') {
441 *line++ = '\0';
442 break;
443 }
444
445 if (!*line || !*args[3]) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200446 s->data_ctx.cli.msg = "Require 'backend/server' and 'weight' or 'weight%'.\n";
447 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200448 return 1;
449 }
450
451 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200452 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
453 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200454 return 1;
455 }
456
457 /* if the weight is terminated with '%', it is set relative to
458 * the initial weight, otherwise it is absolute.
459 */
460 w = atoi(args[3]);
461 if (strchr(args[3], '%') != NULL) {
462 if (w < 0 || w > 100) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200463 s->data_ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
464 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200465 return 1;
466 }
467 w = sv->iweight * w / 100;
468 }
469 else {
470 if (w < 0 || w > 256) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200471 s->data_ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
472 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200473 return 1;
474 }
475 }
476
477 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200478 s->data_ctx.cli.msg = "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
479 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200480 return 1;
481 }
482
483 sv->uweight = w;
484
485 if (px->lbprm.algo & BE_LB_PROP_DYN) {
486 /* we must take care of not pushing the server to full throttle during slow starts */
487 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
488 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
489 else
490 sv->eweight = BE_WEIGHT_SCALE;
491 sv->eweight *= sv->uweight;
492 } else {
493 sv->eweight = sv->uweight;
494 }
495
496 /* static LB algorithms are a bit harder to update */
497 if (px->lbprm.update_server_eweight)
498 px->lbprm.update_server_eweight(sv);
499 else if (sv->eweight)
500 px->lbprm.set_server_status_up(sv);
501 else
502 px->lbprm.set_server_status_down(sv);
503
504 return 1;
505 }
Willy Tarreau7aabd112010-01-26 10:59:06 +0100506 else if (strcmp(args[1], "timeout") == 0) {
507 if (strcmp(args[2], "cli") == 0) {
508 unsigned timeout;
509 const char *res;
510
511 if (!*args[3]) {
512 s->data_ctx.cli.msg = "Expects an integer value.\n";
513 si->st0 = STAT_CLI_PRINT;
514 return 1;
515 }
516
517 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
518 if (res || timeout < 1) {
519 s->data_ctx.cli.msg = "Invalid timeout value.\n";
520 si->st0 = STAT_CLI_PRINT;
521 return 1;
522 }
523
524 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
525 return 1;
526 }
527 else {
528 s->data_ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
529 si->st0 = STAT_CLI_PRINT;
530 return 1;
531 }
532 }
533 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +0200534 return 0;
535 }
536 }
Cyril Bontécd19e512010-01-31 22:34:03 +0100537 else if (strcmp(args[0], "enable") == 0) {
538 if (strcmp(args[1], "server") == 0) {
539 struct proxy *px;
540 struct server *sv;
541
542 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
543 s->data_ctx.cli.msg = stats_permission_denied_msg;
544 si->st0 = STAT_CLI_PRINT;
545 return 1;
546 }
547
548 /* split "backend/server" and make <line> point to server */
549 for (line = args[2]; *line; line++)
550 if (*line == '/') {
551 *line++ = '\0';
552 break;
553 }
554
555 if (!*line || !*args[2]) {
556 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
557 si->st0 = STAT_CLI_PRINT;
558 return 1;
559 }
560
561 if (!get_backend_server(args[2], line, &px, &sv)) {
562 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
563 si->st0 = STAT_CLI_PRINT;
564 return 1;
565 }
566
567 if (sv->state & SRV_MAINTAIN) {
568 /* The server is really in maintenance, we can change the server state */
569 if (sv->tracked) {
570 /* If this server tracks the status of another one,
571 * we must restore the good status.
572 */
573 if (sv->tracked->state & SRV_RUNNING) {
574 set_server_up(sv);
575 } else {
576 sv->state &= ~SRV_MAINTAIN;
577 set_server_down(sv);
578 }
579 } else {
580 set_server_up(sv);
581 }
582 }
583
584 return 1;
585 }
586 else { /* unknown "enable" parameter */
587 return 0;
588 }
589 }
590 else if (strcmp(args[0], "disable") == 0) {
591 if (strcmp(args[1], "server") == 0) {
592 struct proxy *px;
593 struct server *sv;
594
595 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
596 s->data_ctx.cli.msg = stats_permission_denied_msg;
597 si->st0 = STAT_CLI_PRINT;
598 return 1;
599 }
600
601 /* split "backend/server" and make <line> point to server */
602 for (line = args[2]; *line; line++)
603 if (*line == '/') {
604 *line++ = '\0';
605 break;
606 }
607
608 if (!*line || !*args[2]) {
609 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
610 si->st0 = STAT_CLI_PRINT;
611 return 1;
612 }
613
614 if (!get_backend_server(args[2], line, &px, &sv)) {
615 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
616 si->st0 = STAT_CLI_PRINT;
617 return 1;
618 }
619
620 if (! (sv->state & SRV_MAINTAIN)) {
621 /* Not already in maintenance, we can change the server state */
622 sv->state |= SRV_MAINTAIN;
623 set_server_down(sv);
624 }
625
626 return 1;
627 }
628 else { /* unknown "disable" parameter */
629 return 0;
630 }
631 }
632 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200633 return 0;
634 }
635 return 1;
636}
637
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200638/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200639 * used to processes I/O from/to the stats unix socket. The system relies on a
640 * state machine handling requests and various responses. We read a request,
641 * then we process it and send the response, and we possibly display a prompt.
642 * Then we can read again. The state is stored in si->st0 and is one of the
643 * STAT_CLI_* constants. si->st1 is used to indicate whether prompt is enabled
644 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200645 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200646void stats_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200647{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200648 struct session *s = si->private;
649 struct buffer *req = si->ob;
650 struct buffer *res = si->ib;
651 int reql;
652 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200653
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200654 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
655 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200656
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200657 while (1) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200658 if (si->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200659 /* Stats output not initialized yet */
660 memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats));
661 s->data_source = DATA_SRC_STATS;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200662 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200663 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200664 else if (si->st0 == STAT_CLI_END) {
665 /* Let's close for real now. We just close the request
666 * side, the conditions below will complete if needed.
667 */
668 si->shutw(si);
669 break;
670 }
671 else if (si->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +0200672 /* ensure we have some output room left in the event we
673 * would want to return some info right after parsing.
674 */
675 if (buffer_almost_full(si->ib))
676 break;
677
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200678 reql = buffer_si_peekline(si->ob, trash, sizeof(trash));
679 if (reql <= 0) { /* closed or EOL not found */
680 if (reql == 0)
681 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200682 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200683 continue;
684 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200685
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200686 /* seek for a possible semi-colon. If we find one, we
687 * replace it with an LF and skip only this part.
688 */
689 for (len = 0; len < reql; len++)
690 if (trash[len] == ';') {
691 trash[len] = '\n';
692 reql = len + 1;
693 break;
694 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200695
Willy Tarreau816fc222009-10-04 07:36:58 +0200696 /* now it is time to check that we have a full line,
697 * remove the trailing \n and possibly \r, then cut the
698 * line.
699 */
700 len = reql - 1;
701 if (trash[len] != '\n') {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200702 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200703 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200704 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200705
Willy Tarreau816fc222009-10-04 07:36:58 +0200706 if (len && trash[len-1] == '\r')
707 len--;
708
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200709 trash[len] = '\0';
710
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200711 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200712 if (len) {
713 if (strcmp(trash, "quit") == 0) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200714 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200715 continue;
716 }
717 else if (strcmp(trash, "prompt") == 0)
718 si->st1 = !si->st1;
719 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200720 !stats_sock_parse_request(si, trash)) {
721 s->data_ctx.cli.msg = stats_sock_usage_msg;
722 si->st0 = STAT_CLI_PRINT;
723 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200724 /* NB: stats_sock_parse_request() may have put
725 * another STAT_CLI_O_* into si->st0.
726 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200727 }
728 else if (!si->st1) {
729 /* if prompt is disabled, print help on empty lines,
730 * so that the user at least knows how to enable
731 * prompt and find help.
732 */
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200733 s->data_ctx.cli.msg = stats_sock_usage_msg;
734 si->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200735 }
736
737 /* re-adjust req buffer */
738 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200739 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200740 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200741 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200742 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200743 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200744 continue;
745 }
746
747 switch (si->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200748 case STAT_CLI_PRINT:
749 if (buffer_feed(si->ib, s->data_ctx.cli.msg) < 0)
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200750 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200751 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200752 case STAT_CLI_O_INFO:
Willy Tarreau24955a12009-10-04 11:54:04 +0200753 if (stats_dump_raw_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200754 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200755 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200756 case STAT_CLI_O_SESS:
Willy Tarreau7e72a8f2009-10-03 23:55:14 +0200757 if (stats_dump_sess_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200758 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200759 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200760 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau61b34732009-10-03 23:49:35 +0200761 if (stats_dump_errors_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200762 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200763 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200764 default: /* abnormal state */
765 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200766 break;
767 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200768
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200769 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
770 if (si->st0 == STAT_CLI_PROMPT) {
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200771 if (buffer_feed(si->ib, si->st1 ? "\n> " : "\n") < 0)
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200772 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200773 }
774
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200775 /* If the output functions are still there, it means they require more room. */
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200776 if (si->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200777 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200778
779 /* Now we close the output if one of the writers did so,
780 * or if we're not in interactive mode and the request
781 * buffer is empty. This still allows pipelined requests
782 * to be sent in non-interactive mode.
783 */
784 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->st1 && !req->send_max)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200785 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200786 continue;
787 }
788
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200789 /* switch state back to GETREQ to read next requests */
790 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200791 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200792 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200793
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200794 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200795 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
796 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
797 /* Other size has closed, let's abort if we have no more processing to do
798 * and nothing more to consume. This is comparable to a broken pipe, so
799 * we forward the close to the request side so that it flows upstream to
800 * the client.
801 */
802 si->shutw(si);
803 }
804
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200805 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200806 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
807 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
808 /* We have no more processing to do, and nothing more to send, and
809 * the client side has closed. So we'll forward this state downstream
810 * on the response buffer.
811 */
812 si->shutr(si);
813 res->flags |= BF_READ_NULL;
814 }
815
816 /* update all other flags and resync with the other side */
817 si->update(si);
818
819 /* we don't want to expire timeouts while we're processing requests */
820 si->ib->rex = TICK_ETERNITY;
821 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200822
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200823 out:
824 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
825 __FUNCTION__, __LINE__,
826 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
827
828 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
829 /* check that we have released everything then unregister */
830 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200831 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200832}
833
Willy Tarreau24955a12009-10-04 11:54:04 +0200834/* This function is called to send output to the response buffer.
835 * It dumps statistics onto the output buffer <rep> owned by session <s>.
836 * s->data_ctx must have been zeroed first, and the flags properly set.
837 * It returns 0 as long as it does not complete, non-zero upon completion.
838 * Some states are not used but it makes the code more similar to other
839 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200840 */
Willy Tarreau24955a12009-10-04 11:54:04 +0200841int stats_dump_raw_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3e76e722007-10-17 18:57:38 +0200842{
Willy Tarreau3e76e722007-10-17 18:57:38 +0200843 struct proxy *px;
844 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +0100845 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200846
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200847 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +0200848
849 switch (s->data_state) {
850 case DATA_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +0200851 /* the function had not been called yet */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200852 s->data_state = DATA_ST_HEAD;
853 /* fall through */
854
855 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100856 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200857 print_csv_header(&msg);
Willy Tarreau24955a12009-10-04 11:54:04 +0200858 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100859 return 0;
860 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200861
862 s->data_state = DATA_ST_INFO;
863 /* fall through */
864
865 case DATA_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +0100866 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100867 if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200868 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +0100869 "Name: " PRODUCT_NAME "\n"
870 "Version: " HAPROXY_VERSION "\n"
871 "Release_date: " HAPROXY_DATE "\n"
872 "Nbproc: %d\n"
873 "Process_num: %d\n"
874 "Pid: %d\n"
875 "Uptime: %dd %dh%02dm%02ds\n"
876 "Uptime_sec: %d\n"
877 "Memmax_MB: %d\n"
878 "Ulimit-n: %d\n"
879 "Maxsock: %d\n"
880 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100881 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100882 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100883 "PipesUsed: %d\n"
884 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100885 "Tasks: %d\n"
886 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200887 "node: %s\n"
888 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100889 "",
890 global.nbproc,
891 relative_pid,
892 pid,
893 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
894 up,
895 global.rlimit_memmax,
896 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100897 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100898 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200899 nb_tasks_cur, run_queue_cur,
900 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +0100901 );
Willy Tarreau24955a12009-10-04 11:54:04 +0200902 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100903 return 0;
904 }
905
Willy Tarreau3e76e722007-10-17 18:57:38 +0200906 s->data_ctx.stats.px = proxy;
907 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100908
909 s->data_ctx.stats.sv = NULL;
910 s->data_ctx.stats.sv_st = 0;
911
Willy Tarreau3e76e722007-10-17 18:57:38 +0200912 s->data_state = DATA_ST_LIST;
913 /* fall through */
914
915 case DATA_ST_LIST:
916 /* dump proxies */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100917 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100918 while (s->data_ctx.stats.px) {
919 px = s->data_ctx.stats.px;
920 /* skip the disabled proxies and non-networked ones */
921 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +0200922 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100923 if (stats_dump_proxy(s, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100924 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +0200925 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200926
Willy Tarreaua8efd362008-01-03 10:19:15 +0100927 s->data_ctx.stats.px = px->next;
928 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
929 }
930 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200931 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200932
933 s->data_state = DATA_ST_END;
934 /* fall through */
935
936 case DATA_ST_END:
937 s->data_state = DATA_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +0100938 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200939
940 case DATA_ST_FIN:
941 return 1;
942
943 default:
944 /* unknown state ! */
Willy Tarreau24955a12009-10-04 11:54:04 +0200945 s->data_state = DATA_ST_FIN;
946 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200947 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100948}
949
950
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200951/* This I/O handler runs as an applet embedded in a stream interface. It is
952 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
953 * si->st0 becomes non-zero once the transfer is finished. The handler
954 * automatically unregisters itself once transfer is complete.
955 */
956void http_stats_io_handler(struct stream_interface *si)
957{
958 struct session *s = si->private;
959 struct buffer *req = si->ob;
960 struct buffer *res = si->ib;
961
962 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
963 goto out;
964
965 /* check that the output is not closed */
966 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
967 si->st0 = 1;
968
969 if (!si->st0) {
970 if (stats_dump_http(s, res, s->be->uri_auth)) {
971 si->st0 = 1;
972 si->shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200973 }
974 }
975
976 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
977 si->shutw(si);
978
979 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->st0) {
980 si->shutr(si);
981 res->flags |= BF_READ_NULL;
982 }
983
984 /* update all other flags and resync with the other side */
985 si->update(si);
986
987 /* we don't want to expire timeouts while we're processing requests */
988 si->ib->rex = TICK_ETERNITY;
989 si->ob->wex = TICK_ETERNITY;
990
991 out:
992 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
993 /* check that we have released everything then unregister */
994 stream_int_unregister_handler(si);
995 }
996}
997
998
Willy Tarreau3e76e722007-10-17 18:57:38 +0200999/*
1000 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +02001001 * client socket shut down on input. It stops by itself by unsetting the
Willy Tarreau72b179a2008-08-28 16:01:32 +02001002 * BF_HIJACK flag from the buffer, which it uses to keep on being called
Willy Tarreau1ae3a052008-08-16 10:56:30 +02001003 * when there is free space in the buffer, of simply by letting an empty buffer
1004 * upon return.s->data_ctx must have been zeroed before the first call, and the
1005 * flags set. It returns 0 if it had to stop writing data and an I/O is needed,
1006 * 1 if the dump is finished and the session must be closed, or -1 in case of
1007 * any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001008 */
Willy Tarreau0a464892008-12-07 18:30:00 +01001009int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001010{
Willy Tarreau91861262007-10-17 17:06:05 +02001011 struct proxy *px;
1012 struct chunk msg;
1013 unsigned int up;
1014
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001015 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001016
1017 switch (s->data_state) {
1018 case DATA_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001019 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001020 "HTTP/1.0 200 OK\r\n"
1021 "Cache-Control: no-cache\r\n"
1022 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001023 "Content-Type: %s\r\n",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001024 (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001025
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001026 if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001027 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001028 uri->refresh);
1029
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001030 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001031
1032 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001033 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001034 return 0;
1035
Willy Tarreau91861262007-10-17 17:06:05 +02001036 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1037 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1038 if (!(s->flags & SN_FINST_MASK))
1039 s->flags |= SN_FINST_R;
1040
1041 if (s->txn.meth == HTTP_METH_HEAD) {
1042 /* that's all we return in case of HEAD request */
1043 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001044 return 1;
1045 }
1046
1047 s->data_state = DATA_ST_HEAD; /* let's start producing data */
1048 /* fall through */
1049
1050 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001051 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001052 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001053 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001054 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1055 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001056 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001057 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1058 "<style type=\"text/css\"><!--\n"
1059 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001060 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001061 " font-size: 12px;"
1062 " font-weight: normal;"
1063 " color: black;"
1064 " background: white;"
1065 "}\n"
1066 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001067 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001068 "}\n"
1069 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001070 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001071 " margin-bottom: 0.5em;"
1072 "}\n"
1073 "h2 {"
1074 " font-family: helvetica, arial;"
1075 " font-size: x-large;"
1076 " font-weight: bold;"
1077 " font-style: italic;"
1078 " color: #6020a0;"
1079 " margin-top: 0em;"
1080 " margin-bottom: 0em;"
1081 "}\n"
1082 "h3 {"
1083 " font-family: helvetica, arial;"
1084 " font-size: 16px;"
1085 " font-weight: bold;"
1086 " color: #b00040;"
1087 " background: #e8e8d0;"
1088 " margin-top: 0em;"
1089 " margin-bottom: 0em;"
1090 "}\n"
1091 "li {"
1092 " margin-top: 0.25em;"
1093 " margin-right: 2em;"
1094 "}\n"
1095 ".hr {margin-top: 0.25em;"
1096 " border-color: black;"
1097 " border-bottom-style: solid;"
1098 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001099 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001100 ".total {background: #20D0D0;color: #ffff80;}\n"
1101 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001102 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001103 ".backend {background: #e8e8d0;}\n"
1104 ".active0 {background: #ff9090;}\n"
1105 ".active1 {background: #ffd020;}\n"
1106 ".active2 {background: #ffffa0;}\n"
1107 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001108 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1109 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1110 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001111 ".backup0 {background: #ff9090;}\n"
1112 ".backup1 {background: #ff80ff;}\n"
1113 ".backup2 {background: #c060ff;}\n"
1114 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001115 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1116 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1117 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001118 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001119 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001120 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001121 "a.px:link {color: #ffff40; text-decoration: none;}"
1122 "a.px:visited {color: #ffff40; text-decoration: none;}"
1123 "a.px:hover {color: #ffffff; text-decoration: none;}"
1124 "a.lfsb:link {color: #000000; text-decoration: none;}"
1125 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1126 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1127 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001128 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001129 "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"
1130 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001131 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001132 "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 +02001133 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001134 "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 +02001135 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001136 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1137 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1138 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001139 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001140 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001141 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001142 (uri->flags&ST_SHNODE) ? " on " : "",
1143 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001144 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001145 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001146 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001147 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001148 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001149 return 0;
1150
1151 s->data_state = DATA_ST_INFO;
1152 /* fall through */
1153
1154 case DATA_ST_INFO:
1155 up = (now.tv_sec - start_date.tv_sec);
1156
1157 /* WARNING! this has to fit the first packet too.
1158 * We are around 3.5 kB, add adding entries will
1159 * become tricky if we want to support 4kB buffers !
1160 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001161 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001162 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001163 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1164 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001165 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001166 "<hr width=\"100%%\" class=\"hr\">\n"
1167 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001168 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001169 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001170 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001171 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1172 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
1173 "current conns = %d; current pipes = %d/%d<br>\n"
1174 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001175 "</td><td align=\"center\" nowrap>\n"
1176 "<table class=\"lgd\"><tr>\n"
1177 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1178 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1179 "</tr><tr>\n"
1180 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1181 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1182 "</tr><tr>\n"
1183 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1184 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1185 "</tr><tr>\n"
1186 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001187 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001188 "</tr><tr>\n"
1189 "<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 +02001190 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001191 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001192 "</td>"
1193 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1194 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1195 "",
1196 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001197 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1198 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1199 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001200 up / 86400, (up % 86400) / 3600,
1201 (up % 3600) / 60, (up % 60),
1202 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1203 global.rlimit_memmax ? " MB" : "",
1204 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001205 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001206 actconn, pipes_used, pipes_used+pipes_free,
1207 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +02001208 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001209
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001210 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001211 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001212 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1213 uri->uri_prefix,
1214 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001215 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001216 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001217 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001218 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1219 uri->uri_prefix,
1220 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001221 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001222
Willy Tarreau55bb8452007-10-17 18:44:57 +02001223 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001224 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001225 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001226 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1227 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001228 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001229 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001230 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001231 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001232 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1233 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001234 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001235 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001236 }
Willy Tarreau91861262007-10-17 17:06:05 +02001237
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001238 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001239 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
1240 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001241 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
1242 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001243
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001244 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02001245 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
1246 uri->uri_prefix,
1247 (uri->refresh > 0) ? ";norefresh" : "");
1248
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001249 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001250 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001251 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1252 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
1253 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
1254 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
1255 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
1256 "</ul>"
1257 "</td>"
1258 "</tr></table>\n"
1259 ""
1260 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001261
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001262 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001263 return 0;
1264 }
Willy Tarreau91861262007-10-17 17:06:05 +02001265
Willy Tarreau91861262007-10-17 17:06:05 +02001266 s->data_ctx.stats.px = proxy;
1267 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1268 s->data_state = DATA_ST_LIST;
1269 /* fall through */
1270
1271 case DATA_ST_LIST:
1272 /* dump proxies */
1273 while (s->data_ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001274 if (buffer_almost_full(rep))
1275 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02001276 px = s->data_ctx.stats.px;
1277 /* skip the disabled proxies and non-networked ones */
1278 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001279 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001280 return 0;
1281
1282 s->data_ctx.stats.px = px->next;
1283 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1284 }
1285 /* here, we just have reached the last proxy */
1286
1287 s->data_state = DATA_ST_END;
1288 /* fall through */
1289
1290 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001291 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001292 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001293 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001294 return 0;
1295 }
Willy Tarreau91861262007-10-17 17:06:05 +02001296
1297 s->data_state = DATA_ST_FIN;
1298 /* fall through */
1299
1300 case DATA_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02001301 return 1;
1302
1303 default:
1304 /* unknown state ! */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001305 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001306 return -1;
1307 }
1308}
1309
1310
1311/*
1312 * Dumps statistics for a proxy.
1313 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1314 * ot non-zero if everything completed.
1315 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001316int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001317{
1318 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001319 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001320 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02001321 struct chunk msg;
1322
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001323 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001324
1325 switch (s->data_ctx.stats.px_st) {
1326 case DATA_ST_PX_INIT:
1327 /* we are on a new proxy */
1328
1329 if (uri && uri->scope) {
1330 /* we have a limited scope, we have to check the proxy name */
1331 struct stat_scope *scope;
1332 int len;
1333
1334 len = strlen(px->id);
1335 scope = uri->scope;
1336
1337 while (scope) {
1338 /* match exact proxy name */
1339 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1340 break;
1341
1342 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001343 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001344 break;
1345 scope = scope->next;
1346 }
1347
1348 /* proxy name not found : don't dump anything */
1349 if (scope == NULL)
1350 return 1;
1351 }
1352
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001353 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001354 (px->uuid != s->data_ctx.stats.iid))
1355 return 1;
1356
Willy Tarreau91861262007-10-17 17:06:05 +02001357 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
1358 /* fall through */
1359
1360 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001361 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001362 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001363 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001364 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001365 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001366 "<th class=\"pxname\" width=\"10%%\"");
1367
1368 if (uri->flags&ST_SHLGNDS) {
1369 /* cap, mode, id */
1370 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
1371 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1372 px->uuid);
1373
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001374 chunk_printf(&msg, "\"");
1375 }
1376
1377 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001378 ">%s<a name=\"%s\"></a>"
1379 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001380 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001381 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001382 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001383 "<table class=\"tbl\" width=\"100%%\">\n"
1384 "<tr class=\"titre\">"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001385 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001386 "<th colspan=3>Queue</th>"
1387 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001388 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001389 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001390 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001391 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001392 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001393 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001394 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001395 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001396 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001397 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001398 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001399 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1400 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001401 "</tr>",
Willy Tarreaue0454092010-02-26 12:29:07 +01001402 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001403 px->id, px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01001404 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001405 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001406
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001407 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001408 return 0;
1409 }
Willy Tarreau91861262007-10-17 17:06:05 +02001410
1411 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
1412 /* fall through */
1413
1414 case DATA_ST_PX_FE:
1415 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001416 if ((px->cap & PR_CAP_FE) &&
1417 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
1418 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001419 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001420 /* name, queue */
Krzysztof Piotr Oledzki516ed492009-10-22 22:48:09 +02001421 "<tr class=\"frontend\"><td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001422 "<a name=\"%s/Frontend\"></a>"
1423 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td><td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01001424 "",
1425 px->id, px->id);
1426
1427 if (px->mode == PR_MODE_HTTP) {
1428 chunk_printf(&msg,
1429 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01001430 "<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 +01001431 "",
1432 read_freq_ctr(&px->fe_req_per_sec),
1433 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
1434 px->counters.fe_rps_max,
1435 U2H2(px->counters.fe_sps_max),
1436 LIM2A2(px->fe_sps_lim, "-"));
1437 } else {
1438 chunk_printf(&msg,
1439 /* sessions rate : current, max, limit */
1440 "<td>%s</td><td>%s</td><td>%s</td>"
1441 "",
1442 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
1443 U2H1(px->counters.fe_sps_max), LIM2A2(px->fe_sps_lim, "-"));
1444 }
1445
1446 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001447 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001448 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001449 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001450 "",
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001451 U2H3(px->feconn), U2H4(px->counters.feconn_max), U2H5(px->maxconn));
1452
1453 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1454 if (px->mode == PR_MODE_HTTP) {
1455 int i;
1456
Willy Tarreaub44939a2010-02-26 11:35:39 +01001457 chunk_printf(&msg, " title=\"%lld requests:", px->counters.cum_fe_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001458
1459 for (i = 1; i < 6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01001460 chunk_printf(&msg, " %dxx=%lld,", i, px->counters.fe.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001461
Willy Tarreau24657792010-02-26 10:30:28 +01001462 chunk_printf(&msg, " other=%lld\"", px->counters.fe.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001463 }
1464
1465 chunk_printf(&msg,
1466 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01001467 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001468 /* bytes : in, out */
1469 "<td>%s</td><td>%s</td>"
1470 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001471 (px->mode == PR_MODE_HTTP)?"<u>":"",
1472 U2H6(px->counters.cum_feconn),
1473 (px->mode == PR_MODE_HTTP)?"</u>":"",
1474 U2H7(px->counters.bytes_in), U2H8(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001475
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001476 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001477 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001478 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001479 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001480 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001481 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001482 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001483 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001484 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001485 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001486 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02001487 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001488 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
1489 U2H2(px->counters.failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +02001490 px->state == PR_STRUN ? "OPEN" :
1491 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001492 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001493 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001494 /* pxid, name, queue cur, queue max, */
1495 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001496 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001497 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001498 /* bytes : in, out */
1499 "%lld,%lld,"
1500 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001501 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001502 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001503 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001504 /* warnings: retries, redispatches */
1505 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001506 /* server status : reflect frontend status */
1507 "%s,"
1508 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001509 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001510 /* pid, iid, sid, throttle, lbtot, tracked, type */
1511 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001512 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001513 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001514 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001515 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02001516 px->id,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001517 px->feconn, px->counters.feconn_max, px->maxconn, px->counters.cum_feconn,
1518 px->counters.bytes_in, px->counters.bytes_out,
1519 px->counters.denied_req, px->counters.denied_resp,
1520 px->counters.failed_req,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001521 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +01001522 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01001523 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001524 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001525 px->fe_sps_lim, px->counters.fe_sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001526
1527 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1528 if (px->mode == PR_MODE_HTTP) {
1529 int i;
1530
1531 for (i=1; i<6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01001532 chunk_printf(&msg, "%lld,", px->counters.fe.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001533
Willy Tarreau24657792010-02-26 10:30:28 +01001534 chunk_printf(&msg, "%lld,", px->counters.fe.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001535 } else {
1536 chunk_printf(&msg, ",,,,,,");
1537 }
1538
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001539 /* failed health analyses */
1540 chunk_printf(&msg, ",");
1541
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001542 /* requests : req_rate, req_rate_max, req_tot, */
1543 chunk_printf(&msg, "%u,%u,%lld,",
1544 read_freq_ctr(&px->fe_req_per_sec),
1545 px->counters.fe_rps_max, px->counters.cum_fe_req);
1546
Willy Tarreauae526782010-03-04 20:34:23 +01001547 /* errors: cli_aborts, srv_aborts */
1548 chunk_printf(&msg, ",,");
1549
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001550 /* finish with EOL */
1551 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001552 }
Willy Tarreau91861262007-10-17 17:06:05 +02001553
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001554 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001555 return 0;
1556 }
1557
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001558 s->data_ctx.stats.l = px->listen; /* may be NULL */
1559 s->data_ctx.stats.px_st = DATA_ST_PX_LI;
1560 /* fall through */
1561
1562 case DATA_ST_PX_LI:
1563 /* stats.l has been initialized above */
1564 for (; s->data_ctx.stats.l != NULL; s->data_ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001565 if (buffer_almost_full(rep))
1566 return 0;
1567
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001568 l = s->data_ctx.stats.l;
1569 if (!l->counters)
1570 continue;
1571
1572 if (s->data_ctx.stats.flags & STAT_BOUND) {
1573 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SO)))
1574 break;
1575
1576 if (s->data_ctx.stats.sid != -1 && l->luid != s->data_ctx.stats.sid)
1577 continue;
1578 }
1579
1580 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001581 chunk_printf(&msg, "<tr class=socket><td class=ac");
1582
1583 if (uri->flags&ST_SHLGNDS) {
1584 char str[INET6_ADDRSTRLEN], *fmt = NULL;
1585 int port;
1586
1587 chunk_printf(&msg, " title=\"IP: ");
1588
1589 port = (l->addr.ss_family == AF_INET6)
1590 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
1591 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
1592
1593 if (l->addr.ss_family == AF_INET) {
1594 if (inet_ntop(AF_INET,
1595 (const void *)&((struct sockaddr_in *)&l->addr)->sin_addr,
1596 str, sizeof(str)))
1597 fmt = "%s:%d";
1598 } else {
1599 if (inet_ntop(AF_INET6,
1600 (const void *)&((struct sockaddr_in6 *)(&l->addr))->sin6_addr,
1601 str, sizeof(str)))
1602 fmt = "[%s]:%d";
1603 }
1604
1605 if (fmt)
1606 chunk_printf(&msg, fmt, str, port);
1607 else
1608 chunk_printf(&msg, "(%s)", strerror(errno));
1609
1610 /* id */
1611 chunk_printf(&msg, ", id: %d", l->luid);
1612
1613 chunk_printf(&msg, "\"");
1614 }
1615
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001616 chunk_printf(&msg,
1617 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01001618 ">%s<a name=\"%s/+%s\"></a>"
1619 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001620 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001621 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001622 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001623 "<td>%s</td><td>%s</td><td>%s</td>"
1624 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001625 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001626 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001627 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001628 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01001629 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01001630 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001631 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
1632 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
1633
1634 chunk_printf(&msg,
1635 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001636 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001637 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001638 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001639 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001640 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001641 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001642 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001643 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001644 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001645 "",
1646 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
1647 U2H2(l->counters->failed_req),
1648 (l->nbconn < l->maxconn) ? "OPEN" : "FULL");
1649 } else {
1650 chunk_printf(&msg,
1651 /* pxid, name, queue cur, queue max, */
1652 "%s,%s,,,"
1653 /* sessions: current, max, limit, total */
1654 "%d,%d,%d,%lld,"
1655 /* bytes: in, out */
1656 "%lld,%lld,"
1657 /* denied: req, resp */
1658 "%lld,%lld,"
1659 /* errors: request, connect, response */
1660 "%lld,,,"
1661 /* warnings: retries, redispatches */
1662 ",,"
1663 /* server status: reflect listener status */
1664 "%s,"
1665 /* rest of server: nothing */
1666 ",,,,,,,,"
1667 /* pid, iid, sid, throttle, lbtot, tracked, type */
1668 "%d,%d,%d,,,,%d,"
1669 /* rate, rate_lim, rate_max */
1670 ",,,"
1671 /* check_status, check_code, check_duration */
1672 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001673 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1674 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001675 /* failed health analyses */
1676 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001677 /* requests : req_rate, req_rate_max, req_tot, */
1678 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01001679 /* errors: cli_aborts, srv_aborts */
1680 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001681 "\n",
1682 px->id, l->name,
1683 l->nbconn, l->counters->conn_max,
1684 l->maxconn, l->counters->cum_conn,
1685 l->counters->bytes_in, l->counters->bytes_out,
1686 l->counters->denied_req, l->counters->denied_resp,
1687 l->counters->failed_req,
1688 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
1689 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
1690 }
1691
1692 if (buffer_feed_chunk(rep, &msg) >= 0)
1693 return 0;
1694 }
1695
Willy Tarreau91861262007-10-17 17:06:05 +02001696 s->data_ctx.stats.sv = px->srv; /* may be NULL */
1697 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
1698 /* fall through */
1699
1700 case DATA_ST_PX_SV:
1701 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001702 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001703 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 +02001704
Willy Tarreau4e33d862009-10-11 23:35:10 +02001705 if (buffer_almost_full(rep))
1706 return 0;
1707
Willy Tarreau91861262007-10-17 17:06:05 +02001708 sv = s->data_ctx.stats.sv;
1709
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001710 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001711 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
1712 break;
1713
1714 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
1715 continue;
1716 }
1717
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001718 if (sv->tracked)
1719 svs = sv->tracked;
1720 else
1721 svs = sv;
1722
Willy Tarreau91861262007-10-17 17:06:05 +02001723 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001724 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01001725 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001726 else if (svs->state & SRV_RUNNING) {
1727 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02001728 sv_state = 3; /* UP */
1729 else
1730 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01001731
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001732 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01001733 sv_state += 2;
1734 }
Willy Tarreau91861262007-10-17 17:06:05 +02001735 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001736 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02001737 sv_state = 1; /* going up */
1738 else
1739 sv_state = 0; /* DOWN */
1740
Cyril Bonté0dae5852010-02-03 00:26:28 +01001741 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02001742 /* do not report servers which are DOWN */
1743 s->data_ctx.stats.sv = sv->next;
1744 continue;
1745 }
1746
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001747 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001748 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
1749 "UP %d/%d &darr;", "UP",
1750 "NOLB %d/%d &darr;", "NOLB",
1751 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01001752 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001753 chunk_printf(&msg,
1754 /* name */
1755 "<tr class=\"maintain\"><td class=ac"
1756 );
1757 }
1758 else {
1759 chunk_printf(&msg,
1760 /* name */
1761 "<tr class=\"%s%d\"><td class=ac",
1762 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
1763 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001764
1765 if (uri->flags&ST_SHLGNDS) {
1766 char str[INET6_ADDRSTRLEN];
1767
1768 chunk_printf(&msg, " title=\"IP: ");
1769
1770 /* IP */
1771 if (inet_ntop(sv->addr.sin_family, &sv->addr.sin_addr, str, sizeof(str)))
1772 chunk_printf(&msg, "%s:%d", str, htons(sv->addr.sin_port));
1773 else
1774 chunk_printf(&msg, "(%s)", strerror(errno));
1775
1776 /* id */
1777 chunk_printf(&msg, ", id: %d", sv->puid);
1778
1779 /* cookie */
1780 if (sv->cookie) {
1781 struct chunk src;
1782
1783 chunk_printf(&msg, ", cookie: '");
1784
1785 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
1786 chunk_htmlencode(&msg, &src);
1787
1788 chunk_printf(&msg, "'");
1789 }
1790
1791 chunk_printf(&msg, "\"");
1792 }
1793
1794 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001795 ">%s<a name=\"%s/%s\"></a>"
1796 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001797 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001798 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001799 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001800 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001801 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001802 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001803 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001804 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001805 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001806 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01001807 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001808 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
1809 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001810 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
1811
1812 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1813 if (px->mode == PR_MODE_HTTP) {
1814 int i;
1815
1816 chunk_printf(&msg, " title=\"rsp codes:");
1817
1818 for (i = 1; i < 6; i++)
1819 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
1820
1821 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
1822 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001823
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001824 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001825 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01001826 ">%s%s%s</td><td>%s</td>",
1827 (px->mode == PR_MODE_HTTP)?"<u>":"",
1828 U2H0(sv->counters.cum_sess),
1829 (px->mode == PR_MODE_HTTP)?"</u>":"",
1830 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001831
1832 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001833 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001834 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001835 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001836 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01001837 /* errors : request, connect */
1838 "<td></td><td>%s</td>"
1839 /* errors : response */
1840 "<td title=\"Connection resets during transfers: %s client, %s server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001841 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001842 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001843 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001844 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
1845 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01001846 U2H3(sv->counters.failed_conns),
1847 U2H4(sv->counters.cli_aborts),
1848 U2H5(sv->counters.srv_aborts),
1849 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001850 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001851
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001852 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001853 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001854
Cyril Bontécd19e512010-01-31 22:34:03 +01001855 if (sv->state & SRV_MAINTAIN) {
1856 chunk_printf(&msg, "%s ",
1857 human_time(now.tv_sec - sv->last_change, 1));
1858 chunk_printf(&msg, "MAINT");
1859 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01001860 else if (svs != sv && svs->state & SRV_MAINTAIN) {
1861 chunk_printf(&msg, "%s ",
1862 human_time(now.tv_sec - svs->last_change, 1));
1863 chunk_printf(&msg, "MAINT(via)");
1864 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001865 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001866 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01001867 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001868
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001869 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001870 srv_hlt_st[sv_state],
1871 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
1872 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001873 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001874
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001875 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001876 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001877 get_check_status_description(sv->check_status));
1878
1879 if (*sv->check_desc) {
1880 struct chunk src;
1881
1882 chunk_printf(&msg, ": ");
1883
1884 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
1885 chunk_htmlencode(&msg, &src);
1886 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001887
Willy Tarreaue0454092010-02-26 12:29:07 +01001888 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001889 tv_iszero(&sv->check_start)?"":"* ",
1890 get_check_status_info(sv->check_status));
1891
1892 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001893 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001894
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001895 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01001896 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001897 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001898 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02001899
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001900 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001901 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001902 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001903 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001904 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001905 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001906 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02001907 (sv->state & SRV_BACKUP) ? "-" : "Y",
1908 (sv->state & SRV_BACKUP) ? "Y" : "-");
1909
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001910 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01001911 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01001912 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01001913 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
1914
1915 if (svs->observe)
1916 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
1917
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001918 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001919 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001920 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001921 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001922 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01001923 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001924 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01001925 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
1926 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001927 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001928 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001929 "<td colspan=3></td>");
1930
1931 /* throttle */
1932 if ((sv->state & SRV_WARMINGUP) &&
1933 now.tv_sec < sv->last_change + sv->slowstart &&
1934 now.tv_sec >= sv->last_change) {
1935 unsigned int ratio;
1936 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001937 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001938 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001939 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001940 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001941 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001942 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02001943 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001944 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
1945 "UP %d/%d,", "UP,",
1946 "NOLB %d/%d,", "NOLB,",
1947 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001948 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001949 /* pxid, name */
1950 "%s,%s,"
1951 /* queue : current, max */
1952 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001953 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001954 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001955 /* bytes : in, out */
1956 "%lld,%lld,"
1957 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001958 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001959 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001960 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001961 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001962 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001963 "",
1964 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001965 sv->nbpend, sv->counters.nbpend_max,
1966 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001967 sv->counters.bytes_in, sv->counters.bytes_out,
1968 sv->counters.failed_secu,
1969 sv->counters.failed_conns, sv->counters.failed_resp,
1970 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001971
Willy Tarreau55bb8452007-10-17 18:44:57 +02001972 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01001973 if (sv->state & SRV_MAINTAIN) {
1974 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01001975 }
1976 else if (svs != sv && svs->state & SRV_MAINTAIN) {
1977 chunk_printf(&msg, "MAINT(via),");
1978 }
1979 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01001980 chunk_printf(&msg,
1981 srv_hlt_st[sv_state],
1982 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
1983 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
1984 }
Willy Tarreau91861262007-10-17 17:06:05 +02001985
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001986 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001987 /* weight, active, backup */
1988 "%d,%d,%d,"
1989 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001990 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001991 (sv->state & SRV_BACKUP) ? 0 : 1,
1992 (sv->state & SRV_BACKUP) ? 1 : 0);
1993
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001994 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001995 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001996 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02001997 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001998 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001999 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002000 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002001 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002002 ",,,,");
2003
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002004 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002005 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002006 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002007 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002008 LIM2A0(sv->maxqueue, ""),
2009 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002010
2011 /* throttle */
2012 if ((sv->state & SRV_WARMINGUP) &&
2013 now.tv_sec < sv->last_change + sv->slowstart &&
2014 now.tv_sec >= sv->last_change) {
2015 unsigned int ratio;
2016 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002017 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002018 }
2019
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002020 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002021 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002022
2023 /* tracked */
2024 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002025 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002026 sv->tracked->proxy->id, sv->tracked->id);
2027 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002028 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002029
Willy Tarreau7f062c42009-03-05 18:43:00 +01002030 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002031 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002032
2033 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002034 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002035 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002036 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002037
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002038 if (sv->state & SRV_CHECKED) {
2039 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002040 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002041
2042 /* check_code */
2043 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002044 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002045 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002046 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002047
2048 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002049 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002050 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002051 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002052 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002053
2054 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002055 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002056 }
2057
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002058 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2059 if (px->mode == PR_MODE_HTTP) {
2060 int i;
2061
2062 for (i=1; i<6; i++)
2063 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2064
2065 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2066 } else {
2067 chunk_printf(&msg, ",,,,,,");
2068 }
2069
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002070 /* failed health analyses */
2071 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2072
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002073 /* requests : req_rate, req_rate_max, req_tot, */
2074 chunk_printf(&msg, ",,,");
2075
Willy Tarreauae526782010-03-04 20:34:23 +01002076 /* errors: cli_aborts, srv_aborts */
2077 chunk_printf(&msg, "%lld,%lld,",
2078 sv->counters.cli_aborts, sv->counters.srv_aborts);
2079
Willy Tarreau7f062c42009-03-05 18:43:00 +01002080 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002081 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002082 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002083 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002084 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002085 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002086
2087 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
2088 /* fall through */
2089
2090 case DATA_ST_PX_BE:
2091 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002092 if ((px->cap & PR_CAP_BE) &&
2093 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2094 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002095 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002096 /* name */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002097 "<tr class=\"backend\"><td class=ac");
2098
2099 if (uri->flags&ST_SHLGNDS) {
2100 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002101 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002102 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002103
2104 /* cookie */
2105 if (px->cookie_name) {
2106 struct chunk src;
2107
2108 chunk_printf(&msg, ", cookie: '");
2109
2110 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2111 chunk_htmlencode(&msg, &src);
2112
2113 chunk_printf(&msg, "'");
2114 }
2115
2116 chunk_printf(&msg, "\"");
2117
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002118 }
2119
2120 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002121 ">%s<a name=\"%s/Backend\"></a>"
2122 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002123 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002124 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002125 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002126 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002127 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002128 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002129 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002130 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002131 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->counters.nbpend_max),
2132 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->counters.be_sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02002133
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002134 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002135 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002136 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002137 "<td"
2138 "",
2139 U2H2(px->beconn), U2H3(px->counters.beconn_max), U2H4(px->fullconn));
2140
2141 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2142 if (px->mode == PR_MODE_HTTP) {
2143 int i;
2144
2145 chunk_printf(&msg, " title=\"rsp codes:");
2146
2147 for (i = 1; i < 6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01002148 chunk_printf(&msg, " %dxx=%lld", i, px->counters.be.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002149
Willy Tarreau24657792010-02-26 10:30:28 +01002150 chunk_printf(&msg, " other=%lld\"", px->counters.be.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002151 }
2152
2153 chunk_printf(&msg,
2154 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002155 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002156 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002157 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002158 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002159 (px->mode == PR_MODE_HTTP)?"<u>":"",
2160 U2H6(px->counters.cum_beconn),
2161 (px->mode == PR_MODE_HTTP)?"</u>":"",
2162 U2H7(px->counters.cum_lbconn),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002163 U2H8(px->counters.bytes_in), U2H9(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002164
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002165 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002166 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002167 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002168 /* errors : request, connect */
2169 "<td></td><td>%s</td>"
2170 /* errors : response */
2171 "<td title=\"Connection resets during transfers: %s client, %s server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002172 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002173 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002174 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02002175 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002176 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02002177 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002178 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2179 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002180 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002181 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
Willy Tarreauae526782010-03-04 20:34:23 +01002182 U2H2(px->counters.failed_conns),
2183 U2H3(px->counters.cli_aborts),
2184 U2H4(px->counters.srv_aborts),
2185 U2H5(px->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002186 px->counters.retries, px->counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002187 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01002188 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2189 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01002190 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002191 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002192
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002193 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002194 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002195 "<td class=ac>&nbsp;</td><td>%d</td>"
2196 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002197 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002198 "</tr>",
2199 px->down_trans,
2200 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002201 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002202 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002203 /* pxid, name */
2204 "%s,BACKEND,"
2205 /* queue : current, max */
2206 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002207 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002208 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002209 /* bytes : in, out */
2210 "%lld,%lld,"
2211 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002212 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002213 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002214 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002215 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002216 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002217 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02002218 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002219 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02002220 * active and backups. */
2221 "%s,"
2222 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002223 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002224 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002225 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002226 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002227 /* rate, rate_lim, rate_max, */
2228 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002229 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002230 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002231 px->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002232 px->nbpend /* or px->totpend ? */, px->counters.nbpend_max,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002233 px->beconn, px->counters.beconn_max, px->fullconn, px->counters.cum_beconn,
2234 px->counters.bytes_in, px->counters.bytes_out,
2235 px->counters.denied_req, px->counters.denied_resp,
2236 px->counters.failed_conns, px->counters.failed_resp,
2237 px->counters.retries, px->counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01002238 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01002239 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002240 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002241 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01002242 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002243 relative_pid, px->uuid,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002244 px->counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002245 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002246 px->counters.be_sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002247
2248 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2249 if (px->mode == PR_MODE_HTTP) {
2250 int i;
2251
2252 for (i=1; i<6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01002253 chunk_printf(&msg, "%lld,", px->counters.be.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002254
Willy Tarreau24657792010-02-26 10:30:28 +01002255 chunk_printf(&msg, "%lld,", px->counters.be.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002256 } else {
2257 chunk_printf(&msg, ",,,,,,");
2258 }
2259
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002260 /* failed health analyses */
2261 chunk_printf(&msg, ",");
2262
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002263 /* requests : req_rate, req_rate_max, req_tot, */
2264 chunk_printf(&msg, ",,,");
2265
Willy Tarreauae526782010-03-04 20:34:23 +01002266 /* errors: cli_aborts, srv_aborts */
2267 chunk_printf(&msg, "%lld,%lld,",
2268 px->counters.cli_aborts, px->counters.srv_aborts);
2269
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002270 /* finish with EOL */
2271 chunk_printf(&msg, "\n");
2272
Willy Tarreau55bb8452007-10-17 18:44:57 +02002273 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002274 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002275 return 0;
2276 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002277
Willy Tarreau91861262007-10-17 17:06:05 +02002278 s->data_ctx.stats.px_st = DATA_ST_PX_END;
2279 /* fall through */
2280
2281 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002282 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002283 chunk_printf(&msg, "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02002284
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002285 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002286 return 0;
2287 }
Willy Tarreau91861262007-10-17 17:06:05 +02002288
2289 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
2290 /* fall through */
2291
2292 case DATA_ST_PX_FIN:
2293 return 1;
2294
2295 default:
2296 /* unknown state, we should put an abort() here ! */
2297 return 1;
2298 }
2299}
2300
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002301
2302/* This function is called to send output to the response buffer.
2303 * It dumps the sessions states onto the output buffer <rep>.
2304 * Expects to be called with client socket shut down on input.
2305 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002306 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002307 */
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002308int stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002309{
2310 struct chunk msg;
2311
2312 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
2313 /* If we're forced to shut down, we might have to remove our
2314 * reference to the last session being dumped.
2315 */
2316 if (s->data_state == DATA_ST_LIST) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002317 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002318 LIST_DEL(&s->data_ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002319 LIST_INIT(&s->data_ctx.sess.bref.users);
2320 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002321 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002322 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002323 }
2324
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002325 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002326
2327 switch (s->data_state) {
2328 case DATA_ST_INIT:
2329 /* the function had not been called yet, let's prepare the
2330 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002331 * pointer to the first in the global list. When a target
2332 * session is being destroyed, it is responsible for updating
2333 * this pointer. We know we have reached the end when this
2334 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002335 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002336 LIST_INIT(&s->data_ctx.sess.bref.users);
2337 s->data_ctx.sess.bref.ref = sessions.n;
2338 s->data_state = DATA_ST_LIST;
2339 /* fall through */
2340
2341 case DATA_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002342 /* first, let's detach the back-ref from a possible previous session */
2343 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
2344 LIST_DEL(&s->data_ctx.sess.bref.users);
2345 LIST_INIT(&s->data_ctx.sess.bref.users);
2346 }
2347
2348 /* and start from where we stopped */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002349 while (s->data_ctx.sess.bref.ref != &sessions) {
2350 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
2351 struct session *curr_sess;
2352
2353 curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list);
2354
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002355 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002356 "%p: proto=%s",
2357 curr_sess,
2358 curr_sess->listener->proto->name);
2359
2360 switch (curr_sess->listener->proto->sock_family) {
2361 case AF_INET:
2362 inet_ntop(AF_INET,
2363 (const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr,
2364 pn, sizeof(pn));
2365
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002366 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002367 " src=%s:%d fe=%s be=%s srv=%s",
2368 pn,
2369 ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port),
2370 curr_sess->fe->id,
2371 curr_sess->be->id,
2372 curr_sess->srv ? curr_sess->srv->id : "<none>"
2373 );
2374 break;
2375 case AF_INET6:
2376 inet_ntop(AF_INET6,
2377 (const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr,
2378 pn, sizeof(pn));
2379
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002380 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002381 " src=%s:%d fe=%s be=%s srv=%s",
2382 pn,
2383 ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port),
2384 curr_sess->fe->id,
2385 curr_sess->be->id,
2386 curr_sess->srv ? curr_sess->srv->id : "<none>"
2387 );
2388
2389 break;
2390 case AF_UNIX:
2391 /* no more information to print right now */
2392 break;
2393 }
2394
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002395 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02002396 " ts=%02x age=%s calls=%d",
2397 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01002398 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
2399 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002400
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002401 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002402 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
2403 curr_sess->req->flags,
2404 curr_sess->req->l,
2405 curr_sess->req->analysers,
2406 curr_sess->req->rex ?
2407 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
2408 TICKS_TO_MS(1000)) : "");
2409
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002410 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002411 ",wx=%s",
2412 curr_sess->req->wex ?
2413 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
2414 TICKS_TO_MS(1000)) : "");
2415
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002416 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002417 ",ax=%s]",
2418 curr_sess->req->analyse_exp ?
2419 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
2420 TICKS_TO_MS(1000)) : "");
2421
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002422 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002423 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
2424 curr_sess->rep->flags,
2425 curr_sess->rep->l,
2426 curr_sess->rep->analysers,
2427 curr_sess->rep->rex ?
2428 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
2429 TICKS_TO_MS(1000)) : "");
2430
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002431 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002432 ",wx=%s",
2433 curr_sess->rep->wex ?
2434 human_time(TICKS_TO_MS(curr_sess->rep->wex - 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 ",ax=%s]",
2439 curr_sess->rep->analyse_exp ?
2440 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
2441 TICKS_TO_MS(1000)) : "");
2442
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002443 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002444 " s0=[%d,%1xh,fd=%d,ex=%s]",
2445 curr_sess->si[0].state,
2446 curr_sess->si[0].flags,
2447 curr_sess->si[0].fd,
2448 curr_sess->si[0].exp ?
2449 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
2450 TICKS_TO_MS(1000)) : "");
2451
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002452 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002453 " s1=[%d,%1xh,fd=%d,ex=%s]",
2454 curr_sess->si[1].state,
2455 curr_sess->si[1].flags,
2456 curr_sess->si[1].fd,
2457 curr_sess->si[1].exp ?
2458 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
2459 TICKS_TO_MS(1000)) : "");
2460
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002461 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002462 " exp=%s",
2463 curr_sess->task->expire ?
2464 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
2465 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01002466 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002467 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002468
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002469 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002470
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002471 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002472 /* let's try again later from this session. We add ourselves into
2473 * this session's users so that it can remove us upon termination.
2474 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002475 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002476 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002477 }
2478
2479 s->data_ctx.sess.bref.ref = curr_sess->list.n;
2480 }
2481 s->data_state = DATA_ST_FIN;
2482 /* fall through */
2483
2484 default:
2485 s->data_state = DATA_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002486 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002487 }
2488}
2489
Willy Tarreau74808cb2009-03-04 15:53:18 +01002490/* print a line of error buffer (limited to 70 bytes) to <out>. The format is :
2491 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
2492 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
2493 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
2494 * lines are respected within the limit of 70 output chars. Lines that are
2495 * continuation of a previous truncated line begin with "+" instead of " "
2496 * after the offset. The new pointer is returned.
2497 */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002498static int dump_error_line(struct chunk *out, struct error_snapshot *err,
2499 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01002500{
2501 int end;
2502 unsigned char c;
2503
2504 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002505 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01002506 return ptr;
2507
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002508 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01002509
Willy Tarreau61b34732009-10-03 23:49:35 +02002510 while (ptr < err->len && ptr < sizeof(err->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002511 c = err->buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01002512 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002513 if (out->len > end - 2)
2514 break;
2515 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01002516 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002517 if (out->len > end - 3)
2518 break;
2519 out->str[out->len++] = '\\';
2520 switch (c) {
2521 case '\t': c = 't'; break;
2522 case '\n': c = 'n'; break;
2523 case '\r': c = 'r'; break;
2524 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01002525 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002526 }
2527 out->str[out->len++] = c;
2528 } else {
2529 if (out->len > end - 5)
2530 break;
2531 out->str[out->len++] = '\\';
2532 out->str[out->len++] = 'x';
2533 out->str[out->len++] = hextab[(c >> 4) & 0xF];
2534 out->str[out->len++] = hextab[c & 0xF];
2535 }
2536 if (err->buf[ptr++] == '\n') {
2537 /* we had a line break, let's return now */
2538 out->str[out->len++] = '\n';
2539 *line = ptr;
2540 return ptr;
2541 }
2542 }
2543 /* we have an incomplete line, we return it as-is */
2544 out->str[out->len++] = '\n';
2545 return ptr;
2546}
2547
2548/* This function is called to send output to the response buffer.
2549 * It dumps the errors logged in proxies onto the output buffer <rep>.
2550 * Expects to be called with client socket shut down on input.
2551 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau61b34732009-10-03 23:49:35 +02002552 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau74808cb2009-03-04 15:53:18 +01002553 */
Willy Tarreau61b34732009-10-03 23:49:35 +02002554int stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau74808cb2009-03-04 15:53:18 +01002555{
2556 extern const char *monthname[12];
2557 struct chunk msg;
2558
Willy Tarreau61b34732009-10-03 23:49:35 +02002559 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW)))
2560 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002561
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002562 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01002563
2564 if (!s->data_ctx.errors.px) {
2565 /* the function had not been called yet, let's prepare the
2566 * buffer for a response.
2567 */
Willy Tarreau74808cb2009-03-04 15:53:18 +01002568 s->data_ctx.errors.px = proxy;
2569 s->data_ctx.errors.buf = 0;
2570 s->data_ctx.errors.bol = 0;
2571 s->data_ctx.errors.ptr = -1;
2572 }
2573
2574 /* we have two inner loops here, one for the proxy, the other one for
2575 * the buffer.
2576 */
2577 while (s->data_ctx.errors.px) {
2578 struct error_snapshot *es;
2579
2580 if (s->data_ctx.errors.buf == 0)
2581 es = &s->data_ctx.errors.px->invalid_req;
2582 else
2583 es = &s->data_ctx.errors.px->invalid_rep;
2584
2585 if (!es->when.tv_sec)
2586 goto next;
2587
2588 if (s->data_ctx.errors.iid >= 0 &&
2589 s->data_ctx.errors.px->uuid != s->data_ctx.errors.iid &&
2590 es->oe->uuid != s->data_ctx.errors.iid)
2591 goto next;
2592
2593 if (s->data_ctx.errors.ptr < 0) {
2594 /* just print headers now */
2595
2596 char pn[INET6_ADDRSTRLEN];
2597 struct tm tm;
2598
2599 get_localtime(es->when.tv_sec, &tm);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002600 chunk_printf(&msg, "\n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01002601 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002602 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01002603
2604
2605 if (es->src.ss_family == AF_INET)
2606 inet_ntop(AF_INET,
2607 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
2608 pn, sizeof(pn));
2609 else
2610 inet_ntop(AF_INET6,
2611 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
2612 pn, sizeof(pn));
2613
2614 switch (s->data_ctx.errors.buf) {
2615 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002616 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002617 " frontend %s (#%d): invalid request\n"
2618 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
2619 " request length %d bytes, error at position %d:\n\n",
2620 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
2621 pn, es->sid, es->oe->id, es->oe->uuid,
2622 es->srv ? es->srv->id : "<NONE>",
2623 es->srv ? es->srv->puid : -1,
2624 es->len, es->pos);
2625 break;
2626 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002627 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002628 " backend %s (#%d) : invalid response\n"
2629 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
2630 " response length %d bytes, error at position %d:\n\n",
2631 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
2632 pn, es->sid, es->oe->id, es->oe->uuid,
2633 es->srv ? es->srv->id : "<NONE>",
2634 es->srv ? es->srv->puid : -1,
2635 es->len, es->pos);
2636 break;
2637 }
2638
Willy Tarreau61b34732009-10-03 23:49:35 +02002639 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002640 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02002641 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002642 }
2643 s->data_ctx.errors.ptr = 0;
2644 s->data_ctx.errors.sid = es->sid;
2645 }
2646
2647 if (s->data_ctx.errors.sid != es->sid) {
2648 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002649 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002650 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau61b34732009-10-03 23:49:35 +02002651 if (buffer_feed_chunk(rep, &msg) >= 0)
2652 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002653 goto next;
2654 }
2655
2656 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau61b34732009-10-03 23:49:35 +02002657 while (s->data_ctx.errors.ptr < es->len && s->data_ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002658 int newptr;
2659 int newline;
2660
2661 newline = s->data_ctx.errors.bol;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002662 newptr = dump_error_line(&msg, es, &newline, s->data_ctx.errors.ptr);
Willy Tarreau74808cb2009-03-04 15:53:18 +01002663 if (newptr == s->data_ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02002664 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002665
Willy Tarreau61b34732009-10-03 23:49:35 +02002666 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002667 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02002668 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002669 }
2670 s->data_ctx.errors.ptr = newptr;
2671 s->data_ctx.errors.bol = newline;
2672 };
2673 next:
2674 s->data_ctx.errors.bol = 0;
2675 s->data_ctx.errors.ptr = -1;
2676 s->data_ctx.errors.buf++;
2677 if (s->data_ctx.errors.buf > 1) {
2678 s->data_ctx.errors.buf = 0;
2679 s->data_ctx.errors.px = s->data_ctx.errors.px->next;
2680 }
2681 }
2682
2683 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02002684 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002685}
2686
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002687
Willy Tarreau10522fd2008-07-09 20:12:41 +02002688static struct cfg_kw_list cfg_kws = {{ },{
2689 { CFG_GLOBAL, "stats", stats_parse_global },
2690 { 0, NULL, NULL },
2691}};
2692
2693__attribute__((constructor))
2694static void __dumpstats_module_init(void)
2695{
2696 cfg_register_keywords(&cfg_kws);
2697}
2698
Willy Tarreau91861262007-10-17 17:06:05 +02002699/*
2700 * Local variables:
2701 * c-indent-level: 8
2702 * c-basic-offset: 8
2703 * End:
2704 */