blob: a6ce0cf502998337bdd0a5853be2ff7f6ec1decb [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;
Willy Tarreau603861e2010-07-18 08:31:59 +0200384 if (*args[2]) {
385 s->data_ctx.table.target = find_stktable(args[2]);
386 if (!s->data_ctx.table.target) {
387 s->data_ctx.cli.msg = "No such table\n";
388 si->st0 = STAT_CLI_PRINT;
389 return 1;
390 }
391 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200392 else
393 s->data_ctx.table.target = NULL;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +0200394
395 s->data_ctx.table.data_type = -1;
396 if (s->data_ctx.table.target && strncmp(args[3], "data.", 5) == 0) {
397 /* condition on stored data value */
398 s->data_ctx.table.data_type = stktable_get_data_type(args[3] + 5);
399 if (s->data_ctx.table.data_type < 0) {
400 s->data_ctx.cli.msg = "Unknown data type\n";
401 si->st0 = STAT_CLI_PRINT;
402 return 1;
403 }
404
405 if (!((struct proxy *)s->data_ctx.table.target)->table.data_ofs[s->data_ctx.table.data_type]) {
406 s->data_ctx.cli.msg = "Data type not stored in this table\n";
407 si->st0 = STAT_CLI_PRINT;
408 return 1;
409 }
410
411 s->data_ctx.table.data_op = get_std_op(args[4]);
412 if (s->data_ctx.table.data_op < 0) {
413 s->data_ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
414 si->st0 = STAT_CLI_PRINT;
415 return 1;
416 }
417
418 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &s->data_ctx.table.value) != 0) {
419 s->data_ctx.cli.msg = "Require a valid integer value to compare against\n";
420 si->st0 = STAT_CLI_PRINT;
421 return 1;
422 }
423 }
424 else if (*args[3]) {
425 s->data_ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value>\n";
426 si->st0 = STAT_CLI_PRINT;
427 return 1;
428 }
429
Willy Tarreau69f58c82010-07-12 17:55:33 +0200430 s->data_ctx.table.proxy = NULL;
431 s->data_ctx.table.entry = NULL;
432 si->st0 = STAT_CLI_O_TAB; // stats_dump_table_to_buffer
433 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200434 else { /* neither "stat" nor "info" nor "sess" nor "errors" no "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200435 return 0;
436 }
437 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200438 else if (strcmp(args[0], "clear") == 0) {
439 if (strcmp(args[1], "counters") == 0) {
440 struct proxy *px;
441 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200442 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200443 int clrall = 0;
444
445 if (strcmp(args[2], "all") == 0)
446 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200447
Willy Tarreau6162db22009-10-10 17:13:00 +0200448 /* check permissions */
449 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
450 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200451 s->data_ctx.cli.msg = stats_permission_denied_msg;
452 si->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200453 return 1;
454 }
455
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200456 for (px = proxy; px; px = px->next) {
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200457 if (clrall)
458 memset(&px->counters, 0, sizeof(px->counters));
459 else {
460 px->counters.feconn_max = 0;
461 px->counters.beconn_max = 0;
Willy Tarreaud9b587f2010-02-26 10:05:55 +0100462 px->counters.fe_rps_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200463 px->counters.fe_sps_max = 0;
Willy Tarreaub36b4242010-06-04 20:59:39 +0200464 px->counters.fe_cps_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200465 px->counters.be_sps_max = 0;
466 px->counters.nbpend_max = 0;
467 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200468
469 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200470 if (clrall)
471 memset(&sv->counters, 0, sizeof(sv->counters));
472 else {
473 sv->counters.cur_sess_max = 0;
474 sv->counters.nbpend_max = 0;
475 sv->counters.sps_max = 0;
476 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200477
478 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200479 if (li->counters) {
480 if (clrall)
481 memset(li->counters, 0, sizeof(*li->counters));
482 else
483 li->counters->conn_max = 0;
484 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200485 }
486
487 return 1;
488 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200489 else if (strcmp(args[1], "table") == 0) {
490 struct proxy *px;
491 struct stksess *ts;
492 unsigned int ip_key;
493
494 if (!*args[2]) {
Willy Tarreau603861e2010-07-18 08:31:59 +0200495 s->data_ctx.cli.msg = "\"table\" argument expected\n";
Willy Tarreau88ee3972010-07-13 13:48:00 +0200496 si->st0 = STAT_CLI_PRINT;
497 return 1;
498 }
499
500 px = find_stktable(args[2]);
501
502 if (!px) {
Willy Tarreau603861e2010-07-18 08:31:59 +0200503 s->data_ctx.cli.msg = "No such table\n";
Willy Tarreau88ee3972010-07-13 13:48:00 +0200504 si->st0 = STAT_CLI_PRINT;
505 return 1;
506 }
507
508 if (strcmp(args[3], "key") != 0) {
Willy Tarreau603861e2010-07-18 08:31:59 +0200509 s->data_ctx.cli.msg = "\"key\" argument expected\n";
Willy Tarreau88ee3972010-07-13 13:48:00 +0200510 si->st0 = STAT_CLI_PRINT;
511 return 1;
512 }
513
514 if (!*args[4]) {
Willy Tarreau603861e2010-07-18 08:31:59 +0200515 s->data_ctx.cli.msg = "Key value expected\n";
Willy Tarreau88ee3972010-07-13 13:48:00 +0200516 si->st0 = STAT_CLI_PRINT;
517 return 1;
518 }
519
520 if (px->table.type == STKTABLE_TYPE_IP) {
521 ip_key = htonl(inetaddr_host(args[4]));
522 static_table_key.key = (void *)&ip_key;
523 }
524 else {
Willy Tarreau603861e2010-07-18 08:31:59 +0200525 s->data_ctx.cli.msg = "Removing keys from non-ip tables is not supported\n";
Willy Tarreau88ee3972010-07-13 13:48:00 +0200526 si->st0 = STAT_CLI_PRINT;
527 return 1;
528 }
529
530 /* check permissions */
531 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
532 s->data_ctx.cli.msg = stats_permission_denied_msg;
533 si->st0 = STAT_CLI_PRINT;
534 return 1;
535 }
536
537 ts = stktable_lookup_key(&px->table, &static_table_key);
538 if (!ts) {
539 /* silent return, entry was already removed */
540 return 1;
541 }
542 else if (ts->ref_cnt) {
543 /* don't delete an entry which is currently referenced */
Willy Tarreau603861e2010-07-18 08:31:59 +0200544 s->data_ctx.cli.msg = "Entry currently in use, cannot remove\n";
Willy Tarreau88ee3972010-07-13 13:48:00 +0200545 si->st0 = STAT_CLI_PRINT;
546 return 1;
547 }
548
549 eb32_delete(&ts->exp);
550 ebmb_delete(&ts->key);
551 stksess_free(&px->table, ts);
552
553 /* end of processing */
554 return 1;
555 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200556 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200557 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200558 return 0;
559 }
560 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200561 else if (strcmp(args[0], "get") == 0) {
562 if (strcmp(args[1], "weight") == 0) {
563 struct proxy *px;
564 struct server *sv;
565
566 /* split "backend/server" and make <line> point to server */
567 for (line = args[2]; *line; line++)
568 if (*line == '/') {
569 *line++ = '\0';
570 break;
571 }
572
573 if (!*line) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200574 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
575 si->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200576 return 1;
577 }
578
579 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200580 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
581 si->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200582 return 1;
583 }
584
585 /* return server's effective weight at the moment */
586 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
587 buffer_feed(si->ib, trash);
588 return 1;
589 }
590 else { /* not "get weight" */
591 return 0;
592 }
593 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200594 else if (strcmp(args[0], "set") == 0) {
595 if (strcmp(args[1], "weight") == 0) {
596 struct proxy *px;
597 struct server *sv;
598 int w;
599
600 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200601 s->data_ctx.cli.msg = stats_permission_denied_msg;
602 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200603 return 1;
604 }
605
606 /* split "backend/server" and make <line> point to server */
607 for (line = args[2]; *line; line++)
608 if (*line == '/') {
609 *line++ = '\0';
610 break;
611 }
612
613 if (!*line || !*args[3]) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200614 s->data_ctx.cli.msg = "Require 'backend/server' and 'weight' or 'weight%'.\n";
615 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200616 return 1;
617 }
618
619 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200620 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
621 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200622 return 1;
623 }
624
625 /* if the weight is terminated with '%', it is set relative to
626 * the initial weight, otherwise it is absolute.
627 */
628 w = atoi(args[3]);
629 if (strchr(args[3], '%') != NULL) {
630 if (w < 0 || w > 100) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200631 s->data_ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
632 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200633 return 1;
634 }
635 w = sv->iweight * w / 100;
636 }
637 else {
638 if (w < 0 || w > 256) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200639 s->data_ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
640 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200641 return 1;
642 }
643 }
644
645 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200646 s->data_ctx.cli.msg = "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
647 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200648 return 1;
649 }
650
651 sv->uweight = w;
652
653 if (px->lbprm.algo & BE_LB_PROP_DYN) {
654 /* we must take care of not pushing the server to full throttle during slow starts */
655 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
656 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
657 else
658 sv->eweight = BE_WEIGHT_SCALE;
659 sv->eweight *= sv->uweight;
660 } else {
661 sv->eweight = sv->uweight;
662 }
663
664 /* static LB algorithms are a bit harder to update */
665 if (px->lbprm.update_server_eweight)
666 px->lbprm.update_server_eweight(sv);
667 else if (sv->eweight)
668 px->lbprm.set_server_status_up(sv);
669 else
670 px->lbprm.set_server_status_down(sv);
671
672 return 1;
673 }
Willy Tarreau7aabd112010-01-26 10:59:06 +0100674 else if (strcmp(args[1], "timeout") == 0) {
675 if (strcmp(args[2], "cli") == 0) {
676 unsigned timeout;
677 const char *res;
678
679 if (!*args[3]) {
680 s->data_ctx.cli.msg = "Expects an integer value.\n";
681 si->st0 = STAT_CLI_PRINT;
682 return 1;
683 }
684
685 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
686 if (res || timeout < 1) {
687 s->data_ctx.cli.msg = "Invalid timeout value.\n";
688 si->st0 = STAT_CLI_PRINT;
689 return 1;
690 }
691
692 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
693 return 1;
694 }
695 else {
696 s->data_ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
697 si->st0 = STAT_CLI_PRINT;
698 return 1;
699 }
700 }
701 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +0200702 return 0;
703 }
704 }
Cyril Bontécd19e512010-01-31 22:34:03 +0100705 else if (strcmp(args[0], "enable") == 0) {
706 if (strcmp(args[1], "server") == 0) {
707 struct proxy *px;
708 struct server *sv;
709
710 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
711 s->data_ctx.cli.msg = stats_permission_denied_msg;
712 si->st0 = STAT_CLI_PRINT;
713 return 1;
714 }
715
716 /* split "backend/server" and make <line> point to server */
717 for (line = args[2]; *line; line++)
718 if (*line == '/') {
719 *line++ = '\0';
720 break;
721 }
722
723 if (!*line || !*args[2]) {
724 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
725 si->st0 = STAT_CLI_PRINT;
726 return 1;
727 }
728
729 if (!get_backend_server(args[2], line, &px, &sv)) {
730 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
731 si->st0 = STAT_CLI_PRINT;
732 return 1;
733 }
734
735 if (sv->state & SRV_MAINTAIN) {
736 /* The server is really in maintenance, we can change the server state */
737 if (sv->tracked) {
738 /* If this server tracks the status of another one,
739 * we must restore the good status.
740 */
741 if (sv->tracked->state & SRV_RUNNING) {
742 set_server_up(sv);
743 } else {
744 sv->state &= ~SRV_MAINTAIN;
745 set_server_down(sv);
746 }
747 } else {
748 set_server_up(sv);
749 }
750 }
751
752 return 1;
753 }
754 else { /* unknown "enable" parameter */
755 return 0;
756 }
757 }
758 else if (strcmp(args[0], "disable") == 0) {
759 if (strcmp(args[1], "server") == 0) {
760 struct proxy *px;
761 struct server *sv;
762
763 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
764 s->data_ctx.cli.msg = stats_permission_denied_msg;
765 si->st0 = STAT_CLI_PRINT;
766 return 1;
767 }
768
769 /* split "backend/server" and make <line> point to server */
770 for (line = args[2]; *line; line++)
771 if (*line == '/') {
772 *line++ = '\0';
773 break;
774 }
775
776 if (!*line || !*args[2]) {
777 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
778 si->st0 = STAT_CLI_PRINT;
779 return 1;
780 }
781
782 if (!get_backend_server(args[2], line, &px, &sv)) {
783 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
784 si->st0 = STAT_CLI_PRINT;
785 return 1;
786 }
787
788 if (! (sv->state & SRV_MAINTAIN)) {
789 /* Not already in maintenance, we can change the server state */
790 sv->state |= SRV_MAINTAIN;
791 set_server_down(sv);
792 }
793
794 return 1;
795 }
796 else { /* unknown "disable" parameter */
797 return 0;
798 }
799 }
800 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200801 return 0;
802 }
803 return 1;
804}
805
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200806/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200807 * used to processes I/O from/to the stats unix socket. The system relies on a
808 * state machine handling requests and various responses. We read a request,
809 * then we process it and send the response, and we possibly display a prompt.
810 * Then we can read again. The state is stored in si->st0 and is one of the
811 * STAT_CLI_* constants. si->st1 is used to indicate whether prompt is enabled
812 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200813 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200814void stats_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200815{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200816 struct session *s = si->private;
817 struct buffer *req = si->ob;
818 struct buffer *res = si->ib;
819 int reql;
820 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200821
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200822 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
823 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200824
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200825 while (1) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200826 if (si->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200827 /* Stats output not initialized yet */
828 memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats));
829 s->data_source = DATA_SRC_STATS;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200830 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200831 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200832 else if (si->st0 == STAT_CLI_END) {
833 /* Let's close for real now. We just close the request
834 * side, the conditions below will complete if needed.
835 */
836 si->shutw(si);
837 break;
838 }
839 else if (si->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +0200840 /* ensure we have some output room left in the event we
841 * would want to return some info right after parsing.
842 */
843 if (buffer_almost_full(si->ib))
844 break;
845
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200846 reql = buffer_si_peekline(si->ob, trash, sizeof(trash));
847 if (reql <= 0) { /* closed or EOL not found */
848 if (reql == 0)
849 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200850 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200851 continue;
852 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200853
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200854 /* seek for a possible semi-colon. If we find one, we
855 * replace it with an LF and skip only this part.
856 */
857 for (len = 0; len < reql; len++)
858 if (trash[len] == ';') {
859 trash[len] = '\n';
860 reql = len + 1;
861 break;
862 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200863
Willy Tarreau816fc222009-10-04 07:36:58 +0200864 /* now it is time to check that we have a full line,
865 * remove the trailing \n and possibly \r, then cut the
866 * line.
867 */
868 len = reql - 1;
869 if (trash[len] != '\n') {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200870 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200871 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200872 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200873
Willy Tarreau816fc222009-10-04 07:36:58 +0200874 if (len && trash[len-1] == '\r')
875 len--;
876
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200877 trash[len] = '\0';
878
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200879 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200880 if (len) {
881 if (strcmp(trash, "quit") == 0) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200882 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200883 continue;
884 }
885 else if (strcmp(trash, "prompt") == 0)
886 si->st1 = !si->st1;
887 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200888 !stats_sock_parse_request(si, trash)) {
889 s->data_ctx.cli.msg = stats_sock_usage_msg;
890 si->st0 = STAT_CLI_PRINT;
891 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200892 /* NB: stats_sock_parse_request() may have put
893 * another STAT_CLI_O_* into si->st0.
894 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200895 }
896 else if (!si->st1) {
897 /* if prompt is disabled, print help on empty lines,
898 * so that the user at least knows how to enable
899 * prompt and find help.
900 */
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200901 s->data_ctx.cli.msg = stats_sock_usage_msg;
902 si->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200903 }
904
905 /* re-adjust req buffer */
906 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200907 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200908 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200909 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200910 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200911 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200912 continue;
913 }
914
915 switch (si->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200916 case STAT_CLI_PRINT:
917 if (buffer_feed(si->ib, s->data_ctx.cli.msg) < 0)
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200918 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200919 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200920 case STAT_CLI_O_INFO:
Willy Tarreau24955a12009-10-04 11:54:04 +0200921 if (stats_dump_raw_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200922 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200923 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200924 case STAT_CLI_O_SESS:
Willy Tarreau7e72a8f2009-10-03 23:55:14 +0200925 if (stats_dump_sess_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200926 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200927 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200928 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau61b34732009-10-03 23:49:35 +0200929 if (stats_dump_errors_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200930 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200931 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +0200932 case STAT_CLI_O_TAB:
933 if (stats_dump_table_to_buffer(s, res))
934 si->st0 = STAT_CLI_PROMPT;
935 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200936 default: /* abnormal state */
937 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200938 break;
939 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200940
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200941 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
942 if (si->st0 == STAT_CLI_PROMPT) {
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200943 if (buffer_feed(si->ib, si->st1 ? "\n> " : "\n") < 0)
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200944 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200945 }
946
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200947 /* If the output functions are still there, it means they require more room. */
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200948 if (si->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200949 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200950
951 /* Now we close the output if one of the writers did so,
952 * or if we're not in interactive mode and the request
953 * buffer is empty. This still allows pipelined requests
954 * to be sent in non-interactive mode.
955 */
956 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->st1 && !req->send_max)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200957 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200958 continue;
959 }
960
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200961 /* switch state back to GETREQ to read next requests */
962 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200963 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200964 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200965
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200966 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200967 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
968 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
969 /* Other size has closed, let's abort if we have no more processing to do
970 * and nothing more to consume. This is comparable to a broken pipe, so
971 * we forward the close to the request side so that it flows upstream to
972 * the client.
973 */
974 si->shutw(si);
975 }
976
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200977 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200978 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
979 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
980 /* We have no more processing to do, and nothing more to send, and
981 * the client side has closed. So we'll forward this state downstream
982 * on the response buffer.
983 */
984 si->shutr(si);
985 res->flags |= BF_READ_NULL;
986 }
987
988 /* update all other flags and resync with the other side */
989 si->update(si);
990
991 /* we don't want to expire timeouts while we're processing requests */
992 si->ib->rex = TICK_ETERNITY;
993 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200994
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200995 out:
996 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
997 __FUNCTION__, __LINE__,
998 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
999
1000 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1001 /* check that we have released everything then unregister */
1002 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001003 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001004}
1005
Willy Tarreau24955a12009-10-04 11:54:04 +02001006/* This function is called to send output to the response buffer.
1007 * It dumps statistics onto the output buffer <rep> owned by session <s>.
1008 * s->data_ctx must have been zeroed first, and the flags properly set.
1009 * It returns 0 as long as it does not complete, non-zero upon completion.
1010 * Some states are not used but it makes the code more similar to other
1011 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001012 */
Willy Tarreau24955a12009-10-04 11:54:04 +02001013int stats_dump_raw_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001014{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001015 struct proxy *px;
1016 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001017 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001018
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001019 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +02001020
1021 switch (s->data_state) {
1022 case DATA_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001023 /* the function had not been called yet */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001024 s->data_state = DATA_ST_HEAD;
1025 /* fall through */
1026
1027 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001028 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001029 print_csv_header(&msg);
Willy Tarreau24955a12009-10-04 11:54:04 +02001030 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001031 return 0;
1032 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001033
1034 s->data_state = DATA_ST_INFO;
1035 /* fall through */
1036
1037 case DATA_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001038 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001039 if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001040 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001041 "Name: " PRODUCT_NAME "\n"
1042 "Version: " HAPROXY_VERSION "\n"
1043 "Release_date: " HAPROXY_DATE "\n"
1044 "Nbproc: %d\n"
1045 "Process_num: %d\n"
1046 "Pid: %d\n"
1047 "Uptime: %dd %dh%02dm%02ds\n"
1048 "Uptime_sec: %d\n"
1049 "Memmax_MB: %d\n"
1050 "Ulimit-n: %d\n"
1051 "Maxsock: %d\n"
1052 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001053 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001054 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001055 "PipesUsed: %d\n"
1056 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001057 "Tasks: %d\n"
1058 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001059 "node: %s\n"
1060 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001061 "",
1062 global.nbproc,
1063 relative_pid,
1064 pid,
1065 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1066 up,
1067 global.rlimit_memmax,
1068 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001069 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001070 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001071 nb_tasks_cur, run_queue_cur,
1072 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001073 );
Willy Tarreau24955a12009-10-04 11:54:04 +02001074 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001075 return 0;
1076 }
1077
Willy Tarreau3e76e722007-10-17 18:57:38 +02001078 s->data_ctx.stats.px = proxy;
1079 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001080
1081 s->data_ctx.stats.sv = NULL;
1082 s->data_ctx.stats.sv_st = 0;
1083
Willy Tarreau3e76e722007-10-17 18:57:38 +02001084 s->data_state = DATA_ST_LIST;
1085 /* fall through */
1086
1087 case DATA_ST_LIST:
1088 /* dump proxies */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001089 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +01001090 while (s->data_ctx.stats.px) {
1091 px = s->data_ctx.stats.px;
1092 /* skip the disabled proxies and non-networked ones */
1093 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001094 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001095 if (stats_dump_proxy(s, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001096 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001097 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001098
Willy Tarreaua8efd362008-01-03 10:19:15 +01001099 s->data_ctx.stats.px = px->next;
1100 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1101 }
1102 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001103 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001104
1105 s->data_state = DATA_ST_END;
1106 /* fall through */
1107
1108 case DATA_ST_END:
1109 s->data_state = DATA_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001110 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001111
1112 case DATA_ST_FIN:
1113 return 1;
1114
1115 default:
1116 /* unknown state ! */
Willy Tarreau24955a12009-10-04 11:54:04 +02001117 s->data_state = DATA_ST_FIN;
1118 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001119 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001120}
1121
1122
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001123/* This I/O handler runs as an applet embedded in a stream interface. It is
1124 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
1125 * si->st0 becomes non-zero once the transfer is finished. The handler
1126 * automatically unregisters itself once transfer is complete.
1127 */
1128void http_stats_io_handler(struct stream_interface *si)
1129{
1130 struct session *s = si->private;
1131 struct buffer *req = si->ob;
1132 struct buffer *res = si->ib;
1133
1134 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1135 goto out;
1136
1137 /* check that the output is not closed */
1138 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
1139 si->st0 = 1;
1140
1141 if (!si->st0) {
1142 if (stats_dump_http(s, res, s->be->uri_auth)) {
1143 si->st0 = 1;
1144 si->shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001145 }
1146 }
1147
1148 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
1149 si->shutw(si);
1150
1151 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->st0) {
1152 si->shutr(si);
1153 res->flags |= BF_READ_NULL;
1154 }
1155
1156 /* update all other flags and resync with the other side */
1157 si->update(si);
1158
1159 /* we don't want to expire timeouts while we're processing requests */
1160 si->ib->rex = TICK_ETERNITY;
1161 si->ob->wex = TICK_ETERNITY;
1162
1163 out:
1164 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1165 /* check that we have released everything then unregister */
1166 stream_int_unregister_handler(si);
1167 }
1168}
1169
1170
Willy Tarreau3e76e722007-10-17 18:57:38 +02001171/*
1172 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +02001173 * client socket shut down on input. It stops by itself by unsetting the
Willy Tarreau72b179a2008-08-28 16:01:32 +02001174 * BF_HIJACK flag from the buffer, which it uses to keep on being called
Willy Tarreau1ae3a052008-08-16 10:56:30 +02001175 * when there is free space in the buffer, of simply by letting an empty buffer
1176 * upon return.s->data_ctx must have been zeroed before the first call, and the
1177 * flags set. It returns 0 if it had to stop writing data and an I/O is needed,
1178 * 1 if the dump is finished and the session must be closed, or -1 in case of
1179 * any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001180 */
Willy Tarreau0a464892008-12-07 18:30:00 +01001181int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001182{
Willy Tarreau91861262007-10-17 17:06:05 +02001183 struct proxy *px;
1184 struct chunk msg;
1185 unsigned int up;
1186
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001187 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001188
1189 switch (s->data_state) {
1190 case DATA_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001191 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001192 "HTTP/1.0 200 OK\r\n"
1193 "Cache-Control: no-cache\r\n"
1194 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001195 "Content-Type: %s\r\n",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001196 (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001197
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001198 if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001199 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001200 uri->refresh);
1201
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001202 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001203
1204 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001205 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001206 return 0;
1207
Willy Tarreau91861262007-10-17 17:06:05 +02001208 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1209 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1210 if (!(s->flags & SN_FINST_MASK))
1211 s->flags |= SN_FINST_R;
1212
1213 if (s->txn.meth == HTTP_METH_HEAD) {
1214 /* that's all we return in case of HEAD request */
1215 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001216 return 1;
1217 }
1218
1219 s->data_state = DATA_ST_HEAD; /* let's start producing data */
1220 /* fall through */
1221
1222 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001223 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001224 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001225 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001226 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1227 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001228 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001229 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1230 "<style type=\"text/css\"><!--\n"
1231 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001232 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001233 " font-size: 12px;"
1234 " font-weight: normal;"
1235 " color: black;"
1236 " background: white;"
1237 "}\n"
1238 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001239 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001240 "}\n"
1241 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001242 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001243 " margin-bottom: 0.5em;"
1244 "}\n"
1245 "h2 {"
1246 " font-family: helvetica, arial;"
1247 " font-size: x-large;"
1248 " font-weight: bold;"
1249 " font-style: italic;"
1250 " color: #6020a0;"
1251 " margin-top: 0em;"
1252 " margin-bottom: 0em;"
1253 "}\n"
1254 "h3 {"
1255 " font-family: helvetica, arial;"
1256 " font-size: 16px;"
1257 " font-weight: bold;"
1258 " color: #b00040;"
1259 " background: #e8e8d0;"
1260 " margin-top: 0em;"
1261 " margin-bottom: 0em;"
1262 "}\n"
1263 "li {"
1264 " margin-top: 0.25em;"
1265 " margin-right: 2em;"
1266 "}\n"
1267 ".hr {margin-top: 0.25em;"
1268 " border-color: black;"
1269 " border-bottom-style: solid;"
1270 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001271 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001272 ".total {background: #20D0D0;color: #ffff80;}\n"
1273 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001274 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001275 ".backend {background: #e8e8d0;}\n"
1276 ".active0 {background: #ff9090;}\n"
1277 ".active1 {background: #ffd020;}\n"
1278 ".active2 {background: #ffffa0;}\n"
1279 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001280 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1281 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1282 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001283 ".backup0 {background: #ff9090;}\n"
1284 ".backup1 {background: #ff80ff;}\n"
1285 ".backup2 {background: #c060ff;}\n"
1286 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001287 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1288 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1289 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001290 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001291 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001292 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001293 "a.px:link {color: #ffff40; text-decoration: none;}"
1294 "a.px:visited {color: #ffff40; text-decoration: none;}"
1295 "a.px:hover {color: #ffffff; text-decoration: none;}"
1296 "a.lfsb:link {color: #000000; text-decoration: none;}"
1297 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1298 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1299 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001300 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001301 "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"
1302 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001303 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001304 "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 +02001305 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001306 "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 +02001307 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001308 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1309 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1310 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001311 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001312 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001313 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001314 (uri->flags&ST_SHNODE) ? " on " : "",
1315 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001316 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001317 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001318 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001319 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001320 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001321 return 0;
1322
1323 s->data_state = DATA_ST_INFO;
1324 /* fall through */
1325
1326 case DATA_ST_INFO:
1327 up = (now.tv_sec - start_date.tv_sec);
1328
1329 /* WARNING! this has to fit the first packet too.
1330 * We are around 3.5 kB, add adding entries will
1331 * become tricky if we want to support 4kB buffers !
1332 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001333 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001334 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001335 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1336 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001337 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001338 "<hr width=\"100%%\" class=\"hr\">\n"
1339 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001340 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001341 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001342 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001343 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1344 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
1345 "current conns = %d; current pipes = %d/%d<br>\n"
1346 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001347 "</td><td align=\"center\" nowrap>\n"
1348 "<table class=\"lgd\"><tr>\n"
1349 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1350 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1351 "</tr><tr>\n"
1352 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1353 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1354 "</tr><tr>\n"
1355 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1356 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1357 "</tr><tr>\n"
1358 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001359 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001360 "</tr><tr>\n"
1361 "<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 +02001362 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001363 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001364 "</td>"
1365 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1366 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1367 "",
1368 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001369 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1370 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1371 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001372 up / 86400, (up % 86400) / 3600,
1373 (up % 3600) / 60, (up % 60),
1374 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1375 global.rlimit_memmax ? " MB" : "",
1376 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001377 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001378 actconn, pipes_used, pipes_used+pipes_free,
1379 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +02001380 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001381
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001382 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001383 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001384 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1385 uri->uri_prefix,
1386 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001387 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001388 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001389 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001390 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1391 uri->uri_prefix,
1392 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001393 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001394
Willy Tarreau55bb8452007-10-17 18:44:57 +02001395 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001396 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001397 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001398 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1399 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001400 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001401 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001402 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001403 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001404 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1405 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001406 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001407 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001408 }
Willy Tarreau91861262007-10-17 17:06:05 +02001409
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001410 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001411 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
1412 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001413 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
1414 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001415
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001416 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02001417 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
1418 uri->uri_prefix,
1419 (uri->refresh > 0) ? ";norefresh" : "");
1420
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001421 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001422 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001423 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1424 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
1425 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
1426 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
1427 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
1428 "</ul>"
1429 "</td>"
1430 "</tr></table>\n"
1431 ""
1432 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001433
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001434 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001435 return 0;
1436 }
Willy Tarreau91861262007-10-17 17:06:05 +02001437
Willy Tarreau91861262007-10-17 17:06:05 +02001438 s->data_ctx.stats.px = proxy;
1439 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1440 s->data_state = DATA_ST_LIST;
1441 /* fall through */
1442
1443 case DATA_ST_LIST:
1444 /* dump proxies */
1445 while (s->data_ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001446 if (buffer_almost_full(rep))
1447 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02001448 px = s->data_ctx.stats.px;
1449 /* skip the disabled proxies and non-networked ones */
1450 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001451 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001452 return 0;
1453
1454 s->data_ctx.stats.px = px->next;
1455 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1456 }
1457 /* here, we just have reached the last proxy */
1458
1459 s->data_state = DATA_ST_END;
1460 /* fall through */
1461
1462 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001463 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001464 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001465 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001466 return 0;
1467 }
Willy Tarreau91861262007-10-17 17:06:05 +02001468
1469 s->data_state = DATA_ST_FIN;
1470 /* fall through */
1471
1472 case DATA_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02001473 return 1;
1474
1475 default:
1476 /* unknown state ! */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001477 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001478 return -1;
1479 }
1480}
1481
1482
1483/*
1484 * Dumps statistics for a proxy.
1485 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1486 * ot non-zero if everything completed.
1487 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001488int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001489{
1490 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001491 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001492 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02001493 struct chunk msg;
1494
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001495 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001496
1497 switch (s->data_ctx.stats.px_st) {
1498 case DATA_ST_PX_INIT:
1499 /* we are on a new proxy */
1500
1501 if (uri && uri->scope) {
1502 /* we have a limited scope, we have to check the proxy name */
1503 struct stat_scope *scope;
1504 int len;
1505
1506 len = strlen(px->id);
1507 scope = uri->scope;
1508
1509 while (scope) {
1510 /* match exact proxy name */
1511 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1512 break;
1513
1514 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001515 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001516 break;
1517 scope = scope->next;
1518 }
1519
1520 /* proxy name not found : don't dump anything */
1521 if (scope == NULL)
1522 return 1;
1523 }
1524
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001525 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001526 (px->uuid != s->data_ctx.stats.iid))
1527 return 1;
1528
Willy Tarreau91861262007-10-17 17:06:05 +02001529 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
1530 /* fall through */
1531
1532 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001533 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001534 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001535 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001536 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001537 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001538 "<th class=\"pxname\" width=\"10%%\"");
1539
1540 if (uri->flags&ST_SHLGNDS) {
1541 /* cap, mode, id */
1542 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
1543 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1544 px->uuid);
1545
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001546 chunk_printf(&msg, "\"");
1547 }
1548
1549 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001550 ">%s<a name=\"%s\"></a>"
1551 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001552 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001553 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001554 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001555 "<table class=\"tbl\" width=\"100%%\">\n"
1556 "<tr class=\"titre\">"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001557 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001558 "<th colspan=3>Queue</th>"
1559 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001560 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001561 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001562 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001563 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001564 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001565 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001566 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001567 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001568 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001569 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001570 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001571 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1572 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001573 "</tr>",
Willy Tarreaue0454092010-02-26 12:29:07 +01001574 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001575 px->id, px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01001576 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001577 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001578
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001579 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001580 return 0;
1581 }
Willy Tarreau91861262007-10-17 17:06:05 +02001582
1583 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
1584 /* fall through */
1585
1586 case DATA_ST_PX_FE:
1587 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001588 if ((px->cap & PR_CAP_FE) &&
1589 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
1590 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001591 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001592 /* name, queue */
Krzysztof Piotr Oledzki516ed492009-10-22 22:48:09 +02001593 "<tr class=\"frontend\"><td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001594 "<a name=\"%s/Frontend\"></a>"
1595 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td><td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01001596 "",
1597 px->id, px->id);
1598
1599 if (px->mode == PR_MODE_HTTP) {
1600 chunk_printf(&msg,
1601 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01001602 "<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 +01001603 "",
1604 read_freq_ctr(&px->fe_req_per_sec),
1605 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
1606 px->counters.fe_rps_max,
1607 U2H2(px->counters.fe_sps_max),
1608 LIM2A2(px->fe_sps_lim, "-"));
1609 } else {
1610 chunk_printf(&msg,
1611 /* sessions rate : current, max, limit */
1612 "<td>%s</td><td>%s</td><td>%s</td>"
1613 "",
1614 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
1615 U2H1(px->counters.fe_sps_max), LIM2A2(px->fe_sps_lim, "-"));
1616 }
1617
1618 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001619 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001620 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001621 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001622 "",
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001623 U2H3(px->feconn), U2H4(px->counters.feconn_max), U2H5(px->maxconn));
1624
1625 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1626 if (px->mode == PR_MODE_HTTP) {
1627 int i;
1628
Willy Tarreaub44939a2010-02-26 11:35:39 +01001629 chunk_printf(&msg, " title=\"%lld requests:", px->counters.cum_fe_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001630
1631 for (i = 1; i < 6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01001632 chunk_printf(&msg, " %dxx=%lld,", i, px->counters.fe.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001633
Willy Tarreau24657792010-02-26 10:30:28 +01001634 chunk_printf(&msg, " other=%lld\"", px->counters.fe.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001635 }
1636
1637 chunk_printf(&msg,
1638 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01001639 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001640 /* bytes : in, out */
1641 "<td>%s</td><td>%s</td>"
1642 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001643 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreaub36b4242010-06-04 20:59:39 +02001644 U2H6(px->counters.cum_fesess),
Willy Tarreaue0454092010-02-26 12:29:07 +01001645 (px->mode == PR_MODE_HTTP)?"</u>":"",
1646 U2H7(px->counters.bytes_in), U2H8(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001647
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001648 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001649 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001650 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001651 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001652 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001653 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001654 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001655 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001656 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001657 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001658 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02001659 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001660 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
1661 U2H2(px->counters.failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +02001662 px->state == PR_STRUN ? "OPEN" :
1663 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001664 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001665 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001666 /* pxid, name, queue cur, queue max, */
1667 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001668 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001669 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001670 /* bytes : in, out */
1671 "%lld,%lld,"
1672 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001673 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001674 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001675 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001676 /* warnings: retries, redispatches */
1677 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001678 /* server status : reflect frontend status */
1679 "%s,"
1680 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001681 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001682 /* pid, iid, sid, throttle, lbtot, tracked, type */
1683 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001684 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001685 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001686 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001687 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02001688 px->id,
Willy Tarreaub36b4242010-06-04 20:59:39 +02001689 px->feconn, px->counters.feconn_max, px->maxconn, px->counters.cum_fesess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001690 px->counters.bytes_in, px->counters.bytes_out,
1691 px->counters.denied_req, px->counters.denied_resp,
1692 px->counters.failed_req,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001693 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +01001694 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01001695 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001696 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001697 px->fe_sps_lim, px->counters.fe_sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001698
1699 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1700 if (px->mode == PR_MODE_HTTP) {
1701 int i;
1702
1703 for (i=1; i<6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01001704 chunk_printf(&msg, "%lld,", px->counters.fe.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001705
Willy Tarreau24657792010-02-26 10:30:28 +01001706 chunk_printf(&msg, "%lld,", px->counters.fe.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001707 } else {
1708 chunk_printf(&msg, ",,,,,,");
1709 }
1710
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001711 /* failed health analyses */
1712 chunk_printf(&msg, ",");
1713
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001714 /* requests : req_rate, req_rate_max, req_tot, */
1715 chunk_printf(&msg, "%u,%u,%lld,",
1716 read_freq_ctr(&px->fe_req_per_sec),
1717 px->counters.fe_rps_max, px->counters.cum_fe_req);
1718
Willy Tarreauae526782010-03-04 20:34:23 +01001719 /* errors: cli_aborts, srv_aborts */
1720 chunk_printf(&msg, ",,");
1721
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001722 /* finish with EOL */
1723 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001724 }
Willy Tarreau91861262007-10-17 17:06:05 +02001725
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001726 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001727 return 0;
1728 }
1729
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001730 s->data_ctx.stats.l = px->listen; /* may be NULL */
1731 s->data_ctx.stats.px_st = DATA_ST_PX_LI;
1732 /* fall through */
1733
1734 case DATA_ST_PX_LI:
1735 /* stats.l has been initialized above */
1736 for (; s->data_ctx.stats.l != NULL; s->data_ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001737 if (buffer_almost_full(rep))
1738 return 0;
1739
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001740 l = s->data_ctx.stats.l;
1741 if (!l->counters)
1742 continue;
1743
1744 if (s->data_ctx.stats.flags & STAT_BOUND) {
1745 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SO)))
1746 break;
1747
1748 if (s->data_ctx.stats.sid != -1 && l->luid != s->data_ctx.stats.sid)
1749 continue;
1750 }
1751
1752 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001753 chunk_printf(&msg, "<tr class=socket><td class=ac");
1754
1755 if (uri->flags&ST_SHLGNDS) {
1756 char str[INET6_ADDRSTRLEN], *fmt = NULL;
1757 int port;
1758
1759 chunk_printf(&msg, " title=\"IP: ");
1760
1761 port = (l->addr.ss_family == AF_INET6)
1762 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
1763 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
1764
1765 if (l->addr.ss_family == AF_INET) {
1766 if (inet_ntop(AF_INET,
1767 (const void *)&((struct sockaddr_in *)&l->addr)->sin_addr,
1768 str, sizeof(str)))
1769 fmt = "%s:%d";
1770 } else {
1771 if (inet_ntop(AF_INET6,
1772 (const void *)&((struct sockaddr_in6 *)(&l->addr))->sin6_addr,
1773 str, sizeof(str)))
1774 fmt = "[%s]:%d";
1775 }
1776
1777 if (fmt)
1778 chunk_printf(&msg, fmt, str, port);
1779 else
1780 chunk_printf(&msg, "(%s)", strerror(errno));
1781
1782 /* id */
1783 chunk_printf(&msg, ", id: %d", l->luid);
1784
1785 chunk_printf(&msg, "\"");
1786 }
1787
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001788 chunk_printf(&msg,
1789 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01001790 ">%s<a name=\"%s/+%s\"></a>"
1791 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001792 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001793 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001794 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001795 "<td>%s</td><td>%s</td><td>%s</td>"
1796 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001797 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001798 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001799 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001800 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01001801 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01001802 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001803 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
1804 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
1805
1806 chunk_printf(&msg,
1807 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001808 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001809 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001810 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001811 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001812 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001813 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001814 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001815 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001816 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001817 "",
1818 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
1819 U2H2(l->counters->failed_req),
1820 (l->nbconn < l->maxconn) ? "OPEN" : "FULL");
1821 } else {
1822 chunk_printf(&msg,
1823 /* pxid, name, queue cur, queue max, */
1824 "%s,%s,,,"
1825 /* sessions: current, max, limit, total */
1826 "%d,%d,%d,%lld,"
1827 /* bytes: in, out */
1828 "%lld,%lld,"
1829 /* denied: req, resp */
1830 "%lld,%lld,"
1831 /* errors: request, connect, response */
1832 "%lld,,,"
1833 /* warnings: retries, redispatches */
1834 ",,"
1835 /* server status: reflect listener status */
1836 "%s,"
1837 /* rest of server: nothing */
1838 ",,,,,,,,"
1839 /* pid, iid, sid, throttle, lbtot, tracked, type */
1840 "%d,%d,%d,,,,%d,"
1841 /* rate, rate_lim, rate_max */
1842 ",,,"
1843 /* check_status, check_code, check_duration */
1844 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001845 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1846 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001847 /* failed health analyses */
1848 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001849 /* requests : req_rate, req_rate_max, req_tot, */
1850 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01001851 /* errors: cli_aborts, srv_aborts */
1852 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001853 "\n",
1854 px->id, l->name,
1855 l->nbconn, l->counters->conn_max,
1856 l->maxconn, l->counters->cum_conn,
1857 l->counters->bytes_in, l->counters->bytes_out,
1858 l->counters->denied_req, l->counters->denied_resp,
1859 l->counters->failed_req,
1860 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
1861 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
1862 }
1863
1864 if (buffer_feed_chunk(rep, &msg) >= 0)
1865 return 0;
1866 }
1867
Willy Tarreau91861262007-10-17 17:06:05 +02001868 s->data_ctx.stats.sv = px->srv; /* may be NULL */
1869 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
1870 /* fall through */
1871
1872 case DATA_ST_PX_SV:
1873 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001874 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001875 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 +02001876
Willy Tarreau4e33d862009-10-11 23:35:10 +02001877 if (buffer_almost_full(rep))
1878 return 0;
1879
Willy Tarreau91861262007-10-17 17:06:05 +02001880 sv = s->data_ctx.stats.sv;
1881
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001882 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001883 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
1884 break;
1885
1886 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
1887 continue;
1888 }
1889
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001890 if (sv->tracked)
1891 svs = sv->tracked;
1892 else
1893 svs = sv;
1894
Willy Tarreau91861262007-10-17 17:06:05 +02001895 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001896 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01001897 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001898 else if (svs->state & SRV_RUNNING) {
1899 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02001900 sv_state = 3; /* UP */
1901 else
1902 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01001903
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001904 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01001905 sv_state += 2;
1906 }
Willy Tarreau91861262007-10-17 17:06:05 +02001907 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001908 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02001909 sv_state = 1; /* going up */
1910 else
1911 sv_state = 0; /* DOWN */
1912
Cyril Bonté0dae5852010-02-03 00:26:28 +01001913 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02001914 /* do not report servers which are DOWN */
1915 s->data_ctx.stats.sv = sv->next;
1916 continue;
1917 }
1918
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001919 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001920 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
1921 "UP %d/%d &darr;", "UP",
1922 "NOLB %d/%d &darr;", "NOLB",
1923 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01001924 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001925 chunk_printf(&msg,
1926 /* name */
1927 "<tr class=\"maintain\"><td class=ac"
1928 );
1929 }
1930 else {
1931 chunk_printf(&msg,
1932 /* name */
1933 "<tr class=\"%s%d\"><td class=ac",
1934 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
1935 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001936
1937 if (uri->flags&ST_SHLGNDS) {
1938 char str[INET6_ADDRSTRLEN];
1939
1940 chunk_printf(&msg, " title=\"IP: ");
1941
1942 /* IP */
1943 if (inet_ntop(sv->addr.sin_family, &sv->addr.sin_addr, str, sizeof(str)))
1944 chunk_printf(&msg, "%s:%d", str, htons(sv->addr.sin_port));
1945 else
1946 chunk_printf(&msg, "(%s)", strerror(errno));
1947
1948 /* id */
1949 chunk_printf(&msg, ", id: %d", sv->puid);
1950
1951 /* cookie */
1952 if (sv->cookie) {
1953 struct chunk src;
1954
1955 chunk_printf(&msg, ", cookie: '");
1956
1957 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
1958 chunk_htmlencode(&msg, &src);
1959
1960 chunk_printf(&msg, "'");
1961 }
1962
1963 chunk_printf(&msg, "\"");
1964 }
1965
1966 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001967 ">%s<a name=\"%s/%s\"></a>"
1968 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001969 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001970 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001971 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001972 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001973 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001974 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001975 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001976 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001977 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001978 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01001979 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001980 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
1981 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001982 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
1983
1984 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1985 if (px->mode == PR_MODE_HTTP) {
1986 int i;
1987
1988 chunk_printf(&msg, " title=\"rsp codes:");
1989
1990 for (i = 1; i < 6; i++)
1991 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
1992
1993 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
1994 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001995
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001996 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001997 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01001998 ">%s%s%s</td><td>%s</td>",
1999 (px->mode == PR_MODE_HTTP)?"<u>":"",
2000 U2H0(sv->counters.cum_sess),
2001 (px->mode == PR_MODE_HTTP)?"</u>":"",
2002 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002003
2004 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002005 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002006 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002007 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002008 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002009 /* errors : request, connect */
2010 "<td></td><td>%s</td>"
2011 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002012 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002013 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002014 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002015 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002016 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2017 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002018 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002019 sv->counters.cli_aborts,
2020 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002021 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002022 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002023
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002024 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002025 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002026
Cyril Bontécd19e512010-01-31 22:34:03 +01002027 if (sv->state & SRV_MAINTAIN) {
2028 chunk_printf(&msg, "%s ",
2029 human_time(now.tv_sec - sv->last_change, 1));
2030 chunk_printf(&msg, "MAINT");
2031 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002032 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2033 chunk_printf(&msg, "%s ",
2034 human_time(now.tv_sec - svs->last_change, 1));
2035 chunk_printf(&msg, "MAINT(via)");
2036 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002037 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002038 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002039 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002040
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002041 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002042 srv_hlt_st[sv_state],
2043 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2044 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002045 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002046
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002047 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002048 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002049 get_check_status_description(sv->check_status));
2050
2051 if (*sv->check_desc) {
2052 struct chunk src;
2053
2054 chunk_printf(&msg, ": ");
2055
2056 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2057 chunk_htmlencode(&msg, &src);
2058 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002059
Willy Tarreaue0454092010-02-26 12:29:07 +01002060 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002061 tv_iszero(&sv->check_start)?"":"* ",
2062 get_check_status_info(sv->check_status));
2063
2064 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002065 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002066
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002067 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002068 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002069 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002070 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002071
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002072 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002073 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002074 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002075 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002076 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002077 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002078 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002079 (sv->state & SRV_BACKUP) ? "-" : "Y",
2080 (sv->state & SRV_BACKUP) ? "Y" : "-");
2081
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002082 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002083 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002084 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002085 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2086
2087 if (svs->observe)
2088 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2089
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002090 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002091 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002092 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002093 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002094 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002095 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002096 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002097 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2098 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002099 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002100 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002101 "<td colspan=3></td>");
2102
2103 /* throttle */
2104 if ((sv->state & SRV_WARMINGUP) &&
2105 now.tv_sec < sv->last_change + sv->slowstart &&
2106 now.tv_sec >= sv->last_change) {
2107 unsigned int ratio;
2108 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002109 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002110 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002111 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002112 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002113 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002114 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002115 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002116 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2117 "UP %d/%d,", "UP,",
2118 "NOLB %d/%d,", "NOLB,",
2119 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002120 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002121 /* pxid, name */
2122 "%s,%s,"
2123 /* queue : current, max */
2124 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002125 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002126 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002127 /* bytes : in, out */
2128 "%lld,%lld,"
2129 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002130 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002131 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002132 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002133 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002134 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002135 "",
2136 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002137 sv->nbpend, sv->counters.nbpend_max,
2138 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002139 sv->counters.bytes_in, sv->counters.bytes_out,
2140 sv->counters.failed_secu,
2141 sv->counters.failed_conns, sv->counters.failed_resp,
2142 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002143
Willy Tarreau55bb8452007-10-17 18:44:57 +02002144 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002145 if (sv->state & SRV_MAINTAIN) {
2146 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002147 }
2148 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2149 chunk_printf(&msg, "MAINT(via),");
2150 }
2151 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002152 chunk_printf(&msg,
2153 srv_hlt_st[sv_state],
2154 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2155 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2156 }
Willy Tarreau91861262007-10-17 17:06:05 +02002157
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002158 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002159 /* weight, active, backup */
2160 "%d,%d,%d,"
2161 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002162 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002163 (sv->state & SRV_BACKUP) ? 0 : 1,
2164 (sv->state & SRV_BACKUP) ? 1 : 0);
2165
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002166 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002167 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002168 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002169 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002170 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002171 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002172 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002173 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002174 ",,,,");
2175
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002176 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002177 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002178 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002179 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002180 LIM2A0(sv->maxqueue, ""),
2181 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002182
2183 /* throttle */
2184 if ((sv->state & SRV_WARMINGUP) &&
2185 now.tv_sec < sv->last_change + sv->slowstart &&
2186 now.tv_sec >= sv->last_change) {
2187 unsigned int ratio;
2188 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002189 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002190 }
2191
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002192 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002193 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002194
2195 /* tracked */
2196 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002197 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002198 sv->tracked->proxy->id, sv->tracked->id);
2199 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002200 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002201
Willy Tarreau7f062c42009-03-05 18:43:00 +01002202 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002203 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002204
2205 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002206 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002207 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002208 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002209
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002210 if (sv->state & SRV_CHECKED) {
2211 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002212 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002213
2214 /* check_code */
2215 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002216 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002217 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002218 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002219
2220 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002221 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002222 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002223 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002224 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002225
2226 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002227 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002228 }
2229
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002230 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2231 if (px->mode == PR_MODE_HTTP) {
2232 int i;
2233
2234 for (i=1; i<6; i++)
2235 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2236
2237 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2238 } else {
2239 chunk_printf(&msg, ",,,,,,");
2240 }
2241
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002242 /* failed health analyses */
2243 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2244
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002245 /* requests : req_rate, req_rate_max, req_tot, */
2246 chunk_printf(&msg, ",,,");
2247
Willy Tarreauae526782010-03-04 20:34:23 +01002248 /* errors: cli_aborts, srv_aborts */
2249 chunk_printf(&msg, "%lld,%lld,",
2250 sv->counters.cli_aborts, sv->counters.srv_aborts);
2251
Willy Tarreau7f062c42009-03-05 18:43:00 +01002252 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002253 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002254 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002255 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002256 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002257 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002258
2259 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
2260 /* fall through */
2261
2262 case DATA_ST_PX_BE:
2263 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002264 if ((px->cap & PR_CAP_BE) &&
2265 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2266 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002267 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002268 /* name */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002269 "<tr class=\"backend\"><td class=ac");
2270
2271 if (uri->flags&ST_SHLGNDS) {
2272 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002273 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002274 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002275
2276 /* cookie */
2277 if (px->cookie_name) {
2278 struct chunk src;
2279
2280 chunk_printf(&msg, ", cookie: '");
2281
2282 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2283 chunk_htmlencode(&msg, &src);
2284
2285 chunk_printf(&msg, "'");
2286 }
2287
2288 chunk_printf(&msg, "\"");
2289
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002290 }
2291
2292 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002293 ">%s<a name=\"%s/Backend\"></a>"
2294 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002295 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002296 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002297 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002298 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002299 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002300 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002301 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002302 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002303 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->counters.nbpend_max),
2304 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->counters.be_sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02002305
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002306 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002307 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002308 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002309 "<td"
2310 "",
2311 U2H2(px->beconn), U2H3(px->counters.beconn_max), U2H4(px->fullconn));
2312
2313 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2314 if (px->mode == PR_MODE_HTTP) {
2315 int i;
2316
2317 chunk_printf(&msg, " title=\"rsp codes:");
2318
2319 for (i = 1; i < 6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01002320 chunk_printf(&msg, " %dxx=%lld", i, px->counters.be.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002321
Willy Tarreau24657792010-02-26 10:30:28 +01002322 chunk_printf(&msg, " other=%lld\"", px->counters.be.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002323 }
2324
2325 chunk_printf(&msg,
2326 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002327 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002328 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002329 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002330 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002331 (px->mode == PR_MODE_HTTP)?"<u>":"",
2332 U2H6(px->counters.cum_beconn),
2333 (px->mode == PR_MODE_HTTP)?"</u>":"",
2334 U2H7(px->counters.cum_lbconn),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002335 U2H8(px->counters.bytes_in), U2H9(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002336
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002337 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002338 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002339 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002340 /* errors : request, connect */
2341 "<td></td><td>%s</td>"
2342 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002343 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002344 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002345 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002346 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02002347 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002348 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02002349 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002350 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2351 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002352 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002353 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
Willy Tarreauae526782010-03-04 20:34:23 +01002354 U2H2(px->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002355 px->counters.cli_aborts,
2356 px->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002357 U2H5(px->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002358 px->counters.retries, px->counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002359 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01002360 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2361 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01002362 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002363 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002364
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002365 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002366 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002367 "<td class=ac>&nbsp;</td><td>%d</td>"
2368 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002369 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002370 "</tr>",
2371 px->down_trans,
2372 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002373 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002374 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002375 /* pxid, name */
2376 "%s,BACKEND,"
2377 /* queue : current, max */
2378 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002379 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002380 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002381 /* bytes : in, out */
2382 "%lld,%lld,"
2383 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002384 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002385 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002386 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002387 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002388 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002389 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02002390 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002391 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02002392 * active and backups. */
2393 "%s,"
2394 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002395 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002396 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002397 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002398 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002399 /* rate, rate_lim, rate_max, */
2400 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002401 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002402 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002403 px->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002404 px->nbpend /* or px->totpend ? */, px->counters.nbpend_max,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002405 px->beconn, px->counters.beconn_max, px->fullconn, px->counters.cum_beconn,
2406 px->counters.bytes_in, px->counters.bytes_out,
2407 px->counters.denied_req, px->counters.denied_resp,
2408 px->counters.failed_conns, px->counters.failed_resp,
2409 px->counters.retries, px->counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01002410 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01002411 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002412 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002413 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01002414 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002415 relative_pid, px->uuid,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002416 px->counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002417 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002418 px->counters.be_sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002419
2420 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2421 if (px->mode == PR_MODE_HTTP) {
2422 int i;
2423
2424 for (i=1; i<6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01002425 chunk_printf(&msg, "%lld,", px->counters.be.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002426
Willy Tarreau24657792010-02-26 10:30:28 +01002427 chunk_printf(&msg, "%lld,", px->counters.be.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002428 } else {
2429 chunk_printf(&msg, ",,,,,,");
2430 }
2431
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002432 /* failed health analyses */
2433 chunk_printf(&msg, ",");
2434
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002435 /* requests : req_rate, req_rate_max, req_tot, */
2436 chunk_printf(&msg, ",,,");
2437
Willy Tarreauae526782010-03-04 20:34:23 +01002438 /* errors: cli_aborts, srv_aborts */
2439 chunk_printf(&msg, "%lld,%lld,",
2440 px->counters.cli_aborts, px->counters.srv_aborts);
2441
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002442 /* finish with EOL */
2443 chunk_printf(&msg, "\n");
2444
Willy Tarreau55bb8452007-10-17 18:44:57 +02002445 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002446 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002447 return 0;
2448 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002449
Willy Tarreau91861262007-10-17 17:06:05 +02002450 s->data_ctx.stats.px_st = DATA_ST_PX_END;
2451 /* fall through */
2452
2453 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002454 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002455 chunk_printf(&msg, "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02002456
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002457 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002458 return 0;
2459 }
Willy Tarreau91861262007-10-17 17:06:05 +02002460
2461 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
2462 /* fall through */
2463
2464 case DATA_ST_PX_FIN:
2465 return 1;
2466
2467 default:
2468 /* unknown state, we should put an abort() here ! */
2469 return 1;
2470 }
2471}
2472
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002473/* This function is called to send output to the response buffer. It dumps a
2474 * complete session state onto the output buffer <rep>. The session has to be
2475 * set in data_ctx.sess.target. It returns 0 if the output buffer is full and
2476 * it needs to be called again, otherwise non-zero. It is designed to be called
2477 * from stats_dump_sess_to_buffer() below.
2478 */
2479
2480/* returns 1 if dump is not complete */
2481int stats_dump_full_sess_to_buffer(struct session *s, struct buffer *rep)
2482{
2483 struct tm tm;
2484 struct chunk msg;
2485 struct session *sess;
2486 extern const char *monthname[12];
2487 char pn[INET6_ADDRSTRLEN];
2488
2489 chunk_init(&msg, trash, sizeof(trash));
2490 sess = s->data_ctx.sess.target;
2491
2492 if (s->data_ctx.sess.section > 0 && s->data_ctx.sess.uid != sess->uniq_id) {
2493 /* session changed, no need to go any further */
2494 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
2495 if (buffer_feed_chunk(rep, &msg) >= 0)
2496 return 0;
2497 s->data_ctx.sess.target = NULL;
2498 s->data_ctx.sess.uid = 0;
2499 return 1;
2500 }
2501
2502 switch (s->data_ctx.sess.section) {
2503 case 0: /* main status of the session */
2504 s->data_ctx.sess.uid = sess->uniq_id;
2505 s->data_ctx.sess.section = 1;
2506 /* fall through */
2507
2508 case 1:
2509 chunk_printf(&msg,
2510 "%p: id=%u, proto=%s",
2511 sess,
2512 sess->uniq_id,
2513 sess->listener->proto->name);
2514
2515 switch (sess->listener->proto->sock_family) {
2516 case AF_INET:
2517 inet_ntop(AF_INET,
2518 (const void *)&((struct sockaddr_in *)&sess->cli_addr)->sin_addr,
2519 pn, sizeof(pn));
2520
2521 chunk_printf(&msg,
2522 " source=%s:%d\n",
2523 pn,
2524 ntohs(((struct sockaddr_in *)&sess->cli_addr)->sin_port));
2525 break;
2526 case AF_INET6:
2527 inet_ntop(AF_INET6,
2528 (const void *)&((struct sockaddr_in6 *)(&sess->cli_addr))->sin6_addr,
2529 pn, sizeof(pn));
2530
2531 chunk_printf(&msg,
2532 " source=%s:%d\n",
2533 pn,
2534 ntohs(((struct sockaddr_in6 *)&sess->cli_addr)->sin6_port));
2535 break;
2536 case AF_UNIX:
2537 default:
2538 /* no more information to print right now */
2539 chunk_printf(&msg, "\n");
2540 break;
2541 }
2542
2543 chunk_printf(&msg,
2544 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02002545 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002546
2547 chunk_printf(&msg,
2548 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)\n",
2549 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
2550 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
2551 sess->listener ? sess->listener->luid : 0);
2552
2553 chunk_printf(&msg,
2554 " backend=%s (id=%u mode=%s) server=%s (id=%u)\n",
2555 sess->be->id, sess->be->uuid, sess->be->mode ? "http" : "tcp",
2556 sess->srv ? sess->srv->id : "<none>",
2557 sess->srv ? sess->srv->puid : 0);
2558
2559 chunk_printf(&msg,
2560 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
2561 sess->task,
2562 sess->task->state,
2563 sess->task->nice, sess->task->calls,
2564 sess->task->expire ?
2565 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
2566 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
2567 TICKS_TO_MS(1000)) : "<NEVER>",
2568 task_in_rq(sess->task) ? ", running" : "");
2569
2570 get_localtime(sess->logs.accept_date.tv_sec, &tm);
2571 chunk_printf(&msg,
2572 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
2573 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2574 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
2575 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
2576
2577 chunk_printf(&msg,
2578 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
2579 &sess->si[0],
2580 sess->si[0].state,
2581 sess->si[0].flags,
2582 sess->si[0].fd,
2583 sess->si[0].exp ?
2584 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
2585 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
2586 TICKS_TO_MS(1000)) : "<NEVER>",
2587 sess->si[0].err_type);
2588
2589 chunk_printf(&msg,
2590 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
2591 &sess->si[1],
2592 sess->si[1].state,
2593 sess->si[1].flags,
2594 sess->si[1].fd,
2595 sess->si[1].exp ?
2596 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
2597 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
2598 TICKS_TO_MS(1000)) : "<NEVER>",
2599 sess->si[1].err_type);
2600
2601 chunk_printf(&msg,
2602 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
2603 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
2604 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
2605
2606
2607 chunk_printf(&msg,
2608 " req=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%ld)\n"
2609 " an_exp=%s",
2610 sess->req,
2611 sess->req->flags, sess->req->analysers,
2612 sess->req->l, sess->req->send_max,
2613 sess->req->pipe ? sess->req->pipe->data : 0,
2614 sess->req->to_forward,
2615 sess->req->analyse_exp ?
2616 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
2617 TICKS_TO_MS(1000)) : "<NEVER>");
2618
2619 chunk_printf(&msg,
2620 " rex=%s",
2621 sess->req->rex ?
2622 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
2623 TICKS_TO_MS(1000)) : "<NEVER>");
2624
2625 chunk_printf(&msg,
2626 " wex=%s\n"
2627 " data=%p r=%d w=%d lr=%d total=%lld\n",
2628 sess->req->wex ?
2629 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
2630 TICKS_TO_MS(1000)) : "<NEVER>",
2631 sess->req->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01002632 (int)(sess->req->r - sess->req->data),
2633 (int)(sess->req->w - sess->req->data),
2634 (int)(sess->req->lr - sess->req->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002635 sess->req->total);
2636
2637 chunk_printf(&msg,
2638 " res=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%ld)\n"
2639 " an_exp=%s",
2640 sess->rep,
2641 sess->rep->flags, sess->rep->analysers,
2642 sess->rep->l, sess->rep->send_max,
2643 sess->rep->pipe ? sess->rep->pipe->data : 0,
2644 sess->rep->to_forward,
2645 sess->rep->analyse_exp ?
2646 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
2647 TICKS_TO_MS(1000)) : "<NEVER>");
2648
2649 chunk_printf(&msg,
2650 " rex=%s",
2651 sess->rep->rex ?
2652 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
2653 TICKS_TO_MS(1000)) : "<NEVER>");
2654
2655 chunk_printf(&msg,
2656 " wex=%s\n"
2657 " data=%p r=%d w=%d lr=%d total=%lld\n",
2658 sess->rep->wex ?
2659 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
2660 TICKS_TO_MS(1000)) : "<NEVER>",
2661 sess->rep->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01002662 (int)(sess->rep->r - sess->rep->data),
2663 (int)(sess->rep->w - sess->rep->data),
2664 (int)(sess->rep->lr - sess->rep->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002665 sess->rep->total);
2666
2667 if (buffer_feed_chunk(rep, &msg) >= 0)
2668 return 0;
2669
2670 /* use other states to dump the contents */
2671 }
2672 /* end of dump */
2673 s->data_ctx.sess.uid = 0;
2674 return 1;
2675}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002676
2677/* This function is called to send output to the response buffer.
2678 * It dumps the sessions states onto the output buffer <rep>.
2679 * Expects to be called with client socket shut down on input.
2680 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002681 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002682 */
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002683int stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002684{
2685 struct chunk msg;
2686
2687 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
2688 /* If we're forced to shut down, we might have to remove our
2689 * reference to the last session being dumped.
2690 */
2691 if (s->data_state == DATA_ST_LIST) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002692 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002693 LIST_DEL(&s->data_ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002694 LIST_INIT(&s->data_ctx.sess.bref.users);
2695 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002696 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002697 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002698 }
2699
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002700 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002701
2702 switch (s->data_state) {
2703 case DATA_ST_INIT:
2704 /* the function had not been called yet, let's prepare the
2705 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002706 * pointer to the first in the global list. When a target
2707 * session is being destroyed, it is responsible for updating
2708 * this pointer. We know we have reached the end when this
2709 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002710 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002711 LIST_INIT(&s->data_ctx.sess.bref.users);
2712 s->data_ctx.sess.bref.ref = sessions.n;
2713 s->data_state = DATA_ST_LIST;
2714 /* fall through */
2715
2716 case DATA_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002717 /* first, let's detach the back-ref from a possible previous session */
2718 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
2719 LIST_DEL(&s->data_ctx.sess.bref.users);
2720 LIST_INIT(&s->data_ctx.sess.bref.users);
2721 }
2722
2723 /* and start from where we stopped */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002724 while (s->data_ctx.sess.bref.ref != &sessions) {
2725 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
2726 struct session *curr_sess;
2727
2728 curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list);
2729
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002730 if (s->data_ctx.sess.target) {
2731 if (s->data_ctx.sess.target != curr_sess)
2732 goto next_sess;
2733
2734 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
2735 /* call the proper dump() function and return if we're missing space */
2736 if (!stats_dump_full_sess_to_buffer(s, rep))
2737 return 0;
2738
2739 /* session dump complete */
2740 LIST_DEL(&s->data_ctx.sess.bref.users);
2741 LIST_INIT(&s->data_ctx.sess.bref.users);
2742 s->data_ctx.sess.target = NULL;
2743 break;
2744 }
2745
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002746 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002747 "%p: proto=%s",
2748 curr_sess,
2749 curr_sess->listener->proto->name);
2750
2751 switch (curr_sess->listener->proto->sock_family) {
2752 case AF_INET:
2753 inet_ntop(AF_INET,
2754 (const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr,
2755 pn, sizeof(pn));
2756
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002757 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002758 " src=%s:%d fe=%s be=%s srv=%s",
2759 pn,
2760 ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port),
2761 curr_sess->fe->id,
2762 curr_sess->be->id,
2763 curr_sess->srv ? curr_sess->srv->id : "<none>"
2764 );
2765 break;
2766 case AF_INET6:
2767 inet_ntop(AF_INET6,
2768 (const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr,
2769 pn, sizeof(pn));
2770
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002771 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002772 " src=%s:%d fe=%s be=%s srv=%s",
2773 pn,
2774 ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port),
2775 curr_sess->fe->id,
2776 curr_sess->be->id,
2777 curr_sess->srv ? curr_sess->srv->id : "<none>"
2778 );
2779
2780 break;
2781 case AF_UNIX:
2782 /* no more information to print right now */
2783 break;
2784 }
2785
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002786 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02002787 " ts=%02x age=%s calls=%d",
2788 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01002789 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
2790 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002791
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002792 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002793 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
2794 curr_sess->req->flags,
2795 curr_sess->req->l,
2796 curr_sess->req->analysers,
2797 curr_sess->req->rex ?
2798 human_time(TICKS_TO_MS(curr_sess->req->rex - 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 ",wx=%s",
2803 curr_sess->req->wex ?
2804 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
2805 TICKS_TO_MS(1000)) : "");
2806
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002807 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002808 ",ax=%s]",
2809 curr_sess->req->analyse_exp ?
2810 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
2811 TICKS_TO_MS(1000)) : "");
2812
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002813 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002814 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
2815 curr_sess->rep->flags,
2816 curr_sess->rep->l,
2817 curr_sess->rep->analysers,
2818 curr_sess->rep->rex ?
2819 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
2820 TICKS_TO_MS(1000)) : "");
2821
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002822 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002823 ",wx=%s",
2824 curr_sess->rep->wex ?
2825 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
2826 TICKS_TO_MS(1000)) : "");
2827
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002828 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002829 ",ax=%s]",
2830 curr_sess->rep->analyse_exp ?
2831 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
2832 TICKS_TO_MS(1000)) : "");
2833
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002834 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002835 " s0=[%d,%1xh,fd=%d,ex=%s]",
2836 curr_sess->si[0].state,
2837 curr_sess->si[0].flags,
2838 curr_sess->si[0].fd,
2839 curr_sess->si[0].exp ?
2840 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
2841 TICKS_TO_MS(1000)) : "");
2842
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002843 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002844 " s1=[%d,%1xh,fd=%d,ex=%s]",
2845 curr_sess->si[1].state,
2846 curr_sess->si[1].flags,
2847 curr_sess->si[1].fd,
2848 curr_sess->si[1].exp ?
2849 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
2850 TICKS_TO_MS(1000)) : "");
2851
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002852 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002853 " exp=%s",
2854 curr_sess->task->expire ?
2855 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
2856 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01002857 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002858 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002859
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002860 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002861
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002862 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002863 /* let's try again later from this session. We add ourselves into
2864 * this session's users so that it can remove us upon termination.
2865 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002866 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002867 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002868 }
2869
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002870 next_sess:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002871 s->data_ctx.sess.bref.ref = curr_sess->list.n;
2872 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002873
2874 if (s->data_ctx.sess.target) {
2875 /* specified session not found */
2876 if (s->data_ctx.sess.section > 0)
2877 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
2878 else
2879 chunk_printf(&msg, "Session not found.\n");
2880
2881 if (buffer_feed_chunk(rep, &msg) >= 0)
2882 return 0;
2883
2884 s->data_ctx.sess.target = NULL;
2885 s->data_ctx.sess.uid = 0;
2886 return 1;
2887 }
2888
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002889 s->data_state = DATA_ST_FIN;
2890 /* fall through */
2891
2892 default:
2893 s->data_state = DATA_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002894 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002895 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02002896}
2897
2898/* This function is called to send output to the response buffer.
2899 * It dumps the tables states onto the output buffer <rep>.
2900 * Expects to be called with client socket shut down on input.
2901 * s->data_ctx must have been zeroed first, and the flags properly set.
2902 * It returns 0 as long as it does not complete, non-zero upon completion.
2903 */
2904int stats_dump_table_to_buffer(struct session *s, struct buffer *rep)
2905{
2906 struct chunk msg;
2907 struct ebmb_node *eb;
2908 int dt;
2909
2910 /*
2911 * We have 3 possible states in s->data_state :
2912 * - DATA_ST_INIT : the first call
2913 * - DATA_ST_INFO : the proxy pointer points to the next table to
2914 * dump, the entry pointer is NULL ;
2915 * - DATA_ST_LIST : the proxy pointer points to the current table
2916 * and the entry pointer points to the next entry to be dumped,
2917 * and the refcount on the next entry is held ;
2918 * - DATA_ST_END : nothing left to dump, the buffer may contain some
2919 * data though.
2920 */
2921
2922 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
2923 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreauf6efda12010-08-03 20:34:06 +02002924 if (s->data_state == DATA_ST_LIST) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02002925 s->data_ctx.table.entry->ref_cnt--;
Willy Tarreauf6efda12010-08-03 20:34:06 +02002926 stksess_kill_if_expired(&s->data_ctx.table.proxy->table, s->data_ctx.table.entry);
2927 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02002928 return 1;
2929 }
2930
2931 chunk_init(&msg, trash, sizeof(trash));
2932
2933 while (s->data_state != DATA_ST_FIN) {
2934 switch (s->data_state) {
2935 case DATA_ST_INIT:
2936 s->data_ctx.table.proxy = s->data_ctx.table.target;
2937 if (!s->data_ctx.table.proxy)
2938 s->data_ctx.table.proxy = proxy;
2939
2940 s->data_ctx.table.entry = NULL;
2941 s->data_state = DATA_ST_INFO;
2942 break;
2943
2944 case DATA_ST_INFO:
2945 if (!s->data_ctx.table.proxy ||
2946 (s->data_ctx.table.target &&
2947 s->data_ctx.table.proxy != s->data_ctx.table.target)) {
2948 s->data_state = DATA_ST_END;
2949 break;
2950 }
2951
2952 if (s->data_ctx.table.proxy->table.size) {
2953 chunk_printf(&msg, "# table: %s, type: %ld, size:%d, used:%d\n",
2954 s->data_ctx.table.proxy->id,
2955 s->data_ctx.table.proxy->table.type,
2956 s->data_ctx.table.proxy->table.size,
2957 s->data_ctx.table.proxy->table.current);
2958
2959 /* any other information should be dumped here */
2960
2961 if (s->data_ctx.table.target &&
2962 s->listener->perm.ux.level < ACCESS_LVL_OPER)
2963 chunk_printf(&msg, "# contents not dumped due to insufficient privileges\n");
2964
2965 if (buffer_feed_chunk(rep, &msg) >= 0)
2966 return 0;
2967
2968 if (s->data_ctx.table.target &&
2969 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
2970 /* dump entries only if table explicitly requested */
2971 eb = ebmb_first(&s->data_ctx.table.proxy->table.keys);
2972 if (eb) {
2973 s->data_ctx.table.entry = ebmb_entry(eb, struct stksess, key);
2974 s->data_ctx.table.entry->ref_cnt++;
2975 s->data_state = DATA_ST_LIST;
2976 break;
2977 }
2978 }
2979 }
2980 s->data_ctx.table.proxy = s->data_ctx.table.proxy->next;
2981 break;
2982
2983 case DATA_ST_LIST:
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02002984 if (s->data_ctx.table.data_type >= 0) {
2985 /* we're filtering on some data contents */
2986 void *ptr;
2987 long long data;
2988
2989 dt = s->data_ctx.table.data_type;
2990 ptr = stktable_data_ptr(&s->data_ctx.table.proxy->table,
2991 s->data_ctx.table.entry,
2992 dt);
2993
2994 data = 0;
2995 switch (stktable_data_types[dt].std_type) {
2996 case STD_T_SINT:
2997 data = stktable_data_cast(ptr, std_t_sint);
2998 break;
2999 case STD_T_UINT:
3000 data = stktable_data_cast(ptr, std_t_uint);
3001 break;
3002 case STD_T_ULL:
3003 data = stktable_data_cast(ptr, std_t_ull);
3004 break;
3005 case STD_T_FRQP:
3006 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
3007 s->data_ctx.table.proxy->table.data_arg[dt].u);
3008 break;
3009 }
3010
3011 /* skip the entry if the data does not match the test and the value */
3012 if ((data < s->data_ctx.table.value &&
3013 (s->data_ctx.table.data_op == STD_OP_EQ ||
3014 s->data_ctx.table.data_op == STD_OP_GT ||
3015 s->data_ctx.table.data_op == STD_OP_GE)) ||
3016 (data == s->data_ctx.table.value &&
3017 (s->data_ctx.table.data_op == STD_OP_NE ||
3018 s->data_ctx.table.data_op == STD_OP_GT ||
3019 s->data_ctx.table.data_op == STD_OP_LT)) ||
3020 (data > s->data_ctx.table.value &&
3021 (s->data_ctx.table.data_op == STD_OP_EQ ||
3022 s->data_ctx.table.data_op == STD_OP_LT ||
3023 s->data_ctx.table.data_op == STD_OP_LE)))
3024 goto skip_entry;
3025 }
3026
Willy Tarreau69f58c82010-07-12 17:55:33 +02003027 chunk_printf(&msg, "%p:", s->data_ctx.table.entry);
3028
3029 if (s->data_ctx.table.proxy->table.type == STKTABLE_TYPE_IP) {
3030 char addr[16];
3031 inet_ntop(AF_INET,
3032 (const void *)&s->data_ctx.table.entry->key.key,
3033 addr, sizeof(addr));
3034 chunk_printf(&msg, " key=%s", addr);
3035 }
3036 else
3037 chunk_printf(&msg, " key=?");
3038
3039 chunk_printf(&msg, " use=%d exp=%d",
3040 s->data_ctx.table.entry->ref_cnt - 1,
3041 tick_remain(now_ms, s->data_ctx.table.entry->expire));
3042
3043 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
3044 void *ptr;
3045
3046 if (s->data_ctx.table.proxy->table.data_ofs[dt] == 0)
3047 continue;
3048 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
3049 chunk_printf(&msg, " %s(%d)=",
3050 stktable_data_types[dt].name,
3051 s->data_ctx.table.proxy->table.data_arg[dt].u);
3052 else
3053 chunk_printf(&msg, " %s=", stktable_data_types[dt].name);
3054
3055 ptr = stktable_data_ptr(&s->data_ctx.table.proxy->table,
3056 s->data_ctx.table.entry,
3057 dt);
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02003058 switch (stktable_data_types[dt].std_type) {
3059 case STD_T_SINT:
3060 chunk_printf(&msg, "%d", stktable_data_cast(ptr, std_t_sint));
Willy Tarreau69f58c82010-07-12 17:55:33 +02003061 break;
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02003062 case STD_T_UINT:
3063 chunk_printf(&msg, "%u", stktable_data_cast(ptr, std_t_uint));
3064 break;
3065 case STD_T_ULL:
3066 chunk_printf(&msg, "%lld", stktable_data_cast(ptr, std_t_ull));
3067 break;
3068 case STD_T_FRQP:
Willy Tarreau69f58c82010-07-12 17:55:33 +02003069 chunk_printf(&msg, "%d",
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02003070 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau69f58c82010-07-12 17:55:33 +02003071 s->data_ctx.table.proxy->table.data_arg[dt].u));
3072 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003073 }
3074 }
3075 chunk_printf(&msg, "\n");
3076
3077 if (buffer_feed_chunk(rep, &msg) >= 0)
3078 return 0;
3079
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003080 skip_entry:
Willy Tarreau69f58c82010-07-12 17:55:33 +02003081 s->data_ctx.table.entry->ref_cnt--;
3082
3083 eb = ebmb_next(&s->data_ctx.table.entry->key);
3084 if (eb) {
Willy Tarreauf6efda12010-08-03 20:34:06 +02003085 struct stksess *old = s->data_ctx.table.entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003086 s->data_ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003087 stksess_kill_if_expired(&s->data_ctx.table.proxy->table, old);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003088 s->data_ctx.table.entry->ref_cnt++;
3089 break;
3090 }
3091
Willy Tarreauf6efda12010-08-03 20:34:06 +02003092 stksess_kill_if_expired(&s->data_ctx.table.proxy->table, s->data_ctx.table.entry);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003093 s->data_ctx.table.proxy = s->data_ctx.table.proxy->next;
3094 s->data_state = DATA_ST_INFO;
3095 break;
3096
3097 case DATA_ST_END:
3098 s->data_state = DATA_ST_FIN;
3099 break;
3100 }
3101 }
3102 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003103}
3104
Willy Tarreaud426a182010-03-05 14:58:26 +01003105/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003106 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3107 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3108 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3109 * lines are respected within the limit of 70 output chars. Lines that are
3110 * continuation of a previous truncated line begin with "+" instead of " "
3111 * after the offset. The new pointer is returned.
3112 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003113static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3114 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003115{
3116 int end;
3117 unsigned char c;
3118
3119 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003120 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003121 return ptr;
3122
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003123 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003124
Willy Tarreaud426a182010-03-05 14:58:26 +01003125 while (ptr < len && ptr < bsize) {
3126 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003127 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003128 if (out->len > end - 2)
3129 break;
3130 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003131 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003132 if (out->len > end - 3)
3133 break;
3134 out->str[out->len++] = '\\';
3135 switch (c) {
3136 case '\t': c = 't'; break;
3137 case '\n': c = 'n'; break;
3138 case '\r': c = 'r'; break;
3139 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003140 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003141 }
3142 out->str[out->len++] = c;
3143 } else {
3144 if (out->len > end - 5)
3145 break;
3146 out->str[out->len++] = '\\';
3147 out->str[out->len++] = 'x';
3148 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3149 out->str[out->len++] = hextab[c & 0xF];
3150 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003151 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003152 /* we had a line break, let's return now */
3153 out->str[out->len++] = '\n';
3154 *line = ptr;
3155 return ptr;
3156 }
3157 }
3158 /* we have an incomplete line, we return it as-is */
3159 out->str[out->len++] = '\n';
3160 return ptr;
3161}
3162
3163/* This function is called to send output to the response buffer.
3164 * It dumps the errors logged in proxies onto the output buffer <rep>.
3165 * Expects to be called with client socket shut down on input.
3166 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau61b34732009-10-03 23:49:35 +02003167 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003168 */
Willy Tarreau61b34732009-10-03 23:49:35 +02003169int stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003170{
3171 extern const char *monthname[12];
3172 struct chunk msg;
3173
Willy Tarreau61b34732009-10-03 23:49:35 +02003174 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW)))
3175 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003176
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003177 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003178
3179 if (!s->data_ctx.errors.px) {
3180 /* the function had not been called yet, let's prepare the
3181 * buffer for a response.
3182 */
Willy Tarreau74808cb2009-03-04 15:53:18 +01003183 s->data_ctx.errors.px = proxy;
3184 s->data_ctx.errors.buf = 0;
3185 s->data_ctx.errors.bol = 0;
3186 s->data_ctx.errors.ptr = -1;
3187 }
3188
3189 /* we have two inner loops here, one for the proxy, the other one for
3190 * the buffer.
3191 */
3192 while (s->data_ctx.errors.px) {
3193 struct error_snapshot *es;
3194
3195 if (s->data_ctx.errors.buf == 0)
3196 es = &s->data_ctx.errors.px->invalid_req;
3197 else
3198 es = &s->data_ctx.errors.px->invalid_rep;
3199
3200 if (!es->when.tv_sec)
3201 goto next;
3202
3203 if (s->data_ctx.errors.iid >= 0 &&
3204 s->data_ctx.errors.px->uuid != s->data_ctx.errors.iid &&
3205 es->oe->uuid != s->data_ctx.errors.iid)
3206 goto next;
3207
3208 if (s->data_ctx.errors.ptr < 0) {
3209 /* just print headers now */
3210
3211 char pn[INET6_ADDRSTRLEN];
3212 struct tm tm;
3213
3214 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003215 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003216 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003217 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003218
3219
3220 if (es->src.ss_family == AF_INET)
3221 inet_ntop(AF_INET,
3222 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
3223 pn, sizeof(pn));
3224 else
3225 inet_ntop(AF_INET6,
3226 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
3227 pn, sizeof(pn));
3228
3229 switch (s->data_ctx.errors.buf) {
3230 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003231 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003232 " frontend %s (#%d): invalid request\n"
3233 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003234 " request length %d bytes, error at position %d:\n \n",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003235 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
Willy Tarreau3fd6cec2010-03-25 06:45:07 +01003236 pn, es->sid, (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3237 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003238 es->srv ? es->srv->id : "<NONE>",
3239 es->srv ? es->srv->puid : -1,
3240 es->len, es->pos);
3241 break;
3242 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003243 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003244 " backend %s (#%d) : invalid response\n"
3245 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003246 " response length %d bytes, error at position %d:\n \n",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003247 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
3248 pn, es->sid, es->oe->id, es->oe->uuid,
3249 es->srv ? es->srv->id : "<NONE>",
3250 es->srv ? es->srv->puid : -1,
3251 es->len, es->pos);
3252 break;
3253 }
3254
Willy Tarreau61b34732009-10-03 23:49:35 +02003255 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003256 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003257 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003258 }
3259 s->data_ctx.errors.ptr = 0;
3260 s->data_ctx.errors.sid = es->sid;
3261 }
3262
3263 if (s->data_ctx.errors.sid != es->sid) {
3264 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003265 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003266 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau61b34732009-10-03 23:49:35 +02003267 if (buffer_feed_chunk(rep, &msg) >= 0)
3268 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003269 goto next;
3270 }
3271
3272 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau61b34732009-10-03 23:49:35 +02003273 while (s->data_ctx.errors.ptr < es->len && s->data_ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003274 int newptr;
3275 int newline;
3276
3277 newline = s->data_ctx.errors.bol;
Willy Tarreaud426a182010-03-05 14:58:26 +01003278 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 +01003279 if (newptr == s->data_ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02003280 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003281
Willy Tarreau61b34732009-10-03 23:49:35 +02003282 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003283 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003284 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003285 }
3286 s->data_ctx.errors.ptr = newptr;
3287 s->data_ctx.errors.bol = newline;
3288 };
3289 next:
3290 s->data_ctx.errors.bol = 0;
3291 s->data_ctx.errors.ptr = -1;
3292 s->data_ctx.errors.buf++;
3293 if (s->data_ctx.errors.buf > 1) {
3294 s->data_ctx.errors.buf = 0;
3295 s->data_ctx.errors.px = s->data_ctx.errors.px->next;
3296 }
3297 }
3298
3299 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02003300 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003301}
3302
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003303
Willy Tarreau10522fd2008-07-09 20:12:41 +02003304static struct cfg_kw_list cfg_kws = {{ },{
3305 { CFG_GLOBAL, "stats", stats_parse_global },
3306 { 0, NULL, NULL },
3307}};
3308
3309__attribute__((constructor))
3310static void __dumpstats_module_init(void)
3311{
3312 cfg_register_keywords(&cfg_kws);
3313}
3314
Willy Tarreau91861262007-10-17 17:06:05 +02003315/*
3316 * Local variables:
3317 * c-indent-level: 8
3318 * c-basic-offset: 8
3319 * End:
3320 */