blob: 15174b81e2a666a1b92675d8d60ffcf5cf98ebd3 [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"
Willy Tarreau66dc20a2010-03-05 17:53:32 +010064 " show sess [id] : report the list of current sessions or dump this session\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 Tarreau66dc20a2010-03-05 17:53:32 +0100319 if (*args[2])
320 s->data_ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
321 else
322 s->data_ctx.sess.target = NULL;
323 s->data_ctx.sess.section = 0; /* start with session status */
324 s->data_ctx.sess.pos = 0;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200325 si->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200326 }
327 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200328 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200329 s->data_ctx.cli.msg = stats_permission_denied_msg;
330 si->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200331 return 1;
332 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200333 if (*args[2])
334 s->data_ctx.errors.iid = atoi(args[2]);
335 else
336 s->data_ctx.errors.iid = -1;
337 s->data_ctx.errors.px = NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200338 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200339 si->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200340 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200341 else { /* neither "stat" nor "info" nor "sess" nor "errors"*/
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200342 return 0;
343 }
344 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200345 else if (strcmp(args[0], "clear") == 0) {
346 if (strcmp(args[1], "counters") == 0) {
347 struct proxy *px;
348 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200349 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200350 int clrall = 0;
351
352 if (strcmp(args[2], "all") == 0)
353 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200354
Willy Tarreau6162db22009-10-10 17:13:00 +0200355 /* check permissions */
356 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
357 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200358 s->data_ctx.cli.msg = stats_permission_denied_msg;
359 si->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200360 return 1;
361 }
362
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200363 for (px = proxy; px; px = px->next) {
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200364 if (clrall)
365 memset(&px->counters, 0, sizeof(px->counters));
366 else {
367 px->counters.feconn_max = 0;
368 px->counters.beconn_max = 0;
Willy Tarreaud9b587f2010-02-26 10:05:55 +0100369 px->counters.fe_rps_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200370 px->counters.fe_sps_max = 0;
371 px->counters.be_sps_max = 0;
372 px->counters.nbpend_max = 0;
373 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200374
375 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200376 if (clrall)
377 memset(&sv->counters, 0, sizeof(sv->counters));
378 else {
379 sv->counters.cur_sess_max = 0;
380 sv->counters.nbpend_max = 0;
381 sv->counters.sps_max = 0;
382 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200383
384 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200385 if (li->counters) {
386 if (clrall)
387 memset(li->counters, 0, sizeof(*li->counters));
388 else
389 li->counters->conn_max = 0;
390 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200391 }
392
393 return 1;
394 }
395 else {
396 return 0;
397 }
398 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200399 else if (strcmp(args[0], "get") == 0) {
400 if (strcmp(args[1], "weight") == 0) {
401 struct proxy *px;
402 struct server *sv;
403
404 /* split "backend/server" and make <line> point to server */
405 for (line = args[2]; *line; line++)
406 if (*line == '/') {
407 *line++ = '\0';
408 break;
409 }
410
411 if (!*line) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200412 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
413 si->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200414 return 1;
415 }
416
417 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200418 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
419 si->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200420 return 1;
421 }
422
423 /* return server's effective weight at the moment */
424 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
425 buffer_feed(si->ib, trash);
426 return 1;
427 }
428 else { /* not "get weight" */
429 return 0;
430 }
431 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200432 else if (strcmp(args[0], "set") == 0) {
433 if (strcmp(args[1], "weight") == 0) {
434 struct proxy *px;
435 struct server *sv;
436 int w;
437
438 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200439 s->data_ctx.cli.msg = stats_permission_denied_msg;
440 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200441 return 1;
442 }
443
444 /* split "backend/server" and make <line> point to server */
445 for (line = args[2]; *line; line++)
446 if (*line == '/') {
447 *line++ = '\0';
448 break;
449 }
450
451 if (!*line || !*args[3]) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200452 s->data_ctx.cli.msg = "Require 'backend/server' and 'weight' or 'weight%'.\n";
453 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200454 return 1;
455 }
456
457 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200458 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
459 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200460 return 1;
461 }
462
463 /* if the weight is terminated with '%', it is set relative to
464 * the initial weight, otherwise it is absolute.
465 */
466 w = atoi(args[3]);
467 if (strchr(args[3], '%') != NULL) {
468 if (w < 0 || w > 100) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200469 s->data_ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
470 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200471 return 1;
472 }
473 w = sv->iweight * w / 100;
474 }
475 else {
476 if (w < 0 || w > 256) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200477 s->data_ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
478 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200479 return 1;
480 }
481 }
482
483 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200484 s->data_ctx.cli.msg = "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
485 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200486 return 1;
487 }
488
489 sv->uweight = w;
490
491 if (px->lbprm.algo & BE_LB_PROP_DYN) {
492 /* we must take care of not pushing the server to full throttle during slow starts */
493 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
494 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
495 else
496 sv->eweight = BE_WEIGHT_SCALE;
497 sv->eweight *= sv->uweight;
498 } else {
499 sv->eweight = sv->uweight;
500 }
501
502 /* static LB algorithms are a bit harder to update */
503 if (px->lbprm.update_server_eweight)
504 px->lbprm.update_server_eweight(sv);
505 else if (sv->eweight)
506 px->lbprm.set_server_status_up(sv);
507 else
508 px->lbprm.set_server_status_down(sv);
509
510 return 1;
511 }
Willy Tarreau7aabd112010-01-26 10:59:06 +0100512 else if (strcmp(args[1], "timeout") == 0) {
513 if (strcmp(args[2], "cli") == 0) {
514 unsigned timeout;
515 const char *res;
516
517 if (!*args[3]) {
518 s->data_ctx.cli.msg = "Expects an integer value.\n";
519 si->st0 = STAT_CLI_PRINT;
520 return 1;
521 }
522
523 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
524 if (res || timeout < 1) {
525 s->data_ctx.cli.msg = "Invalid timeout value.\n";
526 si->st0 = STAT_CLI_PRINT;
527 return 1;
528 }
529
530 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
531 return 1;
532 }
533 else {
534 s->data_ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
535 si->st0 = STAT_CLI_PRINT;
536 return 1;
537 }
538 }
539 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +0200540 return 0;
541 }
542 }
Cyril Bontécd19e512010-01-31 22:34:03 +0100543 else if (strcmp(args[0], "enable") == 0) {
544 if (strcmp(args[1], "server") == 0) {
545 struct proxy *px;
546 struct server *sv;
547
548 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
549 s->data_ctx.cli.msg = stats_permission_denied_msg;
550 si->st0 = STAT_CLI_PRINT;
551 return 1;
552 }
553
554 /* split "backend/server" and make <line> point to server */
555 for (line = args[2]; *line; line++)
556 if (*line == '/') {
557 *line++ = '\0';
558 break;
559 }
560
561 if (!*line || !*args[2]) {
562 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
563 si->st0 = STAT_CLI_PRINT;
564 return 1;
565 }
566
567 if (!get_backend_server(args[2], line, &px, &sv)) {
568 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
569 si->st0 = STAT_CLI_PRINT;
570 return 1;
571 }
572
573 if (sv->state & SRV_MAINTAIN) {
574 /* The server is really in maintenance, we can change the server state */
575 if (sv->tracked) {
576 /* If this server tracks the status of another one,
577 * we must restore the good status.
578 */
579 if (sv->tracked->state & SRV_RUNNING) {
580 set_server_up(sv);
581 } else {
582 sv->state &= ~SRV_MAINTAIN;
583 set_server_down(sv);
584 }
585 } else {
586 set_server_up(sv);
587 }
588 }
589
590 return 1;
591 }
592 else { /* unknown "enable" parameter */
593 return 0;
594 }
595 }
596 else if (strcmp(args[0], "disable") == 0) {
597 if (strcmp(args[1], "server") == 0) {
598 struct proxy *px;
599 struct server *sv;
600
601 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
602 s->data_ctx.cli.msg = stats_permission_denied_msg;
603 si->st0 = STAT_CLI_PRINT;
604 return 1;
605 }
606
607 /* split "backend/server" and make <line> point to server */
608 for (line = args[2]; *line; line++)
609 if (*line == '/') {
610 *line++ = '\0';
611 break;
612 }
613
614 if (!*line || !*args[2]) {
615 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
616 si->st0 = STAT_CLI_PRINT;
617 return 1;
618 }
619
620 if (!get_backend_server(args[2], line, &px, &sv)) {
621 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
622 si->st0 = STAT_CLI_PRINT;
623 return 1;
624 }
625
626 if (! (sv->state & SRV_MAINTAIN)) {
627 /* Not already in maintenance, we can change the server state */
628 sv->state |= SRV_MAINTAIN;
629 set_server_down(sv);
630 }
631
632 return 1;
633 }
634 else { /* unknown "disable" parameter */
635 return 0;
636 }
637 }
638 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200639 return 0;
640 }
641 return 1;
642}
643
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200644/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200645 * used to processes I/O from/to the stats unix socket. The system relies on a
646 * state machine handling requests and various responses. We read a request,
647 * then we process it and send the response, and we possibly display a prompt.
648 * Then we can read again. The state is stored in si->st0 and is one of the
649 * STAT_CLI_* constants. si->st1 is used to indicate whether prompt is enabled
650 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200651 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200652void stats_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200653{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200654 struct session *s = si->private;
655 struct buffer *req = si->ob;
656 struct buffer *res = si->ib;
657 int reql;
658 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200659
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200660 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
661 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200662
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200663 while (1) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200664 if (si->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200665 /* Stats output not initialized yet */
666 memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats));
667 s->data_source = DATA_SRC_STATS;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200668 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200669 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200670 else if (si->st0 == STAT_CLI_END) {
671 /* Let's close for real now. We just close the request
672 * side, the conditions below will complete if needed.
673 */
674 si->shutw(si);
675 break;
676 }
677 else if (si->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +0200678 /* ensure we have some output room left in the event we
679 * would want to return some info right after parsing.
680 */
681 if (buffer_almost_full(si->ib))
682 break;
683
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200684 reql = buffer_si_peekline(si->ob, trash, sizeof(trash));
685 if (reql <= 0) { /* closed or EOL not found */
686 if (reql == 0)
687 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200688 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200689 continue;
690 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200691
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200692 /* seek for a possible semi-colon. If we find one, we
693 * replace it with an LF and skip only this part.
694 */
695 for (len = 0; len < reql; len++)
696 if (trash[len] == ';') {
697 trash[len] = '\n';
698 reql = len + 1;
699 break;
700 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200701
Willy Tarreau816fc222009-10-04 07:36:58 +0200702 /* now it is time to check that we have a full line,
703 * remove the trailing \n and possibly \r, then cut the
704 * line.
705 */
706 len = reql - 1;
707 if (trash[len] != '\n') {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200708 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200709 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200710 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200711
Willy Tarreau816fc222009-10-04 07:36:58 +0200712 if (len && trash[len-1] == '\r')
713 len--;
714
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200715 trash[len] = '\0';
716
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200717 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200718 if (len) {
719 if (strcmp(trash, "quit") == 0) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200720 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200721 continue;
722 }
723 else if (strcmp(trash, "prompt") == 0)
724 si->st1 = !si->st1;
725 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200726 !stats_sock_parse_request(si, trash)) {
727 s->data_ctx.cli.msg = stats_sock_usage_msg;
728 si->st0 = STAT_CLI_PRINT;
729 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200730 /* NB: stats_sock_parse_request() may have put
731 * another STAT_CLI_O_* into si->st0.
732 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200733 }
734 else if (!si->st1) {
735 /* if prompt is disabled, print help on empty lines,
736 * so that the user at least knows how to enable
737 * prompt and find help.
738 */
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200739 s->data_ctx.cli.msg = stats_sock_usage_msg;
740 si->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200741 }
742
743 /* re-adjust req buffer */
744 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200745 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200746 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200747 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200748 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200749 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200750 continue;
751 }
752
753 switch (si->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200754 case STAT_CLI_PRINT:
755 if (buffer_feed(si->ib, s->data_ctx.cli.msg) < 0)
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200756 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200757 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200758 case STAT_CLI_O_INFO:
Willy Tarreau24955a12009-10-04 11:54:04 +0200759 if (stats_dump_raw_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200760 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200761 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200762 case STAT_CLI_O_SESS:
Willy Tarreau7e72a8f2009-10-03 23:55:14 +0200763 if (stats_dump_sess_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200764 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200765 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200766 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau61b34732009-10-03 23:49:35 +0200767 if (stats_dump_errors_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200768 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200769 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200770 default: /* abnormal state */
771 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200772 break;
773 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200774
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200775 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
776 if (si->st0 == STAT_CLI_PROMPT) {
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200777 if (buffer_feed(si->ib, si->st1 ? "\n> " : "\n") < 0)
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200778 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200779 }
780
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200781 /* If the output functions are still there, it means they require more room. */
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200782 if (si->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200783 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200784
785 /* Now we close the output if one of the writers did so,
786 * or if we're not in interactive mode and the request
787 * buffer is empty. This still allows pipelined requests
788 * to be sent in non-interactive mode.
789 */
790 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->st1 && !req->send_max)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200791 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200792 continue;
793 }
794
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200795 /* switch state back to GETREQ to read next requests */
796 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200797 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200798 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200799
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200800 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200801 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
802 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
803 /* Other size has closed, let's abort if we have no more processing to do
804 * and nothing more to consume. This is comparable to a broken pipe, so
805 * we forward the close to the request side so that it flows upstream to
806 * the client.
807 */
808 si->shutw(si);
809 }
810
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200811 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200812 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
813 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
814 /* We have no more processing to do, and nothing more to send, and
815 * the client side has closed. So we'll forward this state downstream
816 * on the response buffer.
817 */
818 si->shutr(si);
819 res->flags |= BF_READ_NULL;
820 }
821
822 /* update all other flags and resync with the other side */
823 si->update(si);
824
825 /* we don't want to expire timeouts while we're processing requests */
826 si->ib->rex = TICK_ETERNITY;
827 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200828
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200829 out:
830 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
831 __FUNCTION__, __LINE__,
832 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
833
834 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
835 /* check that we have released everything then unregister */
836 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200837 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200838}
839
Willy Tarreau24955a12009-10-04 11:54:04 +0200840/* This function is called to send output to the response buffer.
841 * It dumps statistics onto the output buffer <rep> owned by session <s>.
842 * s->data_ctx must have been zeroed first, and the flags properly set.
843 * It returns 0 as long as it does not complete, non-zero upon completion.
844 * Some states are not used but it makes the code more similar to other
845 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200846 */
Willy Tarreau24955a12009-10-04 11:54:04 +0200847int stats_dump_raw_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3e76e722007-10-17 18:57:38 +0200848{
Willy Tarreau3e76e722007-10-17 18:57:38 +0200849 struct proxy *px;
850 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +0100851 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200852
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200853 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +0200854
855 switch (s->data_state) {
856 case DATA_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +0200857 /* the function had not been called yet */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200858 s->data_state = DATA_ST_HEAD;
859 /* fall through */
860
861 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100862 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200863 print_csv_header(&msg);
Willy Tarreau24955a12009-10-04 11:54:04 +0200864 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100865 return 0;
866 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200867
868 s->data_state = DATA_ST_INFO;
869 /* fall through */
870
871 case DATA_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +0100872 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100873 if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200874 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +0100875 "Name: " PRODUCT_NAME "\n"
876 "Version: " HAPROXY_VERSION "\n"
877 "Release_date: " HAPROXY_DATE "\n"
878 "Nbproc: %d\n"
879 "Process_num: %d\n"
880 "Pid: %d\n"
881 "Uptime: %dd %dh%02dm%02ds\n"
882 "Uptime_sec: %d\n"
883 "Memmax_MB: %d\n"
884 "Ulimit-n: %d\n"
885 "Maxsock: %d\n"
886 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100887 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100888 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100889 "PipesUsed: %d\n"
890 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100891 "Tasks: %d\n"
892 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200893 "node: %s\n"
894 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100895 "",
896 global.nbproc,
897 relative_pid,
898 pid,
899 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
900 up,
901 global.rlimit_memmax,
902 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100903 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100904 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200905 nb_tasks_cur, run_queue_cur,
906 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +0100907 );
Willy Tarreau24955a12009-10-04 11:54:04 +0200908 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100909 return 0;
910 }
911
Willy Tarreau3e76e722007-10-17 18:57:38 +0200912 s->data_ctx.stats.px = proxy;
913 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100914
915 s->data_ctx.stats.sv = NULL;
916 s->data_ctx.stats.sv_st = 0;
917
Willy Tarreau3e76e722007-10-17 18:57:38 +0200918 s->data_state = DATA_ST_LIST;
919 /* fall through */
920
921 case DATA_ST_LIST:
922 /* dump proxies */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100923 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100924 while (s->data_ctx.stats.px) {
925 px = s->data_ctx.stats.px;
926 /* skip the disabled proxies and non-networked ones */
927 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +0200928 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100929 if (stats_dump_proxy(s, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100930 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +0200931 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200932
Willy Tarreaua8efd362008-01-03 10:19:15 +0100933 s->data_ctx.stats.px = px->next;
934 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
935 }
936 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200937 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200938
939 s->data_state = DATA_ST_END;
940 /* fall through */
941
942 case DATA_ST_END:
943 s->data_state = DATA_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +0100944 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200945
946 case DATA_ST_FIN:
947 return 1;
948
949 default:
950 /* unknown state ! */
Willy Tarreau24955a12009-10-04 11:54:04 +0200951 s->data_state = DATA_ST_FIN;
952 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200953 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100954}
955
956
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200957/* This I/O handler runs as an applet embedded in a stream interface. It is
958 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
959 * si->st0 becomes non-zero once the transfer is finished. The handler
960 * automatically unregisters itself once transfer is complete.
961 */
962void http_stats_io_handler(struct stream_interface *si)
963{
964 struct session *s = si->private;
965 struct buffer *req = si->ob;
966 struct buffer *res = si->ib;
967
968 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
969 goto out;
970
971 /* check that the output is not closed */
972 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
973 si->st0 = 1;
974
975 if (!si->st0) {
976 if (stats_dump_http(s, res, s->be->uri_auth)) {
977 si->st0 = 1;
978 si->shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200979 }
980 }
981
982 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
983 si->shutw(si);
984
985 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->st0) {
986 si->shutr(si);
987 res->flags |= BF_READ_NULL;
988 }
989
990 /* update all other flags and resync with the other side */
991 si->update(si);
992
993 /* we don't want to expire timeouts while we're processing requests */
994 si->ib->rex = TICK_ETERNITY;
995 si->ob->wex = TICK_ETERNITY;
996
997 out:
998 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
999 /* check that we have released everything then unregister */
1000 stream_int_unregister_handler(si);
1001 }
1002}
1003
1004
Willy Tarreau3e76e722007-10-17 18:57:38 +02001005/*
1006 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +02001007 * client socket shut down on input. It stops by itself by unsetting the
Willy Tarreau72b179a2008-08-28 16:01:32 +02001008 * BF_HIJACK flag from the buffer, which it uses to keep on being called
Willy Tarreau1ae3a052008-08-16 10:56:30 +02001009 * when there is free space in the buffer, of simply by letting an empty buffer
1010 * upon return.s->data_ctx must have been zeroed before the first call, and the
1011 * flags set. It returns 0 if it had to stop writing data and an I/O is needed,
1012 * 1 if the dump is finished and the session must be closed, or -1 in case of
1013 * any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001014 */
Willy Tarreau0a464892008-12-07 18:30:00 +01001015int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001016{
Willy Tarreau91861262007-10-17 17:06:05 +02001017 struct proxy *px;
1018 struct chunk msg;
1019 unsigned int up;
1020
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001021 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001022
1023 switch (s->data_state) {
1024 case DATA_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001025 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001026 "HTTP/1.0 200 OK\r\n"
1027 "Cache-Control: no-cache\r\n"
1028 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001029 "Content-Type: %s\r\n",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001030 (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001031
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001032 if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001033 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001034 uri->refresh);
1035
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001036 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001037
1038 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001039 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001040 return 0;
1041
Willy Tarreau91861262007-10-17 17:06:05 +02001042 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1043 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1044 if (!(s->flags & SN_FINST_MASK))
1045 s->flags |= SN_FINST_R;
1046
1047 if (s->txn.meth == HTTP_METH_HEAD) {
1048 /* that's all we return in case of HEAD request */
1049 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001050 return 1;
1051 }
1052
1053 s->data_state = DATA_ST_HEAD; /* let's start producing data */
1054 /* fall through */
1055
1056 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001057 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001058 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001059 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001060 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1061 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001062 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001063 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1064 "<style type=\"text/css\"><!--\n"
1065 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001066 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001067 " font-size: 12px;"
1068 " font-weight: normal;"
1069 " color: black;"
1070 " background: white;"
1071 "}\n"
1072 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001073 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001074 "}\n"
1075 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001076 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001077 " margin-bottom: 0.5em;"
1078 "}\n"
1079 "h2 {"
1080 " font-family: helvetica, arial;"
1081 " font-size: x-large;"
1082 " font-weight: bold;"
1083 " font-style: italic;"
1084 " color: #6020a0;"
1085 " margin-top: 0em;"
1086 " margin-bottom: 0em;"
1087 "}\n"
1088 "h3 {"
1089 " font-family: helvetica, arial;"
1090 " font-size: 16px;"
1091 " font-weight: bold;"
1092 " color: #b00040;"
1093 " background: #e8e8d0;"
1094 " margin-top: 0em;"
1095 " margin-bottom: 0em;"
1096 "}\n"
1097 "li {"
1098 " margin-top: 0.25em;"
1099 " margin-right: 2em;"
1100 "}\n"
1101 ".hr {margin-top: 0.25em;"
1102 " border-color: black;"
1103 " border-bottom-style: solid;"
1104 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001105 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001106 ".total {background: #20D0D0;color: #ffff80;}\n"
1107 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001108 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001109 ".backend {background: #e8e8d0;}\n"
1110 ".active0 {background: #ff9090;}\n"
1111 ".active1 {background: #ffd020;}\n"
1112 ".active2 {background: #ffffa0;}\n"
1113 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001114 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1115 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1116 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001117 ".backup0 {background: #ff9090;}\n"
1118 ".backup1 {background: #ff80ff;}\n"
1119 ".backup2 {background: #c060ff;}\n"
1120 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001121 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1122 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1123 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001124 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001125 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001126 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001127 "a.px:link {color: #ffff40; text-decoration: none;}"
1128 "a.px:visited {color: #ffff40; text-decoration: none;}"
1129 "a.px:hover {color: #ffffff; text-decoration: none;}"
1130 "a.lfsb:link {color: #000000; text-decoration: none;}"
1131 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1132 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1133 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001134 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001135 "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"
1136 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001137 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001138 "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 +02001139 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001140 "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 +02001141 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001142 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1143 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1144 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001145 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001146 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001147 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001148 (uri->flags&ST_SHNODE) ? " on " : "",
1149 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001150 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001151 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001152 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001153 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001154 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001155 return 0;
1156
1157 s->data_state = DATA_ST_INFO;
1158 /* fall through */
1159
1160 case DATA_ST_INFO:
1161 up = (now.tv_sec - start_date.tv_sec);
1162
1163 /* WARNING! this has to fit the first packet too.
1164 * We are around 3.5 kB, add adding entries will
1165 * become tricky if we want to support 4kB buffers !
1166 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001167 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001168 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001169 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1170 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001171 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001172 "<hr width=\"100%%\" class=\"hr\">\n"
1173 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001174 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001175 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001176 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001177 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1178 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
1179 "current conns = %d; current pipes = %d/%d<br>\n"
1180 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001181 "</td><td align=\"center\" nowrap>\n"
1182 "<table class=\"lgd\"><tr>\n"
1183 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1184 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1185 "</tr><tr>\n"
1186 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1187 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1188 "</tr><tr>\n"
1189 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1190 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1191 "</tr><tr>\n"
1192 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001193 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001194 "</tr><tr>\n"
1195 "<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 +02001196 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001197 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001198 "</td>"
1199 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1200 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1201 "",
1202 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001203 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1204 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1205 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001206 up / 86400, (up % 86400) / 3600,
1207 (up % 3600) / 60, (up % 60),
1208 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1209 global.rlimit_memmax ? " MB" : "",
1210 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001211 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001212 actconn, pipes_used, pipes_used+pipes_free,
1213 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +02001214 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001215
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001216 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
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\">Show all servers</a><br>\n",
1219 uri->uri_prefix,
1220 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001221 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001222 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001223 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001224 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1225 uri->uri_prefix,
1226 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001227 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001228
Willy Tarreau55bb8452007-10-17 18:44:57 +02001229 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001230 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
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\">Enable 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 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001236 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001237 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001238 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1239 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001240 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001241 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001242 }
Willy Tarreau91861262007-10-17 17:06:05 +02001243
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001244 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001245 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
1246 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001247 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
1248 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001249
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001250 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02001251 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
1252 uri->uri_prefix,
1253 (uri->refresh > 0) ? ";norefresh" : "");
1254
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001255 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001256 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001257 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1258 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
1259 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
1260 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
1261 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
1262 "</ul>"
1263 "</td>"
1264 "</tr></table>\n"
1265 ""
1266 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001267
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001268 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001269 return 0;
1270 }
Willy Tarreau91861262007-10-17 17:06:05 +02001271
Willy Tarreau91861262007-10-17 17:06:05 +02001272 s->data_ctx.stats.px = proxy;
1273 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1274 s->data_state = DATA_ST_LIST;
1275 /* fall through */
1276
1277 case DATA_ST_LIST:
1278 /* dump proxies */
1279 while (s->data_ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001280 if (buffer_almost_full(rep))
1281 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02001282 px = s->data_ctx.stats.px;
1283 /* skip the disabled proxies and non-networked ones */
1284 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001285 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001286 return 0;
1287
1288 s->data_ctx.stats.px = px->next;
1289 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1290 }
1291 /* here, we just have reached the last proxy */
1292
1293 s->data_state = DATA_ST_END;
1294 /* fall through */
1295
1296 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001297 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001298 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001299 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001300 return 0;
1301 }
Willy Tarreau91861262007-10-17 17:06:05 +02001302
1303 s->data_state = DATA_ST_FIN;
1304 /* fall through */
1305
1306 case DATA_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02001307 return 1;
1308
1309 default:
1310 /* unknown state ! */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001311 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001312 return -1;
1313 }
1314}
1315
1316
1317/*
1318 * Dumps statistics for a proxy.
1319 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1320 * ot non-zero if everything completed.
1321 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001322int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001323{
1324 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001325 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001326 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02001327 struct chunk msg;
1328
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001329 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001330
1331 switch (s->data_ctx.stats.px_st) {
1332 case DATA_ST_PX_INIT:
1333 /* we are on a new proxy */
1334
1335 if (uri && uri->scope) {
1336 /* we have a limited scope, we have to check the proxy name */
1337 struct stat_scope *scope;
1338 int len;
1339
1340 len = strlen(px->id);
1341 scope = uri->scope;
1342
1343 while (scope) {
1344 /* match exact proxy name */
1345 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1346 break;
1347
1348 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001349 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001350 break;
1351 scope = scope->next;
1352 }
1353
1354 /* proxy name not found : don't dump anything */
1355 if (scope == NULL)
1356 return 1;
1357 }
1358
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001359 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001360 (px->uuid != s->data_ctx.stats.iid))
1361 return 1;
1362
Willy Tarreau91861262007-10-17 17:06:05 +02001363 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
1364 /* fall through */
1365
1366 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001367 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001368 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001369 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001370 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001371 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001372 "<th class=\"pxname\" width=\"10%%\"");
1373
1374 if (uri->flags&ST_SHLGNDS) {
1375 /* cap, mode, id */
1376 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
1377 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1378 px->uuid);
1379
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001380 chunk_printf(&msg, "\"");
1381 }
1382
1383 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001384 ">%s<a name=\"%s\"></a>"
1385 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001386 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001387 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001388 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001389 "<table class=\"tbl\" width=\"100%%\">\n"
1390 "<tr class=\"titre\">"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001391 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001392 "<th colspan=3>Queue</th>"
1393 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001394 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001395 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001396 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001397 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001398 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001399 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001400 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001401 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001402 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001403 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001404 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001405 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1406 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001407 "</tr>",
Willy Tarreaue0454092010-02-26 12:29:07 +01001408 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001409 px->id, px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01001410 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001411 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001412
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001413 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001414 return 0;
1415 }
Willy Tarreau91861262007-10-17 17:06:05 +02001416
1417 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
1418 /* fall through */
1419
1420 case DATA_ST_PX_FE:
1421 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001422 if ((px->cap & PR_CAP_FE) &&
1423 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
1424 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001425 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001426 /* name, queue */
Krzysztof Piotr Oledzki516ed492009-10-22 22:48:09 +02001427 "<tr class=\"frontend\"><td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001428 "<a name=\"%s/Frontend\"></a>"
1429 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td><td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01001430 "",
1431 px->id, px->id);
1432
1433 if (px->mode == PR_MODE_HTTP) {
1434 chunk_printf(&msg,
1435 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01001436 "<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 +01001437 "",
1438 read_freq_ctr(&px->fe_req_per_sec),
1439 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
1440 px->counters.fe_rps_max,
1441 U2H2(px->counters.fe_sps_max),
1442 LIM2A2(px->fe_sps_lim, "-"));
1443 } else {
1444 chunk_printf(&msg,
1445 /* sessions rate : current, max, limit */
1446 "<td>%s</td><td>%s</td><td>%s</td>"
1447 "",
1448 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
1449 U2H1(px->counters.fe_sps_max), LIM2A2(px->fe_sps_lim, "-"));
1450 }
1451
1452 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001453 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001454 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001455 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001456 "",
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001457 U2H3(px->feconn), U2H4(px->counters.feconn_max), U2H5(px->maxconn));
1458
1459 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1460 if (px->mode == PR_MODE_HTTP) {
1461 int i;
1462
Willy Tarreaub44939a2010-02-26 11:35:39 +01001463 chunk_printf(&msg, " title=\"%lld requests:", px->counters.cum_fe_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001464
1465 for (i = 1; i < 6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01001466 chunk_printf(&msg, " %dxx=%lld,", i, px->counters.fe.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001467
Willy Tarreau24657792010-02-26 10:30:28 +01001468 chunk_printf(&msg, " other=%lld\"", px->counters.fe.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001469 }
1470
1471 chunk_printf(&msg,
1472 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01001473 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001474 /* bytes : in, out */
1475 "<td>%s</td><td>%s</td>"
1476 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001477 (px->mode == PR_MODE_HTTP)?"<u>":"",
1478 U2H6(px->counters.cum_feconn),
1479 (px->mode == PR_MODE_HTTP)?"</u>":"",
1480 U2H7(px->counters.bytes_in), U2H8(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001481
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001482 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001483 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001484 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001485 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001486 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001487 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001488 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001489 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001490 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001491 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001492 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02001493 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001494 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
1495 U2H2(px->counters.failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +02001496 px->state == PR_STRUN ? "OPEN" :
1497 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001498 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001499 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001500 /* pxid, name, queue cur, queue max, */
1501 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001502 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001503 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001504 /* bytes : in, out */
1505 "%lld,%lld,"
1506 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001507 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001508 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001509 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001510 /* warnings: retries, redispatches */
1511 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001512 /* server status : reflect frontend status */
1513 "%s,"
1514 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001515 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001516 /* pid, iid, sid, throttle, lbtot, tracked, type */
1517 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001518 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001519 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001520 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001521 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02001522 px->id,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001523 px->feconn, px->counters.feconn_max, px->maxconn, px->counters.cum_feconn,
1524 px->counters.bytes_in, px->counters.bytes_out,
1525 px->counters.denied_req, px->counters.denied_resp,
1526 px->counters.failed_req,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001527 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +01001528 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01001529 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001530 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001531 px->fe_sps_lim, px->counters.fe_sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001532
1533 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1534 if (px->mode == PR_MODE_HTTP) {
1535 int i;
1536
1537 for (i=1; i<6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01001538 chunk_printf(&msg, "%lld,", px->counters.fe.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001539
Willy Tarreau24657792010-02-26 10:30:28 +01001540 chunk_printf(&msg, "%lld,", px->counters.fe.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001541 } else {
1542 chunk_printf(&msg, ",,,,,,");
1543 }
1544
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001545 /* failed health analyses */
1546 chunk_printf(&msg, ",");
1547
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001548 /* requests : req_rate, req_rate_max, req_tot, */
1549 chunk_printf(&msg, "%u,%u,%lld,",
1550 read_freq_ctr(&px->fe_req_per_sec),
1551 px->counters.fe_rps_max, px->counters.cum_fe_req);
1552
Willy Tarreauae526782010-03-04 20:34:23 +01001553 /* errors: cli_aborts, srv_aborts */
1554 chunk_printf(&msg, ",,");
1555
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001556 /* finish with EOL */
1557 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001558 }
Willy Tarreau91861262007-10-17 17:06:05 +02001559
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001560 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001561 return 0;
1562 }
1563
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001564 s->data_ctx.stats.l = px->listen; /* may be NULL */
1565 s->data_ctx.stats.px_st = DATA_ST_PX_LI;
1566 /* fall through */
1567
1568 case DATA_ST_PX_LI:
1569 /* stats.l has been initialized above */
1570 for (; s->data_ctx.stats.l != NULL; s->data_ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001571 if (buffer_almost_full(rep))
1572 return 0;
1573
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001574 l = s->data_ctx.stats.l;
1575 if (!l->counters)
1576 continue;
1577
1578 if (s->data_ctx.stats.flags & STAT_BOUND) {
1579 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SO)))
1580 break;
1581
1582 if (s->data_ctx.stats.sid != -1 && l->luid != s->data_ctx.stats.sid)
1583 continue;
1584 }
1585
1586 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001587 chunk_printf(&msg, "<tr class=socket><td class=ac");
1588
1589 if (uri->flags&ST_SHLGNDS) {
1590 char str[INET6_ADDRSTRLEN], *fmt = NULL;
1591 int port;
1592
1593 chunk_printf(&msg, " title=\"IP: ");
1594
1595 port = (l->addr.ss_family == AF_INET6)
1596 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
1597 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
1598
1599 if (l->addr.ss_family == AF_INET) {
1600 if (inet_ntop(AF_INET,
1601 (const void *)&((struct sockaddr_in *)&l->addr)->sin_addr,
1602 str, sizeof(str)))
1603 fmt = "%s:%d";
1604 } else {
1605 if (inet_ntop(AF_INET6,
1606 (const void *)&((struct sockaddr_in6 *)(&l->addr))->sin6_addr,
1607 str, sizeof(str)))
1608 fmt = "[%s]:%d";
1609 }
1610
1611 if (fmt)
1612 chunk_printf(&msg, fmt, str, port);
1613 else
1614 chunk_printf(&msg, "(%s)", strerror(errno));
1615
1616 /* id */
1617 chunk_printf(&msg, ", id: %d", l->luid);
1618
1619 chunk_printf(&msg, "\"");
1620 }
1621
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001622 chunk_printf(&msg,
1623 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01001624 ">%s<a name=\"%s/+%s\"></a>"
1625 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001626 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001627 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001628 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001629 "<td>%s</td><td>%s</td><td>%s</td>"
1630 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001631 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001632 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001633 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001634 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01001635 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01001636 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001637 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
1638 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
1639
1640 chunk_printf(&msg,
1641 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001642 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001643 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001644 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001645 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001646 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001647 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001648 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001649 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001650 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001651 "",
1652 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
1653 U2H2(l->counters->failed_req),
1654 (l->nbconn < l->maxconn) ? "OPEN" : "FULL");
1655 } else {
1656 chunk_printf(&msg,
1657 /* pxid, name, queue cur, queue max, */
1658 "%s,%s,,,"
1659 /* sessions: current, max, limit, total */
1660 "%d,%d,%d,%lld,"
1661 /* bytes: in, out */
1662 "%lld,%lld,"
1663 /* denied: req, resp */
1664 "%lld,%lld,"
1665 /* errors: request, connect, response */
1666 "%lld,,,"
1667 /* warnings: retries, redispatches */
1668 ",,"
1669 /* server status: reflect listener status */
1670 "%s,"
1671 /* rest of server: nothing */
1672 ",,,,,,,,"
1673 /* pid, iid, sid, throttle, lbtot, tracked, type */
1674 "%d,%d,%d,,,,%d,"
1675 /* rate, rate_lim, rate_max */
1676 ",,,"
1677 /* check_status, check_code, check_duration */
1678 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001679 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1680 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001681 /* failed health analyses */
1682 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001683 /* requests : req_rate, req_rate_max, req_tot, */
1684 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01001685 /* errors: cli_aborts, srv_aborts */
1686 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001687 "\n",
1688 px->id, l->name,
1689 l->nbconn, l->counters->conn_max,
1690 l->maxconn, l->counters->cum_conn,
1691 l->counters->bytes_in, l->counters->bytes_out,
1692 l->counters->denied_req, l->counters->denied_resp,
1693 l->counters->failed_req,
1694 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
1695 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
1696 }
1697
1698 if (buffer_feed_chunk(rep, &msg) >= 0)
1699 return 0;
1700 }
1701
Willy Tarreau91861262007-10-17 17:06:05 +02001702 s->data_ctx.stats.sv = px->srv; /* may be NULL */
1703 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
1704 /* fall through */
1705
1706 case DATA_ST_PX_SV:
1707 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001708 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001709 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 +02001710
Willy Tarreau4e33d862009-10-11 23:35:10 +02001711 if (buffer_almost_full(rep))
1712 return 0;
1713
Willy Tarreau91861262007-10-17 17:06:05 +02001714 sv = s->data_ctx.stats.sv;
1715
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001716 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001717 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
1718 break;
1719
1720 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
1721 continue;
1722 }
1723
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001724 if (sv->tracked)
1725 svs = sv->tracked;
1726 else
1727 svs = sv;
1728
Willy Tarreau91861262007-10-17 17:06:05 +02001729 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001730 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01001731 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001732 else if (svs->state & SRV_RUNNING) {
1733 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02001734 sv_state = 3; /* UP */
1735 else
1736 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01001737
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001738 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01001739 sv_state += 2;
1740 }
Willy Tarreau91861262007-10-17 17:06:05 +02001741 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001742 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02001743 sv_state = 1; /* going up */
1744 else
1745 sv_state = 0; /* DOWN */
1746
Cyril Bonté0dae5852010-02-03 00:26:28 +01001747 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02001748 /* do not report servers which are DOWN */
1749 s->data_ctx.stats.sv = sv->next;
1750 continue;
1751 }
1752
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001753 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001754 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
1755 "UP %d/%d &darr;", "UP",
1756 "NOLB %d/%d &darr;", "NOLB",
1757 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01001758 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001759 chunk_printf(&msg,
1760 /* name */
1761 "<tr class=\"maintain\"><td class=ac"
1762 );
1763 }
1764 else {
1765 chunk_printf(&msg,
1766 /* name */
1767 "<tr class=\"%s%d\"><td class=ac",
1768 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
1769 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001770
1771 if (uri->flags&ST_SHLGNDS) {
1772 char str[INET6_ADDRSTRLEN];
1773
1774 chunk_printf(&msg, " title=\"IP: ");
1775
1776 /* IP */
1777 if (inet_ntop(sv->addr.sin_family, &sv->addr.sin_addr, str, sizeof(str)))
1778 chunk_printf(&msg, "%s:%d", str, htons(sv->addr.sin_port));
1779 else
1780 chunk_printf(&msg, "(%s)", strerror(errno));
1781
1782 /* id */
1783 chunk_printf(&msg, ", id: %d", sv->puid);
1784
1785 /* cookie */
1786 if (sv->cookie) {
1787 struct chunk src;
1788
1789 chunk_printf(&msg, ", cookie: '");
1790
1791 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
1792 chunk_htmlencode(&msg, &src);
1793
1794 chunk_printf(&msg, "'");
1795 }
1796
1797 chunk_printf(&msg, "\"");
1798 }
1799
1800 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001801 ">%s<a name=\"%s/%s\"></a>"
1802 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001803 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001804 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001805 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001806 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001807 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001808 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001809 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001810 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001811 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001812 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01001813 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001814 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
1815 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001816 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
1817
1818 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1819 if (px->mode == PR_MODE_HTTP) {
1820 int i;
1821
1822 chunk_printf(&msg, " title=\"rsp codes:");
1823
1824 for (i = 1; i < 6; i++)
1825 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
1826
1827 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
1828 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001829
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001830 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001831 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01001832 ">%s%s%s</td><td>%s</td>",
1833 (px->mode == PR_MODE_HTTP)?"<u>":"",
1834 U2H0(sv->counters.cum_sess),
1835 (px->mode == PR_MODE_HTTP)?"</u>":"",
1836 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001837
1838 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001839 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001840 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001841 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001842 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01001843 /* errors : request, connect */
1844 "<td></td><td>%s</td>"
1845 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01001846 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001847 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001848 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001849 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001850 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
1851 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01001852 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01001853 sv->counters.cli_aborts,
1854 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01001855 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001856 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001857
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001858 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001859 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001860
Cyril Bontécd19e512010-01-31 22:34:03 +01001861 if (sv->state & SRV_MAINTAIN) {
1862 chunk_printf(&msg, "%s ",
1863 human_time(now.tv_sec - sv->last_change, 1));
1864 chunk_printf(&msg, "MAINT");
1865 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01001866 else if (svs != sv && svs->state & SRV_MAINTAIN) {
1867 chunk_printf(&msg, "%s ",
1868 human_time(now.tv_sec - svs->last_change, 1));
1869 chunk_printf(&msg, "MAINT(via)");
1870 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001871 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001872 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01001873 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001874
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001875 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001876 srv_hlt_st[sv_state],
1877 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
1878 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001879 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001880
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001881 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001882 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001883 get_check_status_description(sv->check_status));
1884
1885 if (*sv->check_desc) {
1886 struct chunk src;
1887
1888 chunk_printf(&msg, ": ");
1889
1890 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
1891 chunk_htmlencode(&msg, &src);
1892 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001893
Willy Tarreaue0454092010-02-26 12:29:07 +01001894 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001895 tv_iszero(&sv->check_start)?"":"* ",
1896 get_check_status_info(sv->check_status));
1897
1898 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001899 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001900
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001901 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01001902 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001903 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001904 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02001905
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001906 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001907 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001908 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001909 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001910 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001911 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001912 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02001913 (sv->state & SRV_BACKUP) ? "-" : "Y",
1914 (sv->state & SRV_BACKUP) ? "Y" : "-");
1915
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001916 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01001917 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01001918 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01001919 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
1920
1921 if (svs->observe)
1922 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
1923
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001924 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001925 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001926 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001927 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001928 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01001929 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001930 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01001931 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
1932 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001933 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001934 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001935 "<td colspan=3></td>");
1936
1937 /* throttle */
1938 if ((sv->state & SRV_WARMINGUP) &&
1939 now.tv_sec < sv->last_change + sv->slowstart &&
1940 now.tv_sec >= sv->last_change) {
1941 unsigned int ratio;
1942 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001943 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001944 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001945 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001946 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001947 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001948 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02001949 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001950 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
1951 "UP %d/%d,", "UP,",
1952 "NOLB %d/%d,", "NOLB,",
1953 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001954 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001955 /* pxid, name */
1956 "%s,%s,"
1957 /* queue : current, max */
1958 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001959 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001960 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001961 /* bytes : in, out */
1962 "%lld,%lld,"
1963 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001964 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001965 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001966 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001967 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001968 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001969 "",
1970 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001971 sv->nbpend, sv->counters.nbpend_max,
1972 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001973 sv->counters.bytes_in, sv->counters.bytes_out,
1974 sv->counters.failed_secu,
1975 sv->counters.failed_conns, sv->counters.failed_resp,
1976 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001977
Willy Tarreau55bb8452007-10-17 18:44:57 +02001978 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01001979 if (sv->state & SRV_MAINTAIN) {
1980 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01001981 }
1982 else if (svs != sv && svs->state & SRV_MAINTAIN) {
1983 chunk_printf(&msg, "MAINT(via),");
1984 }
1985 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01001986 chunk_printf(&msg,
1987 srv_hlt_st[sv_state],
1988 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
1989 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
1990 }
Willy Tarreau91861262007-10-17 17:06:05 +02001991
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001992 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001993 /* weight, active, backup */
1994 "%d,%d,%d,"
1995 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001996 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001997 (sv->state & SRV_BACKUP) ? 0 : 1,
1998 (sv->state & SRV_BACKUP) ? 1 : 0);
1999
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002000 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002001 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002002 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002003 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002004 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002005 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002006 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002007 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002008 ",,,,");
2009
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002010 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002011 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002012 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002013 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002014 LIM2A0(sv->maxqueue, ""),
2015 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002016
2017 /* throttle */
2018 if ((sv->state & SRV_WARMINGUP) &&
2019 now.tv_sec < sv->last_change + sv->slowstart &&
2020 now.tv_sec >= sv->last_change) {
2021 unsigned int ratio;
2022 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002023 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002024 }
2025
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002026 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002027 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002028
2029 /* tracked */
2030 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002031 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002032 sv->tracked->proxy->id, sv->tracked->id);
2033 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002034 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002035
Willy Tarreau7f062c42009-03-05 18:43:00 +01002036 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002037 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002038
2039 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002040 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002041 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002042 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002043
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002044 if (sv->state & SRV_CHECKED) {
2045 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002046 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002047
2048 /* check_code */
2049 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002050 chunk_printf(&msg, "%u,", sv->check_code);
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 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002055 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002056 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002057 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002058 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002059
2060 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002061 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002062 }
2063
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002064 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2065 if (px->mode == PR_MODE_HTTP) {
2066 int i;
2067
2068 for (i=1; i<6; i++)
2069 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2070
2071 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2072 } else {
2073 chunk_printf(&msg, ",,,,,,");
2074 }
2075
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002076 /* failed health analyses */
2077 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2078
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002079 /* requests : req_rate, req_rate_max, req_tot, */
2080 chunk_printf(&msg, ",,,");
2081
Willy Tarreauae526782010-03-04 20:34:23 +01002082 /* errors: cli_aborts, srv_aborts */
2083 chunk_printf(&msg, "%lld,%lld,",
2084 sv->counters.cli_aborts, sv->counters.srv_aborts);
2085
Willy Tarreau7f062c42009-03-05 18:43:00 +01002086 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002087 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002088 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002089 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002090 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002091 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002092
2093 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
2094 /* fall through */
2095
2096 case DATA_ST_PX_BE:
2097 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002098 if ((px->cap & PR_CAP_BE) &&
2099 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2100 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002101 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002102 /* name */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002103 "<tr class=\"backend\"><td class=ac");
2104
2105 if (uri->flags&ST_SHLGNDS) {
2106 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002107 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002108 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002109
2110 /* cookie */
2111 if (px->cookie_name) {
2112 struct chunk src;
2113
2114 chunk_printf(&msg, ", cookie: '");
2115
2116 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2117 chunk_htmlencode(&msg, &src);
2118
2119 chunk_printf(&msg, "'");
2120 }
2121
2122 chunk_printf(&msg, "\"");
2123
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002124 }
2125
2126 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002127 ">%s<a name=\"%s/Backend\"></a>"
2128 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002129 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002130 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002131 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002132 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002133 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002134 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002135 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002136 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002137 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->counters.nbpend_max),
2138 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->counters.be_sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02002139
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002140 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002141 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002142 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002143 "<td"
2144 "",
2145 U2H2(px->beconn), U2H3(px->counters.beconn_max), U2H4(px->fullconn));
2146
2147 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2148 if (px->mode == PR_MODE_HTTP) {
2149 int i;
2150
2151 chunk_printf(&msg, " title=\"rsp codes:");
2152
2153 for (i = 1; i < 6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01002154 chunk_printf(&msg, " %dxx=%lld", i, px->counters.be.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002155
Willy Tarreau24657792010-02-26 10:30:28 +01002156 chunk_printf(&msg, " other=%lld\"", px->counters.be.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002157 }
2158
2159 chunk_printf(&msg,
2160 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002161 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002162 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002163 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002164 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002165 (px->mode == PR_MODE_HTTP)?"<u>":"",
2166 U2H6(px->counters.cum_beconn),
2167 (px->mode == PR_MODE_HTTP)?"</u>":"",
2168 U2H7(px->counters.cum_lbconn),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002169 U2H8(px->counters.bytes_in), U2H9(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002170
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002171 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002172 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002173 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002174 /* errors : request, connect */
2175 "<td></td><td>%s</td>"
2176 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002177 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002178 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002179 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002180 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02002181 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002182 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02002183 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002184 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2185 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002186 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002187 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
Willy Tarreauae526782010-03-04 20:34:23 +01002188 U2H2(px->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002189 px->counters.cli_aborts,
2190 px->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002191 U2H5(px->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002192 px->counters.retries, px->counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002193 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01002194 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2195 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01002196 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002197 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002198
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002199 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002200 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002201 "<td class=ac>&nbsp;</td><td>%d</td>"
2202 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002203 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002204 "</tr>",
2205 px->down_trans,
2206 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002207 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002208 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002209 /* pxid, name */
2210 "%s,BACKEND,"
2211 /* queue : current, max */
2212 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002213 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002214 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002215 /* bytes : in, out */
2216 "%lld,%lld,"
2217 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002218 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002219 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002220 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002221 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002222 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002223 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02002224 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002225 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02002226 * active and backups. */
2227 "%s,"
2228 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002229 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002230 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002231 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002232 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002233 /* rate, rate_lim, rate_max, */
2234 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002235 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002236 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002237 px->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002238 px->nbpend /* or px->totpend ? */, px->counters.nbpend_max,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002239 px->beconn, px->counters.beconn_max, px->fullconn, px->counters.cum_beconn,
2240 px->counters.bytes_in, px->counters.bytes_out,
2241 px->counters.denied_req, px->counters.denied_resp,
2242 px->counters.failed_conns, px->counters.failed_resp,
2243 px->counters.retries, px->counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01002244 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01002245 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002246 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002247 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01002248 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002249 relative_pid, px->uuid,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002250 px->counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002251 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002252 px->counters.be_sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002253
2254 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2255 if (px->mode == PR_MODE_HTTP) {
2256 int i;
2257
2258 for (i=1; i<6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01002259 chunk_printf(&msg, "%lld,", px->counters.be.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002260
Willy Tarreau24657792010-02-26 10:30:28 +01002261 chunk_printf(&msg, "%lld,", px->counters.be.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002262 } else {
2263 chunk_printf(&msg, ",,,,,,");
2264 }
2265
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002266 /* failed health analyses */
2267 chunk_printf(&msg, ",");
2268
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002269 /* requests : req_rate, req_rate_max, req_tot, */
2270 chunk_printf(&msg, ",,,");
2271
Willy Tarreauae526782010-03-04 20:34:23 +01002272 /* errors: cli_aborts, srv_aborts */
2273 chunk_printf(&msg, "%lld,%lld,",
2274 px->counters.cli_aborts, px->counters.srv_aborts);
2275
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002276 /* finish with EOL */
2277 chunk_printf(&msg, "\n");
2278
Willy Tarreau55bb8452007-10-17 18:44:57 +02002279 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002280 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002281 return 0;
2282 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002283
Willy Tarreau91861262007-10-17 17:06:05 +02002284 s->data_ctx.stats.px_st = DATA_ST_PX_END;
2285 /* fall through */
2286
2287 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002288 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002289 chunk_printf(&msg, "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02002290
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002291 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002292 return 0;
2293 }
Willy Tarreau91861262007-10-17 17:06:05 +02002294
2295 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
2296 /* fall through */
2297
2298 case DATA_ST_PX_FIN:
2299 return 1;
2300
2301 default:
2302 /* unknown state, we should put an abort() here ! */
2303 return 1;
2304 }
2305}
2306
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002307/* This function is called to send output to the response buffer. It dumps a
2308 * complete session state onto the output buffer <rep>. The session has to be
2309 * set in data_ctx.sess.target. It returns 0 if the output buffer is full and
2310 * it needs to be called again, otherwise non-zero. It is designed to be called
2311 * from stats_dump_sess_to_buffer() below.
2312 */
2313
2314/* returns 1 if dump is not complete */
2315int stats_dump_full_sess_to_buffer(struct session *s, struct buffer *rep)
2316{
2317 struct tm tm;
2318 struct chunk msg;
2319 struct session *sess;
2320 extern const char *monthname[12];
2321 char pn[INET6_ADDRSTRLEN];
2322
2323 chunk_init(&msg, trash, sizeof(trash));
2324 sess = s->data_ctx.sess.target;
2325
2326 if (s->data_ctx.sess.section > 0 && s->data_ctx.sess.uid != sess->uniq_id) {
2327 /* session changed, no need to go any further */
2328 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
2329 if (buffer_feed_chunk(rep, &msg) >= 0)
2330 return 0;
2331 s->data_ctx.sess.target = NULL;
2332 s->data_ctx.sess.uid = 0;
2333 return 1;
2334 }
2335
2336 switch (s->data_ctx.sess.section) {
2337 case 0: /* main status of the session */
2338 s->data_ctx.sess.uid = sess->uniq_id;
2339 s->data_ctx.sess.section = 1;
2340 /* fall through */
2341
2342 case 1:
2343 chunk_printf(&msg,
2344 "%p: id=%u, proto=%s",
2345 sess,
2346 sess->uniq_id,
2347 sess->listener->proto->name);
2348
2349 switch (sess->listener->proto->sock_family) {
2350 case AF_INET:
2351 inet_ntop(AF_INET,
2352 (const void *)&((struct sockaddr_in *)&sess->cli_addr)->sin_addr,
2353 pn, sizeof(pn));
2354
2355 chunk_printf(&msg,
2356 " source=%s:%d\n",
2357 pn,
2358 ntohs(((struct sockaddr_in *)&sess->cli_addr)->sin_port));
2359 break;
2360 case AF_INET6:
2361 inet_ntop(AF_INET6,
2362 (const void *)&((struct sockaddr_in6 *)(&sess->cli_addr))->sin6_addr,
2363 pn, sizeof(pn));
2364
2365 chunk_printf(&msg,
2366 " source=%s:%d\n",
2367 pn,
2368 ntohs(((struct sockaddr_in6 *)&sess->cli_addr)->sin6_port));
2369 break;
2370 case AF_UNIX:
2371 default:
2372 /* no more information to print right now */
2373 chunk_printf(&msg, "\n");
2374 break;
2375 }
2376
2377 chunk_printf(&msg,
2378 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
2379 sess->flags, sess->conn_retries, sess->srv_conn, sess->pend_pos);
2380
2381 chunk_printf(&msg,
2382 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)\n",
2383 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
2384 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
2385 sess->listener ? sess->listener->luid : 0);
2386
2387 chunk_printf(&msg,
2388 " backend=%s (id=%u mode=%s) server=%s (id=%u)\n",
2389 sess->be->id, sess->be->uuid, sess->be->mode ? "http" : "tcp",
2390 sess->srv ? sess->srv->id : "<none>",
2391 sess->srv ? sess->srv->puid : 0);
2392
2393 chunk_printf(&msg,
2394 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
2395 sess->task,
2396 sess->task->state,
2397 sess->task->nice, sess->task->calls,
2398 sess->task->expire ?
2399 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
2400 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
2401 TICKS_TO_MS(1000)) : "<NEVER>",
2402 task_in_rq(sess->task) ? ", running" : "");
2403
2404 get_localtime(sess->logs.accept_date.tv_sec, &tm);
2405 chunk_printf(&msg,
2406 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
2407 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2408 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
2409 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
2410
2411 chunk_printf(&msg,
2412 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
2413 &sess->si[0],
2414 sess->si[0].state,
2415 sess->si[0].flags,
2416 sess->si[0].fd,
2417 sess->si[0].exp ?
2418 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
2419 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
2420 TICKS_TO_MS(1000)) : "<NEVER>",
2421 sess->si[0].err_type);
2422
2423 chunk_printf(&msg,
2424 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
2425 &sess->si[1],
2426 sess->si[1].state,
2427 sess->si[1].flags,
2428 sess->si[1].fd,
2429 sess->si[1].exp ?
2430 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
2431 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
2432 TICKS_TO_MS(1000)) : "<NEVER>",
2433 sess->si[1].err_type);
2434
2435 chunk_printf(&msg,
2436 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
2437 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
2438 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
2439
2440
2441 chunk_printf(&msg,
2442 " req=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%ld)\n"
2443 " an_exp=%s",
2444 sess->req,
2445 sess->req->flags, sess->req->analysers,
2446 sess->req->l, sess->req->send_max,
2447 sess->req->pipe ? sess->req->pipe->data : 0,
2448 sess->req->to_forward,
2449 sess->req->analyse_exp ?
2450 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
2451 TICKS_TO_MS(1000)) : "<NEVER>");
2452
2453 chunk_printf(&msg,
2454 " rex=%s",
2455 sess->req->rex ?
2456 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
2457 TICKS_TO_MS(1000)) : "<NEVER>");
2458
2459 chunk_printf(&msg,
2460 " wex=%s\n"
2461 " data=%p r=%d w=%d lr=%d total=%lld\n",
2462 sess->req->wex ?
2463 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
2464 TICKS_TO_MS(1000)) : "<NEVER>",
2465 sess->req->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01002466 (int)(sess->req->r - sess->req->data),
2467 (int)(sess->req->w - sess->req->data),
2468 (int)(sess->req->lr - sess->req->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002469 sess->req->total);
2470
2471 chunk_printf(&msg,
2472 " res=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%ld)\n"
2473 " an_exp=%s",
2474 sess->rep,
2475 sess->rep->flags, sess->rep->analysers,
2476 sess->rep->l, sess->rep->send_max,
2477 sess->rep->pipe ? sess->rep->pipe->data : 0,
2478 sess->rep->to_forward,
2479 sess->rep->analyse_exp ?
2480 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
2481 TICKS_TO_MS(1000)) : "<NEVER>");
2482
2483 chunk_printf(&msg,
2484 " rex=%s",
2485 sess->rep->rex ?
2486 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
2487 TICKS_TO_MS(1000)) : "<NEVER>");
2488
2489 chunk_printf(&msg,
2490 " wex=%s\n"
2491 " data=%p r=%d w=%d lr=%d total=%lld\n",
2492 sess->rep->wex ?
2493 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
2494 TICKS_TO_MS(1000)) : "<NEVER>",
2495 sess->rep->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01002496 (int)(sess->rep->r - sess->rep->data),
2497 (int)(sess->rep->w - sess->rep->data),
2498 (int)(sess->rep->lr - sess->rep->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002499 sess->rep->total);
2500
2501 if (buffer_feed_chunk(rep, &msg) >= 0)
2502 return 0;
2503
2504 /* use other states to dump the contents */
2505 }
2506 /* end of dump */
2507 s->data_ctx.sess.uid = 0;
2508 return 1;
2509}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002510
2511/* This function is called to send output to the response buffer.
2512 * It dumps the sessions states onto the output buffer <rep>.
2513 * Expects to be called with client socket shut down on input.
2514 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002515 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002516 */
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002517int stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002518{
2519 struct chunk msg;
2520
2521 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
2522 /* If we're forced to shut down, we might have to remove our
2523 * reference to the last session being dumped.
2524 */
2525 if (s->data_state == DATA_ST_LIST) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002526 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002527 LIST_DEL(&s->data_ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002528 LIST_INIT(&s->data_ctx.sess.bref.users);
2529 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002530 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002531 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002532 }
2533
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002534 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002535
2536 switch (s->data_state) {
2537 case DATA_ST_INIT:
2538 /* the function had not been called yet, let's prepare the
2539 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002540 * pointer to the first in the global list. When a target
2541 * session is being destroyed, it is responsible for updating
2542 * this pointer. We know we have reached the end when this
2543 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002544 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002545 LIST_INIT(&s->data_ctx.sess.bref.users);
2546 s->data_ctx.sess.bref.ref = sessions.n;
2547 s->data_state = DATA_ST_LIST;
2548 /* fall through */
2549
2550 case DATA_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002551 /* first, let's detach the back-ref from a possible previous session */
2552 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
2553 LIST_DEL(&s->data_ctx.sess.bref.users);
2554 LIST_INIT(&s->data_ctx.sess.bref.users);
2555 }
2556
2557 /* and start from where we stopped */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002558 while (s->data_ctx.sess.bref.ref != &sessions) {
2559 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
2560 struct session *curr_sess;
2561
2562 curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list);
2563
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002564 if (s->data_ctx.sess.target) {
2565 if (s->data_ctx.sess.target != curr_sess)
2566 goto next_sess;
2567
2568 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
2569 /* call the proper dump() function and return if we're missing space */
2570 if (!stats_dump_full_sess_to_buffer(s, rep))
2571 return 0;
2572
2573 /* session dump complete */
2574 LIST_DEL(&s->data_ctx.sess.bref.users);
2575 LIST_INIT(&s->data_ctx.sess.bref.users);
2576 s->data_ctx.sess.target = NULL;
2577 break;
2578 }
2579
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002580 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002581 "%p: proto=%s",
2582 curr_sess,
2583 curr_sess->listener->proto->name);
2584
2585 switch (curr_sess->listener->proto->sock_family) {
2586 case AF_INET:
2587 inet_ntop(AF_INET,
2588 (const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr,
2589 pn, sizeof(pn));
2590
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002591 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002592 " src=%s:%d fe=%s be=%s srv=%s",
2593 pn,
2594 ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port),
2595 curr_sess->fe->id,
2596 curr_sess->be->id,
2597 curr_sess->srv ? curr_sess->srv->id : "<none>"
2598 );
2599 break;
2600 case AF_INET6:
2601 inet_ntop(AF_INET6,
2602 (const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr,
2603 pn, sizeof(pn));
2604
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002605 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002606 " src=%s:%d fe=%s be=%s srv=%s",
2607 pn,
2608 ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port),
2609 curr_sess->fe->id,
2610 curr_sess->be->id,
2611 curr_sess->srv ? curr_sess->srv->id : "<none>"
2612 );
2613
2614 break;
2615 case AF_UNIX:
2616 /* no more information to print right now */
2617 break;
2618 }
2619
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002620 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02002621 " ts=%02x age=%s calls=%d",
2622 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01002623 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
2624 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002625
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002626 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002627 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
2628 curr_sess->req->flags,
2629 curr_sess->req->l,
2630 curr_sess->req->analysers,
2631 curr_sess->req->rex ?
2632 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
2633 TICKS_TO_MS(1000)) : "");
2634
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002635 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002636 ",wx=%s",
2637 curr_sess->req->wex ?
2638 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
2639 TICKS_TO_MS(1000)) : "");
2640
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002641 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002642 ",ax=%s]",
2643 curr_sess->req->analyse_exp ?
2644 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
2645 TICKS_TO_MS(1000)) : "");
2646
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002647 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002648 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
2649 curr_sess->rep->flags,
2650 curr_sess->rep->l,
2651 curr_sess->rep->analysers,
2652 curr_sess->rep->rex ?
2653 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
2654 TICKS_TO_MS(1000)) : "");
2655
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002656 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002657 ",wx=%s",
2658 curr_sess->rep->wex ?
2659 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
2660 TICKS_TO_MS(1000)) : "");
2661
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002662 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002663 ",ax=%s]",
2664 curr_sess->rep->analyse_exp ?
2665 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
2666 TICKS_TO_MS(1000)) : "");
2667
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002668 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002669 " s0=[%d,%1xh,fd=%d,ex=%s]",
2670 curr_sess->si[0].state,
2671 curr_sess->si[0].flags,
2672 curr_sess->si[0].fd,
2673 curr_sess->si[0].exp ?
2674 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
2675 TICKS_TO_MS(1000)) : "");
2676
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002677 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002678 " s1=[%d,%1xh,fd=%d,ex=%s]",
2679 curr_sess->si[1].state,
2680 curr_sess->si[1].flags,
2681 curr_sess->si[1].fd,
2682 curr_sess->si[1].exp ?
2683 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
2684 TICKS_TO_MS(1000)) : "");
2685
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002686 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002687 " exp=%s",
2688 curr_sess->task->expire ?
2689 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
2690 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01002691 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002692 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002693
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002694 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002695
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002696 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002697 /* let's try again later from this session. We add ourselves into
2698 * this session's users so that it can remove us upon termination.
2699 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002700 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002701 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002702 }
2703
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002704 next_sess:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002705 s->data_ctx.sess.bref.ref = curr_sess->list.n;
2706 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002707
2708 if (s->data_ctx.sess.target) {
2709 /* specified session not found */
2710 if (s->data_ctx.sess.section > 0)
2711 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
2712 else
2713 chunk_printf(&msg, "Session not found.\n");
2714
2715 if (buffer_feed_chunk(rep, &msg) >= 0)
2716 return 0;
2717
2718 s->data_ctx.sess.target = NULL;
2719 s->data_ctx.sess.uid = 0;
2720 return 1;
2721 }
2722
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002723 s->data_state = DATA_ST_FIN;
2724 /* fall through */
2725
2726 default:
2727 s->data_state = DATA_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002728 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002729 }
2730}
2731
Willy Tarreaud426a182010-03-05 14:58:26 +01002732/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01002733 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
2734 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
2735 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
2736 * lines are respected within the limit of 70 output chars. Lines that are
2737 * continuation of a previous truncated line begin with "+" instead of " "
2738 * after the offset. The new pointer is returned.
2739 */
Willy Tarreaud426a182010-03-05 14:58:26 +01002740static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
2741 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01002742{
2743 int end;
2744 unsigned char c;
2745
2746 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002747 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01002748 return ptr;
2749
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002750 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01002751
Willy Tarreaud426a182010-03-05 14:58:26 +01002752 while (ptr < len && ptr < bsize) {
2753 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01002754 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002755 if (out->len > end - 2)
2756 break;
2757 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01002758 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002759 if (out->len > end - 3)
2760 break;
2761 out->str[out->len++] = '\\';
2762 switch (c) {
2763 case '\t': c = 't'; break;
2764 case '\n': c = 'n'; break;
2765 case '\r': c = 'r'; break;
2766 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01002767 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002768 }
2769 out->str[out->len++] = c;
2770 } else {
2771 if (out->len > end - 5)
2772 break;
2773 out->str[out->len++] = '\\';
2774 out->str[out->len++] = 'x';
2775 out->str[out->len++] = hextab[(c >> 4) & 0xF];
2776 out->str[out->len++] = hextab[c & 0xF];
2777 }
Willy Tarreaud426a182010-03-05 14:58:26 +01002778 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002779 /* we had a line break, let's return now */
2780 out->str[out->len++] = '\n';
2781 *line = ptr;
2782 return ptr;
2783 }
2784 }
2785 /* we have an incomplete line, we return it as-is */
2786 out->str[out->len++] = '\n';
2787 return ptr;
2788}
2789
2790/* This function is called to send output to the response buffer.
2791 * It dumps the errors logged in proxies onto the output buffer <rep>.
2792 * Expects to be called with client socket shut down on input.
2793 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau61b34732009-10-03 23:49:35 +02002794 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau74808cb2009-03-04 15:53:18 +01002795 */
Willy Tarreau61b34732009-10-03 23:49:35 +02002796int stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau74808cb2009-03-04 15:53:18 +01002797{
2798 extern const char *monthname[12];
2799 struct chunk msg;
2800
Willy Tarreau61b34732009-10-03 23:49:35 +02002801 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW)))
2802 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002803
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002804 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01002805
2806 if (!s->data_ctx.errors.px) {
2807 /* the function had not been called yet, let's prepare the
2808 * buffer for a response.
2809 */
Willy Tarreau74808cb2009-03-04 15:53:18 +01002810 s->data_ctx.errors.px = proxy;
2811 s->data_ctx.errors.buf = 0;
2812 s->data_ctx.errors.bol = 0;
2813 s->data_ctx.errors.ptr = -1;
2814 }
2815
2816 /* we have two inner loops here, one for the proxy, the other one for
2817 * the buffer.
2818 */
2819 while (s->data_ctx.errors.px) {
2820 struct error_snapshot *es;
2821
2822 if (s->data_ctx.errors.buf == 0)
2823 es = &s->data_ctx.errors.px->invalid_req;
2824 else
2825 es = &s->data_ctx.errors.px->invalid_rep;
2826
2827 if (!es->when.tv_sec)
2828 goto next;
2829
2830 if (s->data_ctx.errors.iid >= 0 &&
2831 s->data_ctx.errors.px->uuid != s->data_ctx.errors.iid &&
2832 es->oe->uuid != s->data_ctx.errors.iid)
2833 goto next;
2834
2835 if (s->data_ctx.errors.ptr < 0) {
2836 /* just print headers now */
2837
2838 char pn[INET6_ADDRSTRLEN];
2839 struct tm tm;
2840
2841 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01002842 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01002843 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002844 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01002845
2846
2847 if (es->src.ss_family == AF_INET)
2848 inet_ntop(AF_INET,
2849 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
2850 pn, sizeof(pn));
2851 else
2852 inet_ntop(AF_INET6,
2853 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
2854 pn, sizeof(pn));
2855
2856 switch (s->data_ctx.errors.buf) {
2857 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002858 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002859 " frontend %s (#%d): invalid request\n"
2860 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01002861 " request length %d bytes, error at position %d:\n \n",
Willy Tarreau74808cb2009-03-04 15:53:18 +01002862 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
Willy Tarreau3fd6cec2010-03-25 06:45:07 +01002863 pn, es->sid, (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
2864 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002865 es->srv ? es->srv->id : "<NONE>",
2866 es->srv ? es->srv->puid : -1,
2867 es->len, es->pos);
2868 break;
2869 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002870 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002871 " backend %s (#%d) : invalid response\n"
2872 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01002873 " response length %d bytes, error at position %d:\n \n",
Willy Tarreau74808cb2009-03-04 15:53:18 +01002874 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
2875 pn, es->sid, es->oe->id, es->oe->uuid,
2876 es->srv ? es->srv->id : "<NONE>",
2877 es->srv ? es->srv->puid : -1,
2878 es->len, es->pos);
2879 break;
2880 }
2881
Willy Tarreau61b34732009-10-03 23:49:35 +02002882 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002883 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02002884 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002885 }
2886 s->data_ctx.errors.ptr = 0;
2887 s->data_ctx.errors.sid = es->sid;
2888 }
2889
2890 if (s->data_ctx.errors.sid != es->sid) {
2891 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002892 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002893 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau61b34732009-10-03 23:49:35 +02002894 if (buffer_feed_chunk(rep, &msg) >= 0)
2895 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002896 goto next;
2897 }
2898
2899 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau61b34732009-10-03 23:49:35 +02002900 while (s->data_ctx.errors.ptr < es->len && s->data_ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002901 int newptr;
2902 int newline;
2903
2904 newline = s->data_ctx.errors.bol;
Willy Tarreaud426a182010-03-05 14:58:26 +01002905 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, s->data_ctx.errors.ptr);
Willy Tarreau74808cb2009-03-04 15:53:18 +01002906 if (newptr == s->data_ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02002907 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002908
Willy Tarreau61b34732009-10-03 23:49:35 +02002909 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002910 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02002911 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002912 }
2913 s->data_ctx.errors.ptr = newptr;
2914 s->data_ctx.errors.bol = newline;
2915 };
2916 next:
2917 s->data_ctx.errors.bol = 0;
2918 s->data_ctx.errors.ptr = -1;
2919 s->data_ctx.errors.buf++;
2920 if (s->data_ctx.errors.buf > 1) {
2921 s->data_ctx.errors.buf = 0;
2922 s->data_ctx.errors.px = s->data_ctx.errors.px->next;
2923 }
2924 }
2925
2926 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02002927 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002928}
2929
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002930
Willy Tarreau10522fd2008-07-09 20:12:41 +02002931static struct cfg_kw_list cfg_kws = {{ },{
2932 { CFG_GLOBAL, "stats", stats_parse_global },
2933 { 0, NULL, NULL },
2934}};
2935
2936__attribute__((constructor))
2937static void __dumpstats_module_init(void)
2938{
2939 cfg_register_keywords(&cfg_kws);
2940}
2941
Willy Tarreau91861262007-10-17 17:06:05 +02002942/*
2943 * Local variables:
2944 * c-indent-level: 8
2945 * c-basic-offset: 8
2946 * End:
2947 */