blob: bacd9cc9fe52cc60b90230ffcadf2eba86c2ad8c [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 Tarreau9a42c0d2009-09-22 19:31:03 +020065 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020066
67const struct chunk stats_sock_usage = {
68 .str = (char *)&stats_sock_usage_msg,
69 .len = sizeof(stats_sock_usage_msg)-1
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 */
132
133 global.stats_fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
134 global.stats_sock.timeout = &global.stats_fe->timeout.client;
135
136 global.stats_sock.next = global.stats_fe->listen;
137 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200138
139 cur_arg = 2;
140 while (*args[cur_arg]) {
141 if (!strcmp(args[cur_arg], "uid")) {
142 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
143 cur_arg += 2;
144 }
145 else if (!strcmp(args[cur_arg], "gid")) {
146 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
147 cur_arg += 2;
148 }
149 else if (!strcmp(args[cur_arg], "mode")) {
150 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
151 cur_arg += 2;
152 }
153 else if (!strcmp(args[cur_arg], "user")) {
154 struct passwd *user;
155 user = getpwnam(args[cur_arg + 1]);
156 if (!user) {
157 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
158 args[cur_arg + 1]);
159 return -1;
160 }
161 global.stats_sock.perm.ux.uid = user->pw_uid;
162 cur_arg += 2;
163 }
164 else if (!strcmp(args[cur_arg], "group")) {
165 struct group *group;
166 group = getgrnam(args[cur_arg + 1]);
167 if (!group) {
168 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
169 args[cur_arg + 1]);
170 return -1;
171 }
172 global.stats_sock.perm.ux.gid = group->gr_gid;
173 cur_arg += 2;
174 }
175 else {
176 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', and 'mode'");
177 return -1;
178 }
179 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100180
Willy Tarreaufbee7132007-10-18 13:53:22 +0200181 uxst_add_listener(&global.stats_sock);
182 global.maxsock++;
183 }
184 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100185 unsigned timeout;
186 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
187
188 if (res) {
189 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
190 return -1;
191 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200192
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100193 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200194 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
195 return -1;
196 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200197 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200198 }
199 else if (!strcmp(args[0], "maxconn")) {
200 int maxconn = atol(args[1]);
201
202 if (maxconn <= 0) {
203 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
204 return -1;
205 }
206 global.maxsock -= global.stats_sock.maxconn;
207 global.stats_sock.maxconn = maxconn;
208 global.maxsock += global.stats_sock.maxconn;
209 }
210 else {
211 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
212 return -1;
213 }
214 return 0;
215}
216
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200217int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100218{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200219 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100220 "# pxname,svname,"
221 "qcur,qmax,"
222 "scur,smax,slim,stot,"
223 "bin,bout,"
224 "dreq,dresp,"
225 "ereq,econ,eresp,"
226 "wretr,wredis,"
227 "status,weight,act,bck,"
228 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200229 "pid,iid,sid,throttle,lbtot,tracked,type,"
230 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200231 "check_status,check_code,check_duration,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100232 "\n");
233}
234
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200235/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200236 * called from an applet running in a stream interface. The function returns 1
237 * if the request was understood, otherwise zero. It sets si->st0 to a value
238 * designating the function which will have to process the request.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200239 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200240int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200241{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200242 struct session *s = si->private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200243 char *args[MAX_STATS_ARGS + 1];
244 int arg;
245
246 while (isspace((unsigned char)*line))
247 line++;
248
249 arg = 0;
250 args[arg] = line;
251
252 while (*line && arg < MAX_STATS_ARGS) {
253 if (isspace((unsigned char)*line)) {
254 *line++ = '\0';
255
256 while (isspace((unsigned char)*line))
257 line++;
258
259 args[++arg] = line;
260 continue;
261 }
262
263 line++;
264 }
265
266 while (++arg <= MAX_STATS_ARGS)
267 args[arg] = line;
268
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200269 s->data_ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200270 if (strcmp(args[0], "show") == 0) {
271 if (strcmp(args[1], "stat") == 0) {
272 if (*args[2] && *args[3] && *args[4]) {
273 s->data_ctx.stats.flags |= STAT_BOUND;
274 s->data_ctx.stats.iid = atoi(args[2]);
275 s->data_ctx.stats.type = atoi(args[3]);
276 s->data_ctx.stats.sid = atoi(args[4]);
277 }
278
279 s->data_ctx.stats.flags |= STAT_SHOW_STAT;
280 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200281 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200282 si->st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200283 }
284 else if (strcmp(args[1], "info") == 0) {
285 s->data_ctx.stats.flags |= STAT_SHOW_INFO;
286 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200287 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200288 si->st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200289 }
290 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200291 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200292 si->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200293 }
294 else if (strcmp(args[1], "errors") == 0) {
295 if (*args[2])
296 s->data_ctx.errors.iid = atoi(args[2]);
297 else
298 s->data_ctx.errors.iid = -1;
299 s->data_ctx.errors.px = NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200300 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200301 si->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200302 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200303 else { /* neither "stat" nor "info" nor "sess" nor "errors"*/
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200304 return 0;
305 }
306 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200307 else if (strcmp(args[0], "clear") == 0) {
308 if (strcmp(args[1], "counters") == 0) {
309 struct proxy *px;
310 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200311 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200312 int clrall = 0;
313
314 if (strcmp(args[2], "all") == 0)
315 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200316
317 for (px = proxy; px; px = px->next) {
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200318 if (clrall)
319 memset(&px->counters, 0, sizeof(px->counters));
320 else {
321 px->counters.feconn_max = 0;
322 px->counters.beconn_max = 0;
323 px->counters.fe_sps_max = 0;
324 px->counters.be_sps_max = 0;
325 px->counters.nbpend_max = 0;
326 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200327
328 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200329 if (clrall)
330 memset(&sv->counters, 0, sizeof(sv->counters));
331 else {
332 sv->counters.cur_sess_max = 0;
333 sv->counters.nbpend_max = 0;
334 sv->counters.sps_max = 0;
335 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200336
337 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200338 if (li->counters) {
339 if (clrall)
340 memset(li->counters, 0, sizeof(*li->counters));
341 else
342 li->counters->conn_max = 0;
343 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200344 }
345
346 return 1;
347 }
348 else {
349 return 0;
350 }
351 }
352 else { /* not "show" nor "clear"*/
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200353 return 0;
354 }
355 return 1;
356}
357
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200358/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200359 * used to processes I/O from/to the stats unix socket. The system relies on a
360 * state machine handling requests and various responses. We read a request,
361 * then we process it and send the response, and we possibly display a prompt.
362 * Then we can read again. The state is stored in si->st0 and is one of the
363 * STAT_CLI_* constants. si->st1 is used to indicate whether prompt is enabled
364 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200365 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200366void stats_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200367{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200368 struct session *s = si->private;
369 struct buffer *req = si->ob;
370 struct buffer *res = si->ib;
371 int reql;
372 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200373
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200374 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
375 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200376
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200377 while (1) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200378 if (si->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200379 /* Stats output not initialized yet */
380 memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats));
381 s->data_source = DATA_SRC_STATS;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200382 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200383 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200384 else if (si->st0 == STAT_CLI_END) {
385 /* Let's close for real now. We just close the request
386 * side, the conditions below will complete if needed.
387 */
388 si->shutw(si);
389 break;
390 }
391 else if (si->st0 == STAT_CLI_GETREQ) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200392 reql = buffer_si_peekline(si->ob, trash, sizeof(trash));
393 if (reql <= 0) { /* closed or EOL not found */
394 if (reql == 0)
395 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200396 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200397 continue;
398 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200399
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200400 /* seek for a possible semi-colon. If we find one, we
401 * replace it with an LF and skip only this part.
402 */
403 for (len = 0; len < reql; len++)
404 if (trash[len] == ';') {
405 trash[len] = '\n';
406 reql = len + 1;
407 break;
408 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200409
Willy Tarreau816fc222009-10-04 07:36:58 +0200410 /* now it is time to check that we have a full line,
411 * remove the trailing \n and possibly \r, then cut the
412 * line.
413 */
414 len = reql - 1;
415 if (trash[len] != '\n') {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200416 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200417 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200418 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200419
Willy Tarreau816fc222009-10-04 07:36:58 +0200420 if (len && trash[len-1] == '\r')
421 len--;
422
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200423 trash[len] = '\0';
424
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200425 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200426 if (len) {
427 if (strcmp(trash, "quit") == 0) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200428 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200429 continue;
430 }
431 else if (strcmp(trash, "prompt") == 0)
432 si->st1 = !si->st1;
433 else if (strcmp(trash, "help") == 0 ||
434 !stats_sock_parse_request(si, trash))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200435 si->st0 = STAT_CLI_O_HELP;
436 /* NB: stats_sock_parse_request() may have put
437 * another STAT_CLI_O_* into si->st0.
438 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200439 }
440 else if (!si->st1) {
441 /* if prompt is disabled, print help on empty lines,
442 * so that the user at least knows how to enable
443 * prompt and find help.
444 */
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200445 si->st0 = STAT_CLI_O_HELP;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200446 }
447
448 /* re-adjust req buffer */
449 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200450 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200451 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200452 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200453 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200454 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200455 continue;
456 }
457
458 switch (si->st0) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200459 case STAT_CLI_O_HELP:
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200460 if (buffer_feed(si->ib, stats_sock_usage.str, stats_sock_usage.len) < 0)
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200461 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200462 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200463 case STAT_CLI_O_INFO:
Willy Tarreau24955a12009-10-04 11:54:04 +0200464 if (stats_dump_raw_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200465 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200466 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200467 case STAT_CLI_O_SESS:
Willy Tarreau7e72a8f2009-10-03 23:55:14 +0200468 if (stats_dump_sess_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200469 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200470 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200471 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau61b34732009-10-03 23:49:35 +0200472 if (stats_dump_errors_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200473 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200474 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200475 default: /* abnormal state */
476 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200477 break;
478 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200479
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200480 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
481 if (si->st0 == STAT_CLI_PROMPT) {
482 if (buffer_feed(si->ib, si->st1 ? "\n> " : "\n", si->st1 ? 3 : 1) < 0)
483 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200484 }
485
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200486 /* If the output functions are still there, it means they require more room. */
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200487 if (si->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200488 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200489
490 /* Now we close the output if one of the writers did so,
491 * or if we're not in interactive mode and the request
492 * buffer is empty. This still allows pipelined requests
493 * to be sent in non-interactive mode.
494 */
495 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->st1 && !req->send_max)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200496 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200497 continue;
498 }
499
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200500 /* switch state back to GETREQ to read next requests */
501 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200502 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200503 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200504
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200505 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200506 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
507 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
508 /* Other size has closed, let's abort if we have no more processing to do
509 * and nothing more to consume. This is comparable to a broken pipe, so
510 * we forward the close to the request side so that it flows upstream to
511 * the client.
512 */
513 si->shutw(si);
514 }
515
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200516 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200517 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
518 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
519 /* We have no more processing to do, and nothing more to send, and
520 * the client side has closed. So we'll forward this state downstream
521 * on the response buffer.
522 */
523 si->shutr(si);
524 res->flags |= BF_READ_NULL;
525 }
526
527 /* update all other flags and resync with the other side */
528 si->update(si);
529
530 /* we don't want to expire timeouts while we're processing requests */
531 si->ib->rex = TICK_ETERNITY;
532 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200533
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200534 out:
535 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
536 __FUNCTION__, __LINE__,
537 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
538
539 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
540 /* check that we have released everything then unregister */
541 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200542 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200543}
544
Willy Tarreau24955a12009-10-04 11:54:04 +0200545/* This function is called to send output to the response buffer.
546 * It dumps statistics onto the output buffer <rep> owned by session <s>.
547 * s->data_ctx must have been zeroed first, and the flags properly set.
548 * It returns 0 as long as it does not complete, non-zero upon completion.
549 * Some states are not used but it makes the code more similar to other
550 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200551 */
Willy Tarreau24955a12009-10-04 11:54:04 +0200552int stats_dump_raw_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3e76e722007-10-17 18:57:38 +0200553{
Willy Tarreau3e76e722007-10-17 18:57:38 +0200554 struct proxy *px;
555 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +0100556 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200557
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200558 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +0200559
560 switch (s->data_state) {
561 case DATA_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +0200562 /* the function had not been called yet */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200563 s->data_state = DATA_ST_HEAD;
564 /* fall through */
565
566 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100567 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200568 print_csv_header(&msg);
Willy Tarreau24955a12009-10-04 11:54:04 +0200569 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100570 return 0;
571 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200572
573 s->data_state = DATA_ST_INFO;
574 /* fall through */
575
576 case DATA_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +0100577 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100578 if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200579 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +0100580 "Name: " PRODUCT_NAME "\n"
581 "Version: " HAPROXY_VERSION "\n"
582 "Release_date: " HAPROXY_DATE "\n"
583 "Nbproc: %d\n"
584 "Process_num: %d\n"
585 "Pid: %d\n"
586 "Uptime: %dd %dh%02dm%02ds\n"
587 "Uptime_sec: %d\n"
588 "Memmax_MB: %d\n"
589 "Ulimit-n: %d\n"
590 "Maxsock: %d\n"
591 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100592 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100593 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100594 "PipesUsed: %d\n"
595 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100596 "Tasks: %d\n"
597 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200598 "node: %s\n"
599 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100600 "",
601 global.nbproc,
602 relative_pid,
603 pid,
604 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
605 up,
606 global.rlimit_memmax,
607 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100608 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100609 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200610 nb_tasks_cur, run_queue_cur,
611 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +0100612 );
Willy Tarreau24955a12009-10-04 11:54:04 +0200613 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100614 return 0;
615 }
616
Willy Tarreau3e76e722007-10-17 18:57:38 +0200617 s->data_ctx.stats.px = proxy;
618 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100619
620 s->data_ctx.stats.sv = NULL;
621 s->data_ctx.stats.sv_st = 0;
622
Willy Tarreau3e76e722007-10-17 18:57:38 +0200623 s->data_state = DATA_ST_LIST;
624 /* fall through */
625
626 case DATA_ST_LIST:
627 /* dump proxies */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100628 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100629 while (s->data_ctx.stats.px) {
630 px = s->data_ctx.stats.px;
631 /* skip the disabled proxies and non-networked ones */
632 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +0200633 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100634 if (stats_dump_proxy(s, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100635 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +0200636 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200637
Willy Tarreaua8efd362008-01-03 10:19:15 +0100638 s->data_ctx.stats.px = px->next;
639 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
640 }
641 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200642 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200643
644 s->data_state = DATA_ST_END;
645 /* fall through */
646
647 case DATA_ST_END:
648 s->data_state = DATA_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +0100649 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200650
651 case DATA_ST_FIN:
652 return 1;
653
654 default:
655 /* unknown state ! */
Willy Tarreau24955a12009-10-04 11:54:04 +0200656 s->data_state = DATA_ST_FIN;
657 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200658 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100659}
660
661
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200662/* This I/O handler runs as an applet embedded in a stream interface. It is
663 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
664 * si->st0 becomes non-zero once the transfer is finished. The handler
665 * automatically unregisters itself once transfer is complete.
666 */
667void http_stats_io_handler(struct stream_interface *si)
668{
669 struct session *s = si->private;
670 struct buffer *req = si->ob;
671 struct buffer *res = si->ib;
672
673 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
674 goto out;
675
676 /* check that the output is not closed */
677 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
678 si->st0 = 1;
679
680 if (!si->st0) {
681 if (stats_dump_http(s, res, s->be->uri_auth)) {
682 si->st0 = 1;
683 si->shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200684 }
685 }
686
687 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
688 si->shutw(si);
689
690 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->st0) {
691 si->shutr(si);
692 res->flags |= BF_READ_NULL;
693 }
694
695 /* update all other flags and resync with the other side */
696 si->update(si);
697
698 /* we don't want to expire timeouts while we're processing requests */
699 si->ib->rex = TICK_ETERNITY;
700 si->ob->wex = TICK_ETERNITY;
701
702 out:
703 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
704 /* check that we have released everything then unregister */
705 stream_int_unregister_handler(si);
706 }
707}
708
709
Willy Tarreau3e76e722007-10-17 18:57:38 +0200710/*
711 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200712 * client socket shut down on input. It stops by itself by unsetting the
Willy Tarreau72b179a2008-08-28 16:01:32 +0200713 * BF_HIJACK flag from the buffer, which it uses to keep on being called
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200714 * when there is free space in the buffer, of simply by letting an empty buffer
715 * upon return.s->data_ctx must have been zeroed before the first call, and the
716 * flags set. It returns 0 if it had to stop writing data and an I/O is needed,
717 * 1 if the dump is finished and the session must be closed, or -1 in case of
718 * any error.
Willy Tarreau91861262007-10-17 17:06:05 +0200719 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100720int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200721{
Willy Tarreau91861262007-10-17 17:06:05 +0200722 struct proxy *px;
723 struct chunk msg;
724 unsigned int up;
725
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200726 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +0200727
728 switch (s->data_state) {
729 case DATA_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200730 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200731 "HTTP/1.0 200 OK\r\n"
732 "Cache-Control: no-cache\r\n"
733 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200734 "Content-Type: %s\r\n",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100735 (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +0200736
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100737 if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200738 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +0200739 uri->refresh);
740
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200741 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +0200742
743 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200744 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +0200745 return 0;
746
Willy Tarreau91861262007-10-17 17:06:05 +0200747 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
748 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
749 if (!(s->flags & SN_FINST_MASK))
750 s->flags |= SN_FINST_R;
751
752 if (s->txn.meth == HTTP_METH_HEAD) {
753 /* that's all we return in case of HEAD request */
754 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +0200755 return 1;
756 }
757
758 s->data_state = DATA_ST_HEAD; /* let's start producing data */
759 /* fall through */
760
761 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100762 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200763 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200764 chunk_printf(&msg,
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200765 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200766 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
767 "<style type=\"text/css\"><!--\n"
768 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +0200769 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +0200770 " font-size: 12px;"
771 " font-weight: normal;"
772 " color: black;"
773 " background: white;"
774 "}\n"
775 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +0200776 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +0200777 " align: center;"
778 "}\n"
779 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +0200780 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +0200781 " margin-bottom: 0.5em;"
782 "}\n"
783 "h2 {"
784 " font-family: helvetica, arial;"
785 " font-size: x-large;"
786 " font-weight: bold;"
787 " font-style: italic;"
788 " color: #6020a0;"
789 " margin-top: 0em;"
790 " margin-bottom: 0em;"
791 "}\n"
792 "h3 {"
793 " font-family: helvetica, arial;"
794 " font-size: 16px;"
795 " font-weight: bold;"
796 " color: #b00040;"
797 " background: #e8e8d0;"
798 " margin-top: 0em;"
799 " margin-bottom: 0em;"
800 "}\n"
801 "li {"
802 " margin-top: 0.25em;"
803 " margin-right: 2em;"
804 "}\n"
805 ".hr {margin-top: 0.25em;"
806 " border-color: black;"
807 " border-bottom-style: solid;"
808 "}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200809 ".titre {background: #20D0D0;color: #000000; font-weight: bold;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200810 ".total {background: #20D0D0;color: #ffff80;}\n"
811 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200812 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200813 ".backend {background: #e8e8d0;}\n"
814 ".active0 {background: #ff9090;}\n"
815 ".active1 {background: #ffd020;}\n"
816 ".active2 {background: #ffffa0;}\n"
817 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100818 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
819 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
820 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200821 ".backup0 {background: #ff9090;}\n"
822 ".backup1 {background: #ff80ff;}\n"
823 ".backup2 {background: #c060ff;}\n"
824 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100825 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
826 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
827 ".backup6 {background: #e0e0e0;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +0200828 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Willy Tarreau91861262007-10-17 17:06:05 +0200829 "table.tbl { border-collapse: collapse; border-style: none;}\n"
830 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray;}\n"
831 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzki619caca2009-10-03 15:46:08 +0200832 "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 +0200833 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200834 "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 +0200835 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
836 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
837 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
838 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200839 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200840 (uri->flags&ST_SHNODE) ? " on " : "",
841 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +0200842 );
Willy Tarreau55bb8452007-10-17 18:44:57 +0200843 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200844 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200845 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200846 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200847 return 0;
848
849 s->data_state = DATA_ST_INFO;
850 /* fall through */
851
852 case DATA_ST_INFO:
853 up = (now.tv_sec - start_date.tv_sec);
854
855 /* WARNING! this has to fit the first packet too.
856 * We are around 3.5 kB, add adding entries will
857 * become tricky if we want to support 4kB buffers !
858 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100859 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200860 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200861 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
862 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200863 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200864 "<hr width=\"100%%\" class=\"hr\">\n"
865 "<h3>&gt; General process information</h3>\n"
866 "<table border=0 cols=4><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100867 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200868 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200869 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
870 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
871 "current conns = %d; current pipes = %d/%d<br>\n"
872 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200873 "</td><td align=\"center\" nowrap>\n"
874 "<table class=\"lgd\"><tr>\n"
875 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
876 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
877 "</tr><tr>\n"
878 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
879 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
880 "</tr><tr>\n"
881 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
882 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
883 "</tr><tr>\n"
884 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100885 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200886 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100887 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +0200888 "</td>"
889 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
890 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
891 "",
892 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200893 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
894 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
895 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +0200896 up / 86400, (up % 86400) / 3600,
897 (up % 3600) / 60, (up % 60),
898 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
899 global.rlimit_memmax ? " MB" : "",
900 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100901 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100902 actconn, pipes_used, pipes_used+pipes_free,
903 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +0200904 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100905
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100906 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200907 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200908 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
909 uri->uri_prefix,
910 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100911 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200912 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200913 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200914 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
915 uri->uri_prefix,
916 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100917 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200918
Willy Tarreau55bb8452007-10-17 18:44:57 +0200919 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100920 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200921 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200922 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
923 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100924 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200925 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200926 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200927 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200928 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
929 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100930 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200931 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200932 }
Willy Tarreau91861262007-10-17 17:06:05 +0200933
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200934 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200935 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
936 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100937 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
938 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200939
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200940 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +0200941 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
942 uri->uri_prefix,
943 (uri->refresh > 0) ? ";norefresh" : "");
944
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200945 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +0200946 "</td>"
947 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
948 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
949 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
950 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
951 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
952 "</ul>"
953 "</td>"
954 "</tr></table>\n"
955 ""
956 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100957
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200958 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200959 return 0;
960 }
Willy Tarreau91861262007-10-17 17:06:05 +0200961
Willy Tarreau91861262007-10-17 17:06:05 +0200962 s->data_ctx.stats.px = proxy;
963 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
964 s->data_state = DATA_ST_LIST;
965 /* fall through */
966
967 case DATA_ST_LIST:
968 /* dump proxies */
969 while (s->data_ctx.stats.px) {
970 px = s->data_ctx.stats.px;
971 /* skip the disabled proxies and non-networked ones */
972 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100973 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200974 return 0;
975
976 s->data_ctx.stats.px = px->next;
977 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
978 }
979 /* here, we just have reached the last proxy */
980
981 s->data_state = DATA_ST_END;
982 /* fall through */
983
984 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100985 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200986 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200987 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200988 return 0;
989 }
Willy Tarreau91861262007-10-17 17:06:05 +0200990
991 s->data_state = DATA_ST_FIN;
992 /* fall through */
993
994 case DATA_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +0200995 return 1;
996
997 default:
998 /* unknown state ! */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +0200999 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001000 return -1;
1001 }
1002}
1003
1004
1005/*
1006 * Dumps statistics for a proxy.
1007 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1008 * ot non-zero if everything completed.
1009 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001010int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001011{
1012 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001013 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001014 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02001015 struct chunk msg;
1016
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001017 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001018
1019 switch (s->data_ctx.stats.px_st) {
1020 case DATA_ST_PX_INIT:
1021 /* we are on a new proxy */
1022
1023 if (uri && uri->scope) {
1024 /* we have a limited scope, we have to check the proxy name */
1025 struct stat_scope *scope;
1026 int len;
1027
1028 len = strlen(px->id);
1029 scope = uri->scope;
1030
1031 while (scope) {
1032 /* match exact proxy name */
1033 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1034 break;
1035
1036 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001037 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001038 break;
1039 scope = scope->next;
1040 }
1041
1042 /* proxy name not found : don't dump anything */
1043 if (scope == NULL)
1044 return 1;
1045 }
1046
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001047 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001048 (px->uuid != s->data_ctx.stats.iid))
1049 return 1;
1050
Willy Tarreau91861262007-10-17 17:06:05 +02001051 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
1052 /* fall through */
1053
1054 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001055 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001056 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001057 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001058 "<table class=\"tbl\" width=\"100%%\">\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001059 "<tr align=\"center\" class=\"titre\">"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001060 "<th class=\"pxname\" width=\"10%%\">%s</th>"
1061 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001062 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001063 "</table>\n"
1064 "<table cols=\"29\" class=\"tbl\" width=\"100%%\">\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001065 "<tr align=\"center\" class=\"titre\">"
1066 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001067 "<th colspan=3>Queue</th>"
1068 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001069 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001070 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001071 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001072 "</tr>\n"
1073 "<tr align=\"center\" class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001074 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001075 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001076 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001077 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001078 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001079 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001080 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1081 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001082 "</tr>",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001083 px->id,
1084 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001085
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001086 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001087 return 0;
1088 }
Willy Tarreau91861262007-10-17 17:06:05 +02001089
1090 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
1091 /* fall through */
1092
1093 case DATA_ST_PX_FE:
1094 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001095 if ((px->cap & PR_CAP_FE) &&
1096 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
1097 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001098 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001099 /* name, queue */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001100 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=3></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001101 /* sessions rate : current, max, limit */
1102 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
1103 /* sessions : current, max, limit, total, lbtot */
1104 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001105 "<td align=right>%s</td><td align=right></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001106 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001107 "<td align=right>%s</td><td align=right>%s</td>"
1108 "",
Willy Tarreaua3e49422009-05-10 19:19:41 +02001109 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001110 U2H1(px->counters.fe_sps_max), LIM2A2(px->fe_sps_lim, "-"),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001111 U2H3(px->feconn), U2H4(px->counters.feconn_max), U2H5(px->maxconn),
1112 U2H6(px->counters.cum_feconn), U2H7(px->counters.bytes_in), U2H8(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001113
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001114 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001115 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001116 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001117 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001118 "<td align=right>%s</td><td align=right></td><td align=right></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001119 /* warnings: retries, redispatches */
1120 "<td align=right></td><td align=right></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001121 /* server status : reflect frontend status */
Willy Tarreau91861262007-10-17 17:06:05 +02001122 "<td align=center>%s</td>"
1123 /* rest of server: nothing */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001124 "<td align=center colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02001125 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001126 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
1127 U2H2(px->counters.failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +02001128 px->state == PR_STRUN ? "OPEN" :
1129 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001130 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001131 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001132 /* pxid, name, queue cur, queue max, */
1133 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001134 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001135 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001136 /* bytes : in, out */
1137 "%lld,%lld,"
1138 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001139 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001140 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001141 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001142 /* warnings: retries, redispatches */
1143 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001144 /* server status : reflect frontend status */
1145 "%s,"
1146 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001147 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001148 /* pid, iid, sid, throttle, lbtot, tracked, type */
1149 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001150 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001151 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001152 /* check_status, check_code, check_duration */
1153 ",,,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001154 "\n",
1155 px->id,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001156 px->feconn, px->counters.feconn_max, px->maxconn, px->counters.cum_feconn,
1157 px->counters.bytes_in, px->counters.bytes_out,
1158 px->counters.denied_req, px->counters.denied_resp,
1159 px->counters.failed_req,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001160 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +01001161 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01001162 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001163 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001164 px->fe_sps_lim, px->counters.fe_sps_max);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001165 }
Willy Tarreau91861262007-10-17 17:06:05 +02001166
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001167 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001168 return 0;
1169 }
1170
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001171 s->data_ctx.stats.l = px->listen; /* may be NULL */
1172 s->data_ctx.stats.px_st = DATA_ST_PX_LI;
1173 /* fall through */
1174
1175 case DATA_ST_PX_LI:
1176 /* stats.l has been initialized above */
1177 for (; s->data_ctx.stats.l != NULL; s->data_ctx.stats.l = l->next) {
1178 l = s->data_ctx.stats.l;
1179 if (!l->counters)
1180 continue;
1181
1182 if (s->data_ctx.stats.flags & STAT_BOUND) {
1183 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SO)))
1184 break;
1185
1186 if (s->data_ctx.stats.sid != -1 && l->luid != s->data_ctx.stats.sid)
1187 continue;
1188 }
1189
1190 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
1191 chunk_printf(&msg,
1192 /* name, queue */
1193 "<tr align=center class=\"socket\"><td>%s</td><td colspan=3></td>"
1194 /* sessions rate: current, max, limit */
1195 "<td align=right colspan=3>&nbsp;</td>"
1196 /* sessions: current, max, limit, total, lbtot */
1197 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
1198 "<td align=right>%s</td><td align=right>&nbsp;</td>"
1199 /* bytes: in, out */
1200 "<td align=right>%s</td><td align=right>%s</td>"
1201 "",
1202 l->name,
1203 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
1204 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
1205
1206 chunk_printf(&msg,
1207 /* denied: req, resp */
1208 "<td align=right>%s</td><td align=right>%s</td>"
1209 /* errors: request, connect, response */
1210 "<td align=right>%s</td><td align=right></td><td align=right></td>"
1211 /* warnings: retries, redispatches */
1212 "<td align=right></td><td align=right></td>"
1213 /* server status: reflect listener status */
1214 "<td align=center>%s</td>"
1215 /* rest of server: nothing */
1216 "<td align=center colspan=8></td></tr>"
1217 "",
1218 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
1219 U2H2(l->counters->failed_req),
1220 (l->nbconn < l->maxconn) ? "OPEN" : "FULL");
1221 } else {
1222 chunk_printf(&msg,
1223 /* pxid, name, queue cur, queue max, */
1224 "%s,%s,,,"
1225 /* sessions: current, max, limit, total */
1226 "%d,%d,%d,%lld,"
1227 /* bytes: in, out */
1228 "%lld,%lld,"
1229 /* denied: req, resp */
1230 "%lld,%lld,"
1231 /* errors: request, connect, response */
1232 "%lld,,,"
1233 /* warnings: retries, redispatches */
1234 ",,"
1235 /* server status: reflect listener status */
1236 "%s,"
1237 /* rest of server: nothing */
1238 ",,,,,,,,"
1239 /* pid, iid, sid, throttle, lbtot, tracked, type */
1240 "%d,%d,%d,,,,%d,"
1241 /* rate, rate_lim, rate_max */
1242 ",,,"
1243 /* check_status, check_code, check_duration */
1244 ",,,"
1245 "\n",
1246 px->id, l->name,
1247 l->nbconn, l->counters->conn_max,
1248 l->maxconn, l->counters->cum_conn,
1249 l->counters->bytes_in, l->counters->bytes_out,
1250 l->counters->denied_req, l->counters->denied_resp,
1251 l->counters->failed_req,
1252 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
1253 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
1254 }
1255
1256 if (buffer_feed_chunk(rep, &msg) >= 0)
1257 return 0;
1258 }
1259
Willy Tarreau91861262007-10-17 17:06:05 +02001260 s->data_ctx.stats.sv = px->srv; /* may be NULL */
1261 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
1262 /* fall through */
1263
1264 case DATA_ST_PX_SV:
1265 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001266 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
1267
Willy Tarreau2ea81932007-11-30 12:04:38 +01001268 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 +02001269
1270 sv = s->data_ctx.stats.sv;
1271
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001272 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001273 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
1274 break;
1275
1276 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
1277 continue;
1278 }
1279
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001280 if (sv->tracked)
1281 svs = sv->tracked;
1282 else
1283 svs = sv;
1284
Willy Tarreau91861262007-10-17 17:06:05 +02001285 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001286 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01001287 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001288 else if (svs->state & SRV_RUNNING) {
1289 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02001290 sv_state = 3; /* UP */
1291 else
1292 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01001293
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001294 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01001295 sv_state += 2;
1296 }
Willy Tarreau91861262007-10-17 17:06:05 +02001297 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001298 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02001299 sv_state = 1; /* going up */
1300 else
1301 sv_state = 0; /* DOWN */
1302
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001303 if ((sv_state == 0) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02001304 /* do not report servers which are DOWN */
1305 s->data_ctx.stats.sv = sv->next;
1306 continue;
1307 }
1308
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001309 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001310 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
1311 "UP %d/%d &darr;", "UP",
1312 "NOLB %d/%d &darr;", "NOLB",
1313 "<i>no check</i>" };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001314 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001315 /* name */
1316 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001317 /* queue : current, max, limit */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001318 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001319 /* sessions rate : current, max, limit */
1320 "<td align=right>%s</td><td align=right>%s</td><td align=right></td>"
1321 /* sessions : current, max, limit, total, lbtot */
1322 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001323 "<td align=right>%s</td><td align=right>%s</td>"
1324 "",
1325 (sv->state & SRV_BACKUP) ? "backup" : "active",
1326 sv_state, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001327 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
1328 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
1329 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001330 U2H8(sv->counters.cum_sess), U2H9(sv->counters.cum_lbconn));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001331
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001332 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001333 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001334 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001335 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001336 "<td align=right></td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001337 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001338 "<td align=right></td><td align=right>%s</td><td align=right>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001339 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001340 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001341 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001342 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
1343 U2H2(sv->counters.failed_secu),
1344 U2H3(sv->counters.failed_conns), U2H4(sv->counters.failed_resp),
1345 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001346
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001347 /* status, lest check */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001348 chunk_printf(&msg, "<td nowrap>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001349
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001350 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001351 chunk_printf(&msg, "%s ",
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001352 human_time(now.tv_sec - sv->last_change, 1));
1353
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001354 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001355 srv_hlt_st[sv_state],
1356 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
1357 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
1358
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001359 chunk_printf(&msg, "</td><td title=\"%s\" nowrap> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001360 get_check_status_description(sv->check_status),
1361 tv_iszero(&sv->check_start)?"":"* ",
1362 get_check_status_info(sv->check_status));
1363
1364 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001365 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001366
1367 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001368 chunk_printf(&msg, " in %lums", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001369 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001370 chunk_printf(&msg, "</td><td>");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001371 }
Willy Tarreau91861262007-10-17 17:06:05 +02001372
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001373 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001374 /* weight */
1375 "</td><td>%d</td>"
1376 /* act, bck */
1377 "<td>%s</td><td>%s</td>"
1378 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001379 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02001380 (sv->state & SRV_BACKUP) ? "-" : "Y",
1381 (sv->state & SRV_BACKUP) ? "Y" : "-");
1382
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001383 /* check failures: unique, fatal, down time */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001384 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001385 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02001386 "<td align=right>%lld</td><td align=right>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001387 "<td nowrap align=right>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001388 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001389 svs->counters.failed_checks, svs->counters.down_trans,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001390 human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001391 else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001392 chunk_printf(&msg,
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001393 "<td nowrap colspan=3>via %s/%s</td>", svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001394 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001395 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001396 "<td colspan=3></td>");
1397
1398 /* throttle */
1399 if ((sv->state & SRV_WARMINGUP) &&
1400 now.tv_sec < sv->last_change + sv->slowstart &&
1401 now.tv_sec >= sv->last_change) {
1402 unsigned int ratio;
1403 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001404 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001405 "<td>%d %%</td></tr>\n", ratio);
1406 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001407 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001408 "<td>-</td></tr>\n");
1409 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02001410 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001411 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
1412 "UP %d/%d,", "UP,",
1413 "NOLB %d/%d,", "NOLB,",
1414 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001415 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001416 /* pxid, name */
1417 "%s,%s,"
1418 /* queue : current, max */
1419 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001420 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001421 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001422 /* bytes : in, out */
1423 "%lld,%lld,"
1424 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001425 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001426 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001427 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001428 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001429 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001430 "",
1431 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001432 sv->nbpend, sv->counters.nbpend_max,
1433 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001434 sv->counters.bytes_in, sv->counters.bytes_out,
1435 sv->counters.failed_secu,
1436 sv->counters.failed_conns, sv->counters.failed_resp,
1437 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001438
Willy Tarreau55bb8452007-10-17 18:44:57 +02001439 /* status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001440 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001441 srv_hlt_st[sv_state],
1442 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
1443 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
Willy Tarreau91861262007-10-17 17:06:05 +02001444
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001445 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001446 /* weight, active, backup */
1447 "%d,%d,%d,"
1448 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001449 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001450 (sv->state & SRV_BACKUP) ? 0 : 1,
1451 (sv->state & SRV_BACKUP) ? 1 : 0);
1452
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001453 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001454 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001455 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02001456 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001457 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001458 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02001459 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001460 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001461 ",,,,");
1462
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001463 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001464 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01001465 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001466 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01001467 LIM2A0(sv->maxqueue, ""),
1468 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001469
1470 /* throttle */
1471 if ((sv->state & SRV_WARMINGUP) &&
1472 now.tv_sec < sv->last_change + sv->slowstart &&
1473 now.tv_sec >= sv->last_change) {
1474 unsigned int ratio;
1475 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001476 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001477 }
1478
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001479 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001480 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001481
1482 /* tracked */
1483 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001484 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001485 sv->tracked->proxy->id, sv->tracked->id);
1486 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001487 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001488
Willy Tarreau7f062c42009-03-05 18:43:00 +01001489 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001490 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01001491
1492 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001493 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001494 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001495 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01001496
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001497 if (sv->state & SRV_CHECKED) {
1498 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001499 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001500
1501 /* check_code */
1502 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001503 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001504 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001505 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001506
1507 /* check_duration */
1508 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001509 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001510 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001511 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001512
1513 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001514 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001515 }
1516
Willy Tarreau7f062c42009-03-05 18:43:00 +01001517 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001518 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001519 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001520 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001521 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001522 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02001523
1524 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
1525 /* fall through */
1526
1527 case DATA_ST_PX_BE:
1528 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001529 if ((px->cap & PR_CAP_BE) &&
1530 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
1531 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001532 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001533 /* name */
1534 "<tr align=center class=\"backend\"><td>Backend</td>"
1535 /* queue : current, max */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001536 "<td align=right>%s</td><td align=right>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001537 /* sessions rate : current, max, limit */
1538 "<td align=right>%s</td><td align=right>%s</td><td align=right></td>"
1539 "",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001540 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->counters.nbpend_max),
1541 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->counters.be_sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02001542
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001543 chunk_printf(&msg,
Willy Tarreaua3e49422009-05-10 19:19:41 +02001544 /* sessions : current, max, limit, total, lbtot */
1545 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001546 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001547 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001548 "<td align=right>%s</td><td align=right>%s</td>"
1549 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001550 U2H2(px->beconn), U2H3(px->counters.beconn_max), U2H4(px->fullconn),
1551 U2H6(px->counters.cum_beconn), U2H7(px->counters.cum_lbconn),
1552 U2H8(px->counters.bytes_in), U2H9(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001553
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001554 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001555 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001556 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001557 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001558 "<td align=right></td><td align=right>%s</td><td align=right>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001559 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001560 "<td align=right>%lld</td><td align=right>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001561 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02001562 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001563 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02001564 * active and backups. */
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001565 "<td align=center nowrap>%s %s</td><td align=center>&nbsp;</td><td align=center>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001566 "<td align=center>%d</td><td align=center>%d</td>"
1567 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001568 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
1569 U2H2(px->counters.failed_conns), U2H3(px->counters.failed_resp),
1570 px->counters.retries, px->counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001571 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01001572 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
1573 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01001574 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001575 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001576
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001577 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001578 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001579 "<td align=center>&nbsp;</td><td align=\"right\">%d</td>"
1580 "<td align=\"right\" nowrap>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001581 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001582 "</tr>",
1583 px->down_trans,
1584 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001585 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001586 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001587 /* pxid, name */
1588 "%s,BACKEND,"
1589 /* queue : current, max */
1590 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001591 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001592 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001593 /* bytes : in, out */
1594 "%lld,%lld,"
1595 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001596 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001597 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001598 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001599 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001600 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001601 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02001602 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001603 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02001604 * active and backups. */
1605 "%s,"
1606 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001607 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001608 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001609 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001610 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001611 /* rate, rate_lim, rate_max, */
1612 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001613 /* check_status, check_code, check_duration */
1614 ",,,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001615 "\n",
1616 px->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001617 px->nbpend /* or px->totpend ? */, px->counters.nbpend_max,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001618 px->beconn, px->counters.beconn_max, px->fullconn, px->counters.cum_beconn,
1619 px->counters.bytes_in, px->counters.bytes_out,
1620 px->counters.denied_req, px->counters.denied_resp,
1621 px->counters.failed_conns, px->counters.failed_resp,
1622 px->counters.retries, px->counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01001623 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01001624 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001625 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001626 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01001627 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001628 relative_pid, px->uuid,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001629 px->counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001630 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001631 px->counters.be_sps_max);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001632 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001633 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001634 return 0;
1635 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001636
Willy Tarreau91861262007-10-17 17:06:05 +02001637 s->data_ctx.stats.px_st = DATA_ST_PX_END;
1638 /* fall through */
1639
1640 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001641 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001642 chunk_printf(&msg, "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001643
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001644 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001645 return 0;
1646 }
Willy Tarreau91861262007-10-17 17:06:05 +02001647
1648 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
1649 /* fall through */
1650
1651 case DATA_ST_PX_FIN:
1652 return 1;
1653
1654 default:
1655 /* unknown state, we should put an abort() here ! */
1656 return 1;
1657 }
1658}
1659
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001660
1661/* This function is called to send output to the response buffer.
1662 * It dumps the sessions states onto the output buffer <rep>.
1663 * Expects to be called with client socket shut down on input.
1664 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001665 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001666 */
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001667int stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001668{
1669 struct chunk msg;
1670
1671 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
1672 /* If we're forced to shut down, we might have to remove our
1673 * reference to the last session being dumped.
1674 */
1675 if (s->data_state == DATA_ST_LIST) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001676 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001677 LIST_DEL(&s->data_ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001678 LIST_INIT(&s->data_ctx.sess.bref.users);
1679 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001680 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001681 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001682 }
1683
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001684 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001685
1686 switch (s->data_state) {
1687 case DATA_ST_INIT:
1688 /* the function had not been called yet, let's prepare the
1689 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001690 * pointer to the first in the global list. When a target
1691 * session is being destroyed, it is responsible for updating
1692 * this pointer. We know we have reached the end when this
1693 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001694 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001695 LIST_INIT(&s->data_ctx.sess.bref.users);
1696 s->data_ctx.sess.bref.ref = sessions.n;
1697 s->data_state = DATA_ST_LIST;
1698 /* fall through */
1699
1700 case DATA_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001701 /* first, let's detach the back-ref from a possible previous session */
1702 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
1703 LIST_DEL(&s->data_ctx.sess.bref.users);
1704 LIST_INIT(&s->data_ctx.sess.bref.users);
1705 }
1706
1707 /* and start from where we stopped */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001708 while (s->data_ctx.sess.bref.ref != &sessions) {
1709 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1710 struct session *curr_sess;
1711
1712 curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list);
1713
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001714 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001715 "%p: proto=%s",
1716 curr_sess,
1717 curr_sess->listener->proto->name);
1718
1719 switch (curr_sess->listener->proto->sock_family) {
1720 case AF_INET:
1721 inet_ntop(AF_INET,
1722 (const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr,
1723 pn, sizeof(pn));
1724
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001725 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001726 " src=%s:%d fe=%s be=%s srv=%s",
1727 pn,
1728 ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port),
1729 curr_sess->fe->id,
1730 curr_sess->be->id,
1731 curr_sess->srv ? curr_sess->srv->id : "<none>"
1732 );
1733 break;
1734 case AF_INET6:
1735 inet_ntop(AF_INET6,
1736 (const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr,
1737 pn, sizeof(pn));
1738
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001739 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001740 " src=%s:%d fe=%s be=%s srv=%s",
1741 pn,
1742 ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port),
1743 curr_sess->fe->id,
1744 curr_sess->be->id,
1745 curr_sess->srv ? curr_sess->srv->id : "<none>"
1746 );
1747
1748 break;
1749 case AF_UNIX:
1750 /* no more information to print right now */
1751 break;
1752 }
1753
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001754 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02001755 " ts=%02x age=%s calls=%d",
1756 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01001757 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
1758 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001759
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001760 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001761 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
1762 curr_sess->req->flags,
1763 curr_sess->req->l,
1764 curr_sess->req->analysers,
1765 curr_sess->req->rex ?
1766 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
1767 TICKS_TO_MS(1000)) : "");
1768
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001769 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001770 ",wx=%s",
1771 curr_sess->req->wex ?
1772 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
1773 TICKS_TO_MS(1000)) : "");
1774
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001775 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001776 ",ax=%s]",
1777 curr_sess->req->analyse_exp ?
1778 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
1779 TICKS_TO_MS(1000)) : "");
1780
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001781 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001782 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
1783 curr_sess->rep->flags,
1784 curr_sess->rep->l,
1785 curr_sess->rep->analysers,
1786 curr_sess->rep->rex ?
1787 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
1788 TICKS_TO_MS(1000)) : "");
1789
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001790 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001791 ",wx=%s",
1792 curr_sess->rep->wex ?
1793 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
1794 TICKS_TO_MS(1000)) : "");
1795
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001796 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001797 ",ax=%s]",
1798 curr_sess->rep->analyse_exp ?
1799 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
1800 TICKS_TO_MS(1000)) : "");
1801
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001802 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001803 " s0=[%d,%1xh,fd=%d,ex=%s]",
1804 curr_sess->si[0].state,
1805 curr_sess->si[0].flags,
1806 curr_sess->si[0].fd,
1807 curr_sess->si[0].exp ?
1808 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
1809 TICKS_TO_MS(1000)) : "");
1810
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001811 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001812 " s1=[%d,%1xh,fd=%d,ex=%s]",
1813 curr_sess->si[1].state,
1814 curr_sess->si[1].flags,
1815 curr_sess->si[1].fd,
1816 curr_sess->si[1].exp ?
1817 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
1818 TICKS_TO_MS(1000)) : "");
1819
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001820 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001821 " exp=%s",
1822 curr_sess->task->expire ?
1823 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
1824 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01001825 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001826 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001827
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001828 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001829
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001830 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001831 /* let's try again later from this session. We add ourselves into
1832 * this session's users so that it can remove us upon termination.
1833 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001834 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001835 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001836 }
1837
1838 s->data_ctx.sess.bref.ref = curr_sess->list.n;
1839 }
1840 s->data_state = DATA_ST_FIN;
1841 /* fall through */
1842
1843 default:
1844 s->data_state = DATA_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02001845 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001846 }
1847}
1848
Willy Tarreau74808cb2009-03-04 15:53:18 +01001849/* print a line of error buffer (limited to 70 bytes) to <out>. The format is :
1850 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
1851 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
1852 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
1853 * lines are respected within the limit of 70 output chars. Lines that are
1854 * continuation of a previous truncated line begin with "+" instead of " "
1855 * after the offset. The new pointer is returned.
1856 */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001857static int dump_error_line(struct chunk *out, struct error_snapshot *err,
1858 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01001859{
1860 int end;
1861 unsigned char c;
1862
1863 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001864 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01001865 return ptr;
1866
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001867 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01001868
Willy Tarreau61b34732009-10-03 23:49:35 +02001869 while (ptr < err->len && ptr < sizeof(err->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001870 c = err->buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01001871 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001872 if (out->len > end - 2)
1873 break;
1874 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01001875 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001876 if (out->len > end - 3)
1877 break;
1878 out->str[out->len++] = '\\';
1879 switch (c) {
1880 case '\t': c = 't'; break;
1881 case '\n': c = 'n'; break;
1882 case '\r': c = 'r'; break;
1883 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01001884 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001885 }
1886 out->str[out->len++] = c;
1887 } else {
1888 if (out->len > end - 5)
1889 break;
1890 out->str[out->len++] = '\\';
1891 out->str[out->len++] = 'x';
1892 out->str[out->len++] = hextab[(c >> 4) & 0xF];
1893 out->str[out->len++] = hextab[c & 0xF];
1894 }
1895 if (err->buf[ptr++] == '\n') {
1896 /* we had a line break, let's return now */
1897 out->str[out->len++] = '\n';
1898 *line = ptr;
1899 return ptr;
1900 }
1901 }
1902 /* we have an incomplete line, we return it as-is */
1903 out->str[out->len++] = '\n';
1904 return ptr;
1905}
1906
1907/* This function is called to send output to the response buffer.
1908 * It dumps the errors logged in proxies onto the output buffer <rep>.
1909 * Expects to be called with client socket shut down on input.
1910 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau61b34732009-10-03 23:49:35 +02001911 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau74808cb2009-03-04 15:53:18 +01001912 */
Willy Tarreau61b34732009-10-03 23:49:35 +02001913int stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau74808cb2009-03-04 15:53:18 +01001914{
1915 extern const char *monthname[12];
1916 struct chunk msg;
1917
Willy Tarreau61b34732009-10-03 23:49:35 +02001918 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW)))
1919 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001920
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001921 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01001922
1923 if (!s->data_ctx.errors.px) {
1924 /* the function had not been called yet, let's prepare the
1925 * buffer for a response.
1926 */
Willy Tarreau74808cb2009-03-04 15:53:18 +01001927 s->data_ctx.errors.px = proxy;
1928 s->data_ctx.errors.buf = 0;
1929 s->data_ctx.errors.bol = 0;
1930 s->data_ctx.errors.ptr = -1;
1931 }
1932
1933 /* we have two inner loops here, one for the proxy, the other one for
1934 * the buffer.
1935 */
1936 while (s->data_ctx.errors.px) {
1937 struct error_snapshot *es;
1938
1939 if (s->data_ctx.errors.buf == 0)
1940 es = &s->data_ctx.errors.px->invalid_req;
1941 else
1942 es = &s->data_ctx.errors.px->invalid_rep;
1943
1944 if (!es->when.tv_sec)
1945 goto next;
1946
1947 if (s->data_ctx.errors.iid >= 0 &&
1948 s->data_ctx.errors.px->uuid != s->data_ctx.errors.iid &&
1949 es->oe->uuid != s->data_ctx.errors.iid)
1950 goto next;
1951
1952 if (s->data_ctx.errors.ptr < 0) {
1953 /* just print headers now */
1954
1955 char pn[INET6_ADDRSTRLEN];
1956 struct tm tm;
1957
1958 get_localtime(es->when.tv_sec, &tm);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001959 chunk_printf(&msg, "\n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01001960 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001961 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01001962
1963
1964 if (es->src.ss_family == AF_INET)
1965 inet_ntop(AF_INET,
1966 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
1967 pn, sizeof(pn));
1968 else
1969 inet_ntop(AF_INET6,
1970 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
1971 pn, sizeof(pn));
1972
1973 switch (s->data_ctx.errors.buf) {
1974 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001975 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01001976 " frontend %s (#%d): invalid request\n"
1977 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
1978 " request length %d bytes, error at position %d:\n\n",
1979 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
1980 pn, es->sid, es->oe->id, es->oe->uuid,
1981 es->srv ? es->srv->id : "<NONE>",
1982 es->srv ? es->srv->puid : -1,
1983 es->len, es->pos);
1984 break;
1985 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001986 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01001987 " backend %s (#%d) : invalid response\n"
1988 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
1989 " response length %d bytes, error at position %d:\n\n",
1990 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
1991 pn, es->sid, es->oe->id, es->oe->uuid,
1992 es->srv ? es->srv->id : "<NONE>",
1993 es->srv ? es->srv->puid : -1,
1994 es->len, es->pos);
1995 break;
1996 }
1997
Willy Tarreau61b34732009-10-03 23:49:35 +02001998 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001999 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02002000 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002001 }
2002 s->data_ctx.errors.ptr = 0;
2003 s->data_ctx.errors.sid = es->sid;
2004 }
2005
2006 if (s->data_ctx.errors.sid != es->sid) {
2007 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002008 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01002009 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau61b34732009-10-03 23:49:35 +02002010 if (buffer_feed_chunk(rep, &msg) >= 0)
2011 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002012 goto next;
2013 }
2014
2015 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau61b34732009-10-03 23:49:35 +02002016 while (s->data_ctx.errors.ptr < es->len && s->data_ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002017 int newptr;
2018 int newline;
2019
2020 newline = s->data_ctx.errors.bol;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002021 newptr = dump_error_line(&msg, es, &newline, s->data_ctx.errors.ptr);
Willy Tarreau74808cb2009-03-04 15:53:18 +01002022 if (newptr == s->data_ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02002023 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002024
Willy Tarreau61b34732009-10-03 23:49:35 +02002025 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002026 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02002027 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002028 }
2029 s->data_ctx.errors.ptr = newptr;
2030 s->data_ctx.errors.bol = newline;
2031 };
2032 next:
2033 s->data_ctx.errors.bol = 0;
2034 s->data_ctx.errors.ptr = -1;
2035 s->data_ctx.errors.buf++;
2036 if (s->data_ctx.errors.buf > 1) {
2037 s->data_ctx.errors.buf = 0;
2038 s->data_ctx.errors.px = s->data_ctx.errors.px->next;
2039 }
2040 }
2041
2042 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02002043 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002044}
2045
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002046
Willy Tarreau10522fd2008-07-09 20:12:41 +02002047static struct cfg_kw_list cfg_kws = {{ },{
2048 { CFG_GLOBAL, "stats", stats_parse_global },
2049 { 0, NULL, NULL },
2050}};
2051
2052__attribute__((constructor))
2053static void __dumpstats_module_init(void)
2054{
2055 cfg_register_keywords(&cfg_kws);
2056}
2057
Willy Tarreau91861262007-10-17 17:06:05 +02002058/*
2059 * Local variables:
2060 * c-indent-level: 8
2061 * c-basic-offset: 8
2062 * End:
2063 */