blob: 495e28007d4bbb25fba841c088e50a7c647fd084 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
2 * Functions dedicated to statistics output
3 *
Willy Tarreaua206fa92009-01-25 14:02:00 +01004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020020#include <pwd.h>
21#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020022
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreau10522fd2008-07-09 20:12:41 +020027#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020028#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
38
Willy Tarreau91861262007-10-17 17:06:05 +020039#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020040
41#include <proto/backend.h>
42#include <proto/buffers.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020043#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020044#include <proto/dumpstats.h>
45#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010046#include <proto/freq_ctr.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010047#include <proto/pipe.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020048#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020049#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020050#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020051#include <proto/server.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010052#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010053#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020054
Willy Tarreau5ca791d2009-08-16 19:06:42 +020055const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020056 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +020057 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020058 " help : this message\n"
59 " prompt : toggle interactive mode with prompt\n"
60 " quit : disconnect\n"
61 " show info : report information about the running process\n"
62 " show stat : report counters for each proxy and server\n"
63 " show errors : report last request and response errors for each proxy\n"
64 " show sess : report the list of current sessions\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +020065 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +020066 " set weight : change a server's weight\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020067 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020068
Willy Tarreau6162db22009-10-10 17:13:00 +020069const char stats_permission_denied_msg[] =
70 "Permission denied\n"
71 "";
72
Willy Tarreaufbee7132007-10-18 13:53:22 +020073/* This function parses a "stats" statement in the "global" section. It returns
74 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
75 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
76 * zero included. The trailing '\n' must not be written. The function must be
77 * called with <args> pointing to the first word after "stats".
78 */
Willy Tarreau10522fd2008-07-09 20:12:41 +020079static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
80 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +020081{
Willy Tarreau10522fd2008-07-09 20:12:41 +020082 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +020083 if (!strcmp(args[0], "socket")) {
84 struct sockaddr_un su;
85 int cur_arg;
86
87 if (*args[1] == 0) {
88 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
89 return -1;
90 }
91
92 if (global.stats_sock.state != LI_NEW) {
93 snprintf(err, errlen, "'stats socket' already specified in global section");
94 return -1;
95 }
96
97 su.sun_family = AF_UNIX;
98 strncpy(su.sun_path, args[1], sizeof(su.sun_path));
99 su.sun_path[sizeof(su.sun_path) - 1] = 0;
100 memcpy(&global.stats_sock.addr, &su, sizeof(su)); // guaranteed to fit
101
Willy Tarreau89a63132009-08-16 17:41:45 +0200102 if (!global.stats_fe) {
103 if ((global.stats_fe = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) {
104 snprintf(err, errlen, "out of memory");
105 return -1;
106 }
107
108 LIST_INIT(&global.stats_fe->pendconns);
109 LIST_INIT(&global.stats_fe->acl);
110 LIST_INIT(&global.stats_fe->block_cond);
111 LIST_INIT(&global.stats_fe->redirect_rules);
112 LIST_INIT(&global.stats_fe->mon_fail_cond);
113 LIST_INIT(&global.stats_fe->switching_rules);
114 LIST_INIT(&global.stats_fe->tcp_req.inspect_rules);
115
116 /* Timeouts are defined as -1, so we cannot use the zeroed area
117 * as a default value.
118 */
119 proxy_reset_timeouts(global.stats_fe);
120
121 global.stats_fe->last_change = now.tv_sec;
122 global.stats_fe->id = strdup("GLOBAL");
123 global.stats_fe->cap = PR_CAP_FE;
124 }
125
Willy Tarreaufbee7132007-10-18 13:53:22 +0200126 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100127 global.stats_sock.options = LI_O_NONE;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200128 global.stats_sock.accept = uxst_event_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200129 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200130 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200131 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreau89a63132009-08-16 17:41:45 +0200132 global.stats_sock.private = global.stats_fe; /* must point to the frontend */
Willy Tarreau6162db22009-10-10 17:13:00 +0200133 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau89a63132009-08-16 17:41:45 +0200134
135 global.stats_fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
136 global.stats_sock.timeout = &global.stats_fe->timeout.client;
137
138 global.stats_sock.next = global.stats_fe->listen;
139 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200140
141 cur_arg = 2;
142 while (*args[cur_arg]) {
143 if (!strcmp(args[cur_arg], "uid")) {
144 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
145 cur_arg += 2;
146 }
147 else if (!strcmp(args[cur_arg], "gid")) {
148 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
149 cur_arg += 2;
150 }
151 else if (!strcmp(args[cur_arg], "mode")) {
152 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
153 cur_arg += 2;
154 }
155 else if (!strcmp(args[cur_arg], "user")) {
156 struct passwd *user;
157 user = getpwnam(args[cur_arg + 1]);
158 if (!user) {
159 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
160 args[cur_arg + 1]);
161 return -1;
162 }
163 global.stats_sock.perm.ux.uid = user->pw_uid;
164 cur_arg += 2;
165 }
166 else if (!strcmp(args[cur_arg], "group")) {
167 struct group *group;
168 group = getgrnam(args[cur_arg + 1]);
169 if (!group) {
170 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
171 args[cur_arg + 1]);
172 return -1;
173 }
174 global.stats_sock.perm.ux.gid = group->gr_gid;
175 cur_arg += 2;
176 }
Willy Tarreau6162db22009-10-10 17:13:00 +0200177 else if (!strcmp(args[cur_arg], "level")) {
178 if (!strcmp(args[cur_arg+1], "user"))
179 global.stats_sock.perm.ux.level = ACCESS_LVL_USER;
180 else if (!strcmp(args[cur_arg+1], "operator"))
181 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER;
182 else if (!strcmp(args[cur_arg+1], "admin"))
183 global.stats_sock.perm.ux.level = ACCESS_LVL_ADMIN;
184 else {
185 snprintf(err, errlen, "'stats socket level' only supports 'user', 'operator', and 'admin'");
186 return -1;
187 }
188 cur_arg += 2;
189 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200190 else {
Willy Tarreau6162db22009-10-10 17:13:00 +0200191 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', 'level', and 'mode'");
Willy Tarreaufbee7132007-10-18 13:53:22 +0200192 return -1;
193 }
194 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100195
Willy Tarreaufbee7132007-10-18 13:53:22 +0200196 uxst_add_listener(&global.stats_sock);
197 global.maxsock++;
198 }
199 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100200 unsigned timeout;
201 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
202
203 if (res) {
204 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
205 return -1;
206 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200207
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100208 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200209 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
210 return -1;
211 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200212 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200213 }
214 else if (!strcmp(args[0], "maxconn")) {
215 int maxconn = atol(args[1]);
216
217 if (maxconn <= 0) {
218 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
219 return -1;
220 }
221 global.maxsock -= global.stats_sock.maxconn;
222 global.stats_sock.maxconn = maxconn;
223 global.maxsock += global.stats_sock.maxconn;
224 }
225 else {
226 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
227 return -1;
228 }
229 return 0;
230}
231
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200232int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100233{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200234 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100235 "# pxname,svname,"
236 "qcur,qmax,"
237 "scur,smax,slim,stot,"
238 "bin,bout,"
239 "dreq,dresp,"
240 "ereq,econ,eresp,"
241 "wretr,wredis,"
242 "status,weight,act,bck,"
243 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200244 "pid,iid,sid,throttle,lbtot,tracked,type,"
245 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200246 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100247 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100248 "\n");
249}
250
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200251/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200252 * called from an applet running in a stream interface. The function returns 1
253 * if the request was understood, otherwise zero. It sets si->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200254 * designating the function which will have to process the request, which can
255 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200256 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200257int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200258{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200259 struct session *s = si->private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200260 char *args[MAX_STATS_ARGS + 1];
261 int arg;
262
263 while (isspace((unsigned char)*line))
264 line++;
265
266 arg = 0;
267 args[arg] = line;
268
269 while (*line && arg < MAX_STATS_ARGS) {
270 if (isspace((unsigned char)*line)) {
271 *line++ = '\0';
272
273 while (isspace((unsigned char)*line))
274 line++;
275
276 args[++arg] = line;
277 continue;
278 }
279
280 line++;
281 }
282
283 while (++arg <= MAX_STATS_ARGS)
284 args[arg] = line;
285
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200286 s->data_ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200287 if (strcmp(args[0], "show") == 0) {
288 if (strcmp(args[1], "stat") == 0) {
289 if (*args[2] && *args[3] && *args[4]) {
290 s->data_ctx.stats.flags |= STAT_BOUND;
291 s->data_ctx.stats.iid = atoi(args[2]);
292 s->data_ctx.stats.type = atoi(args[3]);
293 s->data_ctx.stats.sid = atoi(args[4]);
294 }
295
296 s->data_ctx.stats.flags |= STAT_SHOW_STAT;
297 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200298 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200299 si->st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200300 }
301 else if (strcmp(args[1], "info") == 0) {
302 s->data_ctx.stats.flags |= STAT_SHOW_INFO;
303 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200304 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200305 si->st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200306 }
307 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200308 s->data_state = DATA_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200309 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200310 s->data_ctx.cli.msg = stats_permission_denied_msg;
311 si->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200312 return 1;
313 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200314 si->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200315 }
316 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200317 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200318 s->data_ctx.cli.msg = stats_permission_denied_msg;
319 si->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200320 return 1;
321 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200322 if (*args[2])
323 s->data_ctx.errors.iid = atoi(args[2]);
324 else
325 s->data_ctx.errors.iid = -1;
326 s->data_ctx.errors.px = NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200327 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200328 si->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200329 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200330 else { /* neither "stat" nor "info" nor "sess" nor "errors"*/
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200331 return 0;
332 }
333 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200334 else if (strcmp(args[0], "clear") == 0) {
335 if (strcmp(args[1], "counters") == 0) {
336 struct proxy *px;
337 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200338 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200339 int clrall = 0;
340
341 if (strcmp(args[2], "all") == 0)
342 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200343
Willy Tarreau6162db22009-10-10 17:13:00 +0200344 /* check permissions */
345 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
346 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200347 s->data_ctx.cli.msg = stats_permission_denied_msg;
348 si->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200349 return 1;
350 }
351
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200352 for (px = proxy; px; px = px->next) {
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200353 if (clrall)
354 memset(&px->counters, 0, sizeof(px->counters));
355 else {
356 px->counters.feconn_max = 0;
357 px->counters.beconn_max = 0;
358 px->counters.fe_sps_max = 0;
359 px->counters.be_sps_max = 0;
360 px->counters.nbpend_max = 0;
361 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200362
363 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200364 if (clrall)
365 memset(&sv->counters, 0, sizeof(sv->counters));
366 else {
367 sv->counters.cur_sess_max = 0;
368 sv->counters.nbpend_max = 0;
369 sv->counters.sps_max = 0;
370 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200371
372 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200373 if (li->counters) {
374 if (clrall)
375 memset(li->counters, 0, sizeof(*li->counters));
376 else
377 li->counters->conn_max = 0;
378 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200379 }
380
381 return 1;
382 }
383 else {
384 return 0;
385 }
386 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200387 else if (strcmp(args[0], "get") == 0) {
388 if (strcmp(args[1], "weight") == 0) {
389 struct proxy *px;
390 struct server *sv;
391
392 /* split "backend/server" and make <line> point to server */
393 for (line = args[2]; *line; line++)
394 if (*line == '/') {
395 *line++ = '\0';
396 break;
397 }
398
399 if (!*line) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200400 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
401 si->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200402 return 1;
403 }
404
405 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200406 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
407 si->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200408 return 1;
409 }
410
411 /* return server's effective weight at the moment */
412 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
413 buffer_feed(si->ib, trash);
414 return 1;
415 }
416 else { /* not "get weight" */
417 return 0;
418 }
419 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200420 else if (strcmp(args[0], "set") == 0) {
421 if (strcmp(args[1], "weight") == 0) {
422 struct proxy *px;
423 struct server *sv;
424 int w;
425
426 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200427 s->data_ctx.cli.msg = stats_permission_denied_msg;
428 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200429 return 1;
430 }
431
432 /* split "backend/server" and make <line> point to server */
433 for (line = args[2]; *line; line++)
434 if (*line == '/') {
435 *line++ = '\0';
436 break;
437 }
438
439 if (!*line || !*args[3]) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200440 s->data_ctx.cli.msg = "Require 'backend/server' and 'weight' or 'weight%'.\n";
441 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200442 return 1;
443 }
444
445 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200446 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
447 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200448 return 1;
449 }
450
451 /* if the weight is terminated with '%', it is set relative to
452 * the initial weight, otherwise it is absolute.
453 */
454 w = atoi(args[3]);
455 if (strchr(args[3], '%') != NULL) {
456 if (w < 0 || w > 100) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200457 s->data_ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
458 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200459 return 1;
460 }
461 w = sv->iweight * w / 100;
462 }
463 else {
464 if (w < 0 || w > 256) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200465 s->data_ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
466 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200467 return 1;
468 }
469 }
470
471 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200472 s->data_ctx.cli.msg = "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
473 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200474 return 1;
475 }
476
477 sv->uweight = w;
478
479 if (px->lbprm.algo & BE_LB_PROP_DYN) {
480 /* we must take care of not pushing the server to full throttle during slow starts */
481 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
482 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
483 else
484 sv->eweight = BE_WEIGHT_SCALE;
485 sv->eweight *= sv->uweight;
486 } else {
487 sv->eweight = sv->uweight;
488 }
489
490 /* static LB algorithms are a bit harder to update */
491 if (px->lbprm.update_server_eweight)
492 px->lbprm.update_server_eweight(sv);
493 else if (sv->eweight)
494 px->lbprm.set_server_status_up(sv);
495 else
496 px->lbprm.set_server_status_down(sv);
497
498 return 1;
499 }
500 else { /* not "set weight" */
501 return 0;
502 }
503 }
504 else { /* not "show" nor "clear" nor "get" nor "set" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200505 return 0;
506 }
507 return 1;
508}
509
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200510/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200511 * used to processes I/O from/to the stats unix socket. The system relies on a
512 * state machine handling requests and various responses. We read a request,
513 * then we process it and send the response, and we possibly display a prompt.
514 * Then we can read again. The state is stored in si->st0 and is one of the
515 * STAT_CLI_* constants. si->st1 is used to indicate whether prompt is enabled
516 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200517 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200518void stats_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200519{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200520 struct session *s = si->private;
521 struct buffer *req = si->ob;
522 struct buffer *res = si->ib;
523 int reql;
524 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200525
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200526 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
527 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200528
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200529 while (1) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200530 if (si->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200531 /* Stats output not initialized yet */
532 memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats));
533 s->data_source = DATA_SRC_STATS;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200534 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200535 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200536 else if (si->st0 == STAT_CLI_END) {
537 /* Let's close for real now. We just close the request
538 * side, the conditions below will complete if needed.
539 */
540 si->shutw(si);
541 break;
542 }
543 else if (si->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +0200544 /* ensure we have some output room left in the event we
545 * would want to return some info right after parsing.
546 */
547 if (buffer_almost_full(si->ib))
548 break;
549
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200550 reql = buffer_si_peekline(si->ob, trash, sizeof(trash));
551 if (reql <= 0) { /* closed or EOL not found */
552 if (reql == 0)
553 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200554 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200555 continue;
556 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200557
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200558 /* seek for a possible semi-colon. If we find one, we
559 * replace it with an LF and skip only this part.
560 */
561 for (len = 0; len < reql; len++)
562 if (trash[len] == ';') {
563 trash[len] = '\n';
564 reql = len + 1;
565 break;
566 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200567
Willy Tarreau816fc222009-10-04 07:36:58 +0200568 /* now it is time to check that we have a full line,
569 * remove the trailing \n and possibly \r, then cut the
570 * line.
571 */
572 len = reql - 1;
573 if (trash[len] != '\n') {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200574 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200575 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200576 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200577
Willy Tarreau816fc222009-10-04 07:36:58 +0200578 if (len && trash[len-1] == '\r')
579 len--;
580
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200581 trash[len] = '\0';
582
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200583 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200584 if (len) {
585 if (strcmp(trash, "quit") == 0) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200586 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200587 continue;
588 }
589 else if (strcmp(trash, "prompt") == 0)
590 si->st1 = !si->st1;
591 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200592 !stats_sock_parse_request(si, trash)) {
593 s->data_ctx.cli.msg = stats_sock_usage_msg;
594 si->st0 = STAT_CLI_PRINT;
595 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200596 /* NB: stats_sock_parse_request() may have put
597 * another STAT_CLI_O_* into si->st0.
598 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200599 }
600 else if (!si->st1) {
601 /* if prompt is disabled, print help on empty lines,
602 * so that the user at least knows how to enable
603 * prompt and find help.
604 */
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200605 s->data_ctx.cli.msg = stats_sock_usage_msg;
606 si->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200607 }
608
609 /* re-adjust req buffer */
610 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200611 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200612 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200613 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200614 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200615 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200616 continue;
617 }
618
619 switch (si->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200620 case STAT_CLI_PRINT:
621 if (buffer_feed(si->ib, s->data_ctx.cli.msg) < 0)
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200622 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200623 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200624 case STAT_CLI_O_INFO:
Willy Tarreau24955a12009-10-04 11:54:04 +0200625 if (stats_dump_raw_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200626 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200627 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200628 case STAT_CLI_O_SESS:
Willy Tarreau7e72a8f2009-10-03 23:55:14 +0200629 if (stats_dump_sess_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200630 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200631 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200632 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau61b34732009-10-03 23:49:35 +0200633 if (stats_dump_errors_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200634 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200635 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200636 default: /* abnormal state */
637 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200638 break;
639 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200640
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200641 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
642 if (si->st0 == STAT_CLI_PROMPT) {
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200643 if (buffer_feed(si->ib, si->st1 ? "\n> " : "\n") < 0)
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200644 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200645 }
646
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200647 /* If the output functions are still there, it means they require more room. */
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200648 if (si->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200649 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200650
651 /* Now we close the output if one of the writers did so,
652 * or if we're not in interactive mode and the request
653 * buffer is empty. This still allows pipelined requests
654 * to be sent in non-interactive mode.
655 */
656 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->st1 && !req->send_max)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200657 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200658 continue;
659 }
660
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200661 /* switch state back to GETREQ to read next requests */
662 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200663 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200664 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200665
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200666 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200667 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
668 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
669 /* Other size has closed, let's abort if we have no more processing to do
670 * and nothing more to consume. This is comparable to a broken pipe, so
671 * we forward the close to the request side so that it flows upstream to
672 * the client.
673 */
674 si->shutw(si);
675 }
676
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200677 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200678 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
679 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
680 /* We have no more processing to do, and nothing more to send, and
681 * the client side has closed. So we'll forward this state downstream
682 * on the response buffer.
683 */
684 si->shutr(si);
685 res->flags |= BF_READ_NULL;
686 }
687
688 /* update all other flags and resync with the other side */
689 si->update(si);
690
691 /* we don't want to expire timeouts while we're processing requests */
692 si->ib->rex = TICK_ETERNITY;
693 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200694
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200695 out:
696 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
697 __FUNCTION__, __LINE__,
698 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
699
700 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
701 /* check that we have released everything then unregister */
702 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200703 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200704}
705
Willy Tarreau24955a12009-10-04 11:54:04 +0200706/* This function is called to send output to the response buffer.
707 * It dumps statistics onto the output buffer <rep> owned by session <s>.
708 * s->data_ctx must have been zeroed first, and the flags properly set.
709 * It returns 0 as long as it does not complete, non-zero upon completion.
710 * Some states are not used but it makes the code more similar to other
711 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200712 */
Willy Tarreau24955a12009-10-04 11:54:04 +0200713int stats_dump_raw_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3e76e722007-10-17 18:57:38 +0200714{
Willy Tarreau3e76e722007-10-17 18:57:38 +0200715 struct proxy *px;
716 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +0100717 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200718
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200719 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +0200720
721 switch (s->data_state) {
722 case DATA_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +0200723 /* the function had not been called yet */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200724 s->data_state = DATA_ST_HEAD;
725 /* fall through */
726
727 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100728 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200729 print_csv_header(&msg);
Willy Tarreau24955a12009-10-04 11:54:04 +0200730 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100731 return 0;
732 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200733
734 s->data_state = DATA_ST_INFO;
735 /* fall through */
736
737 case DATA_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +0100738 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100739 if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200740 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +0100741 "Name: " PRODUCT_NAME "\n"
742 "Version: " HAPROXY_VERSION "\n"
743 "Release_date: " HAPROXY_DATE "\n"
744 "Nbproc: %d\n"
745 "Process_num: %d\n"
746 "Pid: %d\n"
747 "Uptime: %dd %dh%02dm%02ds\n"
748 "Uptime_sec: %d\n"
749 "Memmax_MB: %d\n"
750 "Ulimit-n: %d\n"
751 "Maxsock: %d\n"
752 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100753 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100754 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100755 "PipesUsed: %d\n"
756 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100757 "Tasks: %d\n"
758 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200759 "node: %s\n"
760 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100761 "",
762 global.nbproc,
763 relative_pid,
764 pid,
765 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
766 up,
767 global.rlimit_memmax,
768 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100769 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100770 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200771 nb_tasks_cur, run_queue_cur,
772 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +0100773 );
Willy Tarreau24955a12009-10-04 11:54:04 +0200774 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100775 return 0;
776 }
777
Willy Tarreau3e76e722007-10-17 18:57:38 +0200778 s->data_ctx.stats.px = proxy;
779 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100780
781 s->data_ctx.stats.sv = NULL;
782 s->data_ctx.stats.sv_st = 0;
783
Willy Tarreau3e76e722007-10-17 18:57:38 +0200784 s->data_state = DATA_ST_LIST;
785 /* fall through */
786
787 case DATA_ST_LIST:
788 /* dump proxies */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100789 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100790 while (s->data_ctx.stats.px) {
791 px = s->data_ctx.stats.px;
792 /* skip the disabled proxies and non-networked ones */
793 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +0200794 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100795 if (stats_dump_proxy(s, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100796 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +0200797 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200798
Willy Tarreaua8efd362008-01-03 10:19:15 +0100799 s->data_ctx.stats.px = px->next;
800 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
801 }
802 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200803 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200804
805 s->data_state = DATA_ST_END;
806 /* fall through */
807
808 case DATA_ST_END:
809 s->data_state = DATA_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +0100810 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200811
812 case DATA_ST_FIN:
813 return 1;
814
815 default:
816 /* unknown state ! */
Willy Tarreau24955a12009-10-04 11:54:04 +0200817 s->data_state = DATA_ST_FIN;
818 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200819 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100820}
821
822
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200823/* This I/O handler runs as an applet embedded in a stream interface. It is
824 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
825 * si->st0 becomes non-zero once the transfer is finished. The handler
826 * automatically unregisters itself once transfer is complete.
827 */
828void http_stats_io_handler(struct stream_interface *si)
829{
830 struct session *s = si->private;
831 struct buffer *req = si->ob;
832 struct buffer *res = si->ib;
833
834 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
835 goto out;
836
837 /* check that the output is not closed */
838 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
839 si->st0 = 1;
840
841 if (!si->st0) {
842 if (stats_dump_http(s, res, s->be->uri_auth)) {
843 si->st0 = 1;
844 si->shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200845 }
846 }
847
848 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
849 si->shutw(si);
850
851 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->st0) {
852 si->shutr(si);
853 res->flags |= BF_READ_NULL;
854 }
855
856 /* update all other flags and resync with the other side */
857 si->update(si);
858
859 /* we don't want to expire timeouts while we're processing requests */
860 si->ib->rex = TICK_ETERNITY;
861 si->ob->wex = TICK_ETERNITY;
862
863 out:
864 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
865 /* check that we have released everything then unregister */
866 stream_int_unregister_handler(si);
867 }
868}
869
870
Willy Tarreau3e76e722007-10-17 18:57:38 +0200871/*
872 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200873 * client socket shut down on input. It stops by itself by unsetting the
Willy Tarreau72b179a2008-08-28 16:01:32 +0200874 * BF_HIJACK flag from the buffer, which it uses to keep on being called
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200875 * when there is free space in the buffer, of simply by letting an empty buffer
876 * upon return.s->data_ctx must have been zeroed before the first call, and the
877 * flags set. It returns 0 if it had to stop writing data and an I/O is needed,
878 * 1 if the dump is finished and the session must be closed, or -1 in case of
879 * any error.
Willy Tarreau91861262007-10-17 17:06:05 +0200880 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100881int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200882{
Willy Tarreau91861262007-10-17 17:06:05 +0200883 struct proxy *px;
884 struct chunk msg;
885 unsigned int up;
886
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200887 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +0200888
889 switch (s->data_state) {
890 case DATA_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200891 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200892 "HTTP/1.0 200 OK\r\n"
893 "Cache-Control: no-cache\r\n"
894 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200895 "Content-Type: %s\r\n",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100896 (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +0200897
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100898 if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200899 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +0200900 uri->refresh);
901
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200902 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +0200903
904 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200905 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +0200906 return 0;
907
Willy Tarreau91861262007-10-17 17:06:05 +0200908 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
909 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
910 if (!(s->flags & SN_FINST_MASK))
911 s->flags |= SN_FINST_R;
912
913 if (s->txn.meth == HTTP_METH_HEAD) {
914 /* that's all we return in case of HEAD request */
915 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +0200916 return 1;
917 }
918
919 s->data_state = DATA_ST_HEAD; /* let's start producing data */
920 /* fall through */
921
922 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100923 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +0100924 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200925 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +0200926 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
927 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200928 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200929 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
930 "<style type=\"text/css\"><!--\n"
931 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +0200932 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +0200933 " font-size: 12px;"
934 " font-weight: normal;"
935 " color: black;"
936 " background: white;"
937 "}\n"
938 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +0200939 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +0200940 "}\n"
941 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +0200942 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +0200943 " margin-bottom: 0.5em;"
944 "}\n"
945 "h2 {"
946 " font-family: helvetica, arial;"
947 " font-size: x-large;"
948 " font-weight: bold;"
949 " font-style: italic;"
950 " color: #6020a0;"
951 " margin-top: 0em;"
952 " margin-bottom: 0em;"
953 "}\n"
954 "h3 {"
955 " font-family: helvetica, arial;"
956 " font-size: 16px;"
957 " font-weight: bold;"
958 " color: #b00040;"
959 " background: #e8e8d0;"
960 " margin-top: 0em;"
961 " margin-bottom: 0em;"
962 "}\n"
963 "li {"
964 " margin-top: 0.25em;"
965 " margin-right: 2em;"
966 "}\n"
967 ".hr {margin-top: 0.25em;"
968 " border-color: black;"
969 " border-bottom-style: solid;"
970 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +0200971 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200972 ".total {background: #20D0D0;color: #ffff80;}\n"
973 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200974 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200975 ".backend {background: #e8e8d0;}\n"
976 ".active0 {background: #ff9090;}\n"
977 ".active1 {background: #ffd020;}\n"
978 ".active2 {background: #ffffa0;}\n"
979 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100980 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
981 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
982 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200983 ".backup0 {background: #ff9090;}\n"
984 ".backup1 {background: #ff80ff;}\n"
985 ".backup2 {background: #c060ff;}\n"
986 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100987 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
988 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
989 ".backup6 {background: #e0e0e0;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +0200990 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +0200991 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +0200992 "a.px:link {color: #ffff40; text-decoration: none;}"
993 "a.px:visited {color: #ffff40; text-decoration: none;}"
994 "a.px:hover {color: #ffffff; text-decoration: none;}"
995 "a.lfsb:link {color: #000000; text-decoration: none;}"
996 "a.lfsb:visited {color: #000000; text-decoration: none;}"
997 "a.lfsb:hover {color: #505050; text-decoration: none;}"
998 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200999 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001000 "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"
1001 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001002 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001003 "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 +02001004 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001005 "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 +02001006 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001007 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1008 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1009 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
1010 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001011 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001012 (uri->flags&ST_SHNODE) ? " on " : "",
1013 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001014 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001015 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001016 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001017 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001018 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001019 return 0;
1020
1021 s->data_state = DATA_ST_INFO;
1022 /* fall through */
1023
1024 case DATA_ST_INFO:
1025 up = (now.tv_sec - start_date.tv_sec);
1026
1027 /* WARNING! this has to fit the first packet too.
1028 * We are around 3.5 kB, add adding entries will
1029 * become tricky if we want to support 4kB buffers !
1030 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001031 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001032 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001033 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1034 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001035 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001036 "<hr width=\"100%%\" class=\"hr\">\n"
1037 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001038 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001039 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001040 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001041 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1042 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
1043 "current conns = %d; current pipes = %d/%d<br>\n"
1044 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001045 "</td><td align=\"center\" nowrap>\n"
1046 "<table class=\"lgd\"><tr>\n"
1047 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1048 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1049 "</tr><tr>\n"
1050 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1051 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1052 "</tr><tr>\n"
1053 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1054 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1055 "</tr><tr>\n"
1056 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001057 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001058 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001059 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001060 "</td>"
1061 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1062 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1063 "",
1064 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001065 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1066 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1067 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001068 up / 86400, (up % 86400) / 3600,
1069 (up % 3600) / 60, (up % 60),
1070 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1071 global.rlimit_memmax ? " MB" : "",
1072 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001073 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001074 actconn, pipes_used, pipes_used+pipes_free,
1075 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +02001076 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001077
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001078 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001079 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001080 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1081 uri->uri_prefix,
1082 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001083 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001084 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001085 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001086 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1087 uri->uri_prefix,
1088 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001089 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001090
Willy Tarreau55bb8452007-10-17 18:44:57 +02001091 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001092 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001093 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001094 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1095 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001096 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001097 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001098 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001099 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001100 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1101 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001102 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001103 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001104 }
Willy Tarreau91861262007-10-17 17:06:05 +02001105
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001106 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001107 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
1108 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001109 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
1110 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001111
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001112 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02001113 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
1114 uri->uri_prefix,
1115 (uri->refresh > 0) ? ";norefresh" : "");
1116
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001117 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001118 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001119 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1120 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
1121 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
1122 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
1123 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
1124 "</ul>"
1125 "</td>"
1126 "</tr></table>\n"
1127 ""
1128 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001129
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001130 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001131 return 0;
1132 }
Willy Tarreau91861262007-10-17 17:06:05 +02001133
Willy Tarreau91861262007-10-17 17:06:05 +02001134 s->data_ctx.stats.px = proxy;
1135 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1136 s->data_state = DATA_ST_LIST;
1137 /* fall through */
1138
1139 case DATA_ST_LIST:
1140 /* dump proxies */
1141 while (s->data_ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001142 if (buffer_almost_full(rep))
1143 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02001144 px = s->data_ctx.stats.px;
1145 /* skip the disabled proxies and non-networked ones */
1146 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001147 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001148 return 0;
1149
1150 s->data_ctx.stats.px = px->next;
1151 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1152 }
1153 /* here, we just have reached the last proxy */
1154
1155 s->data_state = DATA_ST_END;
1156 /* fall through */
1157
1158 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001159 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001160 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001161 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001162 return 0;
1163 }
Willy Tarreau91861262007-10-17 17:06:05 +02001164
1165 s->data_state = DATA_ST_FIN;
1166 /* fall through */
1167
1168 case DATA_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02001169 return 1;
1170
1171 default:
1172 /* unknown state ! */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001173 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001174 return -1;
1175 }
1176}
1177
1178
1179/*
1180 * Dumps statistics for a proxy.
1181 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1182 * ot non-zero if everything completed.
1183 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001184int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001185{
1186 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001187 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001188 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02001189 struct chunk msg;
1190
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001191 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001192
1193 switch (s->data_ctx.stats.px_st) {
1194 case DATA_ST_PX_INIT:
1195 /* we are on a new proxy */
1196
1197 if (uri && uri->scope) {
1198 /* we have a limited scope, we have to check the proxy name */
1199 struct stat_scope *scope;
1200 int len;
1201
1202 len = strlen(px->id);
1203 scope = uri->scope;
1204
1205 while (scope) {
1206 /* match exact proxy name */
1207 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1208 break;
1209
1210 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001211 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001212 break;
1213 scope = scope->next;
1214 }
1215
1216 /* proxy name not found : don't dump anything */
1217 if (scope == NULL)
1218 return 1;
1219 }
1220
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001221 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001222 (px->uuid != s->data_ctx.stats.iid))
1223 return 1;
1224
Willy Tarreau91861262007-10-17 17:06:05 +02001225 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
1226 /* fall through */
1227
1228 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001229 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001230 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001231 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001232 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001233 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001234 "<th class=\"pxname\" width=\"10%%\"");
1235
1236 if (uri->flags&ST_SHLGNDS) {
1237 /* cap, mode, id */
1238 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
1239 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1240 px->uuid);
1241
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001242 chunk_printf(&msg, "\"");
1243 }
1244
1245 chunk_printf(&msg,
1246 "><a name=\"%s\"></a>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001247 "<a class=px href=\"#%s\">%s</a></th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001248 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001249 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001250 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001251 "<table class=\"tbl\" width=\"100%%\">\n"
1252 "<tr class=\"titre\">"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001253 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001254 "<th colspan=3>Queue</th>"
1255 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001256 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001257 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001258 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001259 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001260 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001261 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001262 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001263 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001264 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001265 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001266 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001267 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1268 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001269 "</tr>",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001270 px->id, px->id, px->id,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001271 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001272
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001273 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001274 return 0;
1275 }
Willy Tarreau91861262007-10-17 17:06:05 +02001276
1277 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
1278 /* fall through */
1279
1280 case DATA_ST_PX_FE:
1281 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001282 if ((px->cap & PR_CAP_FE) &&
1283 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
1284 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001285 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001286 /* name, queue */
Krzysztof Piotr Oledzki516ed492009-10-22 22:48:09 +02001287 "<tr class=\"frontend\"><td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001288 "<a name=\"%s/Frontend\"></a>"
1289 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td><td colspan=3></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001290 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001291 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001292 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001293 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001294 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001295 "",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001296 px->id, px->id,
Willy Tarreaua3e49422009-05-10 19:19:41 +02001297 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001298 U2H1(px->counters.fe_sps_max), LIM2A2(px->fe_sps_lim, "-"),
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001299 U2H3(px->feconn), U2H4(px->counters.feconn_max), U2H5(px->maxconn));
1300
1301 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1302 if (px->mode == PR_MODE_HTTP) {
1303 int i;
1304
1305 chunk_printf(&msg, " title=\"rsp codes:");
1306
1307 for (i = 1; i < 6; i++)
1308 chunk_printf(&msg, " %dxx=%lld,", i, px->counters.p.http.rsp[i]);
1309
1310 chunk_printf(&msg, " other=%lld\"", px->counters.p.http.rsp[0]);
1311 }
1312
1313 chunk_printf(&msg,
1314 /* sessions: total, lbtot */
1315 ">%s</td><td></td>"
1316 /* bytes : in, out */
1317 "<td>%s</td><td>%s</td>"
1318 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001319 U2H6(px->counters.cum_feconn), U2H7(px->counters.bytes_in), U2H8(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001320
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001321 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001322 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001323 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001324 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001325 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001326 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001327 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001328 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001329 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001330 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001331 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02001332 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001333 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
1334 U2H2(px->counters.failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +02001335 px->state == PR_STRUN ? "OPEN" :
1336 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001337 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001338 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001339 /* pxid, name, queue cur, queue max, */
1340 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001341 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001342 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001343 /* bytes : in, out */
1344 "%lld,%lld,"
1345 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001346 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001347 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001348 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001349 /* warnings: retries, redispatches */
1350 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001351 /* server status : reflect frontend status */
1352 "%s,"
1353 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001354 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001355 /* pid, iid, sid, throttle, lbtot, tracked, type */
1356 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001357 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001358 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001359 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001360 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02001361 px->id,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001362 px->feconn, px->counters.feconn_max, px->maxconn, px->counters.cum_feconn,
1363 px->counters.bytes_in, px->counters.bytes_out,
1364 px->counters.denied_req, px->counters.denied_resp,
1365 px->counters.failed_req,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001366 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +01001367 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01001368 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001369 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001370 px->fe_sps_lim, px->counters.fe_sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001371
1372 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1373 if (px->mode == PR_MODE_HTTP) {
1374 int i;
1375
1376 for (i=1; i<6; i++)
1377 chunk_printf(&msg, "%lld,", px->counters.p.http.rsp[i]);
1378
1379 chunk_printf(&msg, "%lld,", px->counters.p.http.rsp[0]);
1380 } else {
1381 chunk_printf(&msg, ",,,,,,");
1382 }
1383
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001384 /* failed health analyses */
1385 chunk_printf(&msg, ",");
1386
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001387 /* finish with EOL */
1388 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001389 }
Willy Tarreau91861262007-10-17 17:06:05 +02001390
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001391 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001392 return 0;
1393 }
1394
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001395 s->data_ctx.stats.l = px->listen; /* may be NULL */
1396 s->data_ctx.stats.px_st = DATA_ST_PX_LI;
1397 /* fall through */
1398
1399 case DATA_ST_PX_LI:
1400 /* stats.l has been initialized above */
1401 for (; s->data_ctx.stats.l != NULL; s->data_ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001402 if (buffer_almost_full(rep))
1403 return 0;
1404
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001405 l = s->data_ctx.stats.l;
1406 if (!l->counters)
1407 continue;
1408
1409 if (s->data_ctx.stats.flags & STAT_BOUND) {
1410 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SO)))
1411 break;
1412
1413 if (s->data_ctx.stats.sid != -1 && l->luid != s->data_ctx.stats.sid)
1414 continue;
1415 }
1416
1417 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001418 chunk_printf(&msg, "<tr class=socket><td class=ac");
1419
1420 if (uri->flags&ST_SHLGNDS) {
1421 char str[INET6_ADDRSTRLEN], *fmt = NULL;
1422 int port;
1423
1424 chunk_printf(&msg, " title=\"IP: ");
1425
1426 port = (l->addr.ss_family == AF_INET6)
1427 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
1428 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
1429
1430 if (l->addr.ss_family == AF_INET) {
1431 if (inet_ntop(AF_INET,
1432 (const void *)&((struct sockaddr_in *)&l->addr)->sin_addr,
1433 str, sizeof(str)))
1434 fmt = "%s:%d";
1435 } else {
1436 if (inet_ntop(AF_INET6,
1437 (const void *)&((struct sockaddr_in6 *)(&l->addr))->sin6_addr,
1438 str, sizeof(str)))
1439 fmt = "[%s]:%d";
1440 }
1441
1442 if (fmt)
1443 chunk_printf(&msg, fmt, str, port);
1444 else
1445 chunk_printf(&msg, "(%s)", strerror(errno));
1446
1447 /* id */
1448 chunk_printf(&msg, ", id: %d", l->luid);
1449
1450 chunk_printf(&msg, "\"");
1451 }
1452
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001453 chunk_printf(&msg,
1454 /* name, queue */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001455 "><a name=\"%s/+%s\"></a>"
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01001456 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001457 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001458 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001459 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001460 "<td>%s</td><td>%s</td><td>%s</td>"
1461 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001462 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001463 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001464 "",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01001465 px->id, l->name, px->id, l->name, l->name,
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001466 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
1467 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
1468
1469 chunk_printf(&msg,
1470 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001471 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001472 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001473 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001474 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001475 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001476 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001477 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001478 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001479 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001480 "",
1481 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
1482 U2H2(l->counters->failed_req),
1483 (l->nbconn < l->maxconn) ? "OPEN" : "FULL");
1484 } else {
1485 chunk_printf(&msg,
1486 /* pxid, name, queue cur, queue max, */
1487 "%s,%s,,,"
1488 /* sessions: current, max, limit, total */
1489 "%d,%d,%d,%lld,"
1490 /* bytes: in, out */
1491 "%lld,%lld,"
1492 /* denied: req, resp */
1493 "%lld,%lld,"
1494 /* errors: request, connect, response */
1495 "%lld,,,"
1496 /* warnings: retries, redispatches */
1497 ",,"
1498 /* server status: reflect listener status */
1499 "%s,"
1500 /* rest of server: nothing */
1501 ",,,,,,,,"
1502 /* pid, iid, sid, throttle, lbtot, tracked, type */
1503 "%d,%d,%d,,,,%d,"
1504 /* rate, rate_lim, rate_max */
1505 ",,,"
1506 /* check_status, check_code, check_duration */
1507 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001508 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1509 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001510 /* failed health analyses */
1511 ","
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001512 "\n",
1513 px->id, l->name,
1514 l->nbconn, l->counters->conn_max,
1515 l->maxconn, l->counters->cum_conn,
1516 l->counters->bytes_in, l->counters->bytes_out,
1517 l->counters->denied_req, l->counters->denied_resp,
1518 l->counters->failed_req,
1519 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
1520 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
1521 }
1522
1523 if (buffer_feed_chunk(rep, &msg) >= 0)
1524 return 0;
1525 }
1526
Willy Tarreau91861262007-10-17 17:06:05 +02001527 s->data_ctx.stats.sv = px->srv; /* may be NULL */
1528 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
1529 /* fall through */
1530
1531 case DATA_ST_PX_SV:
1532 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001533 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001534 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 +02001535
Willy Tarreau4e33d862009-10-11 23:35:10 +02001536 if (buffer_almost_full(rep))
1537 return 0;
1538
Willy Tarreau91861262007-10-17 17:06:05 +02001539 sv = s->data_ctx.stats.sv;
1540
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001541 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001542 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
1543 break;
1544
1545 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
1546 continue;
1547 }
1548
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001549 if (sv->tracked)
1550 svs = sv->tracked;
1551 else
1552 svs = sv;
1553
Willy Tarreau91861262007-10-17 17:06:05 +02001554 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001555 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01001556 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001557 else if (svs->state & SRV_RUNNING) {
1558 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02001559 sv_state = 3; /* UP */
1560 else
1561 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01001562
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001563 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01001564 sv_state += 2;
1565 }
Willy Tarreau91861262007-10-17 17:06:05 +02001566 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001567 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02001568 sv_state = 1; /* going up */
1569 else
1570 sv_state = 0; /* DOWN */
1571
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001572 if ((sv_state == 0) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02001573 /* do not report servers which are DOWN */
1574 s->data_ctx.stats.sv = sv->next;
1575 continue;
1576 }
1577
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001578 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001579 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
1580 "UP %d/%d &darr;", "UP",
1581 "NOLB %d/%d &darr;", "NOLB",
1582 "<i>no check</i>" };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001583 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001584 /* name */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001585 "<tr class=\"%s%d\"><td class=ac",
1586 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
1587
1588 if (uri->flags&ST_SHLGNDS) {
1589 char str[INET6_ADDRSTRLEN];
1590
1591 chunk_printf(&msg, " title=\"IP: ");
1592
1593 /* IP */
1594 if (inet_ntop(sv->addr.sin_family, &sv->addr.sin_addr, str, sizeof(str)))
1595 chunk_printf(&msg, "%s:%d", str, htons(sv->addr.sin_port));
1596 else
1597 chunk_printf(&msg, "(%s)", strerror(errno));
1598
1599 /* id */
1600 chunk_printf(&msg, ", id: %d", sv->puid);
1601
1602 /* cookie */
1603 if (sv->cookie) {
1604 struct chunk src;
1605
1606 chunk_printf(&msg, ", cookie: '");
1607
1608 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
1609 chunk_htmlencode(&msg, &src);
1610
1611 chunk_printf(&msg, "'");
1612 }
1613
1614 chunk_printf(&msg, "\"");
1615 }
1616
1617 chunk_printf(&msg,
1618 "><a name=\"%s/%s\"></a>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001619 "<a class=lfsb href=\"#%s/%s\">%s</a></td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001620 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001621 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001622 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001623 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001624 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001625 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001626 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001627 "",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001628 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001629 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
1630 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001631 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
1632
1633 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1634 if (px->mode == PR_MODE_HTTP) {
1635 int i;
1636
1637 chunk_printf(&msg, " title=\"rsp codes:");
1638
1639 for (i = 1; i < 6; i++)
1640 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
1641
1642 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
1643 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001644
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001645 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001646 /* sessions: total, lbtot */
1647 ">%s</td><td>%s</td>",
1648 U2H0(sv->counters.cum_sess), U2H1(sv->counters.cum_lbconn));
1649
1650 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001651 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001652 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001653 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001654 "<td></td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001655 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001656 "<td></td><td>%s</td><td>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001657 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001658 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001659 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001660 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
1661 U2H2(sv->counters.failed_secu),
1662 U2H3(sv->counters.failed_conns), U2H4(sv->counters.failed_resp),
1663 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001664
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001665 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001666 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001667
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001668 if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001669 chunk_printf(&msg, "%s ",
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001670 human_time(now.tv_sec - sv->last_change, 1));
1671
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001672 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001673 srv_hlt_st[sv_state],
1674 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
1675 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001676 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001677
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001678 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001679 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001680 get_check_status_description(sv->check_status));
1681
1682 if (*sv->check_desc) {
1683 struct chunk src;
1684
1685 chunk_printf(&msg, ": ");
1686
1687 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
1688 chunk_htmlencode(&msg, &src);
1689 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001690
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001691 chunk_printf(&msg, "\"> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001692 tv_iszero(&sv->check_start)?"":"* ",
1693 get_check_status_info(sv->check_status));
1694
1695 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001696 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001697
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001698 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001699 chunk_printf(&msg, " in %lums", sv->check_duration);
1700 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001701 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02001702
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001703 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001704 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001705 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001706 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001707 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001708 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001709 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02001710 (sv->state & SRV_BACKUP) ? "-" : "Y",
1711 (sv->state & SRV_BACKUP) ? "Y" : "-");
1712
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001713 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01001714 if (sv->state & SRV_CHECKED) {
1715 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\">%lld",
1716 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
1717
1718 if (svs->observe)
1719 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
1720
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001721 chunk_printf(&msg,
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01001722 "</td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001723 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001724 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001725 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01001726 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001727 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01001728 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
1729 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001730 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001731 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001732 "<td colspan=3></td>");
1733
1734 /* throttle */
1735 if ((sv->state & SRV_WARMINGUP) &&
1736 now.tv_sec < sv->last_change + sv->slowstart &&
1737 now.tv_sec >= sv->last_change) {
1738 unsigned int ratio;
1739 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001740 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001741 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001742 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001743 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001744 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001745 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02001746 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001747 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
1748 "UP %d/%d,", "UP,",
1749 "NOLB %d/%d,", "NOLB,",
1750 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001751 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001752 /* pxid, name */
1753 "%s,%s,"
1754 /* queue : current, max */
1755 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001756 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001757 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001758 /* bytes : in, out */
1759 "%lld,%lld,"
1760 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001761 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001762 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001763 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001764 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001765 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001766 "",
1767 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001768 sv->nbpend, sv->counters.nbpend_max,
1769 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001770 sv->counters.bytes_in, sv->counters.bytes_out,
1771 sv->counters.failed_secu,
1772 sv->counters.failed_conns, sv->counters.failed_resp,
1773 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001774
Willy Tarreau55bb8452007-10-17 18:44:57 +02001775 /* status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001776 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001777 srv_hlt_st[sv_state],
1778 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
1779 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
Willy Tarreau91861262007-10-17 17:06:05 +02001780
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001781 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001782 /* weight, active, backup */
1783 "%d,%d,%d,"
1784 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001785 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001786 (sv->state & SRV_BACKUP) ? 0 : 1,
1787 (sv->state & SRV_BACKUP) ? 1 : 0);
1788
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001789 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001790 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001791 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02001792 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001793 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001794 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02001795 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001796 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001797 ",,,,");
1798
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001799 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001800 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01001801 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001802 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01001803 LIM2A0(sv->maxqueue, ""),
1804 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001805
1806 /* throttle */
1807 if ((sv->state & SRV_WARMINGUP) &&
1808 now.tv_sec < sv->last_change + sv->slowstart &&
1809 now.tv_sec >= sv->last_change) {
1810 unsigned int ratio;
1811 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001812 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001813 }
1814
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001815 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001816 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001817
1818 /* tracked */
1819 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001820 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001821 sv->tracked->proxy->id, sv->tracked->id);
1822 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001823 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001824
Willy Tarreau7f062c42009-03-05 18:43:00 +01001825 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001826 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01001827
1828 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001829 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001830 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001831 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01001832
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001833 if (sv->state & SRV_CHECKED) {
1834 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001835 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001836
1837 /* check_code */
1838 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001839 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001840 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001841 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001842
1843 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001844 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001845 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001846 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001847 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001848
1849 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001850 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001851 }
1852
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001853 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1854 if (px->mode == PR_MODE_HTTP) {
1855 int i;
1856
1857 for (i=1; i<6; i++)
1858 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
1859
1860 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
1861 } else {
1862 chunk_printf(&msg, ",,,,,,");
1863 }
1864
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001865 /* failed health analyses */
1866 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
1867
Willy Tarreau7f062c42009-03-05 18:43:00 +01001868 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001869 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001870 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001871 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001872 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001873 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02001874
1875 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
1876 /* fall through */
1877
1878 case DATA_ST_PX_BE:
1879 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001880 if ((px->cap & PR_CAP_BE) &&
1881 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
1882 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001883 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001884 /* name */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001885 "<tr class=\"backend\"><td class=ac");
1886
1887 if (uri->flags&ST_SHLGNDS) {
1888 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01001889 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001890 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01001891
1892 /* cookie */
1893 if (px->cookie_name) {
1894 struct chunk src;
1895
1896 chunk_printf(&msg, ", cookie: '");
1897
1898 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
1899 chunk_htmlencode(&msg, &src);
1900
1901 chunk_printf(&msg, "'");
1902 }
1903
1904 chunk_printf(&msg, "\"");
1905
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001906 }
1907
1908 chunk_printf(&msg,
1909 "><a name=\"%s/Backend\"></a>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001910 "<a class=lfsb href=\"#%s/Backend\">Backend</a></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001911 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001912 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001913 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001914 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001915 "",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001916 px->id, px->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001917 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->counters.nbpend_max),
1918 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->counters.be_sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02001919
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001920 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001921 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001922 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001923 "<td"
1924 "",
1925 U2H2(px->beconn), U2H3(px->counters.beconn_max), U2H4(px->fullconn));
1926
1927 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1928 if (px->mode == PR_MODE_HTTP) {
1929 int i;
1930
1931 chunk_printf(&msg, " title=\"rsp codes:");
1932
1933 for (i = 1; i < 6; i++)
1934 chunk_printf(&msg, " %dxx=%lld", i, px->counters.p.http.rsp[i]);
1935
1936 chunk_printf(&msg, " other=%lld\"", px->counters.p.http.rsp[0]);
1937 }
1938
1939 chunk_printf(&msg,
1940 /* sessions: total, lbtot */
1941 ">%s</td><td>%s</td>"
1942 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001943 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001944 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001945 U2H6(px->counters.cum_beconn), U2H7(px->counters.cum_lbconn),
1946 U2H8(px->counters.bytes_in), U2H9(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001947
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001948 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001949 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001950 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001951 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001952 "<td></td><td>%s</td><td>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001953 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001954 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001955 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02001956 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001957 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02001958 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001959 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1960 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001961 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001962 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
1963 U2H2(px->counters.failed_conns), U2H3(px->counters.failed_resp),
1964 px->counters.retries, px->counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001965 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01001966 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
1967 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01001968 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001969 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001970
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001971 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001972 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001973 "<td class=ac>&nbsp;</td><td>%d</td>"
1974 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001975 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001976 "</tr>",
1977 px->down_trans,
1978 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001979 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001980 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001981 /* pxid, name */
1982 "%s,BACKEND,"
1983 /* queue : current, max */
1984 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001985 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001986 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001987 /* bytes : in, out */
1988 "%lld,%lld,"
1989 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001990 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001991 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001992 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001993 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001994 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001995 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02001996 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001997 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02001998 * active and backups. */
1999 "%s,"
2000 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002001 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002002 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002003 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002004 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002005 /* rate, rate_lim, rate_max, */
2006 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002007 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002008 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002009 px->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002010 px->nbpend /* or px->totpend ? */, px->counters.nbpend_max,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002011 px->beconn, px->counters.beconn_max, px->fullconn, px->counters.cum_beconn,
2012 px->counters.bytes_in, px->counters.bytes_out,
2013 px->counters.denied_req, px->counters.denied_resp,
2014 px->counters.failed_conns, px->counters.failed_resp,
2015 px->counters.retries, px->counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01002016 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01002017 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002018 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002019 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01002020 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002021 relative_pid, px->uuid,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002022 px->counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002023 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002024 px->counters.be_sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002025
2026 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2027 if (px->mode == PR_MODE_HTTP) {
2028 int i;
2029
2030 for (i=1; i<6; i++)
2031 chunk_printf(&msg, "%lld,", px->counters.p.http.rsp[i]);
2032
2033 chunk_printf(&msg, "%lld,", px->counters.p.http.rsp[0]);
2034 } else {
2035 chunk_printf(&msg, ",,,,,,");
2036 }
2037
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002038 /* failed health analyses */
2039 chunk_printf(&msg, ",");
2040
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002041 /* finish with EOL */
2042 chunk_printf(&msg, "\n");
2043
Willy Tarreau55bb8452007-10-17 18:44:57 +02002044 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002045 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002046 return 0;
2047 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002048
Willy Tarreau91861262007-10-17 17:06:05 +02002049 s->data_ctx.stats.px_st = DATA_ST_PX_END;
2050 /* fall through */
2051
2052 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002053 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002054 chunk_printf(&msg, "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02002055
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002056 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002057 return 0;
2058 }
Willy Tarreau91861262007-10-17 17:06:05 +02002059
2060 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
2061 /* fall through */
2062
2063 case DATA_ST_PX_FIN:
2064 return 1;
2065
2066 default:
2067 /* unknown state, we should put an abort() here ! */
2068 return 1;
2069 }
2070}
2071
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002072
2073/* This function is called to send output to the response buffer.
2074 * It dumps the sessions states onto the output buffer <rep>.
2075 * Expects to be called with client socket shut down on input.
2076 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002077 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002078 */
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002079int stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002080{
2081 struct chunk msg;
2082
2083 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
2084 /* If we're forced to shut down, we might have to remove our
2085 * reference to the last session being dumped.
2086 */
2087 if (s->data_state == DATA_ST_LIST) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002088 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002089 LIST_DEL(&s->data_ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002090 LIST_INIT(&s->data_ctx.sess.bref.users);
2091 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002092 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002093 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002094 }
2095
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002096 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002097
2098 switch (s->data_state) {
2099 case DATA_ST_INIT:
2100 /* the function had not been called yet, let's prepare the
2101 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002102 * pointer to the first in the global list. When a target
2103 * session is being destroyed, it is responsible for updating
2104 * this pointer. We know we have reached the end when this
2105 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002106 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002107 LIST_INIT(&s->data_ctx.sess.bref.users);
2108 s->data_ctx.sess.bref.ref = sessions.n;
2109 s->data_state = DATA_ST_LIST;
2110 /* fall through */
2111
2112 case DATA_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002113 /* first, let's detach the back-ref from a possible previous session */
2114 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
2115 LIST_DEL(&s->data_ctx.sess.bref.users);
2116 LIST_INIT(&s->data_ctx.sess.bref.users);
2117 }
2118
2119 /* and start from where we stopped */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002120 while (s->data_ctx.sess.bref.ref != &sessions) {
2121 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
2122 struct session *curr_sess;
2123
2124 curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list);
2125
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002126 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002127 "%p: proto=%s",
2128 curr_sess,
2129 curr_sess->listener->proto->name);
2130
2131 switch (curr_sess->listener->proto->sock_family) {
2132 case AF_INET:
2133 inet_ntop(AF_INET,
2134 (const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr,
2135 pn, sizeof(pn));
2136
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002137 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002138 " src=%s:%d fe=%s be=%s srv=%s",
2139 pn,
2140 ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port),
2141 curr_sess->fe->id,
2142 curr_sess->be->id,
2143 curr_sess->srv ? curr_sess->srv->id : "<none>"
2144 );
2145 break;
2146 case AF_INET6:
2147 inet_ntop(AF_INET6,
2148 (const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr,
2149 pn, sizeof(pn));
2150
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002151 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002152 " src=%s:%d fe=%s be=%s srv=%s",
2153 pn,
2154 ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port),
2155 curr_sess->fe->id,
2156 curr_sess->be->id,
2157 curr_sess->srv ? curr_sess->srv->id : "<none>"
2158 );
2159
2160 break;
2161 case AF_UNIX:
2162 /* no more information to print right now */
2163 break;
2164 }
2165
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002166 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02002167 " ts=%02x age=%s calls=%d",
2168 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01002169 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
2170 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002171
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002172 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002173 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
2174 curr_sess->req->flags,
2175 curr_sess->req->l,
2176 curr_sess->req->analysers,
2177 curr_sess->req->rex ?
2178 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
2179 TICKS_TO_MS(1000)) : "");
2180
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002181 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002182 ",wx=%s",
2183 curr_sess->req->wex ?
2184 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
2185 TICKS_TO_MS(1000)) : "");
2186
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002187 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002188 ",ax=%s]",
2189 curr_sess->req->analyse_exp ?
2190 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
2191 TICKS_TO_MS(1000)) : "");
2192
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002193 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002194 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
2195 curr_sess->rep->flags,
2196 curr_sess->rep->l,
2197 curr_sess->rep->analysers,
2198 curr_sess->rep->rex ?
2199 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
2200 TICKS_TO_MS(1000)) : "");
2201
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002202 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002203 ",wx=%s",
2204 curr_sess->rep->wex ?
2205 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
2206 TICKS_TO_MS(1000)) : "");
2207
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002208 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002209 ",ax=%s]",
2210 curr_sess->rep->analyse_exp ?
2211 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
2212 TICKS_TO_MS(1000)) : "");
2213
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002214 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002215 " s0=[%d,%1xh,fd=%d,ex=%s]",
2216 curr_sess->si[0].state,
2217 curr_sess->si[0].flags,
2218 curr_sess->si[0].fd,
2219 curr_sess->si[0].exp ?
2220 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
2221 TICKS_TO_MS(1000)) : "");
2222
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002223 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002224 " s1=[%d,%1xh,fd=%d,ex=%s]",
2225 curr_sess->si[1].state,
2226 curr_sess->si[1].flags,
2227 curr_sess->si[1].fd,
2228 curr_sess->si[1].exp ?
2229 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
2230 TICKS_TO_MS(1000)) : "");
2231
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002232 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002233 " exp=%s",
2234 curr_sess->task->expire ?
2235 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
2236 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01002237 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002238 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002239
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002240 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002241
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002242 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002243 /* let's try again later from this session. We add ourselves into
2244 * this session's users so that it can remove us upon termination.
2245 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002246 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002247 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002248 }
2249
2250 s->data_ctx.sess.bref.ref = curr_sess->list.n;
2251 }
2252 s->data_state = DATA_ST_FIN;
2253 /* fall through */
2254
2255 default:
2256 s->data_state = DATA_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002257 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002258 }
2259}
2260
Willy Tarreau74808cb2009-03-04 15:53:18 +01002261/* print a line of error buffer (limited to 70 bytes) to <out>. The format is :
2262 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
2263 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
2264 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
2265 * lines are respected within the limit of 70 output chars. Lines that are
2266 * continuation of a previous truncated line begin with "+" instead of " "
2267 * after the offset. The new pointer is returned.
2268 */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002269static int dump_error_line(struct chunk *out, struct error_snapshot *err,
2270 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01002271{
2272 int end;
2273 unsigned char c;
2274
2275 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002276 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01002277 return ptr;
2278
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002279 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01002280
Willy Tarreau61b34732009-10-03 23:49:35 +02002281 while (ptr < err->len && ptr < sizeof(err->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002282 c = err->buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01002283 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002284 if (out->len > end - 2)
2285 break;
2286 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01002287 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002288 if (out->len > end - 3)
2289 break;
2290 out->str[out->len++] = '\\';
2291 switch (c) {
2292 case '\t': c = 't'; break;
2293 case '\n': c = 'n'; break;
2294 case '\r': c = 'r'; break;
2295 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01002296 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002297 }
2298 out->str[out->len++] = c;
2299 } else {
2300 if (out->len > end - 5)
2301 break;
2302 out->str[out->len++] = '\\';
2303 out->str[out->len++] = 'x';
2304 out->str[out->len++] = hextab[(c >> 4) & 0xF];
2305 out->str[out->len++] = hextab[c & 0xF];
2306 }
2307 if (err->buf[ptr++] == '\n') {
2308 /* we had a line break, let's return now */
2309 out->str[out->len++] = '\n';
2310 *line = ptr;
2311 return ptr;
2312 }
2313 }
2314 /* we have an incomplete line, we return it as-is */
2315 out->str[out->len++] = '\n';
2316 return ptr;
2317}
2318
2319/* This function is called to send output to the response buffer.
2320 * It dumps the errors logged in proxies onto the output buffer <rep>.
2321 * Expects to be called with client socket shut down on input.
2322 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau61b34732009-10-03 23:49:35 +02002323 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau74808cb2009-03-04 15:53:18 +01002324 */
Willy Tarreau61b34732009-10-03 23:49:35 +02002325int stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau74808cb2009-03-04 15:53:18 +01002326{
2327 extern const char *monthname[12];
2328 struct chunk msg;
2329
Willy Tarreau61b34732009-10-03 23:49:35 +02002330 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW)))
2331 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002332
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002333 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01002334
2335 if (!s->data_ctx.errors.px) {
2336 /* the function had not been called yet, let's prepare the
2337 * buffer for a response.
2338 */
Willy Tarreau74808cb2009-03-04 15:53:18 +01002339 s->data_ctx.errors.px = proxy;
2340 s->data_ctx.errors.buf = 0;
2341 s->data_ctx.errors.bol = 0;
2342 s->data_ctx.errors.ptr = -1;
2343 }
2344
2345 /* we have two inner loops here, one for the proxy, the other one for
2346 * the buffer.
2347 */
2348 while (s->data_ctx.errors.px) {
2349 struct error_snapshot *es;
2350
2351 if (s->data_ctx.errors.buf == 0)
2352 es = &s->data_ctx.errors.px->invalid_req;
2353 else
2354 es = &s->data_ctx.errors.px->invalid_rep;
2355
2356 if (!es->when.tv_sec)
2357 goto next;
2358
2359 if (s->data_ctx.errors.iid >= 0 &&
2360 s->data_ctx.errors.px->uuid != s->data_ctx.errors.iid &&
2361 es->oe->uuid != s->data_ctx.errors.iid)
2362 goto next;
2363
2364 if (s->data_ctx.errors.ptr < 0) {
2365 /* just print headers now */
2366
2367 char pn[INET6_ADDRSTRLEN];
2368 struct tm tm;
2369
2370 get_localtime(es->when.tv_sec, &tm);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002371 chunk_printf(&msg, "\n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01002372 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002373 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01002374
2375
2376 if (es->src.ss_family == AF_INET)
2377 inet_ntop(AF_INET,
2378 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
2379 pn, sizeof(pn));
2380 else
2381 inet_ntop(AF_INET6,
2382 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
2383 pn, sizeof(pn));
2384
2385 switch (s->data_ctx.errors.buf) {
2386 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002387 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002388 " frontend %s (#%d): invalid request\n"
2389 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
2390 " request length %d bytes, error at position %d:\n\n",
2391 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
2392 pn, es->sid, es->oe->id, es->oe->uuid,
2393 es->srv ? es->srv->id : "<NONE>",
2394 es->srv ? es->srv->puid : -1,
2395 es->len, es->pos);
2396 break;
2397 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002398 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002399 " backend %s (#%d) : invalid response\n"
2400 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
2401 " response length %d bytes, error at position %d:\n\n",
2402 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
2403 pn, es->sid, es->oe->id, es->oe->uuid,
2404 es->srv ? es->srv->id : "<NONE>",
2405 es->srv ? es->srv->puid : -1,
2406 es->len, es->pos);
2407 break;
2408 }
2409
Willy Tarreau61b34732009-10-03 23:49:35 +02002410 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002411 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02002412 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002413 }
2414 s->data_ctx.errors.ptr = 0;
2415 s->data_ctx.errors.sid = es->sid;
2416 }
2417
2418 if (s->data_ctx.errors.sid != es->sid) {
2419 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002420 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002421 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau61b34732009-10-03 23:49:35 +02002422 if (buffer_feed_chunk(rep, &msg) >= 0)
2423 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002424 goto next;
2425 }
2426
2427 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau61b34732009-10-03 23:49:35 +02002428 while (s->data_ctx.errors.ptr < es->len && s->data_ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002429 int newptr;
2430 int newline;
2431
2432 newline = s->data_ctx.errors.bol;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002433 newptr = dump_error_line(&msg, es, &newline, s->data_ctx.errors.ptr);
Willy Tarreau74808cb2009-03-04 15:53:18 +01002434 if (newptr == s->data_ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02002435 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002436
Willy Tarreau61b34732009-10-03 23:49:35 +02002437 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002438 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02002439 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002440 }
2441 s->data_ctx.errors.ptr = newptr;
2442 s->data_ctx.errors.bol = newline;
2443 };
2444 next:
2445 s->data_ctx.errors.bol = 0;
2446 s->data_ctx.errors.ptr = -1;
2447 s->data_ctx.errors.buf++;
2448 if (s->data_ctx.errors.buf > 1) {
2449 s->data_ctx.errors.buf = 0;
2450 s->data_ctx.errors.px = s->data_ctx.errors.px->next;
2451 }
2452 }
2453
2454 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02002455 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002456}
2457
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002458
Willy Tarreau10522fd2008-07-09 20:12:41 +02002459static struct cfg_kw_list cfg_kws = {{ },{
2460 { CFG_GLOBAL, "stats", stats_parse_global },
2461 { 0, NULL, NULL },
2462}};
2463
2464__attribute__((constructor))
2465static void __dumpstats_module_init(void)
2466{
2467 cfg_register_keywords(&cfg_kws);
2468}
2469
Willy Tarreau91861262007-10-17 17:06:05 +02002470/*
2471 * Local variables:
2472 * c-indent-level: 8
2473 * c-basic-offset: 8
2474 * End:
2475 */