blob: e576b12e76c48a1dcd939d50c6f763032afe146e [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 Tarreau9a42c0d2009-09-22 19:31:03 +020066 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020067
Willy Tarreau6162db22009-10-10 17:13:00 +020068const char stats_permission_denied_msg[] =
69 "Permission denied\n"
70 "";
71
Willy Tarreaufbee7132007-10-18 13:53:22 +020072/* This function parses a "stats" statement in the "global" section. It returns
73 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
74 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
75 * zero included. The trailing '\n' must not be written. The function must be
76 * called with <args> pointing to the first word after "stats".
77 */
Willy Tarreau10522fd2008-07-09 20:12:41 +020078static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
79 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +020080{
Willy Tarreau10522fd2008-07-09 20:12:41 +020081 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +020082 if (!strcmp(args[0], "socket")) {
83 struct sockaddr_un su;
84 int cur_arg;
85
86 if (*args[1] == 0) {
87 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
88 return -1;
89 }
90
91 if (global.stats_sock.state != LI_NEW) {
92 snprintf(err, errlen, "'stats socket' already specified in global section");
93 return -1;
94 }
95
96 su.sun_family = AF_UNIX;
97 strncpy(su.sun_path, args[1], sizeof(su.sun_path));
98 su.sun_path[sizeof(su.sun_path) - 1] = 0;
99 memcpy(&global.stats_sock.addr, &su, sizeof(su)); // guaranteed to fit
100
Willy Tarreau89a63132009-08-16 17:41:45 +0200101 if (!global.stats_fe) {
102 if ((global.stats_fe = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) {
103 snprintf(err, errlen, "out of memory");
104 return -1;
105 }
106
107 LIST_INIT(&global.stats_fe->pendconns);
108 LIST_INIT(&global.stats_fe->acl);
109 LIST_INIT(&global.stats_fe->block_cond);
110 LIST_INIT(&global.stats_fe->redirect_rules);
111 LIST_INIT(&global.stats_fe->mon_fail_cond);
112 LIST_INIT(&global.stats_fe->switching_rules);
113 LIST_INIT(&global.stats_fe->tcp_req.inspect_rules);
114
115 /* Timeouts are defined as -1, so we cannot use the zeroed area
116 * as a default value.
117 */
118 proxy_reset_timeouts(global.stats_fe);
119
120 global.stats_fe->last_change = now.tv_sec;
121 global.stats_fe->id = strdup("GLOBAL");
122 global.stats_fe->cap = PR_CAP_FE;
123 }
124
Willy Tarreaufbee7132007-10-18 13:53:22 +0200125 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100126 global.stats_sock.options = LI_O_NONE;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200127 global.stats_sock.accept = uxst_event_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200128 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200129 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200130 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreau89a63132009-08-16 17:41:45 +0200131 global.stats_sock.private = global.stats_fe; /* must point to the frontend */
Willy Tarreau6162db22009-10-10 17:13:00 +0200132 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau89a63132009-08-16 17:41:45 +0200133
134 global.stats_fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
135 global.stats_sock.timeout = &global.stats_fe->timeout.client;
136
137 global.stats_sock.next = global.stats_fe->listen;
138 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200139
140 cur_arg = 2;
141 while (*args[cur_arg]) {
142 if (!strcmp(args[cur_arg], "uid")) {
143 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
144 cur_arg += 2;
145 }
146 else if (!strcmp(args[cur_arg], "gid")) {
147 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
148 cur_arg += 2;
149 }
150 else if (!strcmp(args[cur_arg], "mode")) {
151 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
152 cur_arg += 2;
153 }
154 else if (!strcmp(args[cur_arg], "user")) {
155 struct passwd *user;
156 user = getpwnam(args[cur_arg + 1]);
157 if (!user) {
158 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
159 args[cur_arg + 1]);
160 return -1;
161 }
162 global.stats_sock.perm.ux.uid = user->pw_uid;
163 cur_arg += 2;
164 }
165 else if (!strcmp(args[cur_arg], "group")) {
166 struct group *group;
167 group = getgrnam(args[cur_arg + 1]);
168 if (!group) {
169 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
170 args[cur_arg + 1]);
171 return -1;
172 }
173 global.stats_sock.perm.ux.gid = group->gr_gid;
174 cur_arg += 2;
175 }
Willy Tarreau6162db22009-10-10 17:13:00 +0200176 else if (!strcmp(args[cur_arg], "level")) {
177 if (!strcmp(args[cur_arg+1], "user"))
178 global.stats_sock.perm.ux.level = ACCESS_LVL_USER;
179 else if (!strcmp(args[cur_arg+1], "operator"))
180 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER;
181 else if (!strcmp(args[cur_arg+1], "admin"))
182 global.stats_sock.perm.ux.level = ACCESS_LVL_ADMIN;
183 else {
184 snprintf(err, errlen, "'stats socket level' only supports 'user', 'operator', and 'admin'");
185 return -1;
186 }
187 cur_arg += 2;
188 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200189 else {
Willy Tarreau6162db22009-10-10 17:13:00 +0200190 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', 'level', and 'mode'");
Willy Tarreaufbee7132007-10-18 13:53:22 +0200191 return -1;
192 }
193 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100194
Willy Tarreaufbee7132007-10-18 13:53:22 +0200195 uxst_add_listener(&global.stats_sock);
196 global.maxsock++;
197 }
198 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100199 unsigned timeout;
200 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
201
202 if (res) {
203 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
204 return -1;
205 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200206
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100207 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200208 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
209 return -1;
210 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200211 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200212 }
213 else if (!strcmp(args[0], "maxconn")) {
214 int maxconn = atol(args[1]);
215
216 if (maxconn <= 0) {
217 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
218 return -1;
219 }
220 global.maxsock -= global.stats_sock.maxconn;
221 global.stats_sock.maxconn = maxconn;
222 global.maxsock += global.stats_sock.maxconn;
223 }
224 else {
225 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
226 return -1;
227 }
228 return 0;
229}
230
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200231int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100232{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200233 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100234 "# pxname,svname,"
235 "qcur,qmax,"
236 "scur,smax,slim,stot,"
237 "bin,bout,"
238 "dreq,dresp,"
239 "ereq,econ,eresp,"
240 "wretr,wredis,"
241 "status,weight,act,bck,"
242 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200243 "pid,iid,sid,throttle,lbtot,tracked,type,"
244 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200245 "check_status,check_code,check_duration,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100246 "\n");
247}
248
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200249/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200250 * called from an applet running in a stream interface. The function returns 1
251 * if the request was understood, otherwise zero. It sets si->st0 to a value
252 * designating the function which will have to process the request.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200253 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200254int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200255{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200256 struct session *s = si->private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200257 char *args[MAX_STATS_ARGS + 1];
258 int arg;
259
260 while (isspace((unsigned char)*line))
261 line++;
262
263 arg = 0;
264 args[arg] = line;
265
266 while (*line && arg < MAX_STATS_ARGS) {
267 if (isspace((unsigned char)*line)) {
268 *line++ = '\0';
269
270 while (isspace((unsigned char)*line))
271 line++;
272
273 args[++arg] = line;
274 continue;
275 }
276
277 line++;
278 }
279
280 while (++arg <= MAX_STATS_ARGS)
281 args[arg] = line;
282
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200283 s->data_ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200284 if (strcmp(args[0], "show") == 0) {
285 if (strcmp(args[1], "stat") == 0) {
286 if (*args[2] && *args[3] && *args[4]) {
287 s->data_ctx.stats.flags |= STAT_BOUND;
288 s->data_ctx.stats.iid = atoi(args[2]);
289 s->data_ctx.stats.type = atoi(args[3]);
290 s->data_ctx.stats.sid = atoi(args[4]);
291 }
292
293 s->data_ctx.stats.flags |= STAT_SHOW_STAT;
294 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200295 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200296 si->st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200297 }
298 else if (strcmp(args[1], "info") == 0) {
299 s->data_ctx.stats.flags |= STAT_SHOW_INFO;
300 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200301 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200302 si->st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200303 }
304 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200305 s->data_state = DATA_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200306 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200307 buffer_feed(si->ib, stats_permission_denied_msg);
Willy Tarreau6162db22009-10-10 17:13:00 +0200308 return 1;
309 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200310 si->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200311 }
312 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200313 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200314 buffer_feed(si->ib, stats_permission_denied_msg);
Willy Tarreau6162db22009-10-10 17:13:00 +0200315 return 1;
316 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200317 if (*args[2])
318 s->data_ctx.errors.iid = atoi(args[2]);
319 else
320 s->data_ctx.errors.iid = -1;
321 s->data_ctx.errors.px = NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200322 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200323 si->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200324 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200325 else { /* neither "stat" nor "info" nor "sess" nor "errors"*/
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200326 return 0;
327 }
328 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200329 else if (strcmp(args[0], "clear") == 0) {
330 if (strcmp(args[1], "counters") == 0) {
331 struct proxy *px;
332 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200333 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200334 int clrall = 0;
335
336 if (strcmp(args[2], "all") == 0)
337 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200338
Willy Tarreau6162db22009-10-10 17:13:00 +0200339 /* check permissions */
340 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
341 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200342 buffer_feed(si->ib, stats_permission_denied_msg);
Willy Tarreau6162db22009-10-10 17:13:00 +0200343 return 1;
344 }
345
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200346 for (px = proxy; px; px = px->next) {
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200347 if (clrall)
348 memset(&px->counters, 0, sizeof(px->counters));
349 else {
350 px->counters.feconn_max = 0;
351 px->counters.beconn_max = 0;
352 px->counters.fe_sps_max = 0;
353 px->counters.be_sps_max = 0;
354 px->counters.nbpend_max = 0;
355 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200356
357 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200358 if (clrall)
359 memset(&sv->counters, 0, sizeof(sv->counters));
360 else {
361 sv->counters.cur_sess_max = 0;
362 sv->counters.nbpend_max = 0;
363 sv->counters.sps_max = 0;
364 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200365
366 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200367 if (li->counters) {
368 if (clrall)
369 memset(li->counters, 0, sizeof(*li->counters));
370 else
371 li->counters->conn_max = 0;
372 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200373 }
374
375 return 1;
376 }
377 else {
378 return 0;
379 }
380 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200381 else if (strcmp(args[0], "get") == 0) {
382 if (strcmp(args[1], "weight") == 0) {
383 struct proxy *px;
384 struct server *sv;
385
386 /* split "backend/server" and make <line> point to server */
387 for (line = args[2]; *line; line++)
388 if (*line == '/') {
389 *line++ = '\0';
390 break;
391 }
392
393 if (!*line) {
394 buffer_feed(si->ib, "Require 'backend/server'.\n");
395 return 1;
396 }
397
398 if (!get_backend_server(args[2], line, &px, &sv)) {
399 if (!px)
400 buffer_feed(si->ib, "No such backend.\n");
401 else
402 buffer_feed(si->ib, "No such server.\n");
403 return 1;
404 }
405
406 /* return server's effective weight at the moment */
407 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
408 buffer_feed(si->ib, trash);
409 return 1;
410 }
411 else { /* not "get weight" */
412 return 0;
413 }
414 }
415 else { /* not "show" nor "clear" nor "get" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200416 return 0;
417 }
418 return 1;
419}
420
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200421/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200422 * used to processes I/O from/to the stats unix socket. The system relies on a
423 * state machine handling requests and various responses. We read a request,
424 * then we process it and send the response, and we possibly display a prompt.
425 * Then we can read again. The state is stored in si->st0 and is one of the
426 * STAT_CLI_* constants. si->st1 is used to indicate whether prompt is enabled
427 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200428 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200429void stats_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200430{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200431 struct session *s = si->private;
432 struct buffer *req = si->ob;
433 struct buffer *res = si->ib;
434 int reql;
435 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200436
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200437 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
438 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200439
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200440 while (1) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200441 if (si->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200442 /* Stats output not initialized yet */
443 memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats));
444 s->data_source = DATA_SRC_STATS;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200445 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200446 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200447 else if (si->st0 == STAT_CLI_END) {
448 /* Let's close for real now. We just close the request
449 * side, the conditions below will complete if needed.
450 */
451 si->shutw(si);
452 break;
453 }
454 else if (si->st0 == STAT_CLI_GETREQ) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200455 reql = buffer_si_peekline(si->ob, trash, sizeof(trash));
456 if (reql <= 0) { /* closed or EOL not found */
457 if (reql == 0)
458 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200459 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200460 continue;
461 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200462
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200463 /* seek for a possible semi-colon. If we find one, we
464 * replace it with an LF and skip only this part.
465 */
466 for (len = 0; len < reql; len++)
467 if (trash[len] == ';') {
468 trash[len] = '\n';
469 reql = len + 1;
470 break;
471 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200472
Willy Tarreau816fc222009-10-04 07:36:58 +0200473 /* now it is time to check that we have a full line,
474 * remove the trailing \n and possibly \r, then cut the
475 * line.
476 */
477 len = reql - 1;
478 if (trash[len] != '\n') {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200479 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200480 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200481 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200482
Willy Tarreau816fc222009-10-04 07:36:58 +0200483 if (len && trash[len-1] == '\r')
484 len--;
485
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200486 trash[len] = '\0';
487
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200488 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200489 if (len) {
490 if (strcmp(trash, "quit") == 0) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200491 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200492 continue;
493 }
494 else if (strcmp(trash, "prompt") == 0)
495 si->st1 = !si->st1;
496 else if (strcmp(trash, "help") == 0 ||
497 !stats_sock_parse_request(si, trash))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200498 si->st0 = STAT_CLI_O_HELP;
499 /* NB: stats_sock_parse_request() may have put
500 * another STAT_CLI_O_* into si->st0.
501 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200502 }
503 else if (!si->st1) {
504 /* if prompt is disabled, print help on empty lines,
505 * so that the user at least knows how to enable
506 * prompt and find help.
507 */
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200508 si->st0 = STAT_CLI_O_HELP;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200509 }
510
511 /* re-adjust req buffer */
512 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200513 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200514 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200515 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200516 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200517 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200518 continue;
519 }
520
521 switch (si->st0) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200522 case STAT_CLI_O_HELP:
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200523 if (buffer_feed(si->ib, stats_sock_usage_msg) < 0)
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200524 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200525 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200526 case STAT_CLI_O_INFO:
Willy Tarreau24955a12009-10-04 11:54:04 +0200527 if (stats_dump_raw_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200528 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200529 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200530 case STAT_CLI_O_SESS:
Willy Tarreau7e72a8f2009-10-03 23:55:14 +0200531 if (stats_dump_sess_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200532 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200533 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200534 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau61b34732009-10-03 23:49:35 +0200535 if (stats_dump_errors_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200536 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200537 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200538 default: /* abnormal state */
539 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200540 break;
541 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200542
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200543 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
544 if (si->st0 == STAT_CLI_PROMPT) {
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200545 if (buffer_feed(si->ib, si->st1 ? "\n> " : "\n") < 0)
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200546 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200547 }
548
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200549 /* If the output functions are still there, it means they require more room. */
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200550 if (si->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200551 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200552
553 /* Now we close the output if one of the writers did so,
554 * or if we're not in interactive mode and the request
555 * buffer is empty. This still allows pipelined requests
556 * to be sent in non-interactive mode.
557 */
558 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->st1 && !req->send_max)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200559 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200560 continue;
561 }
562
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200563 /* switch state back to GETREQ to read next requests */
564 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200565 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200566 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200567
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200568 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200569 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
570 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
571 /* Other size has closed, let's abort if we have no more processing to do
572 * and nothing more to consume. This is comparable to a broken pipe, so
573 * we forward the close to the request side so that it flows upstream to
574 * the client.
575 */
576 si->shutw(si);
577 }
578
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200579 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200580 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
581 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
582 /* We have no more processing to do, and nothing more to send, and
583 * the client side has closed. So we'll forward this state downstream
584 * on the response buffer.
585 */
586 si->shutr(si);
587 res->flags |= BF_READ_NULL;
588 }
589
590 /* update all other flags and resync with the other side */
591 si->update(si);
592
593 /* we don't want to expire timeouts while we're processing requests */
594 si->ib->rex = TICK_ETERNITY;
595 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200596
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200597 out:
598 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
599 __FUNCTION__, __LINE__,
600 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
601
602 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
603 /* check that we have released everything then unregister */
604 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200605 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200606}
607
Willy Tarreau24955a12009-10-04 11:54:04 +0200608/* This function is called to send output to the response buffer.
609 * It dumps statistics onto the output buffer <rep> owned by session <s>.
610 * s->data_ctx must have been zeroed first, and the flags properly set.
611 * It returns 0 as long as it does not complete, non-zero upon completion.
612 * Some states are not used but it makes the code more similar to other
613 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200614 */
Willy Tarreau24955a12009-10-04 11:54:04 +0200615int stats_dump_raw_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3e76e722007-10-17 18:57:38 +0200616{
Willy Tarreau3e76e722007-10-17 18:57:38 +0200617 struct proxy *px;
618 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +0100619 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200620
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200621 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +0200622
623 switch (s->data_state) {
624 case DATA_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +0200625 /* the function had not been called yet */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200626 s->data_state = DATA_ST_HEAD;
627 /* fall through */
628
629 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100630 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200631 print_csv_header(&msg);
Willy Tarreau24955a12009-10-04 11:54:04 +0200632 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100633 return 0;
634 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200635
636 s->data_state = DATA_ST_INFO;
637 /* fall through */
638
639 case DATA_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +0100640 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100641 if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200642 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +0100643 "Name: " PRODUCT_NAME "\n"
644 "Version: " HAPROXY_VERSION "\n"
645 "Release_date: " HAPROXY_DATE "\n"
646 "Nbproc: %d\n"
647 "Process_num: %d\n"
648 "Pid: %d\n"
649 "Uptime: %dd %dh%02dm%02ds\n"
650 "Uptime_sec: %d\n"
651 "Memmax_MB: %d\n"
652 "Ulimit-n: %d\n"
653 "Maxsock: %d\n"
654 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100655 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100656 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100657 "PipesUsed: %d\n"
658 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100659 "Tasks: %d\n"
660 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200661 "node: %s\n"
662 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100663 "",
664 global.nbproc,
665 relative_pid,
666 pid,
667 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
668 up,
669 global.rlimit_memmax,
670 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100671 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100672 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200673 nb_tasks_cur, run_queue_cur,
674 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +0100675 );
Willy Tarreau24955a12009-10-04 11:54:04 +0200676 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100677 return 0;
678 }
679
Willy Tarreau3e76e722007-10-17 18:57:38 +0200680 s->data_ctx.stats.px = proxy;
681 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100682
683 s->data_ctx.stats.sv = NULL;
684 s->data_ctx.stats.sv_st = 0;
685
Willy Tarreau3e76e722007-10-17 18:57:38 +0200686 s->data_state = DATA_ST_LIST;
687 /* fall through */
688
689 case DATA_ST_LIST:
690 /* dump proxies */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100691 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100692 while (s->data_ctx.stats.px) {
693 px = s->data_ctx.stats.px;
694 /* skip the disabled proxies and non-networked ones */
695 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +0200696 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100697 if (stats_dump_proxy(s, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100698 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +0200699 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200700
Willy Tarreaua8efd362008-01-03 10:19:15 +0100701 s->data_ctx.stats.px = px->next;
702 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
703 }
704 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200705 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200706
707 s->data_state = DATA_ST_END;
708 /* fall through */
709
710 case DATA_ST_END:
711 s->data_state = DATA_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +0100712 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200713
714 case DATA_ST_FIN:
715 return 1;
716
717 default:
718 /* unknown state ! */
Willy Tarreau24955a12009-10-04 11:54:04 +0200719 s->data_state = DATA_ST_FIN;
720 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200721 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100722}
723
724
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200725/* This I/O handler runs as an applet embedded in a stream interface. It is
726 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
727 * si->st0 becomes non-zero once the transfer is finished. The handler
728 * automatically unregisters itself once transfer is complete.
729 */
730void http_stats_io_handler(struct stream_interface *si)
731{
732 struct session *s = si->private;
733 struct buffer *req = si->ob;
734 struct buffer *res = si->ib;
735
736 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
737 goto out;
738
739 /* check that the output is not closed */
740 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
741 si->st0 = 1;
742
743 if (!si->st0) {
744 if (stats_dump_http(s, res, s->be->uri_auth)) {
745 si->st0 = 1;
746 si->shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200747 }
748 }
749
750 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
751 si->shutw(si);
752
753 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->st0) {
754 si->shutr(si);
755 res->flags |= BF_READ_NULL;
756 }
757
758 /* update all other flags and resync with the other side */
759 si->update(si);
760
761 /* we don't want to expire timeouts while we're processing requests */
762 si->ib->rex = TICK_ETERNITY;
763 si->ob->wex = TICK_ETERNITY;
764
765 out:
766 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
767 /* check that we have released everything then unregister */
768 stream_int_unregister_handler(si);
769 }
770}
771
772
Willy Tarreau3e76e722007-10-17 18:57:38 +0200773/*
774 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200775 * client socket shut down on input. It stops by itself by unsetting the
Willy Tarreau72b179a2008-08-28 16:01:32 +0200776 * BF_HIJACK flag from the buffer, which it uses to keep on being called
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200777 * when there is free space in the buffer, of simply by letting an empty buffer
778 * upon return.s->data_ctx must have been zeroed before the first call, and the
779 * flags set. It returns 0 if it had to stop writing data and an I/O is needed,
780 * 1 if the dump is finished and the session must be closed, or -1 in case of
781 * any error.
Willy Tarreau91861262007-10-17 17:06:05 +0200782 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100783int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200784{
Willy Tarreau91861262007-10-17 17:06:05 +0200785 struct proxy *px;
786 struct chunk msg;
787 unsigned int up;
788
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200789 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +0200790
791 switch (s->data_state) {
792 case DATA_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200793 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200794 "HTTP/1.0 200 OK\r\n"
795 "Cache-Control: no-cache\r\n"
796 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200797 "Content-Type: %s\r\n",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100798 (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +0200799
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100800 if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200801 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +0200802 uri->refresh);
803
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200804 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +0200805
806 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200807 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +0200808 return 0;
809
Willy Tarreau91861262007-10-17 17:06:05 +0200810 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
811 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
812 if (!(s->flags & SN_FINST_MASK))
813 s->flags |= SN_FINST_R;
814
815 if (s->txn.meth == HTTP_METH_HEAD) {
816 /* that's all we return in case of HEAD request */
817 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +0200818 return 1;
819 }
820
821 s->data_state = DATA_ST_HEAD; /* let's start producing data */
822 /* fall through */
823
824 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100825 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200826 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200827 chunk_printf(&msg,
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200828 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200829 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
830 "<style type=\"text/css\"><!--\n"
831 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +0200832 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +0200833 " font-size: 12px;"
834 " font-weight: normal;"
835 " color: black;"
836 " background: white;"
837 "}\n"
838 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +0200839 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +0200840 " align: center;"
841 "}\n"
842 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +0200843 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +0200844 " margin-bottom: 0.5em;"
845 "}\n"
846 "h2 {"
847 " font-family: helvetica, arial;"
848 " font-size: x-large;"
849 " font-weight: bold;"
850 " font-style: italic;"
851 " color: #6020a0;"
852 " margin-top: 0em;"
853 " margin-bottom: 0em;"
854 "}\n"
855 "h3 {"
856 " font-family: helvetica, arial;"
857 " font-size: 16px;"
858 " font-weight: bold;"
859 " color: #b00040;"
860 " background: #e8e8d0;"
861 " margin-top: 0em;"
862 " margin-bottom: 0em;"
863 "}\n"
864 "li {"
865 " margin-top: 0.25em;"
866 " margin-right: 2em;"
867 "}\n"
868 ".hr {margin-top: 0.25em;"
869 " border-color: black;"
870 " border-bottom-style: solid;"
871 "}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200872 ".titre {background: #20D0D0;color: #000000; font-weight: bold;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200873 ".total {background: #20D0D0;color: #ffff80;}\n"
874 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200875 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200876 ".backend {background: #e8e8d0;}\n"
877 ".active0 {background: #ff9090;}\n"
878 ".active1 {background: #ffd020;}\n"
879 ".active2 {background: #ffffa0;}\n"
880 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100881 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
882 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
883 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200884 ".backup0 {background: #ff9090;}\n"
885 ".backup1 {background: #ff80ff;}\n"
886 ".backup2 {background: #c060ff;}\n"
887 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100888 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
889 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
890 ".backup6 {background: #e0e0e0;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +0200891 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Willy Tarreau91861262007-10-17 17:06:05 +0200892 "table.tbl { border-collapse: collapse; border-style: none;}\n"
893 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray;}\n"
894 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzki619caca2009-10-03 15:46:08 +0200895 "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 +0200896 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200897 "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 +0200898 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
899 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
900 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
901 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200902 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200903 (uri->flags&ST_SHNODE) ? " on " : "",
904 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200905 );
Willy Tarreau55bb8452007-10-17 18:44:57 +0200906 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200907 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200908 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200909 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200910 return 0;
911
912 s->data_state = DATA_ST_INFO;
913 /* fall through */
914
915 case DATA_ST_INFO:
916 up = (now.tv_sec - start_date.tv_sec);
917
918 /* WARNING! this has to fit the first packet too.
919 * We are around 3.5 kB, add adding entries will
920 * become tricky if we want to support 4kB buffers !
921 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100922 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200923 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200924 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
925 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200926 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200927 "<hr width=\"100%%\" class=\"hr\">\n"
928 "<h3>&gt; General process information</h3>\n"
929 "<table border=0 cols=4><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100930 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200931 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200932 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
933 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
934 "current conns = %d; current pipes = %d/%d<br>\n"
935 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200936 "</td><td align=\"center\" nowrap>\n"
937 "<table class=\"lgd\"><tr>\n"
938 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
939 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
940 "</tr><tr>\n"
941 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
942 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
943 "</tr><tr>\n"
944 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
945 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
946 "</tr><tr>\n"
947 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100948 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200949 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100950 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +0200951 "</td>"
952 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
953 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
954 "",
955 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200956 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
957 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
958 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +0200959 up / 86400, (up % 86400) / 3600,
960 (up % 3600) / 60, (up % 60),
961 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
962 global.rlimit_memmax ? " MB" : "",
963 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100964 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100965 actconn, pipes_used, pipes_used+pipes_free,
966 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +0200967 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100968
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100969 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200970 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200971 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
972 uri->uri_prefix,
973 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100974 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200975 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200976 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200977 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
978 uri->uri_prefix,
979 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100980 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200981
Willy Tarreau55bb8452007-10-17 18:44:57 +0200982 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100983 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200984 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200985 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
986 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100987 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200988 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200989 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200990 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200991 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
992 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100993 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200994 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200995 }
Willy Tarreau91861262007-10-17 17:06:05 +0200996
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200997 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200998 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
999 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001000 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
1001 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001002
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001003 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02001004 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
1005 uri->uri_prefix,
1006 (uri->refresh > 0) ? ";norefresh" : "");
1007
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001008 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001009 "</td>"
1010 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1011 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
1012 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
1013 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
1014 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
1015 "</ul>"
1016 "</td>"
1017 "</tr></table>\n"
1018 ""
1019 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001020
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001021 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001022 return 0;
1023 }
Willy Tarreau91861262007-10-17 17:06:05 +02001024
Willy Tarreau91861262007-10-17 17:06:05 +02001025 s->data_ctx.stats.px = proxy;
1026 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1027 s->data_state = DATA_ST_LIST;
1028 /* fall through */
1029
1030 case DATA_ST_LIST:
1031 /* dump proxies */
1032 while (s->data_ctx.stats.px) {
1033 px = s->data_ctx.stats.px;
1034 /* skip the disabled proxies and non-networked ones */
1035 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001036 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001037 return 0;
1038
1039 s->data_ctx.stats.px = px->next;
1040 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1041 }
1042 /* here, we just have reached the last proxy */
1043
1044 s->data_state = DATA_ST_END;
1045 /* fall through */
1046
1047 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001048 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001049 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001050 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001051 return 0;
1052 }
Willy Tarreau91861262007-10-17 17:06:05 +02001053
1054 s->data_state = DATA_ST_FIN;
1055 /* fall through */
1056
1057 case DATA_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02001058 return 1;
1059
1060 default:
1061 /* unknown state ! */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001062 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001063 return -1;
1064 }
1065}
1066
1067
1068/*
1069 * Dumps statistics for a proxy.
1070 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1071 * ot non-zero if everything completed.
1072 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001073int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001074{
1075 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001076 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001077 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02001078 struct chunk msg;
1079
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001080 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001081
1082 switch (s->data_ctx.stats.px_st) {
1083 case DATA_ST_PX_INIT:
1084 /* we are on a new proxy */
1085
1086 if (uri && uri->scope) {
1087 /* we have a limited scope, we have to check the proxy name */
1088 struct stat_scope *scope;
1089 int len;
1090
1091 len = strlen(px->id);
1092 scope = uri->scope;
1093
1094 while (scope) {
1095 /* match exact proxy name */
1096 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1097 break;
1098
1099 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001100 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001101 break;
1102 scope = scope->next;
1103 }
1104
1105 /* proxy name not found : don't dump anything */
1106 if (scope == NULL)
1107 return 1;
1108 }
1109
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001110 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001111 (px->uuid != s->data_ctx.stats.iid))
1112 return 1;
1113
Willy Tarreau91861262007-10-17 17:06:05 +02001114 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
1115 /* fall through */
1116
1117 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001118 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001119 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001120 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001121 "<table class=\"tbl\" width=\"100%%\">\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001122 "<tr align=\"center\" class=\"titre\">"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001123 "<th class=\"pxname\" width=\"10%%\">%s</th>"
1124 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001125 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001126 "</table>\n"
1127 "<table cols=\"29\" class=\"tbl\" width=\"100%%\">\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001128 "<tr align=\"center\" class=\"titre\">"
1129 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001130 "<th colspan=3>Queue</th>"
1131 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001132 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001133 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001134 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001135 "</tr>\n"
1136 "<tr align=\"center\" class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001137 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001138 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001139 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001140 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001141 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001142 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001143 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1144 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001145 "</tr>",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001146 px->id,
1147 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001148
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_ctx.stats.px_st = DATA_ST_PX_FE;
1154 /* fall through */
1155
1156 case DATA_ST_PX_FE:
1157 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001158 if ((px->cap & PR_CAP_FE) &&
1159 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
1160 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001161 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001162 /* name, queue */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001163 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=3></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001164 /* sessions rate : current, max, limit */
1165 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
1166 /* sessions : current, max, limit, total, lbtot */
1167 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001168 "<td align=right>%s</td><td align=right></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001169 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001170 "<td align=right>%s</td><td align=right>%s</td>"
1171 "",
Willy Tarreaua3e49422009-05-10 19:19:41 +02001172 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001173 U2H1(px->counters.fe_sps_max), LIM2A2(px->fe_sps_lim, "-"),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001174 U2H3(px->feconn), U2H4(px->counters.feconn_max), U2H5(px->maxconn),
1175 U2H6(px->counters.cum_feconn), U2H7(px->counters.bytes_in), U2H8(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001176
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001177 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001178 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001179 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001180 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001181 "<td align=right>%s</td><td align=right></td><td align=right></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001182 /* warnings: retries, redispatches */
1183 "<td align=right></td><td align=right></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001184 /* server status : reflect frontend status */
Willy Tarreau91861262007-10-17 17:06:05 +02001185 "<td align=center>%s</td>"
1186 /* rest of server: nothing */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001187 "<td align=center colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02001188 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001189 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
1190 U2H2(px->counters.failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +02001191 px->state == PR_STRUN ? "OPEN" :
1192 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001193 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001194 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001195 /* pxid, name, queue cur, queue max, */
1196 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001197 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001198 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001199 /* bytes : in, out */
1200 "%lld,%lld,"
1201 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001202 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001203 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001204 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001205 /* warnings: retries, redispatches */
1206 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001207 /* server status : reflect frontend status */
1208 "%s,"
1209 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001210 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001211 /* pid, iid, sid, throttle, lbtot, tracked, type */
1212 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001213 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001214 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001215 /* check_status, check_code, check_duration */
1216 ",,,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001217 "\n",
1218 px->id,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001219 px->feconn, px->counters.feconn_max, px->maxconn, px->counters.cum_feconn,
1220 px->counters.bytes_in, px->counters.bytes_out,
1221 px->counters.denied_req, px->counters.denied_resp,
1222 px->counters.failed_req,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001223 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +01001224 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01001225 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001226 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001227 px->fe_sps_lim, px->counters.fe_sps_max);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001228 }
Willy Tarreau91861262007-10-17 17:06:05 +02001229
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001230 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001231 return 0;
1232 }
1233
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001234 s->data_ctx.stats.l = px->listen; /* may be NULL */
1235 s->data_ctx.stats.px_st = DATA_ST_PX_LI;
1236 /* fall through */
1237
1238 case DATA_ST_PX_LI:
1239 /* stats.l has been initialized above */
1240 for (; s->data_ctx.stats.l != NULL; s->data_ctx.stats.l = l->next) {
1241 l = s->data_ctx.stats.l;
1242 if (!l->counters)
1243 continue;
1244
1245 if (s->data_ctx.stats.flags & STAT_BOUND) {
1246 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SO)))
1247 break;
1248
1249 if (s->data_ctx.stats.sid != -1 && l->luid != s->data_ctx.stats.sid)
1250 continue;
1251 }
1252
1253 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
1254 chunk_printf(&msg,
1255 /* name, queue */
1256 "<tr align=center class=\"socket\"><td>%s</td><td colspan=3></td>"
1257 /* sessions rate: current, max, limit */
1258 "<td align=right colspan=3>&nbsp;</td>"
1259 /* sessions: current, max, limit, total, lbtot */
1260 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
1261 "<td align=right>%s</td><td align=right>&nbsp;</td>"
1262 /* bytes: in, out */
1263 "<td align=right>%s</td><td align=right>%s</td>"
1264 "",
1265 l->name,
1266 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
1267 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
1268
1269 chunk_printf(&msg,
1270 /* denied: req, resp */
1271 "<td align=right>%s</td><td align=right>%s</td>"
1272 /* errors: request, connect, response */
1273 "<td align=right>%s</td><td align=right></td><td align=right></td>"
1274 /* warnings: retries, redispatches */
1275 "<td align=right></td><td align=right></td>"
1276 /* server status: reflect listener status */
1277 "<td align=center>%s</td>"
1278 /* rest of server: nothing */
1279 "<td align=center colspan=8></td></tr>"
1280 "",
1281 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
1282 U2H2(l->counters->failed_req),
1283 (l->nbconn < l->maxconn) ? "OPEN" : "FULL");
1284 } else {
1285 chunk_printf(&msg,
1286 /* pxid, name, queue cur, queue max, */
1287 "%s,%s,,,"
1288 /* sessions: current, max, limit, total */
1289 "%d,%d,%d,%lld,"
1290 /* bytes: in, out */
1291 "%lld,%lld,"
1292 /* denied: req, resp */
1293 "%lld,%lld,"
1294 /* errors: request, connect, response */
1295 "%lld,,,"
1296 /* warnings: retries, redispatches */
1297 ",,"
1298 /* server status: reflect listener status */
1299 "%s,"
1300 /* rest of server: nothing */
1301 ",,,,,,,,"
1302 /* pid, iid, sid, throttle, lbtot, tracked, type */
1303 "%d,%d,%d,,,,%d,"
1304 /* rate, rate_lim, rate_max */
1305 ",,,"
1306 /* check_status, check_code, check_duration */
1307 ",,,"
1308 "\n",
1309 px->id, l->name,
1310 l->nbconn, l->counters->conn_max,
1311 l->maxconn, l->counters->cum_conn,
1312 l->counters->bytes_in, l->counters->bytes_out,
1313 l->counters->denied_req, l->counters->denied_resp,
1314 l->counters->failed_req,
1315 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
1316 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
1317 }
1318
1319 if (buffer_feed_chunk(rep, &msg) >= 0)
1320 return 0;
1321 }
1322
Willy Tarreau91861262007-10-17 17:06:05 +02001323 s->data_ctx.stats.sv = px->srv; /* may be NULL */
1324 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
1325 /* fall through */
1326
1327 case DATA_ST_PX_SV:
1328 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001329 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
1330
Willy Tarreau2ea81932007-11-30 12:04:38 +01001331 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 +02001332
1333 sv = s->data_ctx.stats.sv;
1334
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001335 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001336 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
1337 break;
1338
1339 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
1340 continue;
1341 }
1342
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001343 if (sv->tracked)
1344 svs = sv->tracked;
1345 else
1346 svs = sv;
1347
Willy Tarreau91861262007-10-17 17:06:05 +02001348 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001349 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01001350 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001351 else if (svs->state & SRV_RUNNING) {
1352 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02001353 sv_state = 3; /* UP */
1354 else
1355 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01001356
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001357 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01001358 sv_state += 2;
1359 }
Willy Tarreau91861262007-10-17 17:06:05 +02001360 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001361 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02001362 sv_state = 1; /* going up */
1363 else
1364 sv_state = 0; /* DOWN */
1365
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001366 if ((sv_state == 0) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02001367 /* do not report servers which are DOWN */
1368 s->data_ctx.stats.sv = sv->next;
1369 continue;
1370 }
1371
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001372 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001373 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
1374 "UP %d/%d &darr;", "UP",
1375 "NOLB %d/%d &darr;", "NOLB",
1376 "<i>no check</i>" };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001377 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001378 /* name */
1379 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001380 /* queue : current, max, limit */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001381 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001382 /* sessions rate : current, max, limit */
1383 "<td align=right>%s</td><td align=right>%s</td><td align=right></td>"
1384 /* sessions : current, max, limit, total, lbtot */
1385 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001386 "<td align=right>%s</td><td align=right>%s</td>"
1387 "",
1388 (sv->state & SRV_BACKUP) ? "backup" : "active",
1389 sv_state, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001390 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
1391 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
1392 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001393 U2H8(sv->counters.cum_sess), U2H9(sv->counters.cum_lbconn));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001394
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001395 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001396 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001397 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001398 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001399 "<td align=right></td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001400 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001401 "<td align=right></td><td align=right>%s</td><td align=right>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001402 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001403 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001404 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001405 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
1406 U2H2(sv->counters.failed_secu),
1407 U2H3(sv->counters.failed_conns), U2H4(sv->counters.failed_resp),
1408 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001409
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001410 /* status, lest check */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001411 chunk_printf(&msg, "<td nowrap>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001412
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001413 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001414 chunk_printf(&msg, "%s ",
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001415 human_time(now.tv_sec - sv->last_change, 1));
1416
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001417 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001418 srv_hlt_st[sv_state],
1419 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
1420 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
1421
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001422 chunk_printf(&msg, "</td><td title=\"%s\" nowrap> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001423 get_check_status_description(sv->check_status),
1424 tv_iszero(&sv->check_start)?"":"* ",
1425 get_check_status_info(sv->check_status));
1426
1427 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001428 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001429
1430 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001431 chunk_printf(&msg, " in %lums", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001432 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001433 chunk_printf(&msg, "</td><td>");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001434 }
Willy Tarreau91861262007-10-17 17:06:05 +02001435
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001436 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001437 /* weight */
1438 "</td><td>%d</td>"
1439 /* act, bck */
1440 "<td>%s</td><td>%s</td>"
1441 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001442 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02001443 (sv->state & SRV_BACKUP) ? "-" : "Y",
1444 (sv->state & SRV_BACKUP) ? "Y" : "-");
1445
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001446 /* check failures: unique, fatal, down time */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001447 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001448 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02001449 "<td align=right>%lld</td><td align=right>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001450 "<td nowrap align=right>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001451 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001452 svs->counters.failed_checks, svs->counters.down_trans,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001453 human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001454 else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001455 chunk_printf(&msg,
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001456 "<td nowrap colspan=3>via %s/%s</td>", svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001457 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001458 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001459 "<td colspan=3></td>");
1460
1461 /* throttle */
1462 if ((sv->state & SRV_WARMINGUP) &&
1463 now.tv_sec < sv->last_change + sv->slowstart &&
1464 now.tv_sec >= sv->last_change) {
1465 unsigned int ratio;
1466 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001467 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001468 "<td>%d %%</td></tr>\n", ratio);
1469 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001470 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001471 "<td>-</td></tr>\n");
1472 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02001473 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001474 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
1475 "UP %d/%d,", "UP,",
1476 "NOLB %d/%d,", "NOLB,",
1477 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001478 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001479 /* pxid, name */
1480 "%s,%s,"
1481 /* queue : current, max */
1482 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001483 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001484 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001485 /* bytes : in, out */
1486 "%lld,%lld,"
1487 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001488 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001489 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001490 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001491 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001492 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001493 "",
1494 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001495 sv->nbpend, sv->counters.nbpend_max,
1496 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001497 sv->counters.bytes_in, sv->counters.bytes_out,
1498 sv->counters.failed_secu,
1499 sv->counters.failed_conns, sv->counters.failed_resp,
1500 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001501
Willy Tarreau55bb8452007-10-17 18:44:57 +02001502 /* status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001503 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001504 srv_hlt_st[sv_state],
1505 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
1506 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
Willy Tarreau91861262007-10-17 17:06:05 +02001507
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001508 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001509 /* weight, active, backup */
1510 "%d,%d,%d,"
1511 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001512 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001513 (sv->state & SRV_BACKUP) ? 0 : 1,
1514 (sv->state & SRV_BACKUP) ? 1 : 0);
1515
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001516 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001517 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001518 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02001519 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001520 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001521 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02001522 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001523 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001524 ",,,,");
1525
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001526 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001527 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01001528 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001529 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01001530 LIM2A0(sv->maxqueue, ""),
1531 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001532
1533 /* throttle */
1534 if ((sv->state & SRV_WARMINGUP) &&
1535 now.tv_sec < sv->last_change + sv->slowstart &&
1536 now.tv_sec >= sv->last_change) {
1537 unsigned int ratio;
1538 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001539 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001540 }
1541
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001542 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001543 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001544
1545 /* tracked */
1546 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001547 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001548 sv->tracked->proxy->id, sv->tracked->id);
1549 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001550 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001551
Willy Tarreau7f062c42009-03-05 18:43:00 +01001552 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001553 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01001554
1555 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001556 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001557 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001558 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01001559
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001560 if (sv->state & SRV_CHECKED) {
1561 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001562 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001563
1564 /* check_code */
1565 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001566 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001567 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001568 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001569
1570 /* check_duration */
1571 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001572 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001573 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001574 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001575
1576 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001577 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001578 }
1579
Willy Tarreau7f062c42009-03-05 18:43:00 +01001580 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001581 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001582 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001583 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001584 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001585 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02001586
1587 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
1588 /* fall through */
1589
1590 case DATA_ST_PX_BE:
1591 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001592 if ((px->cap & PR_CAP_BE) &&
1593 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
1594 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001595 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001596 /* name */
1597 "<tr align=center class=\"backend\"><td>Backend</td>"
1598 /* queue : current, max */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001599 "<td align=right>%s</td><td align=right>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001600 /* sessions rate : current, max, limit */
1601 "<td align=right>%s</td><td align=right>%s</td><td align=right></td>"
1602 "",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001603 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->counters.nbpend_max),
1604 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->counters.be_sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02001605
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001606 chunk_printf(&msg,
Willy Tarreaua3e49422009-05-10 19:19:41 +02001607 /* sessions : current, max, limit, total, lbtot */
1608 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001609 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001610 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001611 "<td align=right>%s</td><td align=right>%s</td>"
1612 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001613 U2H2(px->beconn), U2H3(px->counters.beconn_max), U2H4(px->fullconn),
1614 U2H6(px->counters.cum_beconn), U2H7(px->counters.cum_lbconn),
1615 U2H8(px->counters.bytes_in), U2H9(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001616
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001617 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001618 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001619 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001620 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001621 "<td align=right></td><td align=right>%s</td><td align=right>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001622 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001623 "<td align=right>%lld</td><td align=right>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001624 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02001625 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001626 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02001627 * active and backups. */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001628 "<td align=center nowrap>%s %s</td><td align=center>&nbsp;</td><td align=center>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001629 "<td align=center>%d</td><td align=center>%d</td>"
1630 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001631 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
1632 U2H2(px->counters.failed_conns), U2H3(px->counters.failed_resp),
1633 px->counters.retries, px->counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001634 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01001635 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
1636 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01001637 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001638 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001639
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001640 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001641 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001642 "<td align=center>&nbsp;</td><td align=\"right\">%d</td>"
1643 "<td align=\"right\" nowrap>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001644 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001645 "</tr>",
1646 px->down_trans,
1647 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001648 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001649 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001650 /* pxid, name */
1651 "%s,BACKEND,"
1652 /* queue : current, max */
1653 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001654 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001655 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001656 /* bytes : in, out */
1657 "%lld,%lld,"
1658 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001659 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001660 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001661 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001662 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001663 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001664 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02001665 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001666 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02001667 * active and backups. */
1668 "%s,"
1669 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001670 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001671 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001672 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001673 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001674 /* rate, rate_lim, rate_max, */
1675 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001676 /* check_status, check_code, check_duration */
1677 ",,,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001678 "\n",
1679 px->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001680 px->nbpend /* or px->totpend ? */, px->counters.nbpend_max,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001681 px->beconn, px->counters.beconn_max, px->fullconn, px->counters.cum_beconn,
1682 px->counters.bytes_in, px->counters.bytes_out,
1683 px->counters.denied_req, px->counters.denied_resp,
1684 px->counters.failed_conns, px->counters.failed_resp,
1685 px->counters.retries, px->counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01001686 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01001687 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001688 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001689 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01001690 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001691 relative_pid, px->uuid,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001692 px->counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001693 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001694 px->counters.be_sps_max);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001695 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001696 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001697 return 0;
1698 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001699
Willy Tarreau91861262007-10-17 17:06:05 +02001700 s->data_ctx.stats.px_st = DATA_ST_PX_END;
1701 /* fall through */
1702
1703 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001704 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001705 chunk_printf(&msg, "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001706
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001707 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001708 return 0;
1709 }
Willy Tarreau91861262007-10-17 17:06:05 +02001710
1711 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
1712 /* fall through */
1713
1714 case DATA_ST_PX_FIN:
1715 return 1;
1716
1717 default:
1718 /* unknown state, we should put an abort() here ! */
1719 return 1;
1720 }
1721}
1722
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001723
1724/* This function is called to send output to the response buffer.
1725 * It dumps the sessions states onto the output buffer <rep>.
1726 * Expects to be called with client socket shut down on input.
1727 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001728 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001729 */
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001730int stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001731{
1732 struct chunk msg;
1733
1734 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
1735 /* If we're forced to shut down, we might have to remove our
1736 * reference to the last session being dumped.
1737 */
1738 if (s->data_state == DATA_ST_LIST) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001739 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001740 LIST_DEL(&s->data_ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001741 LIST_INIT(&s->data_ctx.sess.bref.users);
1742 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001743 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001744 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001745 }
1746
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001747 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001748
1749 switch (s->data_state) {
1750 case DATA_ST_INIT:
1751 /* the function had not been called yet, let's prepare the
1752 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001753 * pointer to the first in the global list. When a target
1754 * session is being destroyed, it is responsible for updating
1755 * this pointer. We know we have reached the end when this
1756 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001757 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001758 LIST_INIT(&s->data_ctx.sess.bref.users);
1759 s->data_ctx.sess.bref.ref = sessions.n;
1760 s->data_state = DATA_ST_LIST;
1761 /* fall through */
1762
1763 case DATA_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001764 /* first, let's detach the back-ref from a possible previous session */
1765 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
1766 LIST_DEL(&s->data_ctx.sess.bref.users);
1767 LIST_INIT(&s->data_ctx.sess.bref.users);
1768 }
1769
1770 /* and start from where we stopped */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001771 while (s->data_ctx.sess.bref.ref != &sessions) {
1772 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1773 struct session *curr_sess;
1774
1775 curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list);
1776
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001777 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001778 "%p: proto=%s",
1779 curr_sess,
1780 curr_sess->listener->proto->name);
1781
1782 switch (curr_sess->listener->proto->sock_family) {
1783 case AF_INET:
1784 inet_ntop(AF_INET,
1785 (const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr,
1786 pn, sizeof(pn));
1787
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001788 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001789 " src=%s:%d fe=%s be=%s srv=%s",
1790 pn,
1791 ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port),
1792 curr_sess->fe->id,
1793 curr_sess->be->id,
1794 curr_sess->srv ? curr_sess->srv->id : "<none>"
1795 );
1796 break;
1797 case AF_INET6:
1798 inet_ntop(AF_INET6,
1799 (const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr,
1800 pn, sizeof(pn));
1801
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001802 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001803 " src=%s:%d fe=%s be=%s srv=%s",
1804 pn,
1805 ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port),
1806 curr_sess->fe->id,
1807 curr_sess->be->id,
1808 curr_sess->srv ? curr_sess->srv->id : "<none>"
1809 );
1810
1811 break;
1812 case AF_UNIX:
1813 /* no more information to print right now */
1814 break;
1815 }
1816
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001817 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02001818 " ts=%02x age=%s calls=%d",
1819 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01001820 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
1821 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001822
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001823 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001824 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
1825 curr_sess->req->flags,
1826 curr_sess->req->l,
1827 curr_sess->req->analysers,
1828 curr_sess->req->rex ?
1829 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
1830 TICKS_TO_MS(1000)) : "");
1831
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001832 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001833 ",wx=%s",
1834 curr_sess->req->wex ?
1835 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
1836 TICKS_TO_MS(1000)) : "");
1837
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001838 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001839 ",ax=%s]",
1840 curr_sess->req->analyse_exp ?
1841 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
1842 TICKS_TO_MS(1000)) : "");
1843
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001844 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001845 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
1846 curr_sess->rep->flags,
1847 curr_sess->rep->l,
1848 curr_sess->rep->analysers,
1849 curr_sess->rep->rex ?
1850 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
1851 TICKS_TO_MS(1000)) : "");
1852
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001853 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001854 ",wx=%s",
1855 curr_sess->rep->wex ?
1856 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
1857 TICKS_TO_MS(1000)) : "");
1858
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001859 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001860 ",ax=%s]",
1861 curr_sess->rep->analyse_exp ?
1862 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
1863 TICKS_TO_MS(1000)) : "");
1864
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001865 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001866 " s0=[%d,%1xh,fd=%d,ex=%s]",
1867 curr_sess->si[0].state,
1868 curr_sess->si[0].flags,
1869 curr_sess->si[0].fd,
1870 curr_sess->si[0].exp ?
1871 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
1872 TICKS_TO_MS(1000)) : "");
1873
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001874 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001875 " s1=[%d,%1xh,fd=%d,ex=%s]",
1876 curr_sess->si[1].state,
1877 curr_sess->si[1].flags,
1878 curr_sess->si[1].fd,
1879 curr_sess->si[1].exp ?
1880 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
1881 TICKS_TO_MS(1000)) : "");
1882
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001883 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001884 " exp=%s",
1885 curr_sess->task->expire ?
1886 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
1887 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01001888 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001889 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001890
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001891 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001892
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001893 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001894 /* let's try again later from this session. We add ourselves into
1895 * this session's users so that it can remove us upon termination.
1896 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001897 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001898 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001899 }
1900
1901 s->data_ctx.sess.bref.ref = curr_sess->list.n;
1902 }
1903 s->data_state = DATA_ST_FIN;
1904 /* fall through */
1905
1906 default:
1907 s->data_state = DATA_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001908 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001909 }
1910}
1911
Willy Tarreau74808cb2009-03-04 15:53:18 +01001912/* print a line of error buffer (limited to 70 bytes) to <out>. The format is :
1913 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
1914 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
1915 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
1916 * lines are respected within the limit of 70 output chars. Lines that are
1917 * continuation of a previous truncated line begin with "+" instead of " "
1918 * after the offset. The new pointer is returned.
1919 */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001920static int dump_error_line(struct chunk *out, struct error_snapshot *err,
1921 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01001922{
1923 int end;
1924 unsigned char c;
1925
1926 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001927 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01001928 return ptr;
1929
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001930 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01001931
Willy Tarreau61b34732009-10-03 23:49:35 +02001932 while (ptr < err->len && ptr < sizeof(err->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001933 c = err->buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01001934 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001935 if (out->len > end - 2)
1936 break;
1937 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01001938 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001939 if (out->len > end - 3)
1940 break;
1941 out->str[out->len++] = '\\';
1942 switch (c) {
1943 case '\t': c = 't'; break;
1944 case '\n': c = 'n'; break;
1945 case '\r': c = 'r'; break;
1946 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01001947 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001948 }
1949 out->str[out->len++] = c;
1950 } else {
1951 if (out->len > end - 5)
1952 break;
1953 out->str[out->len++] = '\\';
1954 out->str[out->len++] = 'x';
1955 out->str[out->len++] = hextab[(c >> 4) & 0xF];
1956 out->str[out->len++] = hextab[c & 0xF];
1957 }
1958 if (err->buf[ptr++] == '\n') {
1959 /* we had a line break, let's return now */
1960 out->str[out->len++] = '\n';
1961 *line = ptr;
1962 return ptr;
1963 }
1964 }
1965 /* we have an incomplete line, we return it as-is */
1966 out->str[out->len++] = '\n';
1967 return ptr;
1968}
1969
1970/* This function is called to send output to the response buffer.
1971 * It dumps the errors logged in proxies onto the output buffer <rep>.
1972 * Expects to be called with client socket shut down on input.
1973 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau61b34732009-10-03 23:49:35 +02001974 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau74808cb2009-03-04 15:53:18 +01001975 */
Willy Tarreau61b34732009-10-03 23:49:35 +02001976int stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau74808cb2009-03-04 15:53:18 +01001977{
1978 extern const char *monthname[12];
1979 struct chunk msg;
1980
Willy Tarreau61b34732009-10-03 23:49:35 +02001981 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW)))
1982 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001983
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001984 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01001985
1986 if (!s->data_ctx.errors.px) {
1987 /* the function had not been called yet, let's prepare the
1988 * buffer for a response.
1989 */
Willy Tarreau74808cb2009-03-04 15:53:18 +01001990 s->data_ctx.errors.px = proxy;
1991 s->data_ctx.errors.buf = 0;
1992 s->data_ctx.errors.bol = 0;
1993 s->data_ctx.errors.ptr = -1;
1994 }
1995
1996 /* we have two inner loops here, one for the proxy, the other one for
1997 * the buffer.
1998 */
1999 while (s->data_ctx.errors.px) {
2000 struct error_snapshot *es;
2001
2002 if (s->data_ctx.errors.buf == 0)
2003 es = &s->data_ctx.errors.px->invalid_req;
2004 else
2005 es = &s->data_ctx.errors.px->invalid_rep;
2006
2007 if (!es->when.tv_sec)
2008 goto next;
2009
2010 if (s->data_ctx.errors.iid >= 0 &&
2011 s->data_ctx.errors.px->uuid != s->data_ctx.errors.iid &&
2012 es->oe->uuid != s->data_ctx.errors.iid)
2013 goto next;
2014
2015 if (s->data_ctx.errors.ptr < 0) {
2016 /* just print headers now */
2017
2018 char pn[INET6_ADDRSTRLEN];
2019 struct tm tm;
2020
2021 get_localtime(es->when.tv_sec, &tm);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002022 chunk_printf(&msg, "\n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01002023 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002024 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01002025
2026
2027 if (es->src.ss_family == AF_INET)
2028 inet_ntop(AF_INET,
2029 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
2030 pn, sizeof(pn));
2031 else
2032 inet_ntop(AF_INET6,
2033 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
2034 pn, sizeof(pn));
2035
2036 switch (s->data_ctx.errors.buf) {
2037 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002038 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002039 " frontend %s (#%d): invalid request\n"
2040 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
2041 " request length %d bytes, error at position %d:\n\n",
2042 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
2043 pn, es->sid, es->oe->id, es->oe->uuid,
2044 es->srv ? es->srv->id : "<NONE>",
2045 es->srv ? es->srv->puid : -1,
2046 es->len, es->pos);
2047 break;
2048 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002049 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002050 " backend %s (#%d) : invalid response\n"
2051 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
2052 " response length %d bytes, error at position %d:\n\n",
2053 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
2054 pn, es->sid, es->oe->id, es->oe->uuid,
2055 es->srv ? es->srv->id : "<NONE>",
2056 es->srv ? es->srv->puid : -1,
2057 es->len, es->pos);
2058 break;
2059 }
2060
Willy Tarreau61b34732009-10-03 23:49:35 +02002061 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002062 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02002063 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002064 }
2065 s->data_ctx.errors.ptr = 0;
2066 s->data_ctx.errors.sid = es->sid;
2067 }
2068
2069 if (s->data_ctx.errors.sid != es->sid) {
2070 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002071 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002072 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau61b34732009-10-03 23:49:35 +02002073 if (buffer_feed_chunk(rep, &msg) >= 0)
2074 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002075 goto next;
2076 }
2077
2078 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau61b34732009-10-03 23:49:35 +02002079 while (s->data_ctx.errors.ptr < es->len && s->data_ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002080 int newptr;
2081 int newline;
2082
2083 newline = s->data_ctx.errors.bol;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002084 newptr = dump_error_line(&msg, es, &newline, s->data_ctx.errors.ptr);
Willy Tarreau74808cb2009-03-04 15:53:18 +01002085 if (newptr == s->data_ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02002086 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002087
Willy Tarreau61b34732009-10-03 23:49:35 +02002088 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002089 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02002090 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002091 }
2092 s->data_ctx.errors.ptr = newptr;
2093 s->data_ctx.errors.bol = newline;
2094 };
2095 next:
2096 s->data_ctx.errors.bol = 0;
2097 s->data_ctx.errors.ptr = -1;
2098 s->data_ctx.errors.buf++;
2099 if (s->data_ctx.errors.buf > 1) {
2100 s->data_ctx.errors.buf = 0;
2101 s->data_ctx.errors.px = s->data_ctx.errors.px->next;
2102 }
2103 }
2104
2105 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02002106 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002107}
2108
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002109
Willy Tarreau10522fd2008-07-09 20:12:41 +02002110static struct cfg_kw_list cfg_kws = {{ },{
2111 { CFG_GLOBAL, "stats", stats_parse_global },
2112 { 0, NULL, NULL },
2113}};
2114
2115__attribute__((constructor))
2116static void __dumpstats_module_init(void)
2117{
2118 cfg_register_keywords(&cfg_kws);
2119}
2120
Willy Tarreau91861262007-10-17 17:06:05 +02002121/*
2122 * Local variables:
2123 * c-indent-level: 8
2124 * c-basic-offset: 8
2125 * End:
2126 */