blob: 5e7312f671867df76c899638109d6886cacc02c8 [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,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100247 "\n");
248}
249
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200250/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200251 * called from an applet running in a stream interface. The function returns 1
252 * if the request was understood, otherwise zero. It sets si->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200253 * designating the function which will have to process the request, which can
254 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200255 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200256int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200257{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200258 struct session *s = si->private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200259 char *args[MAX_STATS_ARGS + 1];
260 int arg;
261
262 while (isspace((unsigned char)*line))
263 line++;
264
265 arg = 0;
266 args[arg] = line;
267
268 while (*line && arg < MAX_STATS_ARGS) {
269 if (isspace((unsigned char)*line)) {
270 *line++ = '\0';
271
272 while (isspace((unsigned char)*line))
273 line++;
274
275 args[++arg] = line;
276 continue;
277 }
278
279 line++;
280 }
281
282 while (++arg <= MAX_STATS_ARGS)
283 args[arg] = line;
284
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200285 s->data_ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200286 if (strcmp(args[0], "show") == 0) {
287 if (strcmp(args[1], "stat") == 0) {
288 if (*args[2] && *args[3] && *args[4]) {
289 s->data_ctx.stats.flags |= STAT_BOUND;
290 s->data_ctx.stats.iid = atoi(args[2]);
291 s->data_ctx.stats.type = atoi(args[3]);
292 s->data_ctx.stats.sid = atoi(args[4]);
293 }
294
295 s->data_ctx.stats.flags |= STAT_SHOW_STAT;
296 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200297 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200298 si->st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200299 }
300 else if (strcmp(args[1], "info") == 0) {
301 s->data_ctx.stats.flags |= STAT_SHOW_INFO;
302 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200303 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200304 si->st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200305 }
306 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200307 s->data_state = DATA_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200308 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200309 s->data_ctx.cli.msg = stats_permission_denied_msg;
310 si->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200311 return 1;
312 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200313 si->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200314 }
315 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200316 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200317 s->data_ctx.cli.msg = stats_permission_denied_msg;
318 si->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200319 return 1;
320 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200321 if (*args[2])
322 s->data_ctx.errors.iid = atoi(args[2]);
323 else
324 s->data_ctx.errors.iid = -1;
325 s->data_ctx.errors.px = NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200326 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200327 si->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200328 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200329 else { /* neither "stat" nor "info" nor "sess" nor "errors"*/
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200330 return 0;
331 }
332 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200333 else if (strcmp(args[0], "clear") == 0) {
334 if (strcmp(args[1], "counters") == 0) {
335 struct proxy *px;
336 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200337 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200338 int clrall = 0;
339
340 if (strcmp(args[2], "all") == 0)
341 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200342
Willy Tarreau6162db22009-10-10 17:13:00 +0200343 /* check permissions */
344 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
345 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200346 s->data_ctx.cli.msg = stats_permission_denied_msg;
347 si->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200348 return 1;
349 }
350
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200351 for (px = proxy; px; px = px->next) {
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200352 if (clrall)
353 memset(&px->counters, 0, sizeof(px->counters));
354 else {
355 px->counters.feconn_max = 0;
356 px->counters.beconn_max = 0;
357 px->counters.fe_sps_max = 0;
358 px->counters.be_sps_max = 0;
359 px->counters.nbpend_max = 0;
360 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200361
362 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200363 if (clrall)
364 memset(&sv->counters, 0, sizeof(sv->counters));
365 else {
366 sv->counters.cur_sess_max = 0;
367 sv->counters.nbpend_max = 0;
368 sv->counters.sps_max = 0;
369 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200370
371 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200372 if (li->counters) {
373 if (clrall)
374 memset(li->counters, 0, sizeof(*li->counters));
375 else
376 li->counters->conn_max = 0;
377 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200378 }
379
380 return 1;
381 }
382 else {
383 return 0;
384 }
385 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200386 else if (strcmp(args[0], "get") == 0) {
387 if (strcmp(args[1], "weight") == 0) {
388 struct proxy *px;
389 struct server *sv;
390
391 /* split "backend/server" and make <line> point to server */
392 for (line = args[2]; *line; line++)
393 if (*line == '/') {
394 *line++ = '\0';
395 break;
396 }
397
398 if (!*line) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200399 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
400 si->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200401 return 1;
402 }
403
404 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200405 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
406 si->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200407 return 1;
408 }
409
410 /* return server's effective weight at the moment */
411 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
412 buffer_feed(si->ib, trash);
413 return 1;
414 }
415 else { /* not "get weight" */
416 return 0;
417 }
418 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200419 else if (strcmp(args[0], "set") == 0) {
420 if (strcmp(args[1], "weight") == 0) {
421 struct proxy *px;
422 struct server *sv;
423 int w;
424
425 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200426 s->data_ctx.cli.msg = stats_permission_denied_msg;
427 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200428 return 1;
429 }
430
431 /* split "backend/server" and make <line> point to server */
432 for (line = args[2]; *line; line++)
433 if (*line == '/') {
434 *line++ = '\0';
435 break;
436 }
437
438 if (!*line || !*args[3]) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200439 s->data_ctx.cli.msg = "Require 'backend/server' and 'weight' or 'weight%'.\n";
440 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200441 return 1;
442 }
443
444 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200445 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
446 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200447 return 1;
448 }
449
450 /* if the weight is terminated with '%', it is set relative to
451 * the initial weight, otherwise it is absolute.
452 */
453 w = atoi(args[3]);
454 if (strchr(args[3], '%') != NULL) {
455 if (w < 0 || w > 100) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200456 s->data_ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
457 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200458 return 1;
459 }
460 w = sv->iweight * w / 100;
461 }
462 else {
463 if (w < 0 || w > 256) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200464 s->data_ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
465 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200466 return 1;
467 }
468 }
469
470 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200471 s->data_ctx.cli.msg = "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
472 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200473 return 1;
474 }
475
476 sv->uweight = w;
477
478 if (px->lbprm.algo & BE_LB_PROP_DYN) {
479 /* we must take care of not pushing the server to full throttle during slow starts */
480 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
481 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
482 else
483 sv->eweight = BE_WEIGHT_SCALE;
484 sv->eweight *= sv->uweight;
485 } else {
486 sv->eweight = sv->uweight;
487 }
488
489 /* static LB algorithms are a bit harder to update */
490 if (px->lbprm.update_server_eweight)
491 px->lbprm.update_server_eweight(sv);
492 else if (sv->eweight)
493 px->lbprm.set_server_status_up(sv);
494 else
495 px->lbprm.set_server_status_down(sv);
496
497 return 1;
498 }
499 else { /* not "set weight" */
500 return 0;
501 }
502 }
503 else { /* not "show" nor "clear" nor "get" nor "set" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200504 return 0;
505 }
506 return 1;
507}
508
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200509/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200510 * used to processes I/O from/to the stats unix socket. The system relies on a
511 * state machine handling requests and various responses. We read a request,
512 * then we process it and send the response, and we possibly display a prompt.
513 * Then we can read again. The state is stored in si->st0 and is one of the
514 * STAT_CLI_* constants. si->st1 is used to indicate whether prompt is enabled
515 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200516 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200517void stats_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200518{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200519 struct session *s = si->private;
520 struct buffer *req = si->ob;
521 struct buffer *res = si->ib;
522 int reql;
523 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200524
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200525 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
526 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200527
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200528 while (1) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200529 if (si->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200530 /* Stats output not initialized yet */
531 memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats));
532 s->data_source = DATA_SRC_STATS;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200533 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200534 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200535 else if (si->st0 == STAT_CLI_END) {
536 /* Let's close for real now. We just close the request
537 * side, the conditions below will complete if needed.
538 */
539 si->shutw(si);
540 break;
541 }
542 else if (si->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +0200543 /* ensure we have some output room left in the event we
544 * would want to return some info right after parsing.
545 */
546 if (buffer_almost_full(si->ib))
547 break;
548
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200549 reql = buffer_si_peekline(si->ob, trash, sizeof(trash));
550 if (reql <= 0) { /* closed or EOL not found */
551 if (reql == 0)
552 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200553 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200554 continue;
555 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200556
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200557 /* seek for a possible semi-colon. If we find one, we
558 * replace it with an LF and skip only this part.
559 */
560 for (len = 0; len < reql; len++)
561 if (trash[len] == ';') {
562 trash[len] = '\n';
563 reql = len + 1;
564 break;
565 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200566
Willy Tarreau816fc222009-10-04 07:36:58 +0200567 /* now it is time to check that we have a full line,
568 * remove the trailing \n and possibly \r, then cut the
569 * line.
570 */
571 len = reql - 1;
572 if (trash[len] != '\n') {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200573 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200574 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200575 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200576
Willy Tarreau816fc222009-10-04 07:36:58 +0200577 if (len && trash[len-1] == '\r')
578 len--;
579
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200580 trash[len] = '\0';
581
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200582 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200583 if (len) {
584 if (strcmp(trash, "quit") == 0) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200585 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200586 continue;
587 }
588 else if (strcmp(trash, "prompt") == 0)
589 si->st1 = !si->st1;
590 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200591 !stats_sock_parse_request(si, trash)) {
592 s->data_ctx.cli.msg = stats_sock_usage_msg;
593 si->st0 = STAT_CLI_PRINT;
594 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200595 /* NB: stats_sock_parse_request() may have put
596 * another STAT_CLI_O_* into si->st0.
597 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200598 }
599 else if (!si->st1) {
600 /* if prompt is disabled, print help on empty lines,
601 * so that the user at least knows how to enable
602 * prompt and find help.
603 */
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200604 s->data_ctx.cli.msg = stats_sock_usage_msg;
605 si->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200606 }
607
608 /* re-adjust req buffer */
609 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200610 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200611 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200612 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200613 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200614 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200615 continue;
616 }
617
618 switch (si->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200619 case STAT_CLI_PRINT:
620 if (buffer_feed(si->ib, s->data_ctx.cli.msg) < 0)
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200621 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200622 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200623 case STAT_CLI_O_INFO:
Willy Tarreau24955a12009-10-04 11:54:04 +0200624 if (stats_dump_raw_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200625 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200626 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200627 case STAT_CLI_O_SESS:
Willy Tarreau7e72a8f2009-10-03 23:55:14 +0200628 if (stats_dump_sess_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200629 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200630 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200631 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau61b34732009-10-03 23:49:35 +0200632 if (stats_dump_errors_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200633 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200634 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200635 default: /* abnormal state */
636 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200637 break;
638 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200639
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200640 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
641 if (si->st0 == STAT_CLI_PROMPT) {
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200642 if (buffer_feed(si->ib, si->st1 ? "\n> " : "\n") < 0)
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200643 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200644 }
645
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200646 /* If the output functions are still there, it means they require more room. */
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200647 if (si->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200648 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200649
650 /* Now we close the output if one of the writers did so,
651 * or if we're not in interactive mode and the request
652 * buffer is empty. This still allows pipelined requests
653 * to be sent in non-interactive mode.
654 */
655 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->st1 && !req->send_max)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200656 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200657 continue;
658 }
659
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200660 /* switch state back to GETREQ to read next requests */
661 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200662 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200663 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200664
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200665 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200666 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
667 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
668 /* Other size has closed, let's abort if we have no more processing to do
669 * and nothing more to consume. This is comparable to a broken pipe, so
670 * we forward the close to the request side so that it flows upstream to
671 * the client.
672 */
673 si->shutw(si);
674 }
675
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200676 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200677 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
678 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
679 /* We have no more processing to do, and nothing more to send, and
680 * the client side has closed. So we'll forward this state downstream
681 * on the response buffer.
682 */
683 si->shutr(si);
684 res->flags |= BF_READ_NULL;
685 }
686
687 /* update all other flags and resync with the other side */
688 si->update(si);
689
690 /* we don't want to expire timeouts while we're processing requests */
691 si->ib->rex = TICK_ETERNITY;
692 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200693
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200694 out:
695 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
696 __FUNCTION__, __LINE__,
697 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
698
699 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
700 /* check that we have released everything then unregister */
701 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200702 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200703}
704
Willy Tarreau24955a12009-10-04 11:54:04 +0200705/* This function is called to send output to the response buffer.
706 * It dumps statistics onto the output buffer <rep> owned by session <s>.
707 * s->data_ctx must have been zeroed first, and the flags properly set.
708 * It returns 0 as long as it does not complete, non-zero upon completion.
709 * Some states are not used but it makes the code more similar to other
710 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200711 */
Willy Tarreau24955a12009-10-04 11:54:04 +0200712int stats_dump_raw_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3e76e722007-10-17 18:57:38 +0200713{
Willy Tarreau3e76e722007-10-17 18:57:38 +0200714 struct proxy *px;
715 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +0100716 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200717
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200718 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +0200719
720 switch (s->data_state) {
721 case DATA_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +0200722 /* the function had not been called yet */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200723 s->data_state = DATA_ST_HEAD;
724 /* fall through */
725
726 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100727 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200728 print_csv_header(&msg);
Willy Tarreau24955a12009-10-04 11:54:04 +0200729 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100730 return 0;
731 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200732
733 s->data_state = DATA_ST_INFO;
734 /* fall through */
735
736 case DATA_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +0100737 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100738 if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200739 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +0100740 "Name: " PRODUCT_NAME "\n"
741 "Version: " HAPROXY_VERSION "\n"
742 "Release_date: " HAPROXY_DATE "\n"
743 "Nbproc: %d\n"
744 "Process_num: %d\n"
745 "Pid: %d\n"
746 "Uptime: %dd %dh%02dm%02ds\n"
747 "Uptime_sec: %d\n"
748 "Memmax_MB: %d\n"
749 "Ulimit-n: %d\n"
750 "Maxsock: %d\n"
751 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100752 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100753 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100754 "PipesUsed: %d\n"
755 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100756 "Tasks: %d\n"
757 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200758 "node: %s\n"
759 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100760 "",
761 global.nbproc,
762 relative_pid,
763 pid,
764 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
765 up,
766 global.rlimit_memmax,
767 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100768 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100769 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200770 nb_tasks_cur, run_queue_cur,
771 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +0100772 );
Willy Tarreau24955a12009-10-04 11:54:04 +0200773 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100774 return 0;
775 }
776
Willy Tarreau3e76e722007-10-17 18:57:38 +0200777 s->data_ctx.stats.px = proxy;
778 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100779
780 s->data_ctx.stats.sv = NULL;
781 s->data_ctx.stats.sv_st = 0;
782
Willy Tarreau3e76e722007-10-17 18:57:38 +0200783 s->data_state = DATA_ST_LIST;
784 /* fall through */
785
786 case DATA_ST_LIST:
787 /* dump proxies */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100788 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100789 while (s->data_ctx.stats.px) {
790 px = s->data_ctx.stats.px;
791 /* skip the disabled proxies and non-networked ones */
792 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +0200793 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100794 if (stats_dump_proxy(s, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100795 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +0200796 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200797
Willy Tarreaua8efd362008-01-03 10:19:15 +0100798 s->data_ctx.stats.px = px->next;
799 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
800 }
801 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200802 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200803
804 s->data_state = DATA_ST_END;
805 /* fall through */
806
807 case DATA_ST_END:
808 s->data_state = DATA_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +0100809 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200810
811 case DATA_ST_FIN:
812 return 1;
813
814 default:
815 /* unknown state ! */
Willy Tarreau24955a12009-10-04 11:54:04 +0200816 s->data_state = DATA_ST_FIN;
817 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200818 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100819}
820
821
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200822/* This I/O handler runs as an applet embedded in a stream interface. It is
823 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
824 * si->st0 becomes non-zero once the transfer is finished. The handler
825 * automatically unregisters itself once transfer is complete.
826 */
827void http_stats_io_handler(struct stream_interface *si)
828{
829 struct session *s = si->private;
830 struct buffer *req = si->ob;
831 struct buffer *res = si->ib;
832
833 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
834 goto out;
835
836 /* check that the output is not closed */
837 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
838 si->st0 = 1;
839
840 if (!si->st0) {
841 if (stats_dump_http(s, res, s->be->uri_auth)) {
842 si->st0 = 1;
843 si->shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200844 }
845 }
846
847 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
848 si->shutw(si);
849
850 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->st0) {
851 si->shutr(si);
852 res->flags |= BF_READ_NULL;
853 }
854
855 /* update all other flags and resync with the other side */
856 si->update(si);
857
858 /* we don't want to expire timeouts while we're processing requests */
859 si->ib->rex = TICK_ETERNITY;
860 si->ob->wex = TICK_ETERNITY;
861
862 out:
863 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
864 /* check that we have released everything then unregister */
865 stream_int_unregister_handler(si);
866 }
867}
868
869
Willy Tarreau3e76e722007-10-17 18:57:38 +0200870/*
871 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200872 * client socket shut down on input. It stops by itself by unsetting the
Willy Tarreau72b179a2008-08-28 16:01:32 +0200873 * BF_HIJACK flag from the buffer, which it uses to keep on being called
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200874 * when there is free space in the buffer, of simply by letting an empty buffer
875 * upon return.s->data_ctx must have been zeroed before the first call, and the
876 * flags set. It returns 0 if it had to stop writing data and an I/O is needed,
877 * 1 if the dump is finished and the session must be closed, or -1 in case of
878 * any error.
Willy Tarreau91861262007-10-17 17:06:05 +0200879 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100880int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200881{
Willy Tarreau91861262007-10-17 17:06:05 +0200882 struct proxy *px;
883 struct chunk msg;
884 unsigned int up;
885
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200886 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +0200887
888 switch (s->data_state) {
889 case DATA_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200890 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200891 "HTTP/1.0 200 OK\r\n"
892 "Cache-Control: no-cache\r\n"
893 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200894 "Content-Type: %s\r\n",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100895 (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +0200896
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100897 if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200898 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +0200899 uri->refresh);
900
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200901 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +0200902
903 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200904 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +0200905 return 0;
906
Willy Tarreau91861262007-10-17 17:06:05 +0200907 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
908 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
909 if (!(s->flags & SN_FINST_MASK))
910 s->flags |= SN_FINST_R;
911
912 if (s->txn.meth == HTTP_METH_HEAD) {
913 /* that's all we return in case of HEAD request */
914 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +0200915 return 1;
916 }
917
918 s->data_state = DATA_ST_HEAD; /* let's start producing data */
919 /* fall through */
920
921 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100922 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200923 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200924 chunk_printf(&msg,
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200925 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200926 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
927 "<style type=\"text/css\"><!--\n"
928 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +0200929 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +0200930 " font-size: 12px;"
931 " font-weight: normal;"
932 " color: black;"
933 " background: white;"
934 "}\n"
935 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +0200936 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +0200937 " align: center;"
938 "}\n"
939 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +0200940 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +0200941 " margin-bottom: 0.5em;"
942 "}\n"
943 "h2 {"
944 " font-family: helvetica, arial;"
945 " font-size: x-large;"
946 " font-weight: bold;"
947 " font-style: italic;"
948 " color: #6020a0;"
949 " margin-top: 0em;"
950 " margin-bottom: 0em;"
951 "}\n"
952 "h3 {"
953 " font-family: helvetica, arial;"
954 " font-size: 16px;"
955 " font-weight: bold;"
956 " color: #b00040;"
957 " background: #e8e8d0;"
958 " margin-top: 0em;"
959 " margin-bottom: 0em;"
960 "}\n"
961 "li {"
962 " margin-top: 0.25em;"
963 " margin-right: 2em;"
964 "}\n"
965 ".hr {margin-top: 0.25em;"
966 " border-color: black;"
967 " border-bottom-style: solid;"
968 "}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200969 ".titre {background: #20D0D0;color: #000000; font-weight: bold;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200970 ".total {background: #20D0D0;color: #ffff80;}\n"
971 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200972 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200973 ".backend {background: #e8e8d0;}\n"
974 ".active0 {background: #ff9090;}\n"
975 ".active1 {background: #ffd020;}\n"
976 ".active2 {background: #ffffa0;}\n"
977 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100978 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
979 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
980 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200981 ".backup0 {background: #ff9090;}\n"
982 ".backup1 {background: #ff80ff;}\n"
983 ".backup2 {background: #c060ff;}\n"
984 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100985 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
986 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
987 ".backup6 {background: #e0e0e0;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +0200988 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Willy Tarreau91861262007-10-17 17:06:05 +0200989 "table.tbl { border-collapse: collapse; border-style: none;}\n"
990 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray;}\n"
991 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzki619caca2009-10-03 15:46:08 +0200992 "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 +0200993 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200994 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200995 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
996 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
997 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
998 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200999 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001000 (uri->flags&ST_SHNODE) ? " on " : "",
1001 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001002 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001003 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001004 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001005 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001006 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001007 return 0;
1008
1009 s->data_state = DATA_ST_INFO;
1010 /* fall through */
1011
1012 case DATA_ST_INFO:
1013 up = (now.tv_sec - start_date.tv_sec);
1014
1015 /* WARNING! this has to fit the first packet too.
1016 * We are around 3.5 kB, add adding entries will
1017 * become tricky if we want to support 4kB buffers !
1018 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001019 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001020 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001021 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1022 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001023 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001024 "<hr width=\"100%%\" class=\"hr\">\n"
1025 "<h3>&gt; General process information</h3>\n"
1026 "<table border=0 cols=4><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001027 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001028 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001029 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1030 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
1031 "current conns = %d; current pipes = %d/%d<br>\n"
1032 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001033 "</td><td align=\"center\" nowrap>\n"
1034 "<table class=\"lgd\"><tr>\n"
1035 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1036 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1037 "</tr><tr>\n"
1038 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1039 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1040 "</tr><tr>\n"
1041 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1042 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1043 "</tr><tr>\n"
1044 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001045 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001046 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001047 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001048 "</td>"
1049 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1050 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1051 "",
1052 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001053 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1054 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1055 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001056 up / 86400, (up % 86400) / 3600,
1057 (up % 3600) / 60, (up % 60),
1058 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1059 global.rlimit_memmax ? " MB" : "",
1060 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001061 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001062 actconn, pipes_used, pipes_used+pipes_free,
1063 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +02001064 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001065
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001066 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001067 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001068 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1069 uri->uri_prefix,
1070 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001071 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001072 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001073 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001074 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1075 uri->uri_prefix,
1076 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001077 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001078
Willy Tarreau55bb8452007-10-17 18:44:57 +02001079 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001080 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001081 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001082 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1083 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001084 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001085 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001086 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001087 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001088 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1089 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001090 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001091 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001092 }
Willy Tarreau91861262007-10-17 17:06:05 +02001093
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001094 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001095 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
1096 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001097 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
1098 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001099
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001100 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02001101 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
1102 uri->uri_prefix,
1103 (uri->refresh > 0) ? ";norefresh" : "");
1104
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001105 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001106 "</td>"
1107 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1108 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
1109 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
1110 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
1111 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
1112 "</ul>"
1113 "</td>"
1114 "</tr></table>\n"
1115 ""
1116 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001117
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001118 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001119 return 0;
1120 }
Willy Tarreau91861262007-10-17 17:06:05 +02001121
Willy Tarreau91861262007-10-17 17:06:05 +02001122 s->data_ctx.stats.px = proxy;
1123 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1124 s->data_state = DATA_ST_LIST;
1125 /* fall through */
1126
1127 case DATA_ST_LIST:
1128 /* dump proxies */
1129 while (s->data_ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001130 if (buffer_almost_full(rep))
1131 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02001132 px = s->data_ctx.stats.px;
1133 /* skip the disabled proxies and non-networked ones */
1134 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001135 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001136 return 0;
1137
1138 s->data_ctx.stats.px = px->next;
1139 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1140 }
1141 /* here, we just have reached the last proxy */
1142
1143 s->data_state = DATA_ST_END;
1144 /* fall through */
1145
1146 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001147 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001148 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001149 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001150 return 0;
1151 }
Willy Tarreau91861262007-10-17 17:06:05 +02001152
1153 s->data_state = DATA_ST_FIN;
1154 /* fall through */
1155
1156 case DATA_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02001157 return 1;
1158
1159 default:
1160 /* unknown state ! */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001161 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001162 return -1;
1163 }
1164}
1165
1166
1167/*
1168 * Dumps statistics for a proxy.
1169 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1170 * ot non-zero if everything completed.
1171 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001172int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001173{
1174 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001175 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001176 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02001177 struct chunk msg;
1178
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001179 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001180
1181 switch (s->data_ctx.stats.px_st) {
1182 case DATA_ST_PX_INIT:
1183 /* we are on a new proxy */
1184
1185 if (uri && uri->scope) {
1186 /* we have a limited scope, we have to check the proxy name */
1187 struct stat_scope *scope;
1188 int len;
1189
1190 len = strlen(px->id);
1191 scope = uri->scope;
1192
1193 while (scope) {
1194 /* match exact proxy name */
1195 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1196 break;
1197
1198 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001199 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001200 break;
1201 scope = scope->next;
1202 }
1203
1204 /* proxy name not found : don't dump anything */
1205 if (scope == NULL)
1206 return 1;
1207 }
1208
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001209 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001210 (px->uuid != s->data_ctx.stats.iid))
1211 return 1;
1212
Willy Tarreau91861262007-10-17 17:06:05 +02001213 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
1214 /* fall through */
1215
1216 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001217 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001218 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001219 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001220 "<table class=\"tbl\" width=\"100%%\">\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001221 "<tr align=\"center\" class=\"titre\">"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001222 "<th class=\"pxname\" width=\"10%%\">%s</th>"
1223 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001224 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001225 "</table>\n"
1226 "<table cols=\"29\" class=\"tbl\" width=\"100%%\">\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001227 "<tr align=\"center\" class=\"titre\">"
1228 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001229 "<th colspan=3>Queue</th>"
1230 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001231 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001232 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001233 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001234 "</tr>\n"
1235 "<tr align=\"center\" class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001236 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001237 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001238 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001239 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001240 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001241 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001242 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1243 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001244 "</tr>",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001245 px->id,
1246 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001247
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001248 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001249 return 0;
1250 }
Willy Tarreau91861262007-10-17 17:06:05 +02001251
1252 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
1253 /* fall through */
1254
1255 case DATA_ST_PX_FE:
1256 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001257 if ((px->cap & PR_CAP_FE) &&
1258 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
1259 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001260 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001261 /* name, queue */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001262 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=3></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001263 /* sessions rate : current, max, limit */
1264 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
1265 /* sessions : current, max, limit, total, lbtot */
1266 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001267 "<td align=right>%s</td><td align=right></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001268 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001269 "<td align=right>%s</td><td align=right>%s</td>"
1270 "",
Willy Tarreaua3e49422009-05-10 19:19:41 +02001271 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001272 U2H1(px->counters.fe_sps_max), LIM2A2(px->fe_sps_lim, "-"),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001273 U2H3(px->feconn), U2H4(px->counters.feconn_max), U2H5(px->maxconn),
1274 U2H6(px->counters.cum_feconn), U2H7(px->counters.bytes_in), U2H8(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001275
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001276 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001277 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001278 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001279 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001280 "<td align=right>%s</td><td align=right></td><td align=right></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001281 /* warnings: retries, redispatches */
1282 "<td align=right></td><td align=right></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001283 /* server status : reflect frontend status */
Willy Tarreau91861262007-10-17 17:06:05 +02001284 "<td align=center>%s</td>"
1285 /* rest of server: nothing */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001286 "<td align=center colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02001287 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001288 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
1289 U2H2(px->counters.failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +02001290 px->state == PR_STRUN ? "OPEN" :
1291 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001292 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001293 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001294 /* pxid, name, queue cur, queue max, */
1295 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001296 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001297 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001298 /* bytes : in, out */
1299 "%lld,%lld,"
1300 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001301 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001302 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001303 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001304 /* warnings: retries, redispatches */
1305 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001306 /* server status : reflect frontend status */
1307 "%s,"
1308 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001309 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001310 /* pid, iid, sid, throttle, lbtot, tracked, type */
1311 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001312 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001313 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001314 /* check_status, check_code, check_duration */
1315 ",,,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001316 "\n",
1317 px->id,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001318 px->feconn, px->counters.feconn_max, px->maxconn, px->counters.cum_feconn,
1319 px->counters.bytes_in, px->counters.bytes_out,
1320 px->counters.denied_req, px->counters.denied_resp,
1321 px->counters.failed_req,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001322 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +01001323 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01001324 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001325 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001326 px->fe_sps_lim, px->counters.fe_sps_max);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001327 }
Willy Tarreau91861262007-10-17 17:06:05 +02001328
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001329 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001330 return 0;
1331 }
1332
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001333 s->data_ctx.stats.l = px->listen; /* may be NULL */
1334 s->data_ctx.stats.px_st = DATA_ST_PX_LI;
1335 /* fall through */
1336
1337 case DATA_ST_PX_LI:
1338 /* stats.l has been initialized above */
1339 for (; s->data_ctx.stats.l != NULL; s->data_ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001340 if (buffer_almost_full(rep))
1341 return 0;
1342
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001343 l = s->data_ctx.stats.l;
1344 if (!l->counters)
1345 continue;
1346
1347 if (s->data_ctx.stats.flags & STAT_BOUND) {
1348 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SO)))
1349 break;
1350
1351 if (s->data_ctx.stats.sid != -1 && l->luid != s->data_ctx.stats.sid)
1352 continue;
1353 }
1354
1355 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
1356 chunk_printf(&msg,
1357 /* name, queue */
1358 "<tr align=center class=\"socket\"><td>%s</td><td colspan=3></td>"
1359 /* sessions rate: current, max, limit */
1360 "<td align=right colspan=3>&nbsp;</td>"
1361 /* sessions: current, max, limit, total, lbtot */
1362 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
1363 "<td align=right>%s</td><td align=right>&nbsp;</td>"
1364 /* bytes: in, out */
1365 "<td align=right>%s</td><td align=right>%s</td>"
1366 "",
1367 l->name,
1368 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
1369 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
1370
1371 chunk_printf(&msg,
1372 /* denied: req, resp */
1373 "<td align=right>%s</td><td align=right>%s</td>"
1374 /* errors: request, connect, response */
1375 "<td align=right>%s</td><td align=right></td><td align=right></td>"
1376 /* warnings: retries, redispatches */
1377 "<td align=right></td><td align=right></td>"
1378 /* server status: reflect listener status */
1379 "<td align=center>%s</td>"
1380 /* rest of server: nothing */
1381 "<td align=center colspan=8></td></tr>"
1382 "",
1383 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
1384 U2H2(l->counters->failed_req),
1385 (l->nbconn < l->maxconn) ? "OPEN" : "FULL");
1386 } else {
1387 chunk_printf(&msg,
1388 /* pxid, name, queue cur, queue max, */
1389 "%s,%s,,,"
1390 /* sessions: current, max, limit, total */
1391 "%d,%d,%d,%lld,"
1392 /* bytes: in, out */
1393 "%lld,%lld,"
1394 /* denied: req, resp */
1395 "%lld,%lld,"
1396 /* errors: request, connect, response */
1397 "%lld,,,"
1398 /* warnings: retries, redispatches */
1399 ",,"
1400 /* server status: reflect listener status */
1401 "%s,"
1402 /* rest of server: nothing */
1403 ",,,,,,,,"
1404 /* pid, iid, sid, throttle, lbtot, tracked, type */
1405 "%d,%d,%d,,,,%d,"
1406 /* rate, rate_lim, rate_max */
1407 ",,,"
1408 /* check_status, check_code, check_duration */
1409 ",,,"
1410 "\n",
1411 px->id, l->name,
1412 l->nbconn, l->counters->conn_max,
1413 l->maxconn, l->counters->cum_conn,
1414 l->counters->bytes_in, l->counters->bytes_out,
1415 l->counters->denied_req, l->counters->denied_resp,
1416 l->counters->failed_req,
1417 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
1418 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
1419 }
1420
1421 if (buffer_feed_chunk(rep, &msg) >= 0)
1422 return 0;
1423 }
1424
Willy Tarreau91861262007-10-17 17:06:05 +02001425 s->data_ctx.stats.sv = px->srv; /* may be NULL */
1426 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
1427 /* fall through */
1428
1429 case DATA_ST_PX_SV:
1430 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001431 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001432 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 +02001433
Willy Tarreau4e33d862009-10-11 23:35:10 +02001434 if (buffer_almost_full(rep))
1435 return 0;
1436
Willy Tarreau91861262007-10-17 17:06:05 +02001437 sv = s->data_ctx.stats.sv;
1438
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001439 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001440 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
1441 break;
1442
1443 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
1444 continue;
1445 }
1446
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001447 if (sv->tracked)
1448 svs = sv->tracked;
1449 else
1450 svs = sv;
1451
Willy Tarreau91861262007-10-17 17:06:05 +02001452 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001453 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01001454 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001455 else if (svs->state & SRV_RUNNING) {
1456 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02001457 sv_state = 3; /* UP */
1458 else
1459 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01001460
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001461 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01001462 sv_state += 2;
1463 }
Willy Tarreau91861262007-10-17 17:06:05 +02001464 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001465 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02001466 sv_state = 1; /* going up */
1467 else
1468 sv_state = 0; /* DOWN */
1469
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001470 if ((sv_state == 0) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02001471 /* do not report servers which are DOWN */
1472 s->data_ctx.stats.sv = sv->next;
1473 continue;
1474 }
1475
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001476 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001477 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
1478 "UP %d/%d &darr;", "UP",
1479 "NOLB %d/%d &darr;", "NOLB",
1480 "<i>no check</i>" };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001481 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001482 /* name */
1483 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001484 /* queue : current, max, limit */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001485 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001486 /* sessions rate : current, max, limit */
1487 "<td align=right>%s</td><td align=right>%s</td><td align=right></td>"
1488 /* sessions : current, max, limit, total, lbtot */
1489 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001490 "<td align=right>%s</td><td align=right>%s</td>"
1491 "",
1492 (sv->state & SRV_BACKUP) ? "backup" : "active",
1493 sv_state, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001494 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
1495 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
1496 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001497 U2H8(sv->counters.cum_sess), U2H9(sv->counters.cum_lbconn));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001498
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001499 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001500 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001501 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001502 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001503 "<td align=right></td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001504 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001505 "<td align=right></td><td align=right>%s</td><td align=right>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001506 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001507 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001508 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001509 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
1510 U2H2(sv->counters.failed_secu),
1511 U2H3(sv->counters.failed_conns), U2H4(sv->counters.failed_resp),
1512 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001513
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001514 /* status, lest check */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001515 chunk_printf(&msg, "<td nowrap>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001516
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001517 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001518 chunk_printf(&msg, "%s ",
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001519 human_time(now.tv_sec - sv->last_change, 1));
1520
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001521 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001522 srv_hlt_st[sv_state],
1523 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
1524 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001525
1526 chunk_printf(&msg, "</td><td title=\"%s",
1527 get_check_status_description(sv->check_status));
1528
1529 if (*sv->check_desc) {
1530 struct chunk src;
1531
1532 chunk_printf(&msg, ": ");
1533
1534 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
1535 chunk_htmlencode(&msg, &src);
1536 }
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001537
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001538 chunk_printf(&msg, "\" nowrap> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001539 tv_iszero(&sv->check_start)?"":"* ",
1540 get_check_status_info(sv->check_status));
1541
1542 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001543 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001544
1545 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001546 chunk_printf(&msg, " in %lums", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001547 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001548 chunk_printf(&msg, "</td><td>");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001549 }
Willy Tarreau91861262007-10-17 17:06:05 +02001550
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001551 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001552 /* weight */
1553 "</td><td>%d</td>"
1554 /* act, bck */
1555 "<td>%s</td><td>%s</td>"
1556 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001557 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02001558 (sv->state & SRV_BACKUP) ? "-" : "Y",
1559 (sv->state & SRV_BACKUP) ? "Y" : "-");
1560
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001561 /* check failures: unique, fatal, down time */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001562 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001563 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02001564 "<td align=right>%lld</td><td align=right>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001565 "<td nowrap align=right>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001566 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001567 svs->counters.failed_checks, svs->counters.down_trans,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001568 human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001569 else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001570 chunk_printf(&msg,
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001571 "<td nowrap colspan=3>via %s/%s</td>", svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001572 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001573 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001574 "<td colspan=3></td>");
1575
1576 /* throttle */
1577 if ((sv->state & SRV_WARMINGUP) &&
1578 now.tv_sec < sv->last_change + sv->slowstart &&
1579 now.tv_sec >= sv->last_change) {
1580 unsigned int ratio;
1581 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001582 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001583 "<td>%d %%</td></tr>\n", ratio);
1584 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001585 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001586 "<td>-</td></tr>\n");
1587 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02001588 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001589 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
1590 "UP %d/%d,", "UP,",
1591 "NOLB %d/%d,", "NOLB,",
1592 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001593 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001594 /* pxid, name */
1595 "%s,%s,"
1596 /* queue : current, max */
1597 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001598 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001599 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001600 /* bytes : in, out */
1601 "%lld,%lld,"
1602 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001603 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001604 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001605 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001606 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001607 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001608 "",
1609 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001610 sv->nbpend, sv->counters.nbpend_max,
1611 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001612 sv->counters.bytes_in, sv->counters.bytes_out,
1613 sv->counters.failed_secu,
1614 sv->counters.failed_conns, sv->counters.failed_resp,
1615 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001616
Willy Tarreau55bb8452007-10-17 18:44:57 +02001617 /* status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001618 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001619 srv_hlt_st[sv_state],
1620 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
1621 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
Willy Tarreau91861262007-10-17 17:06:05 +02001622
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001623 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001624 /* weight, active, backup */
1625 "%d,%d,%d,"
1626 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001627 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001628 (sv->state & SRV_BACKUP) ? 0 : 1,
1629 (sv->state & SRV_BACKUP) ? 1 : 0);
1630
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001631 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001632 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001633 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02001634 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001635 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001636 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02001637 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001638 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001639 ",,,,");
1640
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001641 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001642 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01001643 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001644 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01001645 LIM2A0(sv->maxqueue, ""),
1646 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001647
1648 /* throttle */
1649 if ((sv->state & SRV_WARMINGUP) &&
1650 now.tv_sec < sv->last_change + sv->slowstart &&
1651 now.tv_sec >= sv->last_change) {
1652 unsigned int ratio;
1653 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001654 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001655 }
1656
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001657 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001658 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001659
1660 /* tracked */
1661 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001662 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001663 sv->tracked->proxy->id, sv->tracked->id);
1664 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001665 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001666
Willy Tarreau7f062c42009-03-05 18:43:00 +01001667 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001668 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01001669
1670 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001671 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001672 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001673 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01001674
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001675 if (sv->state & SRV_CHECKED) {
1676 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001677 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001678
1679 /* check_code */
1680 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001681 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001682 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001683 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001684
1685 /* check_duration */
1686 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001687 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001688 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001689 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001690
1691 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001692 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001693 }
1694
Willy Tarreau7f062c42009-03-05 18:43:00 +01001695 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001696 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001697 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001698 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001699 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001700 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02001701
1702 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
1703 /* fall through */
1704
1705 case DATA_ST_PX_BE:
1706 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001707 if ((px->cap & PR_CAP_BE) &&
1708 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
1709 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001710 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001711 /* name */
1712 "<tr align=center class=\"backend\"><td>Backend</td>"
1713 /* queue : current, max */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001714 "<td align=right>%s</td><td align=right>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001715 /* sessions rate : current, max, limit */
1716 "<td align=right>%s</td><td align=right>%s</td><td align=right></td>"
1717 "",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001718 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->counters.nbpend_max),
1719 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->counters.be_sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02001720
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001721 chunk_printf(&msg,
Willy Tarreaua3e49422009-05-10 19:19:41 +02001722 /* sessions : current, max, limit, total, lbtot */
1723 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001724 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001725 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001726 "<td align=right>%s</td><td align=right>%s</td>"
1727 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001728 U2H2(px->beconn), U2H3(px->counters.beconn_max), U2H4(px->fullconn),
1729 U2H6(px->counters.cum_beconn), U2H7(px->counters.cum_lbconn),
1730 U2H8(px->counters.bytes_in), U2H9(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001731
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001732 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001733 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001734 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001735 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001736 "<td align=right></td><td align=right>%s</td><td align=right>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001737 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001738 "<td align=right>%lld</td><td align=right>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001739 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02001740 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001741 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02001742 * active and backups. */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001743 "<td align=center nowrap>%s %s</td><td align=center>&nbsp;</td><td align=center>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001744 "<td align=center>%d</td><td align=center>%d</td>"
1745 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001746 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
1747 U2H2(px->counters.failed_conns), U2H3(px->counters.failed_resp),
1748 px->counters.retries, px->counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001749 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01001750 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
1751 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01001752 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001753 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001754
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001755 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001756 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001757 "<td align=center>&nbsp;</td><td align=\"right\">%d</td>"
1758 "<td align=\"right\" nowrap>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001759 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001760 "</tr>",
1761 px->down_trans,
1762 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001763 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001764 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001765 /* pxid, name */
1766 "%s,BACKEND,"
1767 /* queue : current, max */
1768 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001769 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001770 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001771 /* bytes : in, out */
1772 "%lld,%lld,"
1773 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001774 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001775 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001776 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001777 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001778 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001779 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02001780 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001781 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02001782 * active and backups. */
1783 "%s,"
1784 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001785 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001786 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001787 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001788 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001789 /* rate, rate_lim, rate_max, */
1790 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001791 /* check_status, check_code, check_duration */
1792 ",,,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001793 "\n",
1794 px->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001795 px->nbpend /* or px->totpend ? */, px->counters.nbpend_max,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001796 px->beconn, px->counters.beconn_max, px->fullconn, px->counters.cum_beconn,
1797 px->counters.bytes_in, px->counters.bytes_out,
1798 px->counters.denied_req, px->counters.denied_resp,
1799 px->counters.failed_conns, px->counters.failed_resp,
1800 px->counters.retries, px->counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01001801 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01001802 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001803 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001804 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01001805 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001806 relative_pid, px->uuid,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001807 px->counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001808 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001809 px->counters.be_sps_max);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001810 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001811 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001812 return 0;
1813 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001814
Willy Tarreau91861262007-10-17 17:06:05 +02001815 s->data_ctx.stats.px_st = DATA_ST_PX_END;
1816 /* fall through */
1817
1818 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001819 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001820 chunk_printf(&msg, "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001821
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001822 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001823 return 0;
1824 }
Willy Tarreau91861262007-10-17 17:06:05 +02001825
1826 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
1827 /* fall through */
1828
1829 case DATA_ST_PX_FIN:
1830 return 1;
1831
1832 default:
1833 /* unknown state, we should put an abort() here ! */
1834 return 1;
1835 }
1836}
1837
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001838
1839/* This function is called to send output to the response buffer.
1840 * It dumps the sessions states onto the output buffer <rep>.
1841 * Expects to be called with client socket shut down on input.
1842 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001843 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001844 */
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001845int stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001846{
1847 struct chunk msg;
1848
1849 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
1850 /* If we're forced to shut down, we might have to remove our
1851 * reference to the last session being dumped.
1852 */
1853 if (s->data_state == DATA_ST_LIST) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001854 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001855 LIST_DEL(&s->data_ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001856 LIST_INIT(&s->data_ctx.sess.bref.users);
1857 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001858 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001859 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001860 }
1861
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001862 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001863
1864 switch (s->data_state) {
1865 case DATA_ST_INIT:
1866 /* the function had not been called yet, let's prepare the
1867 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001868 * pointer to the first in the global list. When a target
1869 * session is being destroyed, it is responsible for updating
1870 * this pointer. We know we have reached the end when this
1871 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001872 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001873 LIST_INIT(&s->data_ctx.sess.bref.users);
1874 s->data_ctx.sess.bref.ref = sessions.n;
1875 s->data_state = DATA_ST_LIST;
1876 /* fall through */
1877
1878 case DATA_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001879 /* first, let's detach the back-ref from a possible previous session */
1880 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
1881 LIST_DEL(&s->data_ctx.sess.bref.users);
1882 LIST_INIT(&s->data_ctx.sess.bref.users);
1883 }
1884
1885 /* and start from where we stopped */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001886 while (s->data_ctx.sess.bref.ref != &sessions) {
1887 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1888 struct session *curr_sess;
1889
1890 curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list);
1891
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001892 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001893 "%p: proto=%s",
1894 curr_sess,
1895 curr_sess->listener->proto->name);
1896
1897 switch (curr_sess->listener->proto->sock_family) {
1898 case AF_INET:
1899 inet_ntop(AF_INET,
1900 (const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr,
1901 pn, sizeof(pn));
1902
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001903 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001904 " src=%s:%d fe=%s be=%s srv=%s",
1905 pn,
1906 ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port),
1907 curr_sess->fe->id,
1908 curr_sess->be->id,
1909 curr_sess->srv ? curr_sess->srv->id : "<none>"
1910 );
1911 break;
1912 case AF_INET6:
1913 inet_ntop(AF_INET6,
1914 (const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr,
1915 pn, sizeof(pn));
1916
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001917 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001918 " src=%s:%d fe=%s be=%s srv=%s",
1919 pn,
1920 ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port),
1921 curr_sess->fe->id,
1922 curr_sess->be->id,
1923 curr_sess->srv ? curr_sess->srv->id : "<none>"
1924 );
1925
1926 break;
1927 case AF_UNIX:
1928 /* no more information to print right now */
1929 break;
1930 }
1931
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001932 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02001933 " ts=%02x age=%s calls=%d",
1934 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01001935 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
1936 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001937
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001938 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001939 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
1940 curr_sess->req->flags,
1941 curr_sess->req->l,
1942 curr_sess->req->analysers,
1943 curr_sess->req->rex ?
1944 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
1945 TICKS_TO_MS(1000)) : "");
1946
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001947 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001948 ",wx=%s",
1949 curr_sess->req->wex ?
1950 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
1951 TICKS_TO_MS(1000)) : "");
1952
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001953 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001954 ",ax=%s]",
1955 curr_sess->req->analyse_exp ?
1956 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
1957 TICKS_TO_MS(1000)) : "");
1958
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001959 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001960 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
1961 curr_sess->rep->flags,
1962 curr_sess->rep->l,
1963 curr_sess->rep->analysers,
1964 curr_sess->rep->rex ?
1965 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
1966 TICKS_TO_MS(1000)) : "");
1967
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001968 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001969 ",wx=%s",
1970 curr_sess->rep->wex ?
1971 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
1972 TICKS_TO_MS(1000)) : "");
1973
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001974 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001975 ",ax=%s]",
1976 curr_sess->rep->analyse_exp ?
1977 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
1978 TICKS_TO_MS(1000)) : "");
1979
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001980 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001981 " s0=[%d,%1xh,fd=%d,ex=%s]",
1982 curr_sess->si[0].state,
1983 curr_sess->si[0].flags,
1984 curr_sess->si[0].fd,
1985 curr_sess->si[0].exp ?
1986 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
1987 TICKS_TO_MS(1000)) : "");
1988
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001989 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001990 " s1=[%d,%1xh,fd=%d,ex=%s]",
1991 curr_sess->si[1].state,
1992 curr_sess->si[1].flags,
1993 curr_sess->si[1].fd,
1994 curr_sess->si[1].exp ?
1995 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
1996 TICKS_TO_MS(1000)) : "");
1997
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001998 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001999 " exp=%s",
2000 curr_sess->task->expire ?
2001 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
2002 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01002003 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002004 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002005
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002006 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002007
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002008 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002009 /* let's try again later from this session. We add ourselves into
2010 * this session's users so that it can remove us upon termination.
2011 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002012 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002013 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002014 }
2015
2016 s->data_ctx.sess.bref.ref = curr_sess->list.n;
2017 }
2018 s->data_state = DATA_ST_FIN;
2019 /* fall through */
2020
2021 default:
2022 s->data_state = DATA_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002023 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002024 }
2025}
2026
Willy Tarreau74808cb2009-03-04 15:53:18 +01002027/* print a line of error buffer (limited to 70 bytes) to <out>. The format is :
2028 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
2029 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
2030 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
2031 * lines are respected within the limit of 70 output chars. Lines that are
2032 * continuation of a previous truncated line begin with "+" instead of " "
2033 * after the offset. The new pointer is returned.
2034 */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002035static int dump_error_line(struct chunk *out, struct error_snapshot *err,
2036 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01002037{
2038 int end;
2039 unsigned char c;
2040
2041 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002042 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01002043 return ptr;
2044
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002045 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01002046
Willy Tarreau61b34732009-10-03 23:49:35 +02002047 while (ptr < err->len && ptr < sizeof(err->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002048 c = err->buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01002049 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002050 if (out->len > end - 2)
2051 break;
2052 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01002053 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002054 if (out->len > end - 3)
2055 break;
2056 out->str[out->len++] = '\\';
2057 switch (c) {
2058 case '\t': c = 't'; break;
2059 case '\n': c = 'n'; break;
2060 case '\r': c = 'r'; break;
2061 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01002062 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002063 }
2064 out->str[out->len++] = c;
2065 } else {
2066 if (out->len > end - 5)
2067 break;
2068 out->str[out->len++] = '\\';
2069 out->str[out->len++] = 'x';
2070 out->str[out->len++] = hextab[(c >> 4) & 0xF];
2071 out->str[out->len++] = hextab[c & 0xF];
2072 }
2073 if (err->buf[ptr++] == '\n') {
2074 /* we had a line break, let's return now */
2075 out->str[out->len++] = '\n';
2076 *line = ptr;
2077 return ptr;
2078 }
2079 }
2080 /* we have an incomplete line, we return it as-is */
2081 out->str[out->len++] = '\n';
2082 return ptr;
2083}
2084
2085/* This function is called to send output to the response buffer.
2086 * It dumps the errors logged in proxies onto the output buffer <rep>.
2087 * Expects to be called with client socket shut down on input.
2088 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau61b34732009-10-03 23:49:35 +02002089 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau74808cb2009-03-04 15:53:18 +01002090 */
Willy Tarreau61b34732009-10-03 23:49:35 +02002091int stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau74808cb2009-03-04 15:53:18 +01002092{
2093 extern const char *monthname[12];
2094 struct chunk msg;
2095
Willy Tarreau61b34732009-10-03 23:49:35 +02002096 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW)))
2097 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002098
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002099 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01002100
2101 if (!s->data_ctx.errors.px) {
2102 /* the function had not been called yet, let's prepare the
2103 * buffer for a response.
2104 */
Willy Tarreau74808cb2009-03-04 15:53:18 +01002105 s->data_ctx.errors.px = proxy;
2106 s->data_ctx.errors.buf = 0;
2107 s->data_ctx.errors.bol = 0;
2108 s->data_ctx.errors.ptr = -1;
2109 }
2110
2111 /* we have two inner loops here, one for the proxy, the other one for
2112 * the buffer.
2113 */
2114 while (s->data_ctx.errors.px) {
2115 struct error_snapshot *es;
2116
2117 if (s->data_ctx.errors.buf == 0)
2118 es = &s->data_ctx.errors.px->invalid_req;
2119 else
2120 es = &s->data_ctx.errors.px->invalid_rep;
2121
2122 if (!es->when.tv_sec)
2123 goto next;
2124
2125 if (s->data_ctx.errors.iid >= 0 &&
2126 s->data_ctx.errors.px->uuid != s->data_ctx.errors.iid &&
2127 es->oe->uuid != s->data_ctx.errors.iid)
2128 goto next;
2129
2130 if (s->data_ctx.errors.ptr < 0) {
2131 /* just print headers now */
2132
2133 char pn[INET6_ADDRSTRLEN];
2134 struct tm tm;
2135
2136 get_localtime(es->when.tv_sec, &tm);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002137 chunk_printf(&msg, "\n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01002138 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002139 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01002140
2141
2142 if (es->src.ss_family == AF_INET)
2143 inet_ntop(AF_INET,
2144 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
2145 pn, sizeof(pn));
2146 else
2147 inet_ntop(AF_INET6,
2148 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
2149 pn, sizeof(pn));
2150
2151 switch (s->data_ctx.errors.buf) {
2152 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002153 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002154 " frontend %s (#%d): invalid request\n"
2155 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
2156 " request length %d bytes, error at position %d:\n\n",
2157 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
2158 pn, es->sid, es->oe->id, es->oe->uuid,
2159 es->srv ? es->srv->id : "<NONE>",
2160 es->srv ? es->srv->puid : -1,
2161 es->len, es->pos);
2162 break;
2163 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002164 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002165 " backend %s (#%d) : invalid response\n"
2166 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
2167 " response length %d bytes, error at position %d:\n\n",
2168 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
2169 pn, es->sid, es->oe->id, es->oe->uuid,
2170 es->srv ? es->srv->id : "<NONE>",
2171 es->srv ? es->srv->puid : -1,
2172 es->len, es->pos);
2173 break;
2174 }
2175
Willy Tarreau61b34732009-10-03 23:49:35 +02002176 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002177 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02002178 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002179 }
2180 s->data_ctx.errors.ptr = 0;
2181 s->data_ctx.errors.sid = es->sid;
2182 }
2183
2184 if (s->data_ctx.errors.sid != es->sid) {
2185 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002186 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002187 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau61b34732009-10-03 23:49:35 +02002188 if (buffer_feed_chunk(rep, &msg) >= 0)
2189 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002190 goto next;
2191 }
2192
2193 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau61b34732009-10-03 23:49:35 +02002194 while (s->data_ctx.errors.ptr < es->len && s->data_ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002195 int newptr;
2196 int newline;
2197
2198 newline = s->data_ctx.errors.bol;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002199 newptr = dump_error_line(&msg, es, &newline, s->data_ctx.errors.ptr);
Willy Tarreau74808cb2009-03-04 15:53:18 +01002200 if (newptr == s->data_ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02002201 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002202
Willy Tarreau61b34732009-10-03 23:49:35 +02002203 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002204 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02002205 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002206 }
2207 s->data_ctx.errors.ptr = newptr;
2208 s->data_ctx.errors.bol = newline;
2209 };
2210 next:
2211 s->data_ctx.errors.bol = 0;
2212 s->data_ctx.errors.ptr = -1;
2213 s->data_ctx.errors.buf++;
2214 if (s->data_ctx.errors.buf > 1) {
2215 s->data_ctx.errors.buf = 0;
2216 s->data_ctx.errors.px = s->data_ctx.errors.px->next;
2217 }
2218 }
2219
2220 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02002221 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002222}
2223
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002224
Willy Tarreau10522fd2008-07-09 20:12:41 +02002225static struct cfg_kw_list cfg_kws = {{ },{
2226 { CFG_GLOBAL, "stats", stats_parse_global },
2227 { 0, NULL, NULL },
2228}};
2229
2230__attribute__((constructor))
2231static void __dumpstats_module_init(void)
2232{
2233 cfg_register_keywords(&cfg_kws);
2234}
2235
Willy Tarreau91861262007-10-17 17:06:05 +02002236/*
2237 * Local variables:
2238 * c-indent-level: 8
2239 * c-basic-offset: 8
2240 * End:
2241 */