blob: 32ca1b9607dbdfddb4f2b3abdece61fa75cb7d69 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaueb472682010-05-28 18:46:57 +02004 * Copyright 2000-2010 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 Tarreaueb472682010-05-28 18:46:57 +020047#include <proto/log.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010048#include <proto/pipe.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020049#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020050#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020051#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020052#include <proto/server.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010053#include <proto/stream_interface.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020054#include <proto/stream_sock.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010055#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020056
Willy Tarreau5ca791d2009-08-16 19:06:42 +020057const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020058 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +020059 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +020060 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020061 " help : this message\n"
62 " prompt : toggle interactive mode with prompt\n"
63 " quit : disconnect\n"
64 " show info : report information about the running process\n"
65 " show stat : report counters for each proxy and server\n"
66 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +010067 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +020068 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +020069 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +020070 " set weight : change a server's weight\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +010071 " set timeout : change a timeout setting\n"
Cyril Bontécd19e512010-01-31 22:34:03 +010072 " disable server : set a server in maintenance mode\n"
73 " enable server : re-enable a server that was previously in maintenance mode\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020074 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020075
Willy Tarreau6162db22009-10-10 17:13:00 +020076const char stats_permission_denied_msg[] =
77 "Permission denied\n"
78 "";
79
Willy Tarreaudecd14d2010-06-01 18:03:19 +020080/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +020081 * a new stats socket. It returns a positive value upon success, 0 if the connection
82 * needs to be closed and ignored, or a negative value upon critical failure.
83 */
Willy Tarreaudecd14d2010-06-01 18:03:19 +020084int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +020085{
Willy Tarreaudecd14d2010-06-01 18:03:19 +020086 /* we have a dedicated I/O handler for the stats */
Willy Tarreaueb472682010-05-28 18:46:57 +020087 stream_int_register_handler(&s->si[1], stats_io_handler);
88 s->si[1].private = s;
89 s->si[1].st1 = 0;
90 s->si[1].st0 = STAT_CLI_INIT;
91
Willy Tarreaudecd14d2010-06-01 18:03:19 +020092 tv_zero(&s->logs.tv_request);
93 s->logs.t_queue = 0;
94 s->logs.t_connect = 0;
95 s->logs.t_data = 0;
96 s->logs.t_close = 0;
97 s->logs.bytes_in = s->logs.bytes_out = 0;
98 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
99 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200100
101 s->data_state = DATA_ST_INIT;
102 s->data_source = DATA_SRC_NONE;
Willy Tarreaueb472682010-05-28 18:46:57 +0200103
Willy Tarreaueb472682010-05-28 18:46:57 +0200104 s->req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
105
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200106 if (s->listener->timeout) {
107 s->req->rto = *s->listener->timeout;
108 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200109 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200110 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200111}
112
Willy Tarreaufbee7132007-10-18 13:53:22 +0200113/* This function parses a "stats" statement in the "global" section. It returns
114 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
115 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
116 * zero included. The trailing '\n' must not be written. The function must be
117 * called with <args> pointing to the first word after "stats".
118 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200119static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
120 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200121{
Willy Tarreau10522fd2008-07-09 20:12:41 +0200122 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200123 if (!strcmp(args[0], "socket")) {
124 struct sockaddr_un su;
125 int cur_arg;
126
127 if (*args[1] == 0) {
128 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
129 return -1;
130 }
131
132 if (global.stats_sock.state != LI_NEW) {
133 snprintf(err, errlen, "'stats socket' already specified in global section");
134 return -1;
135 }
136
137 su.sun_family = AF_UNIX;
138 strncpy(su.sun_path, args[1], sizeof(su.sun_path));
139 su.sun_path[sizeof(su.sun_path) - 1] = 0;
140 memcpy(&global.stats_sock.addr, &su, sizeof(su)); // guaranteed to fit
141
Willy Tarreau89a63132009-08-16 17:41:45 +0200142 if (!global.stats_fe) {
143 if ((global.stats_fe = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) {
144 snprintf(err, errlen, "out of memory");
145 return -1;
146 }
147
148 LIST_INIT(&global.stats_fe->pendconns);
149 LIST_INIT(&global.stats_fe->acl);
150 LIST_INIT(&global.stats_fe->block_cond);
151 LIST_INIT(&global.stats_fe->redirect_rules);
152 LIST_INIT(&global.stats_fe->mon_fail_cond);
153 LIST_INIT(&global.stats_fe->switching_rules);
154 LIST_INIT(&global.stats_fe->tcp_req.inspect_rules);
155
156 /* Timeouts are defined as -1, so we cannot use the zeroed area
157 * as a default value.
158 */
159 proxy_reset_timeouts(global.stats_fe);
160
161 global.stats_fe->last_change = now.tv_sec;
162 global.stats_fe->id = strdup("GLOBAL");
163 global.stats_fe->cap = PR_CAP_FE;
Willy Tarreaueb472682010-05-28 18:46:57 +0200164 global.stats_fe->maxconn = global.stats_sock.maxconn;
Willy Tarreau89a63132009-08-16 17:41:45 +0200165 }
166
Willy Tarreaufbee7132007-10-18 13:53:22 +0200167 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100168 global.stats_sock.options = LI_O_NONE;
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200169 global.stats_sock.accept = session_accept;
170 global.stats_fe->accept = stats_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200171 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200172 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200173 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreaueb472682010-05-28 18:46:57 +0200174 global.stats_sock.frontend = global.stats_fe;
Willy Tarreau6162db22009-10-10 17:13:00 +0200175 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau89a63132009-08-16 17:41:45 +0200176
177 global.stats_fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
178 global.stats_sock.timeout = &global.stats_fe->timeout.client;
179
180 global.stats_sock.next = global.stats_fe->listen;
181 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200182
183 cur_arg = 2;
184 while (*args[cur_arg]) {
185 if (!strcmp(args[cur_arg], "uid")) {
186 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
187 cur_arg += 2;
188 }
189 else if (!strcmp(args[cur_arg], "gid")) {
190 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
191 cur_arg += 2;
192 }
193 else if (!strcmp(args[cur_arg], "mode")) {
194 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
195 cur_arg += 2;
196 }
197 else if (!strcmp(args[cur_arg], "user")) {
198 struct passwd *user;
199 user = getpwnam(args[cur_arg + 1]);
200 if (!user) {
201 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
202 args[cur_arg + 1]);
203 return -1;
204 }
205 global.stats_sock.perm.ux.uid = user->pw_uid;
206 cur_arg += 2;
207 }
208 else if (!strcmp(args[cur_arg], "group")) {
209 struct group *group;
210 group = getgrnam(args[cur_arg + 1]);
211 if (!group) {
212 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
213 args[cur_arg + 1]);
214 return -1;
215 }
216 global.stats_sock.perm.ux.gid = group->gr_gid;
217 cur_arg += 2;
218 }
Willy Tarreau6162db22009-10-10 17:13:00 +0200219 else if (!strcmp(args[cur_arg], "level")) {
220 if (!strcmp(args[cur_arg+1], "user"))
221 global.stats_sock.perm.ux.level = ACCESS_LVL_USER;
222 else if (!strcmp(args[cur_arg+1], "operator"))
223 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER;
224 else if (!strcmp(args[cur_arg+1], "admin"))
225 global.stats_sock.perm.ux.level = ACCESS_LVL_ADMIN;
226 else {
227 snprintf(err, errlen, "'stats socket level' only supports 'user', 'operator', and 'admin'");
228 return -1;
229 }
230 cur_arg += 2;
231 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200232 else {
Willy Tarreau6162db22009-10-10 17:13:00 +0200233 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', 'level', and 'mode'");
Willy Tarreaufbee7132007-10-18 13:53:22 +0200234 return -1;
235 }
236 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100237
Willy Tarreaufbee7132007-10-18 13:53:22 +0200238 uxst_add_listener(&global.stats_sock);
239 global.maxsock++;
240 }
241 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100242 unsigned timeout;
243 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
244
245 if (res) {
246 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
247 return -1;
248 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200249
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100250 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200251 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
252 return -1;
253 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200254 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200255 }
256 else if (!strcmp(args[0], "maxconn")) {
257 int maxconn = atol(args[1]);
258
259 if (maxconn <= 0) {
260 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
261 return -1;
262 }
263 global.maxsock -= global.stats_sock.maxconn;
264 global.stats_sock.maxconn = maxconn;
265 global.maxsock += global.stats_sock.maxconn;
Willy Tarreaueb472682010-05-28 18:46:57 +0200266 if (global.stats_fe)
267 global.stats_fe->maxconn = global.stats_sock.maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200268 }
269 else {
270 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
271 return -1;
272 }
273 return 0;
274}
275
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200276int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100277{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200278 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100279 "# pxname,svname,"
280 "qcur,qmax,"
281 "scur,smax,slim,stot,"
282 "bin,bout,"
283 "dreq,dresp,"
284 "ereq,econ,eresp,"
285 "wretr,wredis,"
286 "status,weight,act,bck,"
287 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200288 "pid,iid,sid,throttle,lbtot,tracked,type,"
289 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200290 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100291 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100292 "req_rate,req_rate_max,req_tot,"
293 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100294 "\n");
295}
296
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200297/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200298 * called from an applet running in a stream interface. The function returns 1
299 * if the request was understood, otherwise zero. It sets si->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200300 * designating the function which will have to process the request, which can
301 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200302 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200303int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200304{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200305 struct session *s = si->private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200306 char *args[MAX_STATS_ARGS + 1];
307 int arg;
308
309 while (isspace((unsigned char)*line))
310 line++;
311
312 arg = 0;
313 args[arg] = line;
314
315 while (*line && arg < MAX_STATS_ARGS) {
316 if (isspace((unsigned char)*line)) {
317 *line++ = '\0';
318
319 while (isspace((unsigned char)*line))
320 line++;
321
322 args[++arg] = line;
323 continue;
324 }
325
326 line++;
327 }
328
329 while (++arg <= MAX_STATS_ARGS)
330 args[arg] = line;
331
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200332 s->data_ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200333 if (strcmp(args[0], "show") == 0) {
334 if (strcmp(args[1], "stat") == 0) {
335 if (*args[2] && *args[3] && *args[4]) {
336 s->data_ctx.stats.flags |= STAT_BOUND;
337 s->data_ctx.stats.iid = atoi(args[2]);
338 s->data_ctx.stats.type = atoi(args[3]);
339 s->data_ctx.stats.sid = atoi(args[4]);
340 }
341
342 s->data_ctx.stats.flags |= STAT_SHOW_STAT;
343 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200344 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200345 si->st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200346 }
347 else if (strcmp(args[1], "info") == 0) {
348 s->data_ctx.stats.flags |= STAT_SHOW_INFO;
349 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200350 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200351 si->st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200352 }
353 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200354 s->data_state = DATA_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200355 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200356 s->data_ctx.cli.msg = stats_permission_denied_msg;
357 si->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200358 return 1;
359 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100360 if (*args[2])
361 s->data_ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
362 else
363 s->data_ctx.sess.target = NULL;
364 s->data_ctx.sess.section = 0; /* start with session status */
365 s->data_ctx.sess.pos = 0;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200366 si->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200367 }
368 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200369 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200370 s->data_ctx.cli.msg = stats_permission_denied_msg;
371 si->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200372 return 1;
373 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200374 if (*args[2])
375 s->data_ctx.errors.iid = atoi(args[2]);
376 else
377 s->data_ctx.errors.iid = -1;
378 s->data_ctx.errors.px = NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200379 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200380 si->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200381 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200382 else if (strcmp(args[1], "table") == 0) {
383 s->data_state = DATA_ST_INIT;
384 if (*args[2])
385 s->data_ctx.table.target = findproxy(args[2], 0);
386 else
387 s->data_ctx.table.target = NULL;
388 s->data_ctx.table.proxy = NULL;
389 s->data_ctx.table.entry = NULL;
390 si->st0 = STAT_CLI_O_TAB; // stats_dump_table_to_buffer
391 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200392 else { /* neither "stat" nor "info" nor "sess" nor "errors" no "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200393 return 0;
394 }
395 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200396 else if (strcmp(args[0], "clear") == 0) {
397 if (strcmp(args[1], "counters") == 0) {
398 struct proxy *px;
399 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200400 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200401 int clrall = 0;
402
403 if (strcmp(args[2], "all") == 0)
404 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200405
Willy Tarreau6162db22009-10-10 17:13:00 +0200406 /* check permissions */
407 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
408 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200409 s->data_ctx.cli.msg = stats_permission_denied_msg;
410 si->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200411 return 1;
412 }
413
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200414 for (px = proxy; px; px = px->next) {
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200415 if (clrall)
416 memset(&px->counters, 0, sizeof(px->counters));
417 else {
418 px->counters.feconn_max = 0;
419 px->counters.beconn_max = 0;
Willy Tarreaud9b587f2010-02-26 10:05:55 +0100420 px->counters.fe_rps_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200421 px->counters.fe_sps_max = 0;
Willy Tarreaub36b4242010-06-04 20:59:39 +0200422 px->counters.fe_cps_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200423 px->counters.be_sps_max = 0;
424 px->counters.nbpend_max = 0;
425 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200426
427 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200428 if (clrall)
429 memset(&sv->counters, 0, sizeof(sv->counters));
430 else {
431 sv->counters.cur_sess_max = 0;
432 sv->counters.nbpend_max = 0;
433 sv->counters.sps_max = 0;
434 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200435
436 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200437 if (li->counters) {
438 if (clrall)
439 memset(li->counters, 0, sizeof(*li->counters));
440 else
441 li->counters->conn_max = 0;
442 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200443 }
444
445 return 1;
446 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200447 else if (strcmp(args[1], "table") == 0) {
448 struct proxy *px;
449 struct stksess *ts;
450 unsigned int ip_key;
451
452 if (!*args[2]) {
453 s->data_ctx.cli.msg = "\"table\" argument expected.";
454 si->st0 = STAT_CLI_PRINT;
455 return 1;
456 }
457
458 px = find_stktable(args[2]);
459
460 if (!px) {
461 s->data_ctx.cli.msg = "No such table.";
462 si->st0 = STAT_CLI_PRINT;
463 return 1;
464 }
465
466 if (strcmp(args[3], "key") != 0) {
467 s->data_ctx.cli.msg = "\"key\" argument expected.";
468 si->st0 = STAT_CLI_PRINT;
469 return 1;
470 }
471
472 if (!*args[4]) {
473 s->data_ctx.cli.msg = "Key value expected.";
474 si->st0 = STAT_CLI_PRINT;
475 return 1;
476 }
477
478 if (px->table.type == STKTABLE_TYPE_IP) {
479 ip_key = htonl(inetaddr_host(args[4]));
480 static_table_key.key = (void *)&ip_key;
481 }
482 else {
483 s->data_ctx.cli.msg = "Removing keys from non-ip tables is not supported.";
484 si->st0 = STAT_CLI_PRINT;
485 return 1;
486 }
487
488 /* check permissions */
489 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
490 s->data_ctx.cli.msg = stats_permission_denied_msg;
491 si->st0 = STAT_CLI_PRINT;
492 return 1;
493 }
494
495 ts = stktable_lookup_key(&px->table, &static_table_key);
496 if (!ts) {
497 /* silent return, entry was already removed */
498 return 1;
499 }
500 else if (ts->ref_cnt) {
501 /* don't delete an entry which is currently referenced */
502 s->data_ctx.cli.msg = "Entry currently in use, cannot remove.";
503 si->st0 = STAT_CLI_PRINT;
504 return 1;
505 }
506
507 eb32_delete(&ts->exp);
508 ebmb_delete(&ts->key);
509 stksess_free(&px->table, ts);
510
511 /* end of processing */
512 return 1;
513 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200514 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200515 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200516 return 0;
517 }
518 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200519 else if (strcmp(args[0], "get") == 0) {
520 if (strcmp(args[1], "weight") == 0) {
521 struct proxy *px;
522 struct server *sv;
523
524 /* split "backend/server" and make <line> point to server */
525 for (line = args[2]; *line; line++)
526 if (*line == '/') {
527 *line++ = '\0';
528 break;
529 }
530
531 if (!*line) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200532 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
533 si->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200534 return 1;
535 }
536
537 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200538 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
539 si->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200540 return 1;
541 }
542
543 /* return server's effective weight at the moment */
544 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
545 buffer_feed(si->ib, trash);
546 return 1;
547 }
548 else { /* not "get weight" */
549 return 0;
550 }
551 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200552 else if (strcmp(args[0], "set") == 0) {
553 if (strcmp(args[1], "weight") == 0) {
554 struct proxy *px;
555 struct server *sv;
556 int w;
557
558 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200559 s->data_ctx.cli.msg = stats_permission_denied_msg;
560 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200561 return 1;
562 }
563
564 /* split "backend/server" and make <line> point to server */
565 for (line = args[2]; *line; line++)
566 if (*line == '/') {
567 *line++ = '\0';
568 break;
569 }
570
571 if (!*line || !*args[3]) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200572 s->data_ctx.cli.msg = "Require 'backend/server' and 'weight' or 'weight%'.\n";
573 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200574 return 1;
575 }
576
577 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200578 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
579 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200580 return 1;
581 }
582
583 /* if the weight is terminated with '%', it is set relative to
584 * the initial weight, otherwise it is absolute.
585 */
586 w = atoi(args[3]);
587 if (strchr(args[3], '%') != NULL) {
588 if (w < 0 || w > 100) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200589 s->data_ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
590 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200591 return 1;
592 }
593 w = sv->iweight * w / 100;
594 }
595 else {
596 if (w < 0 || w > 256) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200597 s->data_ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
598 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200599 return 1;
600 }
601 }
602
603 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200604 s->data_ctx.cli.msg = "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
605 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200606 return 1;
607 }
608
609 sv->uweight = w;
610
611 if (px->lbprm.algo & BE_LB_PROP_DYN) {
612 /* we must take care of not pushing the server to full throttle during slow starts */
613 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
614 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
615 else
616 sv->eweight = BE_WEIGHT_SCALE;
617 sv->eweight *= sv->uweight;
618 } else {
619 sv->eweight = sv->uweight;
620 }
621
622 /* static LB algorithms are a bit harder to update */
623 if (px->lbprm.update_server_eweight)
624 px->lbprm.update_server_eweight(sv);
625 else if (sv->eweight)
626 px->lbprm.set_server_status_up(sv);
627 else
628 px->lbprm.set_server_status_down(sv);
629
630 return 1;
631 }
Willy Tarreau7aabd112010-01-26 10:59:06 +0100632 else if (strcmp(args[1], "timeout") == 0) {
633 if (strcmp(args[2], "cli") == 0) {
634 unsigned timeout;
635 const char *res;
636
637 if (!*args[3]) {
638 s->data_ctx.cli.msg = "Expects an integer value.\n";
639 si->st0 = STAT_CLI_PRINT;
640 return 1;
641 }
642
643 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
644 if (res || timeout < 1) {
645 s->data_ctx.cli.msg = "Invalid timeout value.\n";
646 si->st0 = STAT_CLI_PRINT;
647 return 1;
648 }
649
650 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
651 return 1;
652 }
653 else {
654 s->data_ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
655 si->st0 = STAT_CLI_PRINT;
656 return 1;
657 }
658 }
659 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +0200660 return 0;
661 }
662 }
Cyril Bontécd19e512010-01-31 22:34:03 +0100663 else if (strcmp(args[0], "enable") == 0) {
664 if (strcmp(args[1], "server") == 0) {
665 struct proxy *px;
666 struct server *sv;
667
668 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
669 s->data_ctx.cli.msg = stats_permission_denied_msg;
670 si->st0 = STAT_CLI_PRINT;
671 return 1;
672 }
673
674 /* split "backend/server" and make <line> point to server */
675 for (line = args[2]; *line; line++)
676 if (*line == '/') {
677 *line++ = '\0';
678 break;
679 }
680
681 if (!*line || !*args[2]) {
682 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
683 si->st0 = STAT_CLI_PRINT;
684 return 1;
685 }
686
687 if (!get_backend_server(args[2], line, &px, &sv)) {
688 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
689 si->st0 = STAT_CLI_PRINT;
690 return 1;
691 }
692
693 if (sv->state & SRV_MAINTAIN) {
694 /* The server is really in maintenance, we can change the server state */
695 if (sv->tracked) {
696 /* If this server tracks the status of another one,
697 * we must restore the good status.
698 */
699 if (sv->tracked->state & SRV_RUNNING) {
700 set_server_up(sv);
701 } else {
702 sv->state &= ~SRV_MAINTAIN;
703 set_server_down(sv);
704 }
705 } else {
706 set_server_up(sv);
707 }
708 }
709
710 return 1;
711 }
712 else { /* unknown "enable" parameter */
713 return 0;
714 }
715 }
716 else if (strcmp(args[0], "disable") == 0) {
717 if (strcmp(args[1], "server") == 0) {
718 struct proxy *px;
719 struct server *sv;
720
721 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
722 s->data_ctx.cli.msg = stats_permission_denied_msg;
723 si->st0 = STAT_CLI_PRINT;
724 return 1;
725 }
726
727 /* split "backend/server" and make <line> point to server */
728 for (line = args[2]; *line; line++)
729 if (*line == '/') {
730 *line++ = '\0';
731 break;
732 }
733
734 if (!*line || !*args[2]) {
735 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
736 si->st0 = STAT_CLI_PRINT;
737 return 1;
738 }
739
740 if (!get_backend_server(args[2], line, &px, &sv)) {
741 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
742 si->st0 = STAT_CLI_PRINT;
743 return 1;
744 }
745
746 if (! (sv->state & SRV_MAINTAIN)) {
747 /* Not already in maintenance, we can change the server state */
748 sv->state |= SRV_MAINTAIN;
749 set_server_down(sv);
750 }
751
752 return 1;
753 }
754 else { /* unknown "disable" parameter */
755 return 0;
756 }
757 }
758 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200759 return 0;
760 }
761 return 1;
762}
763
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200764/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200765 * used to processes I/O from/to the stats unix socket. The system relies on a
766 * state machine handling requests and various responses. We read a request,
767 * then we process it and send the response, and we possibly display a prompt.
768 * Then we can read again. The state is stored in si->st0 and is one of the
769 * STAT_CLI_* constants. si->st1 is used to indicate whether prompt is enabled
770 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200771 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200772void stats_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200773{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200774 struct session *s = si->private;
775 struct buffer *req = si->ob;
776 struct buffer *res = si->ib;
777 int reql;
778 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200779
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200780 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
781 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200782
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200783 while (1) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200784 if (si->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200785 /* Stats output not initialized yet */
786 memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats));
787 s->data_source = DATA_SRC_STATS;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200788 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200789 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200790 else if (si->st0 == STAT_CLI_END) {
791 /* Let's close for real now. We just close the request
792 * side, the conditions below will complete if needed.
793 */
794 si->shutw(si);
795 break;
796 }
797 else if (si->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +0200798 /* ensure we have some output room left in the event we
799 * would want to return some info right after parsing.
800 */
801 if (buffer_almost_full(si->ib))
802 break;
803
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200804 reql = buffer_si_peekline(si->ob, trash, sizeof(trash));
805 if (reql <= 0) { /* closed or EOL not found */
806 if (reql == 0)
807 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200808 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200809 continue;
810 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200811
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200812 /* seek for a possible semi-colon. If we find one, we
813 * replace it with an LF and skip only this part.
814 */
815 for (len = 0; len < reql; len++)
816 if (trash[len] == ';') {
817 trash[len] = '\n';
818 reql = len + 1;
819 break;
820 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200821
Willy Tarreau816fc222009-10-04 07:36:58 +0200822 /* now it is time to check that we have a full line,
823 * remove the trailing \n and possibly \r, then cut the
824 * line.
825 */
826 len = reql - 1;
827 if (trash[len] != '\n') {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200828 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200829 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200830 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200831
Willy Tarreau816fc222009-10-04 07:36:58 +0200832 if (len && trash[len-1] == '\r')
833 len--;
834
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200835 trash[len] = '\0';
836
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200837 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200838 if (len) {
839 if (strcmp(trash, "quit") == 0) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200840 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200841 continue;
842 }
843 else if (strcmp(trash, "prompt") == 0)
844 si->st1 = !si->st1;
845 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200846 !stats_sock_parse_request(si, trash)) {
847 s->data_ctx.cli.msg = stats_sock_usage_msg;
848 si->st0 = STAT_CLI_PRINT;
849 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200850 /* NB: stats_sock_parse_request() may have put
851 * another STAT_CLI_O_* into si->st0.
852 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200853 }
854 else if (!si->st1) {
855 /* if prompt is disabled, print help on empty lines,
856 * so that the user at least knows how to enable
857 * prompt and find help.
858 */
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200859 s->data_ctx.cli.msg = stats_sock_usage_msg;
860 si->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200861 }
862
863 /* re-adjust req buffer */
864 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200865 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200866 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200867 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200868 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200869 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200870 continue;
871 }
872
873 switch (si->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200874 case STAT_CLI_PRINT:
875 if (buffer_feed(si->ib, s->data_ctx.cli.msg) < 0)
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200876 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200877 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200878 case STAT_CLI_O_INFO:
Willy Tarreau24955a12009-10-04 11:54:04 +0200879 if (stats_dump_raw_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200880 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200881 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200882 case STAT_CLI_O_SESS:
Willy Tarreau7e72a8f2009-10-03 23:55:14 +0200883 if (stats_dump_sess_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200884 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200885 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200886 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau61b34732009-10-03 23:49:35 +0200887 if (stats_dump_errors_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200888 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200889 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +0200890 case STAT_CLI_O_TAB:
891 if (stats_dump_table_to_buffer(s, res))
892 si->st0 = STAT_CLI_PROMPT;
893 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200894 default: /* abnormal state */
895 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200896 break;
897 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200898
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200899 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
900 if (si->st0 == STAT_CLI_PROMPT) {
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200901 if (buffer_feed(si->ib, si->st1 ? "\n> " : "\n") < 0)
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200902 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200903 }
904
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200905 /* If the output functions are still there, it means they require more room. */
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200906 if (si->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200907 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200908
909 /* Now we close the output if one of the writers did so,
910 * or if we're not in interactive mode and the request
911 * buffer is empty. This still allows pipelined requests
912 * to be sent in non-interactive mode.
913 */
914 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->st1 && !req->send_max)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200915 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200916 continue;
917 }
918
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200919 /* switch state back to GETREQ to read next requests */
920 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200921 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200922 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200923
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200924 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200925 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
926 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
927 /* Other size has closed, let's abort if we have no more processing to do
928 * and nothing more to consume. This is comparable to a broken pipe, so
929 * we forward the close to the request side so that it flows upstream to
930 * the client.
931 */
932 si->shutw(si);
933 }
934
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200935 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200936 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
937 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
938 /* We have no more processing to do, and nothing more to send, and
939 * the client side has closed. So we'll forward this state downstream
940 * on the response buffer.
941 */
942 si->shutr(si);
943 res->flags |= BF_READ_NULL;
944 }
945
946 /* update all other flags and resync with the other side */
947 si->update(si);
948
949 /* we don't want to expire timeouts while we're processing requests */
950 si->ib->rex = TICK_ETERNITY;
951 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200952
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200953 out:
954 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
955 __FUNCTION__, __LINE__,
956 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
957
958 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
959 /* check that we have released everything then unregister */
960 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200961 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200962}
963
Willy Tarreau24955a12009-10-04 11:54:04 +0200964/* This function is called to send output to the response buffer.
965 * It dumps statistics onto the output buffer <rep> owned by session <s>.
966 * s->data_ctx must have been zeroed first, and the flags properly set.
967 * It returns 0 as long as it does not complete, non-zero upon completion.
968 * Some states are not used but it makes the code more similar to other
969 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200970 */
Willy Tarreau24955a12009-10-04 11:54:04 +0200971int stats_dump_raw_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3e76e722007-10-17 18:57:38 +0200972{
Willy Tarreau3e76e722007-10-17 18:57:38 +0200973 struct proxy *px;
974 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +0100975 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200976
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200977 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +0200978
979 switch (s->data_state) {
980 case DATA_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +0200981 /* the function had not been called yet */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200982 s->data_state = DATA_ST_HEAD;
983 /* fall through */
984
985 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100986 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200987 print_csv_header(&msg);
Willy Tarreau24955a12009-10-04 11:54:04 +0200988 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100989 return 0;
990 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200991
992 s->data_state = DATA_ST_INFO;
993 /* fall through */
994
995 case DATA_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +0100996 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100997 if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200998 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +0100999 "Name: " PRODUCT_NAME "\n"
1000 "Version: " HAPROXY_VERSION "\n"
1001 "Release_date: " HAPROXY_DATE "\n"
1002 "Nbproc: %d\n"
1003 "Process_num: %d\n"
1004 "Pid: %d\n"
1005 "Uptime: %dd %dh%02dm%02ds\n"
1006 "Uptime_sec: %d\n"
1007 "Memmax_MB: %d\n"
1008 "Ulimit-n: %d\n"
1009 "Maxsock: %d\n"
1010 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001011 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001012 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001013 "PipesUsed: %d\n"
1014 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001015 "Tasks: %d\n"
1016 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001017 "node: %s\n"
1018 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001019 "",
1020 global.nbproc,
1021 relative_pid,
1022 pid,
1023 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1024 up,
1025 global.rlimit_memmax,
1026 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001027 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001028 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001029 nb_tasks_cur, run_queue_cur,
1030 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001031 );
Willy Tarreau24955a12009-10-04 11:54:04 +02001032 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001033 return 0;
1034 }
1035
Willy Tarreau3e76e722007-10-17 18:57:38 +02001036 s->data_ctx.stats.px = proxy;
1037 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001038
1039 s->data_ctx.stats.sv = NULL;
1040 s->data_ctx.stats.sv_st = 0;
1041
Willy Tarreau3e76e722007-10-17 18:57:38 +02001042 s->data_state = DATA_ST_LIST;
1043 /* fall through */
1044
1045 case DATA_ST_LIST:
1046 /* dump proxies */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001047 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +01001048 while (s->data_ctx.stats.px) {
1049 px = s->data_ctx.stats.px;
1050 /* skip the disabled proxies and non-networked ones */
1051 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001052 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001053 if (stats_dump_proxy(s, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001054 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001055 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001056
Willy Tarreaua8efd362008-01-03 10:19:15 +01001057 s->data_ctx.stats.px = px->next;
1058 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1059 }
1060 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001061 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001062
1063 s->data_state = DATA_ST_END;
1064 /* fall through */
1065
1066 case DATA_ST_END:
1067 s->data_state = DATA_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001068 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001069
1070 case DATA_ST_FIN:
1071 return 1;
1072
1073 default:
1074 /* unknown state ! */
Willy Tarreau24955a12009-10-04 11:54:04 +02001075 s->data_state = DATA_ST_FIN;
1076 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001077 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001078}
1079
1080
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001081/* This I/O handler runs as an applet embedded in a stream interface. It is
1082 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
1083 * si->st0 becomes non-zero once the transfer is finished. The handler
1084 * automatically unregisters itself once transfer is complete.
1085 */
1086void http_stats_io_handler(struct stream_interface *si)
1087{
1088 struct session *s = si->private;
1089 struct buffer *req = si->ob;
1090 struct buffer *res = si->ib;
1091
1092 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1093 goto out;
1094
1095 /* check that the output is not closed */
1096 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
1097 si->st0 = 1;
1098
1099 if (!si->st0) {
1100 if (stats_dump_http(s, res, s->be->uri_auth)) {
1101 si->st0 = 1;
1102 si->shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001103 }
1104 }
1105
1106 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
1107 si->shutw(si);
1108
1109 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->st0) {
1110 si->shutr(si);
1111 res->flags |= BF_READ_NULL;
1112 }
1113
1114 /* update all other flags and resync with the other side */
1115 si->update(si);
1116
1117 /* we don't want to expire timeouts while we're processing requests */
1118 si->ib->rex = TICK_ETERNITY;
1119 si->ob->wex = TICK_ETERNITY;
1120
1121 out:
1122 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1123 /* check that we have released everything then unregister */
1124 stream_int_unregister_handler(si);
1125 }
1126}
1127
1128
Willy Tarreau3e76e722007-10-17 18:57:38 +02001129/*
1130 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +02001131 * client socket shut down on input. It stops by itself by unsetting the
Willy Tarreau72b179a2008-08-28 16:01:32 +02001132 * BF_HIJACK flag from the buffer, which it uses to keep on being called
Willy Tarreau1ae3a052008-08-16 10:56:30 +02001133 * when there is free space in the buffer, of simply by letting an empty buffer
1134 * upon return.s->data_ctx must have been zeroed before the first call, and the
1135 * flags set. It returns 0 if it had to stop writing data and an I/O is needed,
1136 * 1 if the dump is finished and the session must be closed, or -1 in case of
1137 * any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001138 */
Willy Tarreau0a464892008-12-07 18:30:00 +01001139int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001140{
Willy Tarreau91861262007-10-17 17:06:05 +02001141 struct proxy *px;
1142 struct chunk msg;
1143 unsigned int up;
1144
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001145 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001146
1147 switch (s->data_state) {
1148 case DATA_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001149 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001150 "HTTP/1.0 200 OK\r\n"
1151 "Cache-Control: no-cache\r\n"
1152 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001153 "Content-Type: %s\r\n",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001154 (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001155
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001156 if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001157 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001158 uri->refresh);
1159
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001160 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001161
1162 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001163 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001164 return 0;
1165
Willy Tarreau91861262007-10-17 17:06:05 +02001166 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1167 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1168 if (!(s->flags & SN_FINST_MASK))
1169 s->flags |= SN_FINST_R;
1170
1171 if (s->txn.meth == HTTP_METH_HEAD) {
1172 /* that's all we return in case of HEAD request */
1173 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001174 return 1;
1175 }
1176
1177 s->data_state = DATA_ST_HEAD; /* let's start producing data */
1178 /* fall through */
1179
1180 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001181 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001182 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001183 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001184 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1185 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001186 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001187 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1188 "<style type=\"text/css\"><!--\n"
1189 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001190 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001191 " font-size: 12px;"
1192 " font-weight: normal;"
1193 " color: black;"
1194 " background: white;"
1195 "}\n"
1196 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001197 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001198 "}\n"
1199 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001200 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001201 " margin-bottom: 0.5em;"
1202 "}\n"
1203 "h2 {"
1204 " font-family: helvetica, arial;"
1205 " font-size: x-large;"
1206 " font-weight: bold;"
1207 " font-style: italic;"
1208 " color: #6020a0;"
1209 " margin-top: 0em;"
1210 " margin-bottom: 0em;"
1211 "}\n"
1212 "h3 {"
1213 " font-family: helvetica, arial;"
1214 " font-size: 16px;"
1215 " font-weight: bold;"
1216 " color: #b00040;"
1217 " background: #e8e8d0;"
1218 " margin-top: 0em;"
1219 " margin-bottom: 0em;"
1220 "}\n"
1221 "li {"
1222 " margin-top: 0.25em;"
1223 " margin-right: 2em;"
1224 "}\n"
1225 ".hr {margin-top: 0.25em;"
1226 " border-color: black;"
1227 " border-bottom-style: solid;"
1228 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001229 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001230 ".total {background: #20D0D0;color: #ffff80;}\n"
1231 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001232 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001233 ".backend {background: #e8e8d0;}\n"
1234 ".active0 {background: #ff9090;}\n"
1235 ".active1 {background: #ffd020;}\n"
1236 ".active2 {background: #ffffa0;}\n"
1237 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001238 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1239 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1240 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001241 ".backup0 {background: #ff9090;}\n"
1242 ".backup1 {background: #ff80ff;}\n"
1243 ".backup2 {background: #c060ff;}\n"
1244 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001245 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1246 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1247 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001248 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001249 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001250 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001251 "a.px:link {color: #ffff40; text-decoration: none;}"
1252 "a.px:visited {color: #ffff40; text-decoration: none;}"
1253 "a.px:hover {color: #ffffff; text-decoration: none;}"
1254 "a.lfsb:link {color: #000000; text-decoration: none;}"
1255 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1256 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1257 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001258 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001259 "table.tbl td { text-align: right; border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray; white-space: nowrap;}\n"
1260 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001261 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001262 "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 +02001263 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001264 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001265 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001266 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1267 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1268 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001269 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001270 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001271 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001272 (uri->flags&ST_SHNODE) ? " on " : "",
1273 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001274 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001275 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001276 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001277 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001278 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001279 return 0;
1280
1281 s->data_state = DATA_ST_INFO;
1282 /* fall through */
1283
1284 case DATA_ST_INFO:
1285 up = (now.tv_sec - start_date.tv_sec);
1286
1287 /* WARNING! this has to fit the first packet too.
1288 * We are around 3.5 kB, add adding entries will
1289 * become tricky if we want to support 4kB buffers !
1290 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001291 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001292 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001293 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1294 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001295 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001296 "<hr width=\"100%%\" class=\"hr\">\n"
1297 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001298 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001299 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001300 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001301 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1302 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
1303 "current conns = %d; current pipes = %d/%d<br>\n"
1304 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001305 "</td><td align=\"center\" nowrap>\n"
1306 "<table class=\"lgd\"><tr>\n"
1307 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1308 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1309 "</tr><tr>\n"
1310 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1311 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1312 "</tr><tr>\n"
1313 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1314 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1315 "</tr><tr>\n"
1316 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001317 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001318 "</tr><tr>\n"
1319 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001320 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001321 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001322 "</td>"
1323 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1324 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1325 "",
1326 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001327 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1328 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1329 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001330 up / 86400, (up % 86400) / 3600,
1331 (up % 3600) / 60, (up % 60),
1332 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1333 global.rlimit_memmax ? " MB" : "",
1334 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001335 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001336 actconn, pipes_used, pipes_used+pipes_free,
1337 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +02001338 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001339
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001340 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001341 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001342 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1343 uri->uri_prefix,
1344 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001345 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001346 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001347 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001348 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1349 uri->uri_prefix,
1350 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001351 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001352
Willy Tarreau55bb8452007-10-17 18:44:57 +02001353 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001354 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001355 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001356 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1357 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001358 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001359 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001360 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001361 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001362 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1363 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001364 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001365 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001366 }
Willy Tarreau91861262007-10-17 17:06:05 +02001367
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001368 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001369 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
1370 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001371 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
1372 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001373
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001374 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02001375 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
1376 uri->uri_prefix,
1377 (uri->refresh > 0) ? ";norefresh" : "");
1378
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001379 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001380 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001381 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1382 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
1383 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
1384 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
1385 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
1386 "</ul>"
1387 "</td>"
1388 "</tr></table>\n"
1389 ""
1390 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001391
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001392 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001393 return 0;
1394 }
Willy Tarreau91861262007-10-17 17:06:05 +02001395
Willy Tarreau91861262007-10-17 17:06:05 +02001396 s->data_ctx.stats.px = proxy;
1397 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1398 s->data_state = DATA_ST_LIST;
1399 /* fall through */
1400
1401 case DATA_ST_LIST:
1402 /* dump proxies */
1403 while (s->data_ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001404 if (buffer_almost_full(rep))
1405 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02001406 px = s->data_ctx.stats.px;
1407 /* skip the disabled proxies and non-networked ones */
1408 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001409 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001410 return 0;
1411
1412 s->data_ctx.stats.px = px->next;
1413 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1414 }
1415 /* here, we just have reached the last proxy */
1416
1417 s->data_state = DATA_ST_END;
1418 /* fall through */
1419
1420 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001421 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001422 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001423 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001424 return 0;
1425 }
Willy Tarreau91861262007-10-17 17:06:05 +02001426
1427 s->data_state = DATA_ST_FIN;
1428 /* fall through */
1429
1430 case DATA_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02001431 return 1;
1432
1433 default:
1434 /* unknown state ! */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001435 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001436 return -1;
1437 }
1438}
1439
1440
1441/*
1442 * Dumps statistics for a proxy.
1443 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1444 * ot non-zero if everything completed.
1445 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001446int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001447{
1448 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001449 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001450 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02001451 struct chunk msg;
1452
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001453 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001454
1455 switch (s->data_ctx.stats.px_st) {
1456 case DATA_ST_PX_INIT:
1457 /* we are on a new proxy */
1458
1459 if (uri && uri->scope) {
1460 /* we have a limited scope, we have to check the proxy name */
1461 struct stat_scope *scope;
1462 int len;
1463
1464 len = strlen(px->id);
1465 scope = uri->scope;
1466
1467 while (scope) {
1468 /* match exact proxy name */
1469 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1470 break;
1471
1472 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001473 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001474 break;
1475 scope = scope->next;
1476 }
1477
1478 /* proxy name not found : don't dump anything */
1479 if (scope == NULL)
1480 return 1;
1481 }
1482
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001483 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001484 (px->uuid != s->data_ctx.stats.iid))
1485 return 1;
1486
Willy Tarreau91861262007-10-17 17:06:05 +02001487 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
1488 /* fall through */
1489
1490 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001491 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001492 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001493 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001494 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001495 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001496 "<th class=\"pxname\" width=\"10%%\"");
1497
1498 if (uri->flags&ST_SHLGNDS) {
1499 /* cap, mode, id */
1500 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
1501 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1502 px->uuid);
1503
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001504 chunk_printf(&msg, "\"");
1505 }
1506
1507 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001508 ">%s<a name=\"%s\"></a>"
1509 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001510 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001511 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001512 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001513 "<table class=\"tbl\" width=\"100%%\">\n"
1514 "<tr class=\"titre\">"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001515 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001516 "<th colspan=3>Queue</th>"
1517 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001518 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001519 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001520 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001521 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001522 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001523 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001524 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001525 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001526 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001527 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001528 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001529 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1530 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001531 "</tr>",
Willy Tarreaue0454092010-02-26 12:29:07 +01001532 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001533 px->id, px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01001534 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001535 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001536
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001537 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001538 return 0;
1539 }
Willy Tarreau91861262007-10-17 17:06:05 +02001540
1541 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
1542 /* fall through */
1543
1544 case DATA_ST_PX_FE:
1545 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001546 if ((px->cap & PR_CAP_FE) &&
1547 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
1548 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001549 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001550 /* name, queue */
Krzysztof Piotr Oledzki516ed492009-10-22 22:48:09 +02001551 "<tr class=\"frontend\"><td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001552 "<a name=\"%s/Frontend\"></a>"
1553 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td><td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01001554 "",
1555 px->id, px->id);
1556
1557 if (px->mode == PR_MODE_HTTP) {
1558 chunk_printf(&msg,
1559 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01001560 "<td title=\"Cur: %u req/s\"><u>%s</u></td><td title=\"Max: %u req/s\"><u>%s</u></td><td>%s</td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01001561 "",
1562 read_freq_ctr(&px->fe_req_per_sec),
1563 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
1564 px->counters.fe_rps_max,
1565 U2H2(px->counters.fe_sps_max),
1566 LIM2A2(px->fe_sps_lim, "-"));
1567 } else {
1568 chunk_printf(&msg,
1569 /* sessions rate : current, max, limit */
1570 "<td>%s</td><td>%s</td><td>%s</td>"
1571 "",
1572 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
1573 U2H1(px->counters.fe_sps_max), LIM2A2(px->fe_sps_lim, "-"));
1574 }
1575
1576 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001577 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001578 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001579 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001580 "",
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001581 U2H3(px->feconn), U2H4(px->counters.feconn_max), U2H5(px->maxconn));
1582
1583 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1584 if (px->mode == PR_MODE_HTTP) {
1585 int i;
1586
Willy Tarreaub44939a2010-02-26 11:35:39 +01001587 chunk_printf(&msg, " title=\"%lld requests:", px->counters.cum_fe_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001588
1589 for (i = 1; i < 6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01001590 chunk_printf(&msg, " %dxx=%lld,", i, px->counters.fe.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001591
Willy Tarreau24657792010-02-26 10:30:28 +01001592 chunk_printf(&msg, " other=%lld\"", px->counters.fe.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001593 }
1594
1595 chunk_printf(&msg,
1596 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01001597 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001598 /* bytes : in, out */
1599 "<td>%s</td><td>%s</td>"
1600 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001601 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreaub36b4242010-06-04 20:59:39 +02001602 U2H6(px->counters.cum_fesess),
Willy Tarreaue0454092010-02-26 12:29:07 +01001603 (px->mode == PR_MODE_HTTP)?"</u>":"",
1604 U2H7(px->counters.bytes_in), U2H8(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001605
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001606 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001607 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001608 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001609 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001610 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001611 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001612 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001613 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001614 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001615 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001616 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02001617 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001618 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
1619 U2H2(px->counters.failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +02001620 px->state == PR_STRUN ? "OPEN" :
1621 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001622 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001623 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001624 /* pxid, name, queue cur, queue max, */
1625 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001626 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001627 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001628 /* bytes : in, out */
1629 "%lld,%lld,"
1630 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001631 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001632 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001633 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001634 /* warnings: retries, redispatches */
1635 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001636 /* server status : reflect frontend status */
1637 "%s,"
1638 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001639 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001640 /* pid, iid, sid, throttle, lbtot, tracked, type */
1641 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001642 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001643 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001644 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001645 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02001646 px->id,
Willy Tarreaub36b4242010-06-04 20:59:39 +02001647 px->feconn, px->counters.feconn_max, px->maxconn, px->counters.cum_fesess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001648 px->counters.bytes_in, px->counters.bytes_out,
1649 px->counters.denied_req, px->counters.denied_resp,
1650 px->counters.failed_req,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001651 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +01001652 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01001653 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001654 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001655 px->fe_sps_lim, px->counters.fe_sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001656
1657 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1658 if (px->mode == PR_MODE_HTTP) {
1659 int i;
1660
1661 for (i=1; i<6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01001662 chunk_printf(&msg, "%lld,", px->counters.fe.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001663
Willy Tarreau24657792010-02-26 10:30:28 +01001664 chunk_printf(&msg, "%lld,", px->counters.fe.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001665 } else {
1666 chunk_printf(&msg, ",,,,,,");
1667 }
1668
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001669 /* failed health analyses */
1670 chunk_printf(&msg, ",");
1671
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001672 /* requests : req_rate, req_rate_max, req_tot, */
1673 chunk_printf(&msg, "%u,%u,%lld,",
1674 read_freq_ctr(&px->fe_req_per_sec),
1675 px->counters.fe_rps_max, px->counters.cum_fe_req);
1676
Willy Tarreauae526782010-03-04 20:34:23 +01001677 /* errors: cli_aborts, srv_aborts */
1678 chunk_printf(&msg, ",,");
1679
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001680 /* finish with EOL */
1681 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001682 }
Willy Tarreau91861262007-10-17 17:06:05 +02001683
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001684 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001685 return 0;
1686 }
1687
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001688 s->data_ctx.stats.l = px->listen; /* may be NULL */
1689 s->data_ctx.stats.px_st = DATA_ST_PX_LI;
1690 /* fall through */
1691
1692 case DATA_ST_PX_LI:
1693 /* stats.l has been initialized above */
1694 for (; s->data_ctx.stats.l != NULL; s->data_ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001695 if (buffer_almost_full(rep))
1696 return 0;
1697
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001698 l = s->data_ctx.stats.l;
1699 if (!l->counters)
1700 continue;
1701
1702 if (s->data_ctx.stats.flags & STAT_BOUND) {
1703 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SO)))
1704 break;
1705
1706 if (s->data_ctx.stats.sid != -1 && l->luid != s->data_ctx.stats.sid)
1707 continue;
1708 }
1709
1710 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001711 chunk_printf(&msg, "<tr class=socket><td class=ac");
1712
1713 if (uri->flags&ST_SHLGNDS) {
1714 char str[INET6_ADDRSTRLEN], *fmt = NULL;
1715 int port;
1716
1717 chunk_printf(&msg, " title=\"IP: ");
1718
1719 port = (l->addr.ss_family == AF_INET6)
1720 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
1721 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
1722
1723 if (l->addr.ss_family == AF_INET) {
1724 if (inet_ntop(AF_INET,
1725 (const void *)&((struct sockaddr_in *)&l->addr)->sin_addr,
1726 str, sizeof(str)))
1727 fmt = "%s:%d";
1728 } else {
1729 if (inet_ntop(AF_INET6,
1730 (const void *)&((struct sockaddr_in6 *)(&l->addr))->sin6_addr,
1731 str, sizeof(str)))
1732 fmt = "[%s]:%d";
1733 }
1734
1735 if (fmt)
1736 chunk_printf(&msg, fmt, str, port);
1737 else
1738 chunk_printf(&msg, "(%s)", strerror(errno));
1739
1740 /* id */
1741 chunk_printf(&msg, ", id: %d", l->luid);
1742
1743 chunk_printf(&msg, "\"");
1744 }
1745
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001746 chunk_printf(&msg,
1747 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01001748 ">%s<a name=\"%s/+%s\"></a>"
1749 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001750 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001751 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001752 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001753 "<td>%s</td><td>%s</td><td>%s</td>"
1754 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001755 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001756 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001757 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001758 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01001759 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01001760 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001761 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
1762 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
1763
1764 chunk_printf(&msg,
1765 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001766 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001767 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001768 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001769 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001770 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001771 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001772 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001773 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001774 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001775 "",
1776 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
1777 U2H2(l->counters->failed_req),
1778 (l->nbconn < l->maxconn) ? "OPEN" : "FULL");
1779 } else {
1780 chunk_printf(&msg,
1781 /* pxid, name, queue cur, queue max, */
1782 "%s,%s,,,"
1783 /* sessions: current, max, limit, total */
1784 "%d,%d,%d,%lld,"
1785 /* bytes: in, out */
1786 "%lld,%lld,"
1787 /* denied: req, resp */
1788 "%lld,%lld,"
1789 /* errors: request, connect, response */
1790 "%lld,,,"
1791 /* warnings: retries, redispatches */
1792 ",,"
1793 /* server status: reflect listener status */
1794 "%s,"
1795 /* rest of server: nothing */
1796 ",,,,,,,,"
1797 /* pid, iid, sid, throttle, lbtot, tracked, type */
1798 "%d,%d,%d,,,,%d,"
1799 /* rate, rate_lim, rate_max */
1800 ",,,"
1801 /* check_status, check_code, check_duration */
1802 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001803 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1804 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001805 /* failed health analyses */
1806 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001807 /* requests : req_rate, req_rate_max, req_tot, */
1808 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01001809 /* errors: cli_aborts, srv_aborts */
1810 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001811 "\n",
1812 px->id, l->name,
1813 l->nbconn, l->counters->conn_max,
1814 l->maxconn, l->counters->cum_conn,
1815 l->counters->bytes_in, l->counters->bytes_out,
1816 l->counters->denied_req, l->counters->denied_resp,
1817 l->counters->failed_req,
1818 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
1819 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
1820 }
1821
1822 if (buffer_feed_chunk(rep, &msg) >= 0)
1823 return 0;
1824 }
1825
Willy Tarreau91861262007-10-17 17:06:05 +02001826 s->data_ctx.stats.sv = px->srv; /* may be NULL */
1827 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
1828 /* fall through */
1829
1830 case DATA_ST_PX_SV:
1831 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001832 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001833 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 +02001834
Willy Tarreau4e33d862009-10-11 23:35:10 +02001835 if (buffer_almost_full(rep))
1836 return 0;
1837
Willy Tarreau91861262007-10-17 17:06:05 +02001838 sv = s->data_ctx.stats.sv;
1839
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001840 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001841 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
1842 break;
1843
1844 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
1845 continue;
1846 }
1847
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001848 if (sv->tracked)
1849 svs = sv->tracked;
1850 else
1851 svs = sv;
1852
Willy Tarreau91861262007-10-17 17:06:05 +02001853 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001854 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01001855 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001856 else if (svs->state & SRV_RUNNING) {
1857 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02001858 sv_state = 3; /* UP */
1859 else
1860 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01001861
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001862 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01001863 sv_state += 2;
1864 }
Willy Tarreau91861262007-10-17 17:06:05 +02001865 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001866 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02001867 sv_state = 1; /* going up */
1868 else
1869 sv_state = 0; /* DOWN */
1870
Cyril Bonté0dae5852010-02-03 00:26:28 +01001871 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02001872 /* do not report servers which are DOWN */
1873 s->data_ctx.stats.sv = sv->next;
1874 continue;
1875 }
1876
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001877 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001878 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
1879 "UP %d/%d &darr;", "UP",
1880 "NOLB %d/%d &darr;", "NOLB",
1881 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01001882 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001883 chunk_printf(&msg,
1884 /* name */
1885 "<tr class=\"maintain\"><td class=ac"
1886 );
1887 }
1888 else {
1889 chunk_printf(&msg,
1890 /* name */
1891 "<tr class=\"%s%d\"><td class=ac",
1892 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
1893 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001894
1895 if (uri->flags&ST_SHLGNDS) {
1896 char str[INET6_ADDRSTRLEN];
1897
1898 chunk_printf(&msg, " title=\"IP: ");
1899
1900 /* IP */
1901 if (inet_ntop(sv->addr.sin_family, &sv->addr.sin_addr, str, sizeof(str)))
1902 chunk_printf(&msg, "%s:%d", str, htons(sv->addr.sin_port));
1903 else
1904 chunk_printf(&msg, "(%s)", strerror(errno));
1905
1906 /* id */
1907 chunk_printf(&msg, ", id: %d", sv->puid);
1908
1909 /* cookie */
1910 if (sv->cookie) {
1911 struct chunk src;
1912
1913 chunk_printf(&msg, ", cookie: '");
1914
1915 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
1916 chunk_htmlencode(&msg, &src);
1917
1918 chunk_printf(&msg, "'");
1919 }
1920
1921 chunk_printf(&msg, "\"");
1922 }
1923
1924 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001925 ">%s<a name=\"%s/%s\"></a>"
1926 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001927 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001928 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001929 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001930 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001931 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001932 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001933 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001934 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001935 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001936 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01001937 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001938 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
1939 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001940 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
1941
1942 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1943 if (px->mode == PR_MODE_HTTP) {
1944 int i;
1945
1946 chunk_printf(&msg, " title=\"rsp codes:");
1947
1948 for (i = 1; i < 6; i++)
1949 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
1950
1951 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
1952 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001953
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001954 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001955 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01001956 ">%s%s%s</td><td>%s</td>",
1957 (px->mode == PR_MODE_HTTP)?"<u>":"",
1958 U2H0(sv->counters.cum_sess),
1959 (px->mode == PR_MODE_HTTP)?"</u>":"",
1960 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001961
1962 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001963 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001964 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001965 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001966 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01001967 /* errors : request, connect */
1968 "<td></td><td>%s</td>"
1969 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01001970 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001971 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001972 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001973 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001974 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
1975 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01001976 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01001977 sv->counters.cli_aborts,
1978 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01001979 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001980 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001981
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001982 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001983 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001984
Cyril Bontécd19e512010-01-31 22:34:03 +01001985 if (sv->state & SRV_MAINTAIN) {
1986 chunk_printf(&msg, "%s ",
1987 human_time(now.tv_sec - sv->last_change, 1));
1988 chunk_printf(&msg, "MAINT");
1989 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01001990 else if (svs != sv && svs->state & SRV_MAINTAIN) {
1991 chunk_printf(&msg, "%s ",
1992 human_time(now.tv_sec - svs->last_change, 1));
1993 chunk_printf(&msg, "MAINT(via)");
1994 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001995 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001996 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01001997 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001998
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001999 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002000 srv_hlt_st[sv_state],
2001 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2002 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002003 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002004
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002005 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002006 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002007 get_check_status_description(sv->check_status));
2008
2009 if (*sv->check_desc) {
2010 struct chunk src;
2011
2012 chunk_printf(&msg, ": ");
2013
2014 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2015 chunk_htmlencode(&msg, &src);
2016 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002017
Willy Tarreaue0454092010-02-26 12:29:07 +01002018 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002019 tv_iszero(&sv->check_start)?"":"* ",
2020 get_check_status_info(sv->check_status));
2021
2022 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002023 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002024
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002025 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002026 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002027 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002028 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002029
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002030 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002031 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002032 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002033 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002034 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002035 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002036 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002037 (sv->state & SRV_BACKUP) ? "-" : "Y",
2038 (sv->state & SRV_BACKUP) ? "Y" : "-");
2039
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002040 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002041 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002042 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002043 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2044
2045 if (svs->observe)
2046 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2047
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002048 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002049 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002050 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002051 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002052 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002053 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002054 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002055 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2056 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002057 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002058 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002059 "<td colspan=3></td>");
2060
2061 /* throttle */
2062 if ((sv->state & SRV_WARMINGUP) &&
2063 now.tv_sec < sv->last_change + sv->slowstart &&
2064 now.tv_sec >= sv->last_change) {
2065 unsigned int ratio;
2066 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002067 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002068 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002069 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002070 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002071 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002072 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002073 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002074 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2075 "UP %d/%d,", "UP,",
2076 "NOLB %d/%d,", "NOLB,",
2077 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002078 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002079 /* pxid, name */
2080 "%s,%s,"
2081 /* queue : current, max */
2082 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002083 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002084 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002085 /* bytes : in, out */
2086 "%lld,%lld,"
2087 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002088 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002089 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002090 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002091 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002092 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002093 "",
2094 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002095 sv->nbpend, sv->counters.nbpend_max,
2096 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002097 sv->counters.bytes_in, sv->counters.bytes_out,
2098 sv->counters.failed_secu,
2099 sv->counters.failed_conns, sv->counters.failed_resp,
2100 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002101
Willy Tarreau55bb8452007-10-17 18:44:57 +02002102 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002103 if (sv->state & SRV_MAINTAIN) {
2104 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002105 }
2106 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2107 chunk_printf(&msg, "MAINT(via),");
2108 }
2109 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002110 chunk_printf(&msg,
2111 srv_hlt_st[sv_state],
2112 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2113 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2114 }
Willy Tarreau91861262007-10-17 17:06:05 +02002115
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002116 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002117 /* weight, active, backup */
2118 "%d,%d,%d,"
2119 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002120 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002121 (sv->state & SRV_BACKUP) ? 0 : 1,
2122 (sv->state & SRV_BACKUP) ? 1 : 0);
2123
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002124 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002125 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002126 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002127 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002128 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002129 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002130 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002131 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002132 ",,,,");
2133
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002134 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002135 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002136 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002137 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002138 LIM2A0(sv->maxqueue, ""),
2139 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002140
2141 /* throttle */
2142 if ((sv->state & SRV_WARMINGUP) &&
2143 now.tv_sec < sv->last_change + sv->slowstart &&
2144 now.tv_sec >= sv->last_change) {
2145 unsigned int ratio;
2146 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002147 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002148 }
2149
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002150 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002151 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002152
2153 /* tracked */
2154 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002155 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002156 sv->tracked->proxy->id, sv->tracked->id);
2157 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002158 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002159
Willy Tarreau7f062c42009-03-05 18:43:00 +01002160 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002161 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002162
2163 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002164 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002165 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002166 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002167
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002168 if (sv->state & SRV_CHECKED) {
2169 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002170 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002171
2172 /* check_code */
2173 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002174 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002175 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002176 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002177
2178 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002179 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002180 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002181 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002182 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002183
2184 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002185 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002186 }
2187
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002188 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2189 if (px->mode == PR_MODE_HTTP) {
2190 int i;
2191
2192 for (i=1; i<6; i++)
2193 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2194
2195 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2196 } else {
2197 chunk_printf(&msg, ",,,,,,");
2198 }
2199
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002200 /* failed health analyses */
2201 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2202
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002203 /* requests : req_rate, req_rate_max, req_tot, */
2204 chunk_printf(&msg, ",,,");
2205
Willy Tarreauae526782010-03-04 20:34:23 +01002206 /* errors: cli_aborts, srv_aborts */
2207 chunk_printf(&msg, "%lld,%lld,",
2208 sv->counters.cli_aborts, sv->counters.srv_aborts);
2209
Willy Tarreau7f062c42009-03-05 18:43:00 +01002210 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002211 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002212 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002213 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002214 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002215 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002216
2217 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
2218 /* fall through */
2219
2220 case DATA_ST_PX_BE:
2221 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002222 if ((px->cap & PR_CAP_BE) &&
2223 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2224 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002225 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002226 /* name */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002227 "<tr class=\"backend\"><td class=ac");
2228
2229 if (uri->flags&ST_SHLGNDS) {
2230 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002231 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002232 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002233
2234 /* cookie */
2235 if (px->cookie_name) {
2236 struct chunk src;
2237
2238 chunk_printf(&msg, ", cookie: '");
2239
2240 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2241 chunk_htmlencode(&msg, &src);
2242
2243 chunk_printf(&msg, "'");
2244 }
2245
2246 chunk_printf(&msg, "\"");
2247
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002248 }
2249
2250 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002251 ">%s<a name=\"%s/Backend\"></a>"
2252 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002253 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002254 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002255 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002256 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002257 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002258 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002259 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002260 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002261 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->counters.nbpend_max),
2262 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->counters.be_sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02002263
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002264 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002265 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002266 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002267 "<td"
2268 "",
2269 U2H2(px->beconn), U2H3(px->counters.beconn_max), U2H4(px->fullconn));
2270
2271 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2272 if (px->mode == PR_MODE_HTTP) {
2273 int i;
2274
2275 chunk_printf(&msg, " title=\"rsp codes:");
2276
2277 for (i = 1; i < 6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01002278 chunk_printf(&msg, " %dxx=%lld", i, px->counters.be.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002279
Willy Tarreau24657792010-02-26 10:30:28 +01002280 chunk_printf(&msg, " other=%lld\"", px->counters.be.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002281 }
2282
2283 chunk_printf(&msg,
2284 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002285 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002286 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002287 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002288 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002289 (px->mode == PR_MODE_HTTP)?"<u>":"",
2290 U2H6(px->counters.cum_beconn),
2291 (px->mode == PR_MODE_HTTP)?"</u>":"",
2292 U2H7(px->counters.cum_lbconn),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002293 U2H8(px->counters.bytes_in), U2H9(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002294
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002295 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002296 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002297 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002298 /* errors : request, connect */
2299 "<td></td><td>%s</td>"
2300 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002301 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002302 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002303 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002304 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02002305 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002306 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02002307 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002308 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2309 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002310 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002311 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
Willy Tarreauae526782010-03-04 20:34:23 +01002312 U2H2(px->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002313 px->counters.cli_aborts,
2314 px->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002315 U2H5(px->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002316 px->counters.retries, px->counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002317 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01002318 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2319 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01002320 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002321 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002322
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002323 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002324 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002325 "<td class=ac>&nbsp;</td><td>%d</td>"
2326 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002327 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002328 "</tr>",
2329 px->down_trans,
2330 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002331 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002332 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002333 /* pxid, name */
2334 "%s,BACKEND,"
2335 /* queue : current, max */
2336 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002337 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002338 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002339 /* bytes : in, out */
2340 "%lld,%lld,"
2341 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002342 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002343 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002344 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002345 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002346 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002347 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02002348 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002349 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02002350 * active and backups. */
2351 "%s,"
2352 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002353 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002354 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002355 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002356 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002357 /* rate, rate_lim, rate_max, */
2358 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002359 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002360 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002361 px->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002362 px->nbpend /* or px->totpend ? */, px->counters.nbpend_max,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002363 px->beconn, px->counters.beconn_max, px->fullconn, px->counters.cum_beconn,
2364 px->counters.bytes_in, px->counters.bytes_out,
2365 px->counters.denied_req, px->counters.denied_resp,
2366 px->counters.failed_conns, px->counters.failed_resp,
2367 px->counters.retries, px->counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01002368 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01002369 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002370 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002371 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01002372 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002373 relative_pid, px->uuid,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002374 px->counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002375 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002376 px->counters.be_sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002377
2378 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2379 if (px->mode == PR_MODE_HTTP) {
2380 int i;
2381
2382 for (i=1; i<6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01002383 chunk_printf(&msg, "%lld,", px->counters.be.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002384
Willy Tarreau24657792010-02-26 10:30:28 +01002385 chunk_printf(&msg, "%lld,", px->counters.be.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002386 } else {
2387 chunk_printf(&msg, ",,,,,,");
2388 }
2389
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002390 /* failed health analyses */
2391 chunk_printf(&msg, ",");
2392
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002393 /* requests : req_rate, req_rate_max, req_tot, */
2394 chunk_printf(&msg, ",,,");
2395
Willy Tarreauae526782010-03-04 20:34:23 +01002396 /* errors: cli_aborts, srv_aborts */
2397 chunk_printf(&msg, "%lld,%lld,",
2398 px->counters.cli_aborts, px->counters.srv_aborts);
2399
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002400 /* finish with EOL */
2401 chunk_printf(&msg, "\n");
2402
Willy Tarreau55bb8452007-10-17 18:44:57 +02002403 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002404 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002405 return 0;
2406 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002407
Willy Tarreau91861262007-10-17 17:06:05 +02002408 s->data_ctx.stats.px_st = DATA_ST_PX_END;
2409 /* fall through */
2410
2411 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002412 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002413 chunk_printf(&msg, "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02002414
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002415 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002416 return 0;
2417 }
Willy Tarreau91861262007-10-17 17:06:05 +02002418
2419 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
2420 /* fall through */
2421
2422 case DATA_ST_PX_FIN:
2423 return 1;
2424
2425 default:
2426 /* unknown state, we should put an abort() here ! */
2427 return 1;
2428 }
2429}
2430
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002431/* This function is called to send output to the response buffer. It dumps a
2432 * complete session state onto the output buffer <rep>. The session has to be
2433 * set in data_ctx.sess.target. It returns 0 if the output buffer is full and
2434 * it needs to be called again, otherwise non-zero. It is designed to be called
2435 * from stats_dump_sess_to_buffer() below.
2436 */
2437
2438/* returns 1 if dump is not complete */
2439int stats_dump_full_sess_to_buffer(struct session *s, struct buffer *rep)
2440{
2441 struct tm tm;
2442 struct chunk msg;
2443 struct session *sess;
2444 extern const char *monthname[12];
2445 char pn[INET6_ADDRSTRLEN];
2446
2447 chunk_init(&msg, trash, sizeof(trash));
2448 sess = s->data_ctx.sess.target;
2449
2450 if (s->data_ctx.sess.section > 0 && s->data_ctx.sess.uid != sess->uniq_id) {
2451 /* session changed, no need to go any further */
2452 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
2453 if (buffer_feed_chunk(rep, &msg) >= 0)
2454 return 0;
2455 s->data_ctx.sess.target = NULL;
2456 s->data_ctx.sess.uid = 0;
2457 return 1;
2458 }
2459
2460 switch (s->data_ctx.sess.section) {
2461 case 0: /* main status of the session */
2462 s->data_ctx.sess.uid = sess->uniq_id;
2463 s->data_ctx.sess.section = 1;
2464 /* fall through */
2465
2466 case 1:
2467 chunk_printf(&msg,
2468 "%p: id=%u, proto=%s",
2469 sess,
2470 sess->uniq_id,
2471 sess->listener->proto->name);
2472
2473 switch (sess->listener->proto->sock_family) {
2474 case AF_INET:
2475 inet_ntop(AF_INET,
2476 (const void *)&((struct sockaddr_in *)&sess->cli_addr)->sin_addr,
2477 pn, sizeof(pn));
2478
2479 chunk_printf(&msg,
2480 " source=%s:%d\n",
2481 pn,
2482 ntohs(((struct sockaddr_in *)&sess->cli_addr)->sin_port));
2483 break;
2484 case AF_INET6:
2485 inet_ntop(AF_INET6,
2486 (const void *)&((struct sockaddr_in6 *)(&sess->cli_addr))->sin6_addr,
2487 pn, sizeof(pn));
2488
2489 chunk_printf(&msg,
2490 " source=%s:%d\n",
2491 pn,
2492 ntohs(((struct sockaddr_in6 *)&sess->cli_addr)->sin6_port));
2493 break;
2494 case AF_UNIX:
2495 default:
2496 /* no more information to print right now */
2497 chunk_printf(&msg, "\n");
2498 break;
2499 }
2500
2501 chunk_printf(&msg,
2502 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02002503 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002504
2505 chunk_printf(&msg,
2506 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)\n",
2507 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
2508 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
2509 sess->listener ? sess->listener->luid : 0);
2510
2511 chunk_printf(&msg,
2512 " backend=%s (id=%u mode=%s) server=%s (id=%u)\n",
2513 sess->be->id, sess->be->uuid, sess->be->mode ? "http" : "tcp",
2514 sess->srv ? sess->srv->id : "<none>",
2515 sess->srv ? sess->srv->puid : 0);
2516
2517 chunk_printf(&msg,
2518 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
2519 sess->task,
2520 sess->task->state,
2521 sess->task->nice, sess->task->calls,
2522 sess->task->expire ?
2523 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
2524 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
2525 TICKS_TO_MS(1000)) : "<NEVER>",
2526 task_in_rq(sess->task) ? ", running" : "");
2527
2528 get_localtime(sess->logs.accept_date.tv_sec, &tm);
2529 chunk_printf(&msg,
2530 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
2531 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2532 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
2533 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
2534
2535 chunk_printf(&msg,
2536 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
2537 &sess->si[0],
2538 sess->si[0].state,
2539 sess->si[0].flags,
2540 sess->si[0].fd,
2541 sess->si[0].exp ?
2542 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
2543 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
2544 TICKS_TO_MS(1000)) : "<NEVER>",
2545 sess->si[0].err_type);
2546
2547 chunk_printf(&msg,
2548 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
2549 &sess->si[1],
2550 sess->si[1].state,
2551 sess->si[1].flags,
2552 sess->si[1].fd,
2553 sess->si[1].exp ?
2554 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
2555 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
2556 TICKS_TO_MS(1000)) : "<NEVER>",
2557 sess->si[1].err_type);
2558
2559 chunk_printf(&msg,
2560 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
2561 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
2562 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
2563
2564
2565 chunk_printf(&msg,
2566 " req=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%ld)\n"
2567 " an_exp=%s",
2568 sess->req,
2569 sess->req->flags, sess->req->analysers,
2570 sess->req->l, sess->req->send_max,
2571 sess->req->pipe ? sess->req->pipe->data : 0,
2572 sess->req->to_forward,
2573 sess->req->analyse_exp ?
2574 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
2575 TICKS_TO_MS(1000)) : "<NEVER>");
2576
2577 chunk_printf(&msg,
2578 " rex=%s",
2579 sess->req->rex ?
2580 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
2581 TICKS_TO_MS(1000)) : "<NEVER>");
2582
2583 chunk_printf(&msg,
2584 " wex=%s\n"
2585 " data=%p r=%d w=%d lr=%d total=%lld\n",
2586 sess->req->wex ?
2587 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
2588 TICKS_TO_MS(1000)) : "<NEVER>",
2589 sess->req->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01002590 (int)(sess->req->r - sess->req->data),
2591 (int)(sess->req->w - sess->req->data),
2592 (int)(sess->req->lr - sess->req->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002593 sess->req->total);
2594
2595 chunk_printf(&msg,
2596 " res=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%ld)\n"
2597 " an_exp=%s",
2598 sess->rep,
2599 sess->rep->flags, sess->rep->analysers,
2600 sess->rep->l, sess->rep->send_max,
2601 sess->rep->pipe ? sess->rep->pipe->data : 0,
2602 sess->rep->to_forward,
2603 sess->rep->analyse_exp ?
2604 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
2605 TICKS_TO_MS(1000)) : "<NEVER>");
2606
2607 chunk_printf(&msg,
2608 " rex=%s",
2609 sess->rep->rex ?
2610 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
2611 TICKS_TO_MS(1000)) : "<NEVER>");
2612
2613 chunk_printf(&msg,
2614 " wex=%s\n"
2615 " data=%p r=%d w=%d lr=%d total=%lld\n",
2616 sess->rep->wex ?
2617 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
2618 TICKS_TO_MS(1000)) : "<NEVER>",
2619 sess->rep->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01002620 (int)(sess->rep->r - sess->rep->data),
2621 (int)(sess->rep->w - sess->rep->data),
2622 (int)(sess->rep->lr - sess->rep->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002623 sess->rep->total);
2624
2625 if (buffer_feed_chunk(rep, &msg) >= 0)
2626 return 0;
2627
2628 /* use other states to dump the contents */
2629 }
2630 /* end of dump */
2631 s->data_ctx.sess.uid = 0;
2632 return 1;
2633}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002634
2635/* This function is called to send output to the response buffer.
2636 * It dumps the sessions states onto the output buffer <rep>.
2637 * Expects to be called with client socket shut down on input.
2638 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002639 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002640 */
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002641int stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002642{
2643 struct chunk msg;
2644
2645 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
2646 /* If we're forced to shut down, we might have to remove our
2647 * reference to the last session being dumped.
2648 */
2649 if (s->data_state == DATA_ST_LIST) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002650 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002651 LIST_DEL(&s->data_ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002652 LIST_INIT(&s->data_ctx.sess.bref.users);
2653 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002654 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002655 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002656 }
2657
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002658 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002659
2660 switch (s->data_state) {
2661 case DATA_ST_INIT:
2662 /* the function had not been called yet, let's prepare the
2663 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002664 * pointer to the first in the global list. When a target
2665 * session is being destroyed, it is responsible for updating
2666 * this pointer. We know we have reached the end when this
2667 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002668 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002669 LIST_INIT(&s->data_ctx.sess.bref.users);
2670 s->data_ctx.sess.bref.ref = sessions.n;
2671 s->data_state = DATA_ST_LIST;
2672 /* fall through */
2673
2674 case DATA_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002675 /* first, let's detach the back-ref from a possible previous session */
2676 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
2677 LIST_DEL(&s->data_ctx.sess.bref.users);
2678 LIST_INIT(&s->data_ctx.sess.bref.users);
2679 }
2680
2681 /* and start from where we stopped */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002682 while (s->data_ctx.sess.bref.ref != &sessions) {
2683 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
2684 struct session *curr_sess;
2685
2686 curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list);
2687
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002688 if (s->data_ctx.sess.target) {
2689 if (s->data_ctx.sess.target != curr_sess)
2690 goto next_sess;
2691
2692 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
2693 /* call the proper dump() function and return if we're missing space */
2694 if (!stats_dump_full_sess_to_buffer(s, rep))
2695 return 0;
2696
2697 /* session dump complete */
2698 LIST_DEL(&s->data_ctx.sess.bref.users);
2699 LIST_INIT(&s->data_ctx.sess.bref.users);
2700 s->data_ctx.sess.target = NULL;
2701 break;
2702 }
2703
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002704 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002705 "%p: proto=%s",
2706 curr_sess,
2707 curr_sess->listener->proto->name);
2708
2709 switch (curr_sess->listener->proto->sock_family) {
2710 case AF_INET:
2711 inet_ntop(AF_INET,
2712 (const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr,
2713 pn, sizeof(pn));
2714
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002715 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002716 " src=%s:%d fe=%s be=%s srv=%s",
2717 pn,
2718 ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port),
2719 curr_sess->fe->id,
2720 curr_sess->be->id,
2721 curr_sess->srv ? curr_sess->srv->id : "<none>"
2722 );
2723 break;
2724 case AF_INET6:
2725 inet_ntop(AF_INET6,
2726 (const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr,
2727 pn, sizeof(pn));
2728
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002729 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002730 " src=%s:%d fe=%s be=%s srv=%s",
2731 pn,
2732 ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port),
2733 curr_sess->fe->id,
2734 curr_sess->be->id,
2735 curr_sess->srv ? curr_sess->srv->id : "<none>"
2736 );
2737
2738 break;
2739 case AF_UNIX:
2740 /* no more information to print right now */
2741 break;
2742 }
2743
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002744 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02002745 " ts=%02x age=%s calls=%d",
2746 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01002747 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
2748 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002749
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002750 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002751 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
2752 curr_sess->req->flags,
2753 curr_sess->req->l,
2754 curr_sess->req->analysers,
2755 curr_sess->req->rex ?
2756 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
2757 TICKS_TO_MS(1000)) : "");
2758
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002759 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002760 ",wx=%s",
2761 curr_sess->req->wex ?
2762 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
2763 TICKS_TO_MS(1000)) : "");
2764
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002765 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002766 ",ax=%s]",
2767 curr_sess->req->analyse_exp ?
2768 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
2769 TICKS_TO_MS(1000)) : "");
2770
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002771 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002772 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
2773 curr_sess->rep->flags,
2774 curr_sess->rep->l,
2775 curr_sess->rep->analysers,
2776 curr_sess->rep->rex ?
2777 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
2778 TICKS_TO_MS(1000)) : "");
2779
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002780 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002781 ",wx=%s",
2782 curr_sess->rep->wex ?
2783 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
2784 TICKS_TO_MS(1000)) : "");
2785
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002786 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002787 ",ax=%s]",
2788 curr_sess->rep->analyse_exp ?
2789 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
2790 TICKS_TO_MS(1000)) : "");
2791
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002792 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002793 " s0=[%d,%1xh,fd=%d,ex=%s]",
2794 curr_sess->si[0].state,
2795 curr_sess->si[0].flags,
2796 curr_sess->si[0].fd,
2797 curr_sess->si[0].exp ?
2798 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
2799 TICKS_TO_MS(1000)) : "");
2800
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002801 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002802 " s1=[%d,%1xh,fd=%d,ex=%s]",
2803 curr_sess->si[1].state,
2804 curr_sess->si[1].flags,
2805 curr_sess->si[1].fd,
2806 curr_sess->si[1].exp ?
2807 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
2808 TICKS_TO_MS(1000)) : "");
2809
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002810 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002811 " exp=%s",
2812 curr_sess->task->expire ?
2813 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
2814 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01002815 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002816 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002817
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002818 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002819
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002820 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002821 /* let's try again later from this session. We add ourselves into
2822 * this session's users so that it can remove us upon termination.
2823 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002824 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002825 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002826 }
2827
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002828 next_sess:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002829 s->data_ctx.sess.bref.ref = curr_sess->list.n;
2830 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002831
2832 if (s->data_ctx.sess.target) {
2833 /* specified session not found */
2834 if (s->data_ctx.sess.section > 0)
2835 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
2836 else
2837 chunk_printf(&msg, "Session not found.\n");
2838
2839 if (buffer_feed_chunk(rep, &msg) >= 0)
2840 return 0;
2841
2842 s->data_ctx.sess.target = NULL;
2843 s->data_ctx.sess.uid = 0;
2844 return 1;
2845 }
2846
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002847 s->data_state = DATA_ST_FIN;
2848 /* fall through */
2849
2850 default:
2851 s->data_state = DATA_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002852 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002853 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02002854}
2855
2856/* This function is called to send output to the response buffer.
2857 * It dumps the tables states onto the output buffer <rep>.
2858 * Expects to be called with client socket shut down on input.
2859 * s->data_ctx must have been zeroed first, and the flags properly set.
2860 * It returns 0 as long as it does not complete, non-zero upon completion.
2861 */
2862int stats_dump_table_to_buffer(struct session *s, struct buffer *rep)
2863{
2864 struct chunk msg;
2865 struct ebmb_node *eb;
2866 int dt;
2867
2868 /*
2869 * We have 3 possible states in s->data_state :
2870 * - DATA_ST_INIT : the first call
2871 * - DATA_ST_INFO : the proxy pointer points to the next table to
2872 * dump, the entry pointer is NULL ;
2873 * - DATA_ST_LIST : the proxy pointer points to the current table
2874 * and the entry pointer points to the next entry to be dumped,
2875 * and the refcount on the next entry is held ;
2876 * - DATA_ST_END : nothing left to dump, the buffer may contain some
2877 * data though.
2878 */
2879
2880 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
2881 /* in case of abort, remove any refcount we might have set on an entry */
2882 if (s->data_state == DATA_ST_LIST)
2883 s->data_ctx.table.entry->ref_cnt--;
2884 return 1;
2885 }
2886
2887 chunk_init(&msg, trash, sizeof(trash));
2888
2889 while (s->data_state != DATA_ST_FIN) {
2890 switch (s->data_state) {
2891 case DATA_ST_INIT:
2892 s->data_ctx.table.proxy = s->data_ctx.table.target;
2893 if (!s->data_ctx.table.proxy)
2894 s->data_ctx.table.proxy = proxy;
2895
2896 s->data_ctx.table.entry = NULL;
2897 s->data_state = DATA_ST_INFO;
2898 break;
2899
2900 case DATA_ST_INFO:
2901 if (!s->data_ctx.table.proxy ||
2902 (s->data_ctx.table.target &&
2903 s->data_ctx.table.proxy != s->data_ctx.table.target)) {
2904 s->data_state = DATA_ST_END;
2905 break;
2906 }
2907
2908 if (s->data_ctx.table.proxy->table.size) {
2909 chunk_printf(&msg, "# table: %s, type: %ld, size:%d, used:%d\n",
2910 s->data_ctx.table.proxy->id,
2911 s->data_ctx.table.proxy->table.type,
2912 s->data_ctx.table.proxy->table.size,
2913 s->data_ctx.table.proxy->table.current);
2914
2915 /* any other information should be dumped here */
2916
2917 if (s->data_ctx.table.target &&
2918 s->listener->perm.ux.level < ACCESS_LVL_OPER)
2919 chunk_printf(&msg, "# contents not dumped due to insufficient privileges\n");
2920
2921 if (buffer_feed_chunk(rep, &msg) >= 0)
2922 return 0;
2923
2924 if (s->data_ctx.table.target &&
2925 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
2926 /* dump entries only if table explicitly requested */
2927 eb = ebmb_first(&s->data_ctx.table.proxy->table.keys);
2928 if (eb) {
2929 s->data_ctx.table.entry = ebmb_entry(eb, struct stksess, key);
2930 s->data_ctx.table.entry->ref_cnt++;
2931 s->data_state = DATA_ST_LIST;
2932 break;
2933 }
2934 }
2935 }
2936 s->data_ctx.table.proxy = s->data_ctx.table.proxy->next;
2937 break;
2938
2939 case DATA_ST_LIST:
2940 chunk_printf(&msg, "%p:", s->data_ctx.table.entry);
2941
2942 if (s->data_ctx.table.proxy->table.type == STKTABLE_TYPE_IP) {
2943 char addr[16];
2944 inet_ntop(AF_INET,
2945 (const void *)&s->data_ctx.table.entry->key.key,
2946 addr, sizeof(addr));
2947 chunk_printf(&msg, " key=%s", addr);
2948 }
2949 else
2950 chunk_printf(&msg, " key=?");
2951
2952 chunk_printf(&msg, " use=%d exp=%d",
2953 s->data_ctx.table.entry->ref_cnt - 1,
2954 tick_remain(now_ms, s->data_ctx.table.entry->expire));
2955
2956 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
2957 void *ptr;
2958
2959 if (s->data_ctx.table.proxy->table.data_ofs[dt] == 0)
2960 continue;
2961 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
2962 chunk_printf(&msg, " %s(%d)=",
2963 stktable_data_types[dt].name,
2964 s->data_ctx.table.proxy->table.data_arg[dt].u);
2965 else
2966 chunk_printf(&msg, " %s=", stktable_data_types[dt].name);
2967
2968 ptr = stktable_data_ptr(&s->data_ctx.table.proxy->table,
2969 s->data_ctx.table.entry,
2970 dt);
2971 switch (dt) {
2972 /* all entries using the same type can be folded */
2973 case STKTABLE_DT_SERVER_ID:
2974 case STKTABLE_DT_GPC0:
2975 case STKTABLE_DT_CONN_CNT:
2976 case STKTABLE_DT_CONN_CUR:
2977 case STKTABLE_DT_SESS_CNT:
2978 case STKTABLE_DT_HTTP_REQ_CNT:
2979 case STKTABLE_DT_HTTP_ERR_CNT:
2980 chunk_printf(&msg, "%u", stktable_data_cast(ptr, server_id));
2981 break;
2982 case STKTABLE_DT_CONN_RATE:
2983 case STKTABLE_DT_SESS_RATE:
2984 case STKTABLE_DT_HTTP_REQ_RATE:
2985 case STKTABLE_DT_HTTP_ERR_RATE:
2986 case STKTABLE_DT_BYTES_IN_RATE:
2987 case STKTABLE_DT_BYTES_OUT_RATE:
2988 chunk_printf(&msg, "%d",
2989 read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
2990 s->data_ctx.table.proxy->table.data_arg[dt].u));
2991 break;
2992 case STKTABLE_DT_BYTES_IN_CNT:
2993 case STKTABLE_DT_BYTES_OUT_CNT:
2994 chunk_printf(&msg, "%lld", stktable_data_cast(ptr, bytes_in_cnt));
2995 break;
2996 }
2997 }
2998 chunk_printf(&msg, "\n");
2999
3000 if (buffer_feed_chunk(rep, &msg) >= 0)
3001 return 0;
3002
3003 s->data_ctx.table.entry->ref_cnt--;
3004
3005 eb = ebmb_next(&s->data_ctx.table.entry->key);
3006 if (eb) {
3007 s->data_ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3008 s->data_ctx.table.entry->ref_cnt++;
3009 break;
3010 }
3011
3012 s->data_ctx.table.proxy = s->data_ctx.table.proxy->next;
3013 s->data_state = DATA_ST_INFO;
3014 break;
3015
3016 case DATA_ST_END:
3017 s->data_state = DATA_ST_FIN;
3018 break;
3019 }
3020 }
3021 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003022}
3023
Willy Tarreaud426a182010-03-05 14:58:26 +01003024/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003025 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3026 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3027 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3028 * lines are respected within the limit of 70 output chars. Lines that are
3029 * continuation of a previous truncated line begin with "+" instead of " "
3030 * after the offset. The new pointer is returned.
3031 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003032static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3033 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003034{
3035 int end;
3036 unsigned char c;
3037
3038 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003039 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003040 return ptr;
3041
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003042 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003043
Willy Tarreaud426a182010-03-05 14:58:26 +01003044 while (ptr < len && ptr < bsize) {
3045 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003046 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003047 if (out->len > end - 2)
3048 break;
3049 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003050 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003051 if (out->len > end - 3)
3052 break;
3053 out->str[out->len++] = '\\';
3054 switch (c) {
3055 case '\t': c = 't'; break;
3056 case '\n': c = 'n'; break;
3057 case '\r': c = 'r'; break;
3058 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003059 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003060 }
3061 out->str[out->len++] = c;
3062 } else {
3063 if (out->len > end - 5)
3064 break;
3065 out->str[out->len++] = '\\';
3066 out->str[out->len++] = 'x';
3067 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3068 out->str[out->len++] = hextab[c & 0xF];
3069 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003070 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003071 /* we had a line break, let's return now */
3072 out->str[out->len++] = '\n';
3073 *line = ptr;
3074 return ptr;
3075 }
3076 }
3077 /* we have an incomplete line, we return it as-is */
3078 out->str[out->len++] = '\n';
3079 return ptr;
3080}
3081
3082/* This function is called to send output to the response buffer.
3083 * It dumps the errors logged in proxies onto the output buffer <rep>.
3084 * Expects to be called with client socket shut down on input.
3085 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau61b34732009-10-03 23:49:35 +02003086 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003087 */
Willy Tarreau61b34732009-10-03 23:49:35 +02003088int stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003089{
3090 extern const char *monthname[12];
3091 struct chunk msg;
3092
Willy Tarreau61b34732009-10-03 23:49:35 +02003093 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW)))
3094 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003095
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003096 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003097
3098 if (!s->data_ctx.errors.px) {
3099 /* the function had not been called yet, let's prepare the
3100 * buffer for a response.
3101 */
Willy Tarreau74808cb2009-03-04 15:53:18 +01003102 s->data_ctx.errors.px = proxy;
3103 s->data_ctx.errors.buf = 0;
3104 s->data_ctx.errors.bol = 0;
3105 s->data_ctx.errors.ptr = -1;
3106 }
3107
3108 /* we have two inner loops here, one for the proxy, the other one for
3109 * the buffer.
3110 */
3111 while (s->data_ctx.errors.px) {
3112 struct error_snapshot *es;
3113
3114 if (s->data_ctx.errors.buf == 0)
3115 es = &s->data_ctx.errors.px->invalid_req;
3116 else
3117 es = &s->data_ctx.errors.px->invalid_rep;
3118
3119 if (!es->when.tv_sec)
3120 goto next;
3121
3122 if (s->data_ctx.errors.iid >= 0 &&
3123 s->data_ctx.errors.px->uuid != s->data_ctx.errors.iid &&
3124 es->oe->uuid != s->data_ctx.errors.iid)
3125 goto next;
3126
3127 if (s->data_ctx.errors.ptr < 0) {
3128 /* just print headers now */
3129
3130 char pn[INET6_ADDRSTRLEN];
3131 struct tm tm;
3132
3133 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003134 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003135 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003136 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003137
3138
3139 if (es->src.ss_family == AF_INET)
3140 inet_ntop(AF_INET,
3141 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
3142 pn, sizeof(pn));
3143 else
3144 inet_ntop(AF_INET6,
3145 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
3146 pn, sizeof(pn));
3147
3148 switch (s->data_ctx.errors.buf) {
3149 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003150 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003151 " frontend %s (#%d): invalid request\n"
3152 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003153 " request length %d bytes, error at position %d:\n \n",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003154 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
Willy Tarreau3fd6cec2010-03-25 06:45:07 +01003155 pn, es->sid, (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3156 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003157 es->srv ? es->srv->id : "<NONE>",
3158 es->srv ? es->srv->puid : -1,
3159 es->len, es->pos);
3160 break;
3161 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003162 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003163 " backend %s (#%d) : invalid response\n"
3164 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003165 " response length %d bytes, error at position %d:\n \n",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003166 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
3167 pn, es->sid, es->oe->id, es->oe->uuid,
3168 es->srv ? es->srv->id : "<NONE>",
3169 es->srv ? es->srv->puid : -1,
3170 es->len, es->pos);
3171 break;
3172 }
3173
Willy Tarreau61b34732009-10-03 23:49:35 +02003174 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003175 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003176 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003177 }
3178 s->data_ctx.errors.ptr = 0;
3179 s->data_ctx.errors.sid = es->sid;
3180 }
3181
3182 if (s->data_ctx.errors.sid != es->sid) {
3183 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003184 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003185 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau61b34732009-10-03 23:49:35 +02003186 if (buffer_feed_chunk(rep, &msg) >= 0)
3187 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003188 goto next;
3189 }
3190
3191 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau61b34732009-10-03 23:49:35 +02003192 while (s->data_ctx.errors.ptr < es->len && s->data_ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003193 int newptr;
3194 int newline;
3195
3196 newline = s->data_ctx.errors.bol;
Willy Tarreaud426a182010-03-05 14:58:26 +01003197 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, s->data_ctx.errors.ptr);
Willy Tarreau74808cb2009-03-04 15:53:18 +01003198 if (newptr == s->data_ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02003199 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003200
Willy Tarreau61b34732009-10-03 23:49:35 +02003201 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003202 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003203 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003204 }
3205 s->data_ctx.errors.ptr = newptr;
3206 s->data_ctx.errors.bol = newline;
3207 };
3208 next:
3209 s->data_ctx.errors.bol = 0;
3210 s->data_ctx.errors.ptr = -1;
3211 s->data_ctx.errors.buf++;
3212 if (s->data_ctx.errors.buf > 1) {
3213 s->data_ctx.errors.buf = 0;
3214 s->data_ctx.errors.px = s->data_ctx.errors.px->next;
3215 }
3216 }
3217
3218 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02003219 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003220}
3221
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003222
Willy Tarreau10522fd2008-07-09 20:12:41 +02003223static struct cfg_kw_list cfg_kws = {{ },{
3224 { CFG_GLOBAL, "stats", stats_parse_global },
3225 { 0, NULL, NULL },
3226}};
3227
3228__attribute__((constructor))
3229static void __dumpstats_module_init(void)
3230{
3231 cfg_register_keywords(&cfg_kws);
3232}
3233
Willy Tarreau91861262007-10-17 17:06:05 +02003234/*
3235 * Local variables:
3236 * c-indent-level: 8
3237 * c-basic-offset: 8
3238 * End:
3239 */