blob: 13e2dd0c43855952a8cbaeb55dd28911aeeb771f [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"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020060 " help : this message\n"
61 " prompt : toggle interactive mode with prompt\n"
62 " quit : disconnect\n"
63 " show info : report information about the running process\n"
64 " show stat : report counters for each proxy and server\n"
65 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +010066 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +020067 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +020068 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +020069 " set weight : change a server's weight\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +010070 " set timeout : change a timeout setting\n"
Cyril Bontécd19e512010-01-31 22:34:03 +010071 " disable server : set a server in maintenance mode\n"
72 " enable server : re-enable a server that was previously in maintenance mode\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020073 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020074
Willy Tarreau6162db22009-10-10 17:13:00 +020075const char stats_permission_denied_msg[] =
76 "Permission denied\n"
77 "";
78
Willy Tarreaudecd14d2010-06-01 18:03:19 +020079/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +020080 * a new stats socket. It returns a positive value upon success, 0 if the connection
81 * needs to be closed and ignored, or a negative value upon critical failure.
82 */
Willy Tarreaudecd14d2010-06-01 18:03:19 +020083int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +020084{
Willy Tarreaudecd14d2010-06-01 18:03:19 +020085 /* we have a dedicated I/O handler for the stats */
Willy Tarreaueb472682010-05-28 18:46:57 +020086 stream_int_register_handler(&s->si[1], stats_io_handler);
87 s->si[1].private = s;
88 s->si[1].st1 = 0;
89 s->si[1].st0 = STAT_CLI_INIT;
90
Willy Tarreaudecd14d2010-06-01 18:03:19 +020091 tv_zero(&s->logs.tv_request);
92 s->logs.t_queue = 0;
93 s->logs.t_connect = 0;
94 s->logs.t_data = 0;
95 s->logs.t_close = 0;
96 s->logs.bytes_in = s->logs.bytes_out = 0;
97 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
98 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +020099
100 s->data_state = DATA_ST_INIT;
101 s->data_source = DATA_SRC_NONE;
Willy Tarreaueb472682010-05-28 18:46:57 +0200102
Willy Tarreaueb472682010-05-28 18:46:57 +0200103 s->req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
104
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200105 if (s->listener->timeout) {
106 s->req->rto = *s->listener->timeout;
107 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200108 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200109 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200110}
111
Willy Tarreaufbee7132007-10-18 13:53:22 +0200112/* This function parses a "stats" statement in the "global" section. It returns
113 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
114 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
115 * zero included. The trailing '\n' must not be written. The function must be
116 * called with <args> pointing to the first word after "stats".
117 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200118static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
119 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200120{
Willy Tarreau10522fd2008-07-09 20:12:41 +0200121 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200122 if (!strcmp(args[0], "socket")) {
123 struct sockaddr_un su;
124 int cur_arg;
125
126 if (*args[1] == 0) {
127 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
128 return -1;
129 }
130
131 if (global.stats_sock.state != LI_NEW) {
132 snprintf(err, errlen, "'stats socket' already specified in global section");
133 return -1;
134 }
135
136 su.sun_family = AF_UNIX;
137 strncpy(su.sun_path, args[1], sizeof(su.sun_path));
138 su.sun_path[sizeof(su.sun_path) - 1] = 0;
139 memcpy(&global.stats_sock.addr, &su, sizeof(su)); // guaranteed to fit
140
Willy Tarreau89a63132009-08-16 17:41:45 +0200141 if (!global.stats_fe) {
142 if ((global.stats_fe = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) {
143 snprintf(err, errlen, "out of memory");
144 return -1;
145 }
146
147 LIST_INIT(&global.stats_fe->pendconns);
148 LIST_INIT(&global.stats_fe->acl);
149 LIST_INIT(&global.stats_fe->block_cond);
150 LIST_INIT(&global.stats_fe->redirect_rules);
151 LIST_INIT(&global.stats_fe->mon_fail_cond);
152 LIST_INIT(&global.stats_fe->switching_rules);
153 LIST_INIT(&global.stats_fe->tcp_req.inspect_rules);
154
155 /* Timeouts are defined as -1, so we cannot use the zeroed area
156 * as a default value.
157 */
158 proxy_reset_timeouts(global.stats_fe);
159
160 global.stats_fe->last_change = now.tv_sec;
161 global.stats_fe->id = strdup("GLOBAL");
162 global.stats_fe->cap = PR_CAP_FE;
Willy Tarreaueb472682010-05-28 18:46:57 +0200163 global.stats_fe->maxconn = global.stats_sock.maxconn;
Willy Tarreau89a63132009-08-16 17:41:45 +0200164 }
165
Willy Tarreaufbee7132007-10-18 13:53:22 +0200166 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +0100167 global.stats_sock.options = LI_O_NONE;
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200168 global.stats_sock.accept = session_accept;
169 global.stats_fe->accept = stats_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200170 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200171 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200172 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreaueb472682010-05-28 18:46:57 +0200173 global.stats_sock.frontend = global.stats_fe;
Willy Tarreau6162db22009-10-10 17:13:00 +0200174 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau89a63132009-08-16 17:41:45 +0200175
176 global.stats_fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
177 global.stats_sock.timeout = &global.stats_fe->timeout.client;
178
179 global.stats_sock.next = global.stats_fe->listen;
180 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200181
182 cur_arg = 2;
183 while (*args[cur_arg]) {
184 if (!strcmp(args[cur_arg], "uid")) {
185 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
186 cur_arg += 2;
187 }
188 else if (!strcmp(args[cur_arg], "gid")) {
189 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
190 cur_arg += 2;
191 }
192 else if (!strcmp(args[cur_arg], "mode")) {
193 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
194 cur_arg += 2;
195 }
196 else if (!strcmp(args[cur_arg], "user")) {
197 struct passwd *user;
198 user = getpwnam(args[cur_arg + 1]);
199 if (!user) {
200 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
201 args[cur_arg + 1]);
202 return -1;
203 }
204 global.stats_sock.perm.ux.uid = user->pw_uid;
205 cur_arg += 2;
206 }
207 else if (!strcmp(args[cur_arg], "group")) {
208 struct group *group;
209 group = getgrnam(args[cur_arg + 1]);
210 if (!group) {
211 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
212 args[cur_arg + 1]);
213 return -1;
214 }
215 global.stats_sock.perm.ux.gid = group->gr_gid;
216 cur_arg += 2;
217 }
Willy Tarreau6162db22009-10-10 17:13:00 +0200218 else if (!strcmp(args[cur_arg], "level")) {
219 if (!strcmp(args[cur_arg+1], "user"))
220 global.stats_sock.perm.ux.level = ACCESS_LVL_USER;
221 else if (!strcmp(args[cur_arg+1], "operator"))
222 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER;
223 else if (!strcmp(args[cur_arg+1], "admin"))
224 global.stats_sock.perm.ux.level = ACCESS_LVL_ADMIN;
225 else {
226 snprintf(err, errlen, "'stats socket level' only supports 'user', 'operator', and 'admin'");
227 return -1;
228 }
229 cur_arg += 2;
230 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200231 else {
Willy Tarreau6162db22009-10-10 17:13:00 +0200232 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', 'level', and 'mode'");
Willy Tarreaufbee7132007-10-18 13:53:22 +0200233 return -1;
234 }
235 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100236
Willy Tarreaufbee7132007-10-18 13:53:22 +0200237 uxst_add_listener(&global.stats_sock);
238 global.maxsock++;
239 }
240 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100241 unsigned timeout;
242 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
243
244 if (res) {
245 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
246 return -1;
247 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200248
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100249 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200250 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
251 return -1;
252 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200253 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200254 }
255 else if (!strcmp(args[0], "maxconn")) {
256 int maxconn = atol(args[1]);
257
258 if (maxconn <= 0) {
259 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
260 return -1;
261 }
262 global.maxsock -= global.stats_sock.maxconn;
263 global.stats_sock.maxconn = maxconn;
264 global.maxsock += global.stats_sock.maxconn;
Willy Tarreaueb472682010-05-28 18:46:57 +0200265 if (global.stats_fe)
266 global.stats_fe->maxconn = global.stats_sock.maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200267 }
268 else {
269 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
270 return -1;
271 }
272 return 0;
273}
274
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200275int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100276{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200277 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100278 "# pxname,svname,"
279 "qcur,qmax,"
280 "scur,smax,slim,stot,"
281 "bin,bout,"
282 "dreq,dresp,"
283 "ereq,econ,eresp,"
284 "wretr,wredis,"
285 "status,weight,act,bck,"
286 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200287 "pid,iid,sid,throttle,lbtot,tracked,type,"
288 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200289 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100290 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100291 "req_rate,req_rate_max,req_tot,"
292 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100293 "\n");
294}
295
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200296/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200297 * called from an applet running in a stream interface. The function returns 1
298 * if the request was understood, otherwise zero. It sets si->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200299 * designating the function which will have to process the request, which can
300 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200301 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200302int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200303{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200304 struct session *s = si->private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200305 char *args[MAX_STATS_ARGS + 1];
306 int arg;
307
308 while (isspace((unsigned char)*line))
309 line++;
310
311 arg = 0;
312 args[arg] = line;
313
314 while (*line && arg < MAX_STATS_ARGS) {
315 if (isspace((unsigned char)*line)) {
316 *line++ = '\0';
317
318 while (isspace((unsigned char)*line))
319 line++;
320
321 args[++arg] = line;
322 continue;
323 }
324
325 line++;
326 }
327
328 while (++arg <= MAX_STATS_ARGS)
329 args[arg] = line;
330
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200331 s->data_ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200332 if (strcmp(args[0], "show") == 0) {
333 if (strcmp(args[1], "stat") == 0) {
334 if (*args[2] && *args[3] && *args[4]) {
335 s->data_ctx.stats.flags |= STAT_BOUND;
336 s->data_ctx.stats.iid = atoi(args[2]);
337 s->data_ctx.stats.type = atoi(args[3]);
338 s->data_ctx.stats.sid = atoi(args[4]);
339 }
340
341 s->data_ctx.stats.flags |= STAT_SHOW_STAT;
342 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200343 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200344 si->st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200345 }
346 else if (strcmp(args[1], "info") == 0) {
347 s->data_ctx.stats.flags |= STAT_SHOW_INFO;
348 s->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200349 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200350 si->st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200351 }
352 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200353 s->data_state = DATA_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200354 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200355 s->data_ctx.cli.msg = stats_permission_denied_msg;
356 si->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200357 return 1;
358 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100359 if (*args[2])
360 s->data_ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
361 else
362 s->data_ctx.sess.target = NULL;
363 s->data_ctx.sess.section = 0; /* start with session status */
364 s->data_ctx.sess.pos = 0;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200365 si->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200366 }
367 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200368 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200369 s->data_ctx.cli.msg = stats_permission_denied_msg;
370 si->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200371 return 1;
372 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200373 if (*args[2])
374 s->data_ctx.errors.iid = atoi(args[2]);
375 else
376 s->data_ctx.errors.iid = -1;
377 s->data_ctx.errors.px = NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200378 s->data_state = DATA_ST_INIT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200379 si->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200380 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200381 else if (strcmp(args[1], "table") == 0) {
382 s->data_state = DATA_ST_INIT;
383 if (*args[2])
384 s->data_ctx.table.target = findproxy(args[2], 0);
385 else
386 s->data_ctx.table.target = NULL;
387 s->data_ctx.table.proxy = NULL;
388 s->data_ctx.table.entry = NULL;
389 si->st0 = STAT_CLI_O_TAB; // stats_dump_table_to_buffer
390 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200391 else { /* neither "stat" nor "info" nor "sess" nor "errors"*/
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200392 return 0;
393 }
394 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200395 else if (strcmp(args[0], "clear") == 0) {
396 if (strcmp(args[1], "counters") == 0) {
397 struct proxy *px;
398 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200399 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200400 int clrall = 0;
401
402 if (strcmp(args[2], "all") == 0)
403 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200404
Willy Tarreau6162db22009-10-10 17:13:00 +0200405 /* check permissions */
406 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
407 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200408 s->data_ctx.cli.msg = stats_permission_denied_msg;
409 si->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200410 return 1;
411 }
412
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200413 for (px = proxy; px; px = px->next) {
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200414 if (clrall)
415 memset(&px->counters, 0, sizeof(px->counters));
416 else {
417 px->counters.feconn_max = 0;
418 px->counters.beconn_max = 0;
Willy Tarreaud9b587f2010-02-26 10:05:55 +0100419 px->counters.fe_rps_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200420 px->counters.fe_sps_max = 0;
Willy Tarreaub36b4242010-06-04 20:59:39 +0200421 px->counters.fe_cps_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200422 px->counters.be_sps_max = 0;
423 px->counters.nbpend_max = 0;
424 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200425
426 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200427 if (clrall)
428 memset(&sv->counters, 0, sizeof(sv->counters));
429 else {
430 sv->counters.cur_sess_max = 0;
431 sv->counters.nbpend_max = 0;
432 sv->counters.sps_max = 0;
433 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200434
435 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200436 if (li->counters) {
437 if (clrall)
438 memset(li->counters, 0, sizeof(*li->counters));
439 else
440 li->counters->conn_max = 0;
441 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200442 }
443
444 return 1;
445 }
446 else {
447 return 0;
448 }
449 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200450 else if (strcmp(args[0], "get") == 0) {
451 if (strcmp(args[1], "weight") == 0) {
452 struct proxy *px;
453 struct server *sv;
454
455 /* split "backend/server" and make <line> point to server */
456 for (line = args[2]; *line; line++)
457 if (*line == '/') {
458 *line++ = '\0';
459 break;
460 }
461
462 if (!*line) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200463 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
464 si->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200465 return 1;
466 }
467
468 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200469 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
470 si->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200471 return 1;
472 }
473
474 /* return server's effective weight at the moment */
475 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
476 buffer_feed(si->ib, trash);
477 return 1;
478 }
479 else { /* not "get weight" */
480 return 0;
481 }
482 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200483 else if (strcmp(args[0], "set") == 0) {
484 if (strcmp(args[1], "weight") == 0) {
485 struct proxy *px;
486 struct server *sv;
487 int w;
488
489 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200490 s->data_ctx.cli.msg = stats_permission_denied_msg;
491 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200492 return 1;
493 }
494
495 /* split "backend/server" and make <line> point to server */
496 for (line = args[2]; *line; line++)
497 if (*line == '/') {
498 *line++ = '\0';
499 break;
500 }
501
502 if (!*line || !*args[3]) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200503 s->data_ctx.cli.msg = "Require 'backend/server' and 'weight' or 'weight%'.\n";
504 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200505 return 1;
506 }
507
508 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200509 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
510 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200511 return 1;
512 }
513
514 /* if the weight is terminated with '%', it is set relative to
515 * the initial weight, otherwise it is absolute.
516 */
517 w = atoi(args[3]);
518 if (strchr(args[3], '%') != NULL) {
519 if (w < 0 || w > 100) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200520 s->data_ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
521 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200522 return 1;
523 }
524 w = sv->iweight * w / 100;
525 }
526 else {
527 if (w < 0 || w > 256) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200528 s->data_ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
529 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200530 return 1;
531 }
532 }
533
534 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200535 s->data_ctx.cli.msg = "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
536 si->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200537 return 1;
538 }
539
540 sv->uweight = w;
541
542 if (px->lbprm.algo & BE_LB_PROP_DYN) {
543 /* we must take care of not pushing the server to full throttle during slow starts */
544 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
545 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
546 else
547 sv->eweight = BE_WEIGHT_SCALE;
548 sv->eweight *= sv->uweight;
549 } else {
550 sv->eweight = sv->uweight;
551 }
552
553 /* static LB algorithms are a bit harder to update */
554 if (px->lbprm.update_server_eweight)
555 px->lbprm.update_server_eweight(sv);
556 else if (sv->eweight)
557 px->lbprm.set_server_status_up(sv);
558 else
559 px->lbprm.set_server_status_down(sv);
560
561 return 1;
562 }
Willy Tarreau7aabd112010-01-26 10:59:06 +0100563 else if (strcmp(args[1], "timeout") == 0) {
564 if (strcmp(args[2], "cli") == 0) {
565 unsigned timeout;
566 const char *res;
567
568 if (!*args[3]) {
569 s->data_ctx.cli.msg = "Expects an integer value.\n";
570 si->st0 = STAT_CLI_PRINT;
571 return 1;
572 }
573
574 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
575 if (res || timeout < 1) {
576 s->data_ctx.cli.msg = "Invalid timeout value.\n";
577 si->st0 = STAT_CLI_PRINT;
578 return 1;
579 }
580
581 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
582 return 1;
583 }
584 else {
585 s->data_ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
586 si->st0 = STAT_CLI_PRINT;
587 return 1;
588 }
589 }
590 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +0200591 return 0;
592 }
593 }
Cyril Bontécd19e512010-01-31 22:34:03 +0100594 else if (strcmp(args[0], "enable") == 0) {
595 if (strcmp(args[1], "server") == 0) {
596 struct proxy *px;
597 struct server *sv;
598
599 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
600 s->data_ctx.cli.msg = stats_permission_denied_msg;
601 si->st0 = STAT_CLI_PRINT;
602 return 1;
603 }
604
605 /* split "backend/server" and make <line> point to server */
606 for (line = args[2]; *line; line++)
607 if (*line == '/') {
608 *line++ = '\0';
609 break;
610 }
611
612 if (!*line || !*args[2]) {
613 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
614 si->st0 = STAT_CLI_PRINT;
615 return 1;
616 }
617
618 if (!get_backend_server(args[2], line, &px, &sv)) {
619 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
620 si->st0 = STAT_CLI_PRINT;
621 return 1;
622 }
623
624 if (sv->state & SRV_MAINTAIN) {
625 /* The server is really in maintenance, we can change the server state */
626 if (sv->tracked) {
627 /* If this server tracks the status of another one,
628 * we must restore the good status.
629 */
630 if (sv->tracked->state & SRV_RUNNING) {
631 set_server_up(sv);
632 } else {
633 sv->state &= ~SRV_MAINTAIN;
634 set_server_down(sv);
635 }
636 } else {
637 set_server_up(sv);
638 }
639 }
640
641 return 1;
642 }
643 else { /* unknown "enable" parameter */
644 return 0;
645 }
646 }
647 else if (strcmp(args[0], "disable") == 0) {
648 if (strcmp(args[1], "server") == 0) {
649 struct proxy *px;
650 struct server *sv;
651
652 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
653 s->data_ctx.cli.msg = stats_permission_denied_msg;
654 si->st0 = STAT_CLI_PRINT;
655 return 1;
656 }
657
658 /* split "backend/server" and make <line> point to server */
659 for (line = args[2]; *line; line++)
660 if (*line == '/') {
661 *line++ = '\0';
662 break;
663 }
664
665 if (!*line || !*args[2]) {
666 s->data_ctx.cli.msg = "Require 'backend/server'.\n";
667 si->st0 = STAT_CLI_PRINT;
668 return 1;
669 }
670
671 if (!get_backend_server(args[2], line, &px, &sv)) {
672 s->data_ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
673 si->st0 = STAT_CLI_PRINT;
674 return 1;
675 }
676
677 if (! (sv->state & SRV_MAINTAIN)) {
678 /* Not already in maintenance, we can change the server state */
679 sv->state |= SRV_MAINTAIN;
680 set_server_down(sv);
681 }
682
683 return 1;
684 }
685 else { /* unknown "disable" parameter */
686 return 0;
687 }
688 }
689 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200690 return 0;
691 }
692 return 1;
693}
694
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200695/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200696 * used to processes I/O from/to the stats unix socket. The system relies on a
697 * state machine handling requests and various responses. We read a request,
698 * then we process it and send the response, and we possibly display a prompt.
699 * Then we can read again. The state is stored in si->st0 and is one of the
700 * STAT_CLI_* constants. si->st1 is used to indicate whether prompt is enabled
701 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200702 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200703void stats_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200704{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200705 struct session *s = si->private;
706 struct buffer *req = si->ob;
707 struct buffer *res = si->ib;
708 int reql;
709 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200710
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200711 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
712 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200713
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200714 while (1) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200715 if (si->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200716 /* Stats output not initialized yet */
717 memset(&s->data_ctx.stats, 0, sizeof(s->data_ctx.stats));
718 s->data_source = DATA_SRC_STATS;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200719 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200720 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200721 else if (si->st0 == STAT_CLI_END) {
722 /* Let's close for real now. We just close the request
723 * side, the conditions below will complete if needed.
724 */
725 si->shutw(si);
726 break;
727 }
728 else if (si->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +0200729 /* ensure we have some output room left in the event we
730 * would want to return some info right after parsing.
731 */
732 if (buffer_almost_full(si->ib))
733 break;
734
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200735 reql = buffer_si_peekline(si->ob, trash, sizeof(trash));
736 if (reql <= 0) { /* closed or EOL not found */
737 if (reql == 0)
738 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200739 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200740 continue;
741 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200742
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200743 /* seek for a possible semi-colon. If we find one, we
744 * replace it with an LF and skip only this part.
745 */
746 for (len = 0; len < reql; len++)
747 if (trash[len] == ';') {
748 trash[len] = '\n';
749 reql = len + 1;
750 break;
751 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200752
Willy Tarreau816fc222009-10-04 07:36:58 +0200753 /* now it is time to check that we have a full line,
754 * remove the trailing \n and possibly \r, then cut the
755 * line.
756 */
757 len = reql - 1;
758 if (trash[len] != '\n') {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200759 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200760 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200761 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200762
Willy Tarreau816fc222009-10-04 07:36:58 +0200763 if (len && trash[len-1] == '\r')
764 len--;
765
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200766 trash[len] = '\0';
767
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200768 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200769 if (len) {
770 if (strcmp(trash, "quit") == 0) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200771 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200772 continue;
773 }
774 else if (strcmp(trash, "prompt") == 0)
775 si->st1 = !si->st1;
776 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200777 !stats_sock_parse_request(si, trash)) {
778 s->data_ctx.cli.msg = stats_sock_usage_msg;
779 si->st0 = STAT_CLI_PRINT;
780 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200781 /* NB: stats_sock_parse_request() may have put
782 * another STAT_CLI_O_* into si->st0.
783 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200784 }
785 else if (!si->st1) {
786 /* if prompt is disabled, print help on empty lines,
787 * so that the user at least knows how to enable
788 * prompt and find help.
789 */
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200790 s->data_ctx.cli.msg = stats_sock_usage_msg;
791 si->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200792 }
793
794 /* re-adjust req buffer */
795 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200796 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200797 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200798 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200799 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200800 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200801 continue;
802 }
803
804 switch (si->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200805 case STAT_CLI_PRINT:
806 if (buffer_feed(si->ib, s->data_ctx.cli.msg) < 0)
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200807 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200808 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200809 case STAT_CLI_O_INFO:
Willy Tarreau24955a12009-10-04 11:54:04 +0200810 if (stats_dump_raw_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200811 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200812 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200813 case STAT_CLI_O_SESS:
Willy Tarreau7e72a8f2009-10-03 23:55:14 +0200814 if (stats_dump_sess_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200815 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200816 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200817 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau61b34732009-10-03 23:49:35 +0200818 if (stats_dump_errors_to_buffer(s, res))
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200819 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200820 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +0200821 case STAT_CLI_O_TAB:
822 if (stats_dump_table_to_buffer(s, res))
823 si->st0 = STAT_CLI_PROMPT;
824 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200825 default: /* abnormal state */
826 si->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200827 break;
828 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200829
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200830 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
831 if (si->st0 == STAT_CLI_PROMPT) {
Willy Tarreau9bcc91e2009-10-10 18:01:44 +0200832 if (buffer_feed(si->ib, si->st1 ? "\n> " : "\n") < 0)
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200833 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200834 }
835
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200836 /* If the output functions are still there, it means they require more room. */
Willy Tarreau96fd4b52009-10-04 17:18:35 +0200837 if (si->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200838 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200839
840 /* Now we close the output if one of the writers did so,
841 * or if we're not in interactive mode and the request
842 * buffer is empty. This still allows pipelined requests
843 * to be sent in non-interactive mode.
844 */
845 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->st1 && !req->send_max)) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200846 si->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200847 continue;
848 }
849
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200850 /* switch state back to GETREQ to read next requests */
851 si->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200852 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200853 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200854
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200855 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200856 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
857 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
858 /* Other size has closed, let's abort if we have no more processing to do
859 * and nothing more to consume. This is comparable to a broken pipe, so
860 * we forward the close to the request side so that it flows upstream to
861 * the client.
862 */
863 si->shutw(si);
864 }
865
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200866 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200867 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
868 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
869 /* We have no more processing to do, and nothing more to send, and
870 * the client side has closed. So we'll forward this state downstream
871 * on the response buffer.
872 */
873 si->shutr(si);
874 res->flags |= BF_READ_NULL;
875 }
876
877 /* update all other flags and resync with the other side */
878 si->update(si);
879
880 /* we don't want to expire timeouts while we're processing requests */
881 si->ib->rex = TICK_ETERNITY;
882 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200883
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200884 out:
885 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
886 __FUNCTION__, __LINE__,
887 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
888
889 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
890 /* check that we have released everything then unregister */
891 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200892 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200893}
894
Willy Tarreau24955a12009-10-04 11:54:04 +0200895/* This function is called to send output to the response buffer.
896 * It dumps statistics onto the output buffer <rep> owned by session <s>.
897 * s->data_ctx must have been zeroed first, and the flags properly set.
898 * It returns 0 as long as it does not complete, non-zero upon completion.
899 * Some states are not used but it makes the code more similar to other
900 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200901 */
Willy Tarreau24955a12009-10-04 11:54:04 +0200902int stats_dump_raw_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3e76e722007-10-17 18:57:38 +0200903{
Willy Tarreau3e76e722007-10-17 18:57:38 +0200904 struct proxy *px;
905 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +0100906 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200907
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200908 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +0200909
910 switch (s->data_state) {
911 case DATA_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +0200912 /* the function had not been called yet */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200913 s->data_state = DATA_ST_HEAD;
914 /* fall through */
915
916 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100917 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200918 print_csv_header(&msg);
Willy Tarreau24955a12009-10-04 11:54:04 +0200919 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100920 return 0;
921 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200922
923 s->data_state = DATA_ST_INFO;
924 /* fall through */
925
926 case DATA_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +0100927 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100928 if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200929 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +0100930 "Name: " PRODUCT_NAME "\n"
931 "Version: " HAPROXY_VERSION "\n"
932 "Release_date: " HAPROXY_DATE "\n"
933 "Nbproc: %d\n"
934 "Process_num: %d\n"
935 "Pid: %d\n"
936 "Uptime: %dd %dh%02dm%02ds\n"
937 "Uptime_sec: %d\n"
938 "Memmax_MB: %d\n"
939 "Ulimit-n: %d\n"
940 "Maxsock: %d\n"
941 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100942 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100943 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100944 "PipesUsed: %d\n"
945 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100946 "Tasks: %d\n"
947 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200948 "node: %s\n"
949 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100950 "",
951 global.nbproc,
952 relative_pid,
953 pid,
954 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
955 up,
956 global.rlimit_memmax,
957 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100958 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100959 actconn, pipes_used, pipes_free,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +0200960 nb_tasks_cur, run_queue_cur,
961 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +0100962 );
Willy Tarreau24955a12009-10-04 11:54:04 +0200963 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100964 return 0;
965 }
966
Willy Tarreau3e76e722007-10-17 18:57:38 +0200967 s->data_ctx.stats.px = proxy;
968 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100969
970 s->data_ctx.stats.sv = NULL;
971 s->data_ctx.stats.sv_st = 0;
972
Willy Tarreau3e76e722007-10-17 18:57:38 +0200973 s->data_state = DATA_ST_LIST;
974 /* fall through */
975
976 case DATA_ST_LIST:
977 /* dump proxies */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100978 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100979 while (s->data_ctx.stats.px) {
980 px = s->data_ctx.stats.px;
981 /* skip the disabled proxies and non-networked ones */
982 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +0200983 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100984 if (stats_dump_proxy(s, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100985 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +0200986 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200987
Willy Tarreaua8efd362008-01-03 10:19:15 +0100988 s->data_ctx.stats.px = px->next;
989 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
990 }
991 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200992 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200993
994 s->data_state = DATA_ST_END;
995 /* fall through */
996
997 case DATA_ST_END:
998 s->data_state = DATA_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +0100999 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001000
1001 case DATA_ST_FIN:
1002 return 1;
1003
1004 default:
1005 /* unknown state ! */
Willy Tarreau24955a12009-10-04 11:54:04 +02001006 s->data_state = DATA_ST_FIN;
1007 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001008 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001009}
1010
1011
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001012/* This I/O handler runs as an applet embedded in a stream interface. It is
1013 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
1014 * si->st0 becomes non-zero once the transfer is finished. The handler
1015 * automatically unregisters itself once transfer is complete.
1016 */
1017void http_stats_io_handler(struct stream_interface *si)
1018{
1019 struct session *s = si->private;
1020 struct buffer *req = si->ob;
1021 struct buffer *res = si->ib;
1022
1023 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1024 goto out;
1025
1026 /* check that the output is not closed */
1027 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
1028 si->st0 = 1;
1029
1030 if (!si->st0) {
1031 if (stats_dump_http(s, res, s->be->uri_auth)) {
1032 si->st0 = 1;
1033 si->shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001034 }
1035 }
1036
1037 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
1038 si->shutw(si);
1039
1040 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->st0) {
1041 si->shutr(si);
1042 res->flags |= BF_READ_NULL;
1043 }
1044
1045 /* update all other flags and resync with the other side */
1046 si->update(si);
1047
1048 /* we don't want to expire timeouts while we're processing requests */
1049 si->ib->rex = TICK_ETERNITY;
1050 si->ob->wex = TICK_ETERNITY;
1051
1052 out:
1053 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1054 /* check that we have released everything then unregister */
1055 stream_int_unregister_handler(si);
1056 }
1057}
1058
1059
Willy Tarreau3e76e722007-10-17 18:57:38 +02001060/*
1061 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +02001062 * client socket shut down on input. It stops by itself by unsetting the
Willy Tarreau72b179a2008-08-28 16:01:32 +02001063 * BF_HIJACK flag from the buffer, which it uses to keep on being called
Willy Tarreau1ae3a052008-08-16 10:56:30 +02001064 * when there is free space in the buffer, of simply by letting an empty buffer
1065 * upon return.s->data_ctx must have been zeroed before the first call, and the
1066 * flags set. It returns 0 if it had to stop writing data and an I/O is needed,
1067 * 1 if the dump is finished and the session must be closed, or -1 in case of
1068 * any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001069 */
Willy Tarreau0a464892008-12-07 18:30:00 +01001070int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001071{
Willy Tarreau91861262007-10-17 17:06:05 +02001072 struct proxy *px;
1073 struct chunk msg;
1074 unsigned int up;
1075
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001076 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001077
1078 switch (s->data_state) {
1079 case DATA_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001080 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001081 "HTTP/1.0 200 OK\r\n"
1082 "Cache-Control: no-cache\r\n"
1083 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001084 "Content-Type: %s\r\n",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001085 (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001086
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001087 if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001088 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001089 uri->refresh);
1090
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001091 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001092
1093 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001094 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001095 return 0;
1096
Willy Tarreau91861262007-10-17 17:06:05 +02001097 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1098 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1099 if (!(s->flags & SN_FINST_MASK))
1100 s->flags |= SN_FINST_R;
1101
1102 if (s->txn.meth == HTTP_METH_HEAD) {
1103 /* that's all we return in case of HEAD request */
1104 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001105 return 1;
1106 }
1107
1108 s->data_state = DATA_ST_HEAD; /* let's start producing data */
1109 /* fall through */
1110
1111 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001112 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001113 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001114 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001115 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1116 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001117 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001118 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1119 "<style type=\"text/css\"><!--\n"
1120 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001121 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001122 " font-size: 12px;"
1123 " font-weight: normal;"
1124 " color: black;"
1125 " background: white;"
1126 "}\n"
1127 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001128 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001129 "}\n"
1130 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001131 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001132 " margin-bottom: 0.5em;"
1133 "}\n"
1134 "h2 {"
1135 " font-family: helvetica, arial;"
1136 " font-size: x-large;"
1137 " font-weight: bold;"
1138 " font-style: italic;"
1139 " color: #6020a0;"
1140 " margin-top: 0em;"
1141 " margin-bottom: 0em;"
1142 "}\n"
1143 "h3 {"
1144 " font-family: helvetica, arial;"
1145 " font-size: 16px;"
1146 " font-weight: bold;"
1147 " color: #b00040;"
1148 " background: #e8e8d0;"
1149 " margin-top: 0em;"
1150 " margin-bottom: 0em;"
1151 "}\n"
1152 "li {"
1153 " margin-top: 0.25em;"
1154 " margin-right: 2em;"
1155 "}\n"
1156 ".hr {margin-top: 0.25em;"
1157 " border-color: black;"
1158 " border-bottom-style: solid;"
1159 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001160 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001161 ".total {background: #20D0D0;color: #ffff80;}\n"
1162 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001163 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001164 ".backend {background: #e8e8d0;}\n"
1165 ".active0 {background: #ff9090;}\n"
1166 ".active1 {background: #ffd020;}\n"
1167 ".active2 {background: #ffffa0;}\n"
1168 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001169 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1170 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1171 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001172 ".backup0 {background: #ff9090;}\n"
1173 ".backup1 {background: #ff80ff;}\n"
1174 ".backup2 {background: #c060ff;}\n"
1175 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001176 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1177 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1178 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001179 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001180 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001181 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001182 "a.px:link {color: #ffff40; text-decoration: none;}"
1183 "a.px:visited {color: #ffff40; text-decoration: none;}"
1184 "a.px:hover {color: #ffffff; text-decoration: none;}"
1185 "a.lfsb:link {color: #000000; text-decoration: none;}"
1186 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1187 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1188 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001189 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001190 "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"
1191 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001192 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001193 "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 +02001194 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001195 "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 +02001196 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001197 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1198 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1199 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001200 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001201 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001202 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001203 (uri->flags&ST_SHNODE) ? " on " : "",
1204 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001205 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001206 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001207 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001208 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001209 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001210 return 0;
1211
1212 s->data_state = DATA_ST_INFO;
1213 /* fall through */
1214
1215 case DATA_ST_INFO:
1216 up = (now.tv_sec - start_date.tv_sec);
1217
1218 /* WARNING! this has to fit the first packet too.
1219 * We are around 3.5 kB, add adding entries will
1220 * become tricky if we want to support 4kB buffers !
1221 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001222 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001223 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001224 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1225 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001226 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001227 "<hr width=\"100%%\" class=\"hr\">\n"
1228 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001229 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001230 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001231 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001232 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1233 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
1234 "current conns = %d; current pipes = %d/%d<br>\n"
1235 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001236 "</td><td align=\"center\" nowrap>\n"
1237 "<table class=\"lgd\"><tr>\n"
1238 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1239 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1240 "</tr><tr>\n"
1241 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1242 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1243 "</tr><tr>\n"
1244 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1245 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1246 "</tr><tr>\n"
1247 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001248 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001249 "</tr><tr>\n"
1250 "<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 +02001251 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001252 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001253 "</td>"
1254 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1255 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1256 "",
1257 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001258 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1259 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1260 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001261 up / 86400, (up % 86400) / 3600,
1262 (up % 3600) / 60, (up % 60),
1263 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1264 global.rlimit_memmax ? " MB" : "",
1265 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001266 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001267 actconn, pipes_used, pipes_used+pipes_free,
1268 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +02001269 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001270
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001271 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001272 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001273 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1274 uri->uri_prefix,
1275 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001276 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001277 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001278 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001279 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1280 uri->uri_prefix,
1281 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001282 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001283
Willy Tarreau55bb8452007-10-17 18:44:57 +02001284 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001285 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001286 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001287 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1288 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001289 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001290 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001291 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001292 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001293 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1294 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001295 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001296 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001297 }
Willy Tarreau91861262007-10-17 17:06:05 +02001298
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001299 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001300 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
1301 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001302 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
1303 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001304
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001305 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02001306 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
1307 uri->uri_prefix,
1308 (uri->refresh > 0) ? ";norefresh" : "");
1309
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001310 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001311 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001312 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1313 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
1314 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
1315 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
1316 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
1317 "</ul>"
1318 "</td>"
1319 "</tr></table>\n"
1320 ""
1321 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001322
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001323 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001324 return 0;
1325 }
Willy Tarreau91861262007-10-17 17:06:05 +02001326
Willy Tarreau91861262007-10-17 17:06:05 +02001327 s->data_ctx.stats.px = proxy;
1328 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1329 s->data_state = DATA_ST_LIST;
1330 /* fall through */
1331
1332 case DATA_ST_LIST:
1333 /* dump proxies */
1334 while (s->data_ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001335 if (buffer_almost_full(rep))
1336 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02001337 px = s->data_ctx.stats.px;
1338 /* skip the disabled proxies and non-networked ones */
1339 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001340 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001341 return 0;
1342
1343 s->data_ctx.stats.px = px->next;
1344 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
1345 }
1346 /* here, we just have reached the last proxy */
1347
1348 s->data_state = DATA_ST_END;
1349 /* fall through */
1350
1351 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001352 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001353 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001354 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001355 return 0;
1356 }
Willy Tarreau91861262007-10-17 17:06:05 +02001357
1358 s->data_state = DATA_ST_FIN;
1359 /* fall through */
1360
1361 case DATA_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02001362 return 1;
1363
1364 default:
1365 /* unknown state ! */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001366 s->data_state = DATA_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001367 return -1;
1368 }
1369}
1370
1371
1372/*
1373 * Dumps statistics for a proxy.
1374 * Returns 0 if it had to stop dumping data because of lack of buffer space,
1375 * ot non-zero if everything completed.
1376 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001377int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001378{
1379 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001380 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001381 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02001382 struct chunk msg;
1383
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001384 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001385
1386 switch (s->data_ctx.stats.px_st) {
1387 case DATA_ST_PX_INIT:
1388 /* we are on a new proxy */
1389
1390 if (uri && uri->scope) {
1391 /* we have a limited scope, we have to check the proxy name */
1392 struct stat_scope *scope;
1393 int len;
1394
1395 len = strlen(px->id);
1396 scope = uri->scope;
1397
1398 while (scope) {
1399 /* match exact proxy name */
1400 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1401 break;
1402
1403 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02001404 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02001405 break;
1406 scope = scope->next;
1407 }
1408
1409 /* proxy name not found : don't dump anything */
1410 if (scope == NULL)
1411 return 1;
1412 }
1413
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001414 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001415 (px->uuid != s->data_ctx.stats.iid))
1416 return 1;
1417
Willy Tarreau91861262007-10-17 17:06:05 +02001418 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
1419 /* fall through */
1420
1421 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001422 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001423 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001424 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001425 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001426 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001427 "<th class=\"pxname\" width=\"10%%\"");
1428
1429 if (uri->flags&ST_SHLGNDS) {
1430 /* cap, mode, id */
1431 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
1432 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1433 px->uuid);
1434
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001435 chunk_printf(&msg, "\"");
1436 }
1437
1438 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001439 ">%s<a name=\"%s\"></a>"
1440 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001441 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001442 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001443 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001444 "<table class=\"tbl\" width=\"100%%\">\n"
1445 "<tr class=\"titre\">"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001446 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001447 "<th colspan=3>Queue</th>"
1448 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001449 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001450 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001451 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001452 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001453 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001454 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001455 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001456 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001457 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001458 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001459 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001460 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1461 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001462 "</tr>",
Willy Tarreaue0454092010-02-26 12:29:07 +01001463 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001464 px->id, px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01001465 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001466 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001467
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001468 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001469 return 0;
1470 }
Willy Tarreau91861262007-10-17 17:06:05 +02001471
1472 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
1473 /* fall through */
1474
1475 case DATA_ST_PX_FE:
1476 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001477 if ((px->cap & PR_CAP_FE) &&
1478 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
1479 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001480 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001481 /* name, queue */
Krzysztof Piotr Oledzki516ed492009-10-22 22:48:09 +02001482 "<tr class=\"frontend\"><td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001483 "<a name=\"%s/Frontend\"></a>"
1484 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td><td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01001485 "",
1486 px->id, px->id);
1487
1488 if (px->mode == PR_MODE_HTTP) {
1489 chunk_printf(&msg,
1490 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01001491 "<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 +01001492 "",
1493 read_freq_ctr(&px->fe_req_per_sec),
1494 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
1495 px->counters.fe_rps_max,
1496 U2H2(px->counters.fe_sps_max),
1497 LIM2A2(px->fe_sps_lim, "-"));
1498 } else {
1499 chunk_printf(&msg,
1500 /* sessions rate : current, max, limit */
1501 "<td>%s</td><td>%s</td><td>%s</td>"
1502 "",
1503 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
1504 U2H1(px->counters.fe_sps_max), LIM2A2(px->fe_sps_lim, "-"));
1505 }
1506
1507 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001508 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001509 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001510 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001511 "",
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001512 U2H3(px->feconn), U2H4(px->counters.feconn_max), U2H5(px->maxconn));
1513
1514 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1515 if (px->mode == PR_MODE_HTTP) {
1516 int i;
1517
Willy Tarreaub44939a2010-02-26 11:35:39 +01001518 chunk_printf(&msg, " title=\"%lld requests:", px->counters.cum_fe_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001519
1520 for (i = 1; i < 6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01001521 chunk_printf(&msg, " %dxx=%lld,", i, px->counters.fe.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001522
Willy Tarreau24657792010-02-26 10:30:28 +01001523 chunk_printf(&msg, " other=%lld\"", px->counters.fe.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001524 }
1525
1526 chunk_printf(&msg,
1527 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01001528 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001529 /* bytes : in, out */
1530 "<td>%s</td><td>%s</td>"
1531 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001532 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreaub36b4242010-06-04 20:59:39 +02001533 U2H6(px->counters.cum_fesess),
Willy Tarreaue0454092010-02-26 12:29:07 +01001534 (px->mode == PR_MODE_HTTP)?"</u>":"",
1535 U2H7(px->counters.bytes_in), U2H8(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001536
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001537 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001538 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001539 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001540 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001541 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001542 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001543 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001544 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001545 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001546 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001547 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02001548 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001549 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
1550 U2H2(px->counters.failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +02001551 px->state == PR_STRUN ? "OPEN" :
1552 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001553 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001554 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001555 /* pxid, name, queue cur, queue max, */
1556 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001557 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001558 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001559 /* bytes : in, out */
1560 "%lld,%lld,"
1561 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001562 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001563 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001564 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001565 /* warnings: retries, redispatches */
1566 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001567 /* server status : reflect frontend status */
1568 "%s,"
1569 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001570 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001571 /* pid, iid, sid, throttle, lbtot, tracked, type */
1572 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001573 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001574 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001575 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001576 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02001577 px->id,
Willy Tarreaub36b4242010-06-04 20:59:39 +02001578 px->feconn, px->counters.feconn_max, px->maxconn, px->counters.cum_fesess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001579 px->counters.bytes_in, px->counters.bytes_out,
1580 px->counters.denied_req, px->counters.denied_resp,
1581 px->counters.failed_req,
Willy Tarreau55bb8452007-10-17 18:44:57 +02001582 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +01001583 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01001584 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001585 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001586 px->fe_sps_lim, px->counters.fe_sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001587
1588 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1589 if (px->mode == PR_MODE_HTTP) {
1590 int i;
1591
1592 for (i=1; i<6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01001593 chunk_printf(&msg, "%lld,", px->counters.fe.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001594
Willy Tarreau24657792010-02-26 10:30:28 +01001595 chunk_printf(&msg, "%lld,", px->counters.fe.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001596 } else {
1597 chunk_printf(&msg, ",,,,,,");
1598 }
1599
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001600 /* failed health analyses */
1601 chunk_printf(&msg, ",");
1602
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001603 /* requests : req_rate, req_rate_max, req_tot, */
1604 chunk_printf(&msg, "%u,%u,%lld,",
1605 read_freq_ctr(&px->fe_req_per_sec),
1606 px->counters.fe_rps_max, px->counters.cum_fe_req);
1607
Willy Tarreauae526782010-03-04 20:34:23 +01001608 /* errors: cli_aborts, srv_aborts */
1609 chunk_printf(&msg, ",,");
1610
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02001611 /* finish with EOL */
1612 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001613 }
Willy Tarreau91861262007-10-17 17:06:05 +02001614
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001615 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001616 return 0;
1617 }
1618
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001619 s->data_ctx.stats.l = px->listen; /* may be NULL */
1620 s->data_ctx.stats.px_st = DATA_ST_PX_LI;
1621 /* fall through */
1622
1623 case DATA_ST_PX_LI:
1624 /* stats.l has been initialized above */
1625 for (; s->data_ctx.stats.l != NULL; s->data_ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001626 if (buffer_almost_full(rep))
1627 return 0;
1628
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001629 l = s->data_ctx.stats.l;
1630 if (!l->counters)
1631 continue;
1632
1633 if (s->data_ctx.stats.flags & STAT_BOUND) {
1634 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SO)))
1635 break;
1636
1637 if (s->data_ctx.stats.sid != -1 && l->luid != s->data_ctx.stats.sid)
1638 continue;
1639 }
1640
1641 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001642 chunk_printf(&msg, "<tr class=socket><td class=ac");
1643
1644 if (uri->flags&ST_SHLGNDS) {
1645 char str[INET6_ADDRSTRLEN], *fmt = NULL;
1646 int port;
1647
1648 chunk_printf(&msg, " title=\"IP: ");
1649
1650 port = (l->addr.ss_family == AF_INET6)
1651 ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
1652 : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
1653
1654 if (l->addr.ss_family == AF_INET) {
1655 if (inet_ntop(AF_INET,
1656 (const void *)&((struct sockaddr_in *)&l->addr)->sin_addr,
1657 str, sizeof(str)))
1658 fmt = "%s:%d";
1659 } else {
1660 if (inet_ntop(AF_INET6,
1661 (const void *)&((struct sockaddr_in6 *)(&l->addr))->sin6_addr,
1662 str, sizeof(str)))
1663 fmt = "[%s]:%d";
1664 }
1665
1666 if (fmt)
1667 chunk_printf(&msg, fmt, str, port);
1668 else
1669 chunk_printf(&msg, "(%s)", strerror(errno));
1670
1671 /* id */
1672 chunk_printf(&msg, ", id: %d", l->luid);
1673
1674 chunk_printf(&msg, "\"");
1675 }
1676
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001677 chunk_printf(&msg,
1678 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01001679 ">%s<a name=\"%s/+%s\"></a>"
1680 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001681 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001682 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001683 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001684 "<td>%s</td><td>%s</td><td>%s</td>"
1685 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001686 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001687 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001688 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001689 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01001690 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01001691 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001692 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
1693 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
1694
1695 chunk_printf(&msg,
1696 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001697 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001698 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001699 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001700 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001701 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001702 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001703 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001704 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001705 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001706 "",
1707 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
1708 U2H2(l->counters->failed_req),
1709 (l->nbconn < l->maxconn) ? "OPEN" : "FULL");
1710 } else {
1711 chunk_printf(&msg,
1712 /* pxid, name, queue cur, queue max, */
1713 "%s,%s,,,"
1714 /* sessions: current, max, limit, total */
1715 "%d,%d,%d,%lld,"
1716 /* bytes: in, out */
1717 "%lld,%lld,"
1718 /* denied: req, resp */
1719 "%lld,%lld,"
1720 /* errors: request, connect, response */
1721 "%lld,,,"
1722 /* warnings: retries, redispatches */
1723 ",,"
1724 /* server status: reflect listener status */
1725 "%s,"
1726 /* rest of server: nothing */
1727 ",,,,,,,,"
1728 /* pid, iid, sid, throttle, lbtot, tracked, type */
1729 "%d,%d,%d,,,,%d,"
1730 /* rate, rate_lim, rate_max */
1731 ",,,"
1732 /* check_status, check_code, check_duration */
1733 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001734 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1735 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001736 /* failed health analyses */
1737 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001738 /* requests : req_rate, req_rate_max, req_tot, */
1739 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01001740 /* errors: cli_aborts, srv_aborts */
1741 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001742 "\n",
1743 px->id, l->name,
1744 l->nbconn, l->counters->conn_max,
1745 l->maxconn, l->counters->cum_conn,
1746 l->counters->bytes_in, l->counters->bytes_out,
1747 l->counters->denied_req, l->counters->denied_resp,
1748 l->counters->failed_req,
1749 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
1750 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
1751 }
1752
1753 if (buffer_feed_chunk(rep, &msg) >= 0)
1754 return 0;
1755 }
1756
Willy Tarreau91861262007-10-17 17:06:05 +02001757 s->data_ctx.stats.sv = px->srv; /* may be NULL */
1758 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
1759 /* fall through */
1760
1761 case DATA_ST_PX_SV:
1762 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001763 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001764 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 +02001765
Willy Tarreau4e33d862009-10-11 23:35:10 +02001766 if (buffer_almost_full(rep))
1767 return 0;
1768
Willy Tarreau91861262007-10-17 17:06:05 +02001769 sv = s->data_ctx.stats.sv;
1770
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001771 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001772 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
1773 break;
1774
1775 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
1776 continue;
1777 }
1778
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001779 if (sv->tracked)
1780 svs = sv->tracked;
1781 else
1782 svs = sv;
1783
Willy Tarreau91861262007-10-17 17:06:05 +02001784 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001785 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01001786 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001787 else if (svs->state & SRV_RUNNING) {
1788 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02001789 sv_state = 3; /* UP */
1790 else
1791 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01001792
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001793 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01001794 sv_state += 2;
1795 }
Willy Tarreau91861262007-10-17 17:06:05 +02001796 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001797 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02001798 sv_state = 1; /* going up */
1799 else
1800 sv_state = 0; /* DOWN */
1801
Cyril Bonté0dae5852010-02-03 00:26:28 +01001802 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02001803 /* do not report servers which are DOWN */
1804 s->data_ctx.stats.sv = sv->next;
1805 continue;
1806 }
1807
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001808 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01001809 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
1810 "UP %d/%d &darr;", "UP",
1811 "NOLB %d/%d &darr;", "NOLB",
1812 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01001813 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001814 chunk_printf(&msg,
1815 /* name */
1816 "<tr class=\"maintain\"><td class=ac"
1817 );
1818 }
1819 else {
1820 chunk_printf(&msg,
1821 /* name */
1822 "<tr class=\"%s%d\"><td class=ac",
1823 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
1824 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001825
1826 if (uri->flags&ST_SHLGNDS) {
1827 char str[INET6_ADDRSTRLEN];
1828
1829 chunk_printf(&msg, " title=\"IP: ");
1830
1831 /* IP */
1832 if (inet_ntop(sv->addr.sin_family, &sv->addr.sin_addr, str, sizeof(str)))
1833 chunk_printf(&msg, "%s:%d", str, htons(sv->addr.sin_port));
1834 else
1835 chunk_printf(&msg, "(%s)", strerror(errno));
1836
1837 /* id */
1838 chunk_printf(&msg, ", id: %d", sv->puid);
1839
1840 /* cookie */
1841 if (sv->cookie) {
1842 struct chunk src;
1843
1844 chunk_printf(&msg, ", cookie: '");
1845
1846 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
1847 chunk_htmlencode(&msg, &src);
1848
1849 chunk_printf(&msg, "'");
1850 }
1851
1852 chunk_printf(&msg, "\"");
1853 }
1854
1855 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001856 ">%s<a name=\"%s/%s\"></a>"
1857 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001858 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001859 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001860 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001861 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001862 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001863 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001864 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001865 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01001866 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01001867 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01001868 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02001869 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
1870 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001871 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
1872
1873 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1874 if (px->mode == PR_MODE_HTTP) {
1875 int i;
1876
1877 chunk_printf(&msg, " title=\"rsp codes:");
1878
1879 for (i = 1; i < 6; i++)
1880 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
1881
1882 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
1883 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001884
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001885 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001886 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01001887 ">%s%s%s</td><td>%s</td>",
1888 (px->mode == PR_MODE_HTTP)?"<u>":"",
1889 U2H0(sv->counters.cum_sess),
1890 (px->mode == PR_MODE_HTTP)?"</u>":"",
1891 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02001892
1893 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001894 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001895 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001896 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001897 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01001898 /* errors : request, connect */
1899 "<td></td><td>%s</td>"
1900 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01001901 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001902 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001903 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001904 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001905 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
1906 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01001907 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01001908 sv->counters.cli_aborts,
1909 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01001910 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001911 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001912
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001913 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001914 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001915
Cyril Bontécd19e512010-01-31 22:34:03 +01001916 if (sv->state & SRV_MAINTAIN) {
1917 chunk_printf(&msg, "%s ",
1918 human_time(now.tv_sec - sv->last_change, 1));
1919 chunk_printf(&msg, "MAINT");
1920 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01001921 else if (svs != sv && svs->state & SRV_MAINTAIN) {
1922 chunk_printf(&msg, "%s ",
1923 human_time(now.tv_sec - svs->last_change, 1));
1924 chunk_printf(&msg, "MAINT(via)");
1925 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001926 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001927 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01001928 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001929
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001930 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001931 srv_hlt_st[sv_state],
1932 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
1933 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001934 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001935
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001936 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001937 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02001938 get_check_status_description(sv->check_status));
1939
1940 if (*sv->check_desc) {
1941 struct chunk src;
1942
1943 chunk_printf(&msg, ": ");
1944
1945 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
1946 chunk_htmlencode(&msg, &src);
1947 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001948
Willy Tarreaue0454092010-02-26 12:29:07 +01001949 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001950 tv_iszero(&sv->check_start)?"":"* ",
1951 get_check_status_info(sv->check_status));
1952
1953 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001954 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02001955
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001956 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01001957 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001958 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001959 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02001960
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001961 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001962 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001963 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001964 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001965 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001966 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01001967 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02001968 (sv->state & SRV_BACKUP) ? "-" : "Y",
1969 (sv->state & SRV_BACKUP) ? "Y" : "-");
1970
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001971 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01001972 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01001973 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01001974 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
1975
1976 if (svs->observe)
1977 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
1978
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001979 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01001980 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001981 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001982 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01001983 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01001984 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001985 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01001986 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
1987 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001988 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001989 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001990 "<td colspan=3></td>");
1991
1992 /* throttle */
1993 if ((sv->state & SRV_WARMINGUP) &&
1994 now.tv_sec < sv->last_change + sv->slowstart &&
1995 now.tv_sec >= sv->last_change) {
1996 unsigned int ratio;
1997 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001998 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001999 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002000 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002001 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002002 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002003 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002004 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002005 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2006 "UP %d/%d,", "UP,",
2007 "NOLB %d/%d,", "NOLB,",
2008 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002009 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002010 /* pxid, name */
2011 "%s,%s,"
2012 /* queue : current, max */
2013 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002014 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002015 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002016 /* bytes : in, out */
2017 "%lld,%lld,"
2018 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002019 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002020 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002021 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002022 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002023 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002024 "",
2025 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002026 sv->nbpend, sv->counters.nbpend_max,
2027 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002028 sv->counters.bytes_in, sv->counters.bytes_out,
2029 sv->counters.failed_secu,
2030 sv->counters.failed_conns, sv->counters.failed_resp,
2031 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002032
Willy Tarreau55bb8452007-10-17 18:44:57 +02002033 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002034 if (sv->state & SRV_MAINTAIN) {
2035 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002036 }
2037 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2038 chunk_printf(&msg, "MAINT(via),");
2039 }
2040 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002041 chunk_printf(&msg,
2042 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));
2045 }
Willy Tarreau91861262007-10-17 17:06:05 +02002046
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002047 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002048 /* weight, active, backup */
2049 "%d,%d,%d,"
2050 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002051 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002052 (sv->state & SRV_BACKUP) ? 0 : 1,
2053 (sv->state & SRV_BACKUP) ? 1 : 0);
2054
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002055 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002056 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002057 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002058 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002059 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002060 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002061 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002062 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002063 ",,,,");
2064
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002065 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002066 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002067 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002068 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002069 LIM2A0(sv->maxqueue, ""),
2070 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002071
2072 /* throttle */
2073 if ((sv->state & SRV_WARMINGUP) &&
2074 now.tv_sec < sv->last_change + sv->slowstart &&
2075 now.tv_sec >= sv->last_change) {
2076 unsigned int ratio;
2077 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002078 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002079 }
2080
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002081 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002082 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002083
2084 /* tracked */
2085 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002086 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002087 sv->tracked->proxy->id, sv->tracked->id);
2088 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002089 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002090
Willy Tarreau7f062c42009-03-05 18:43:00 +01002091 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002092 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002093
2094 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002095 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002096 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002097 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002098
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002099 if (sv->state & SRV_CHECKED) {
2100 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002101 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002102
2103 /* check_code */
2104 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002105 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002106 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002107 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002108
2109 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002110 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002111 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002112 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002113 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002114
2115 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002116 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002117 }
2118
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002119 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2120 if (px->mode == PR_MODE_HTTP) {
2121 int i;
2122
2123 for (i=1; i<6; i++)
2124 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2125
2126 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2127 } else {
2128 chunk_printf(&msg, ",,,,,,");
2129 }
2130
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002131 /* failed health analyses */
2132 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2133
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002134 /* requests : req_rate, req_rate_max, req_tot, */
2135 chunk_printf(&msg, ",,,");
2136
Willy Tarreauae526782010-03-04 20:34:23 +01002137 /* errors: cli_aborts, srv_aborts */
2138 chunk_printf(&msg, "%lld,%lld,",
2139 sv->counters.cli_aborts, sv->counters.srv_aborts);
2140
Willy Tarreau7f062c42009-03-05 18:43:00 +01002141 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002142 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002143 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002144 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002145 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002146 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002147
2148 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
2149 /* fall through */
2150
2151 case DATA_ST_PX_BE:
2152 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002153 if ((px->cap & PR_CAP_BE) &&
2154 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2155 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002156 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002157 /* name */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002158 "<tr class=\"backend\"><td class=ac");
2159
2160 if (uri->flags&ST_SHLGNDS) {
2161 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002162 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002163 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002164
2165 /* cookie */
2166 if (px->cookie_name) {
2167 struct chunk src;
2168
2169 chunk_printf(&msg, ", cookie: '");
2170
2171 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2172 chunk_htmlencode(&msg, &src);
2173
2174 chunk_printf(&msg, "'");
2175 }
2176
2177 chunk_printf(&msg, "\"");
2178
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002179 }
2180
2181 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002182 ">%s<a name=\"%s/Backend\"></a>"
2183 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002184 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002185 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002186 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002187 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002188 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002189 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002190 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002191 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002192 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->counters.nbpend_max),
2193 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->counters.be_sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02002194
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002195 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002196 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002197 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002198 "<td"
2199 "",
2200 U2H2(px->beconn), U2H3(px->counters.beconn_max), U2H4(px->fullconn));
2201
2202 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2203 if (px->mode == PR_MODE_HTTP) {
2204 int i;
2205
2206 chunk_printf(&msg, " title=\"rsp codes:");
2207
2208 for (i = 1; i < 6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01002209 chunk_printf(&msg, " %dxx=%lld", i, px->counters.be.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002210
Willy Tarreau24657792010-02-26 10:30:28 +01002211 chunk_printf(&msg, " other=%lld\"", px->counters.be.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002212 }
2213
2214 chunk_printf(&msg,
2215 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002216 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002217 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002218 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002219 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002220 (px->mode == PR_MODE_HTTP)?"<u>":"",
2221 U2H6(px->counters.cum_beconn),
2222 (px->mode == PR_MODE_HTTP)?"</u>":"",
2223 U2H7(px->counters.cum_lbconn),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002224 U2H8(px->counters.bytes_in), U2H9(px->counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002225
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002226 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002227 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002228 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002229 /* errors : request, connect */
2230 "<td></td><td>%s</td>"
2231 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002232 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002233 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002234 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002235 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02002236 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002237 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02002238 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002239 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2240 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002241 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002242 U2H0(px->counters.denied_req), U2H1(px->counters.denied_resp),
Willy Tarreauae526782010-03-04 20:34:23 +01002243 U2H2(px->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002244 px->counters.cli_aborts,
2245 px->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002246 U2H5(px->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002247 px->counters.retries, px->counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002248 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01002249 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2250 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01002251 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002252 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002253
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002254 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002255 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002256 "<td class=ac>&nbsp;</td><td>%d</td>"
2257 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002258 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002259 "</tr>",
2260 px->down_trans,
2261 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002262 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002263 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002264 /* pxid, name */
2265 "%s,BACKEND,"
2266 /* queue : current, max */
2267 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002268 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002269 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002270 /* bytes : in, out */
2271 "%lld,%lld,"
2272 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002273 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002274 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002275 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002276 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002277 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002278 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02002279 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002280 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02002281 * active and backups. */
2282 "%s,"
2283 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002284 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002285 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002286 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002287 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002288 /* rate, rate_lim, rate_max, */
2289 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002290 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002291 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002292 px->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002293 px->nbpend /* or px->totpend ? */, px->counters.nbpend_max,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002294 px->beconn, px->counters.beconn_max, px->fullconn, px->counters.cum_beconn,
2295 px->counters.bytes_in, px->counters.bytes_out,
2296 px->counters.denied_req, px->counters.denied_resp,
2297 px->counters.failed_conns, px->counters.failed_resp,
2298 px->counters.retries, px->counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01002299 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01002300 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01002301 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002302 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01002303 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002304 relative_pid, px->uuid,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002305 px->counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002306 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002307 px->counters.be_sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002308
2309 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2310 if (px->mode == PR_MODE_HTTP) {
2311 int i;
2312
2313 for (i=1; i<6; i++)
Willy Tarreau24657792010-02-26 10:30:28 +01002314 chunk_printf(&msg, "%lld,", px->counters.be.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002315
Willy Tarreau24657792010-02-26 10:30:28 +01002316 chunk_printf(&msg, "%lld,", px->counters.be.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002317 } else {
2318 chunk_printf(&msg, ",,,,,,");
2319 }
2320
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002321 /* failed health analyses */
2322 chunk_printf(&msg, ",");
2323
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002324 /* requests : req_rate, req_rate_max, req_tot, */
2325 chunk_printf(&msg, ",,,");
2326
Willy Tarreauae526782010-03-04 20:34:23 +01002327 /* errors: cli_aborts, srv_aborts */
2328 chunk_printf(&msg, "%lld,%lld,",
2329 px->counters.cli_aborts, px->counters.srv_aborts);
2330
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002331 /* finish with EOL */
2332 chunk_printf(&msg, "\n");
2333
Willy Tarreau55bb8452007-10-17 18:44:57 +02002334 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002335 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002336 return 0;
2337 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002338
Willy Tarreau91861262007-10-17 17:06:05 +02002339 s->data_ctx.stats.px_st = DATA_ST_PX_END;
2340 /* fall through */
2341
2342 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002343 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002344 chunk_printf(&msg, "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02002345
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002346 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002347 return 0;
2348 }
Willy Tarreau91861262007-10-17 17:06:05 +02002349
2350 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
2351 /* fall through */
2352
2353 case DATA_ST_PX_FIN:
2354 return 1;
2355
2356 default:
2357 /* unknown state, we should put an abort() here ! */
2358 return 1;
2359 }
2360}
2361
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002362/* This function is called to send output to the response buffer. It dumps a
2363 * complete session state onto the output buffer <rep>. The session has to be
2364 * set in data_ctx.sess.target. It returns 0 if the output buffer is full and
2365 * it needs to be called again, otherwise non-zero. It is designed to be called
2366 * from stats_dump_sess_to_buffer() below.
2367 */
2368
2369/* returns 1 if dump is not complete */
2370int stats_dump_full_sess_to_buffer(struct session *s, struct buffer *rep)
2371{
2372 struct tm tm;
2373 struct chunk msg;
2374 struct session *sess;
2375 extern const char *monthname[12];
2376 char pn[INET6_ADDRSTRLEN];
2377
2378 chunk_init(&msg, trash, sizeof(trash));
2379 sess = s->data_ctx.sess.target;
2380
2381 if (s->data_ctx.sess.section > 0 && s->data_ctx.sess.uid != sess->uniq_id) {
2382 /* session changed, no need to go any further */
2383 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
2384 if (buffer_feed_chunk(rep, &msg) >= 0)
2385 return 0;
2386 s->data_ctx.sess.target = NULL;
2387 s->data_ctx.sess.uid = 0;
2388 return 1;
2389 }
2390
2391 switch (s->data_ctx.sess.section) {
2392 case 0: /* main status of the session */
2393 s->data_ctx.sess.uid = sess->uniq_id;
2394 s->data_ctx.sess.section = 1;
2395 /* fall through */
2396
2397 case 1:
2398 chunk_printf(&msg,
2399 "%p: id=%u, proto=%s",
2400 sess,
2401 sess->uniq_id,
2402 sess->listener->proto->name);
2403
2404 switch (sess->listener->proto->sock_family) {
2405 case AF_INET:
2406 inet_ntop(AF_INET,
2407 (const void *)&((struct sockaddr_in *)&sess->cli_addr)->sin_addr,
2408 pn, sizeof(pn));
2409
2410 chunk_printf(&msg,
2411 " source=%s:%d\n",
2412 pn,
2413 ntohs(((struct sockaddr_in *)&sess->cli_addr)->sin_port));
2414 break;
2415 case AF_INET6:
2416 inet_ntop(AF_INET6,
2417 (const void *)&((struct sockaddr_in6 *)(&sess->cli_addr))->sin6_addr,
2418 pn, sizeof(pn));
2419
2420 chunk_printf(&msg,
2421 " source=%s:%d\n",
2422 pn,
2423 ntohs(((struct sockaddr_in6 *)&sess->cli_addr)->sin6_port));
2424 break;
2425 case AF_UNIX:
2426 default:
2427 /* no more information to print right now */
2428 chunk_printf(&msg, "\n");
2429 break;
2430 }
2431
2432 chunk_printf(&msg,
2433 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02002434 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002435
2436 chunk_printf(&msg,
2437 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)\n",
2438 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
2439 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
2440 sess->listener ? sess->listener->luid : 0);
2441
2442 chunk_printf(&msg,
2443 " backend=%s (id=%u mode=%s) server=%s (id=%u)\n",
2444 sess->be->id, sess->be->uuid, sess->be->mode ? "http" : "tcp",
2445 sess->srv ? sess->srv->id : "<none>",
2446 sess->srv ? sess->srv->puid : 0);
2447
2448 chunk_printf(&msg,
2449 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
2450 sess->task,
2451 sess->task->state,
2452 sess->task->nice, sess->task->calls,
2453 sess->task->expire ?
2454 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
2455 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
2456 TICKS_TO_MS(1000)) : "<NEVER>",
2457 task_in_rq(sess->task) ? ", running" : "");
2458
2459 get_localtime(sess->logs.accept_date.tv_sec, &tm);
2460 chunk_printf(&msg,
2461 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
2462 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2463 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
2464 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
2465
2466 chunk_printf(&msg,
2467 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
2468 &sess->si[0],
2469 sess->si[0].state,
2470 sess->si[0].flags,
2471 sess->si[0].fd,
2472 sess->si[0].exp ?
2473 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
2474 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
2475 TICKS_TO_MS(1000)) : "<NEVER>",
2476 sess->si[0].err_type);
2477
2478 chunk_printf(&msg,
2479 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
2480 &sess->si[1],
2481 sess->si[1].state,
2482 sess->si[1].flags,
2483 sess->si[1].fd,
2484 sess->si[1].exp ?
2485 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
2486 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
2487 TICKS_TO_MS(1000)) : "<NEVER>",
2488 sess->si[1].err_type);
2489
2490 chunk_printf(&msg,
2491 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
2492 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
2493 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
2494
2495
2496 chunk_printf(&msg,
2497 " req=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%ld)\n"
2498 " an_exp=%s",
2499 sess->req,
2500 sess->req->flags, sess->req->analysers,
2501 sess->req->l, sess->req->send_max,
2502 sess->req->pipe ? sess->req->pipe->data : 0,
2503 sess->req->to_forward,
2504 sess->req->analyse_exp ?
2505 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
2506 TICKS_TO_MS(1000)) : "<NEVER>");
2507
2508 chunk_printf(&msg,
2509 " rex=%s",
2510 sess->req->rex ?
2511 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
2512 TICKS_TO_MS(1000)) : "<NEVER>");
2513
2514 chunk_printf(&msg,
2515 " wex=%s\n"
2516 " data=%p r=%d w=%d lr=%d total=%lld\n",
2517 sess->req->wex ?
2518 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
2519 TICKS_TO_MS(1000)) : "<NEVER>",
2520 sess->req->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01002521 (int)(sess->req->r - sess->req->data),
2522 (int)(sess->req->w - sess->req->data),
2523 (int)(sess->req->lr - sess->req->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002524 sess->req->total);
2525
2526 chunk_printf(&msg,
2527 " res=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%ld)\n"
2528 " an_exp=%s",
2529 sess->rep,
2530 sess->rep->flags, sess->rep->analysers,
2531 sess->rep->l, sess->rep->send_max,
2532 sess->rep->pipe ? sess->rep->pipe->data : 0,
2533 sess->rep->to_forward,
2534 sess->rep->analyse_exp ?
2535 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
2536 TICKS_TO_MS(1000)) : "<NEVER>");
2537
2538 chunk_printf(&msg,
2539 " rex=%s",
2540 sess->rep->rex ?
2541 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
2542 TICKS_TO_MS(1000)) : "<NEVER>");
2543
2544 chunk_printf(&msg,
2545 " wex=%s\n"
2546 " data=%p r=%d w=%d lr=%d total=%lld\n",
2547 sess->rep->wex ?
2548 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
2549 TICKS_TO_MS(1000)) : "<NEVER>",
2550 sess->rep->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01002551 (int)(sess->rep->r - sess->rep->data),
2552 (int)(sess->rep->w - sess->rep->data),
2553 (int)(sess->rep->lr - sess->rep->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002554 sess->rep->total);
2555
2556 if (buffer_feed_chunk(rep, &msg) >= 0)
2557 return 0;
2558
2559 /* use other states to dump the contents */
2560 }
2561 /* end of dump */
2562 s->data_ctx.sess.uid = 0;
2563 return 1;
2564}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002565
2566/* This function is called to send output to the response buffer.
2567 * It dumps the sessions states onto the output buffer <rep>.
2568 * Expects to be called with client socket shut down on input.
2569 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002570 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002571 */
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002572int stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002573{
2574 struct chunk msg;
2575
2576 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
2577 /* If we're forced to shut down, we might have to remove our
2578 * reference to the last session being dumped.
2579 */
2580 if (s->data_state == DATA_ST_LIST) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002581 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002582 LIST_DEL(&s->data_ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002583 LIST_INIT(&s->data_ctx.sess.bref.users);
2584 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002585 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002586 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002587 }
2588
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002589 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002590
2591 switch (s->data_state) {
2592 case DATA_ST_INIT:
2593 /* the function had not been called yet, let's prepare the
2594 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002595 * pointer to the first in the global list. When a target
2596 * session is being destroyed, it is responsible for updating
2597 * this pointer. We know we have reached the end when this
2598 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002599 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002600 LIST_INIT(&s->data_ctx.sess.bref.users);
2601 s->data_ctx.sess.bref.ref = sessions.n;
2602 s->data_state = DATA_ST_LIST;
2603 /* fall through */
2604
2605 case DATA_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002606 /* first, let's detach the back-ref from a possible previous session */
2607 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
2608 LIST_DEL(&s->data_ctx.sess.bref.users);
2609 LIST_INIT(&s->data_ctx.sess.bref.users);
2610 }
2611
2612 /* and start from where we stopped */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002613 while (s->data_ctx.sess.bref.ref != &sessions) {
2614 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
2615 struct session *curr_sess;
2616
2617 curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list);
2618
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002619 if (s->data_ctx.sess.target) {
2620 if (s->data_ctx.sess.target != curr_sess)
2621 goto next_sess;
2622
2623 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
2624 /* call the proper dump() function and return if we're missing space */
2625 if (!stats_dump_full_sess_to_buffer(s, rep))
2626 return 0;
2627
2628 /* session dump complete */
2629 LIST_DEL(&s->data_ctx.sess.bref.users);
2630 LIST_INIT(&s->data_ctx.sess.bref.users);
2631 s->data_ctx.sess.target = NULL;
2632 break;
2633 }
2634
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002635 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002636 "%p: proto=%s",
2637 curr_sess,
2638 curr_sess->listener->proto->name);
2639
2640 switch (curr_sess->listener->proto->sock_family) {
2641 case AF_INET:
2642 inet_ntop(AF_INET,
2643 (const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr,
2644 pn, sizeof(pn));
2645
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002646 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002647 " src=%s:%d fe=%s be=%s srv=%s",
2648 pn,
2649 ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port),
2650 curr_sess->fe->id,
2651 curr_sess->be->id,
2652 curr_sess->srv ? curr_sess->srv->id : "<none>"
2653 );
2654 break;
2655 case AF_INET6:
2656 inet_ntop(AF_INET6,
2657 (const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr,
2658 pn, sizeof(pn));
2659
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002660 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002661 " src=%s:%d fe=%s be=%s srv=%s",
2662 pn,
2663 ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port),
2664 curr_sess->fe->id,
2665 curr_sess->be->id,
2666 curr_sess->srv ? curr_sess->srv->id : "<none>"
2667 );
2668
2669 break;
2670 case AF_UNIX:
2671 /* no more information to print right now */
2672 break;
2673 }
2674
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002675 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02002676 " ts=%02x age=%s calls=%d",
2677 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01002678 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
2679 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002680
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002681 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002682 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
2683 curr_sess->req->flags,
2684 curr_sess->req->l,
2685 curr_sess->req->analysers,
2686 curr_sess->req->rex ?
2687 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
2688 TICKS_TO_MS(1000)) : "");
2689
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002690 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002691 ",wx=%s",
2692 curr_sess->req->wex ?
2693 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
2694 TICKS_TO_MS(1000)) : "");
2695
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002696 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002697 ",ax=%s]",
2698 curr_sess->req->analyse_exp ?
2699 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
2700 TICKS_TO_MS(1000)) : "");
2701
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002702 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002703 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
2704 curr_sess->rep->flags,
2705 curr_sess->rep->l,
2706 curr_sess->rep->analysers,
2707 curr_sess->rep->rex ?
2708 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
2709 TICKS_TO_MS(1000)) : "");
2710
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002711 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002712 ",wx=%s",
2713 curr_sess->rep->wex ?
2714 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
2715 TICKS_TO_MS(1000)) : "");
2716
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002717 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002718 ",ax=%s]",
2719 curr_sess->rep->analyse_exp ?
2720 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
2721 TICKS_TO_MS(1000)) : "");
2722
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002723 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002724 " s0=[%d,%1xh,fd=%d,ex=%s]",
2725 curr_sess->si[0].state,
2726 curr_sess->si[0].flags,
2727 curr_sess->si[0].fd,
2728 curr_sess->si[0].exp ?
2729 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
2730 TICKS_TO_MS(1000)) : "");
2731
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002732 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002733 " s1=[%d,%1xh,fd=%d,ex=%s]",
2734 curr_sess->si[1].state,
2735 curr_sess->si[1].flags,
2736 curr_sess->si[1].fd,
2737 curr_sess->si[1].exp ?
2738 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
2739 TICKS_TO_MS(1000)) : "");
2740
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002741 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002742 " exp=%s",
2743 curr_sess->task->expire ?
2744 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
2745 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01002746 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002747 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01002748
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002749 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002750
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002751 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01002752 /* let's try again later from this session. We add ourselves into
2753 * this session's users so that it can remove us upon termination.
2754 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002755 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002756 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002757 }
2758
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002759 next_sess:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002760 s->data_ctx.sess.bref.ref = curr_sess->list.n;
2761 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01002762
2763 if (s->data_ctx.sess.target) {
2764 /* specified session not found */
2765 if (s->data_ctx.sess.section > 0)
2766 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
2767 else
2768 chunk_printf(&msg, "Session not found.\n");
2769
2770 if (buffer_feed_chunk(rep, &msg) >= 0)
2771 return 0;
2772
2773 s->data_ctx.sess.target = NULL;
2774 s->data_ctx.sess.uid = 0;
2775 return 1;
2776 }
2777
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002778 s->data_state = DATA_ST_FIN;
2779 /* fall through */
2780
2781 default:
2782 s->data_state = DATA_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02002783 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002784 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02002785}
2786
2787/* This function is called to send output to the response buffer.
2788 * It dumps the tables states onto the output buffer <rep>.
2789 * Expects to be called with client socket shut down on input.
2790 * s->data_ctx must have been zeroed first, and the flags properly set.
2791 * It returns 0 as long as it does not complete, non-zero upon completion.
2792 */
2793int stats_dump_table_to_buffer(struct session *s, struct buffer *rep)
2794{
2795 struct chunk msg;
2796 struct ebmb_node *eb;
2797 int dt;
2798
2799 /*
2800 * We have 3 possible states in s->data_state :
2801 * - DATA_ST_INIT : the first call
2802 * - DATA_ST_INFO : the proxy pointer points to the next table to
2803 * dump, the entry pointer is NULL ;
2804 * - DATA_ST_LIST : the proxy pointer points to the current table
2805 * and the entry pointer points to the next entry to be dumped,
2806 * and the refcount on the next entry is held ;
2807 * - DATA_ST_END : nothing left to dump, the buffer may contain some
2808 * data though.
2809 */
2810
2811 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
2812 /* in case of abort, remove any refcount we might have set on an entry */
2813 if (s->data_state == DATA_ST_LIST)
2814 s->data_ctx.table.entry->ref_cnt--;
2815 return 1;
2816 }
2817
2818 chunk_init(&msg, trash, sizeof(trash));
2819
2820 while (s->data_state != DATA_ST_FIN) {
2821 switch (s->data_state) {
2822 case DATA_ST_INIT:
2823 s->data_ctx.table.proxy = s->data_ctx.table.target;
2824 if (!s->data_ctx.table.proxy)
2825 s->data_ctx.table.proxy = proxy;
2826
2827 s->data_ctx.table.entry = NULL;
2828 s->data_state = DATA_ST_INFO;
2829 break;
2830
2831 case DATA_ST_INFO:
2832 if (!s->data_ctx.table.proxy ||
2833 (s->data_ctx.table.target &&
2834 s->data_ctx.table.proxy != s->data_ctx.table.target)) {
2835 s->data_state = DATA_ST_END;
2836 break;
2837 }
2838
2839 if (s->data_ctx.table.proxy->table.size) {
2840 chunk_printf(&msg, "# table: %s, type: %ld, size:%d, used:%d\n",
2841 s->data_ctx.table.proxy->id,
2842 s->data_ctx.table.proxy->table.type,
2843 s->data_ctx.table.proxy->table.size,
2844 s->data_ctx.table.proxy->table.current);
2845
2846 /* any other information should be dumped here */
2847
2848 if (s->data_ctx.table.target &&
2849 s->listener->perm.ux.level < ACCESS_LVL_OPER)
2850 chunk_printf(&msg, "# contents not dumped due to insufficient privileges\n");
2851
2852 if (buffer_feed_chunk(rep, &msg) >= 0)
2853 return 0;
2854
2855 if (s->data_ctx.table.target &&
2856 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
2857 /* dump entries only if table explicitly requested */
2858 eb = ebmb_first(&s->data_ctx.table.proxy->table.keys);
2859 if (eb) {
2860 s->data_ctx.table.entry = ebmb_entry(eb, struct stksess, key);
2861 s->data_ctx.table.entry->ref_cnt++;
2862 s->data_state = DATA_ST_LIST;
2863 break;
2864 }
2865 }
2866 }
2867 s->data_ctx.table.proxy = s->data_ctx.table.proxy->next;
2868 break;
2869
2870 case DATA_ST_LIST:
2871 chunk_printf(&msg, "%p:", s->data_ctx.table.entry);
2872
2873 if (s->data_ctx.table.proxy->table.type == STKTABLE_TYPE_IP) {
2874 char addr[16];
2875 inet_ntop(AF_INET,
2876 (const void *)&s->data_ctx.table.entry->key.key,
2877 addr, sizeof(addr));
2878 chunk_printf(&msg, " key=%s", addr);
2879 }
2880 else
2881 chunk_printf(&msg, " key=?");
2882
2883 chunk_printf(&msg, " use=%d exp=%d",
2884 s->data_ctx.table.entry->ref_cnt - 1,
2885 tick_remain(now_ms, s->data_ctx.table.entry->expire));
2886
2887 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
2888 void *ptr;
2889
2890 if (s->data_ctx.table.proxy->table.data_ofs[dt] == 0)
2891 continue;
2892 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
2893 chunk_printf(&msg, " %s(%d)=",
2894 stktable_data_types[dt].name,
2895 s->data_ctx.table.proxy->table.data_arg[dt].u);
2896 else
2897 chunk_printf(&msg, " %s=", stktable_data_types[dt].name);
2898
2899 ptr = stktable_data_ptr(&s->data_ctx.table.proxy->table,
2900 s->data_ctx.table.entry,
2901 dt);
2902 switch (dt) {
2903 /* all entries using the same type can be folded */
2904 case STKTABLE_DT_SERVER_ID:
2905 case STKTABLE_DT_GPC0:
2906 case STKTABLE_DT_CONN_CNT:
2907 case STKTABLE_DT_CONN_CUR:
2908 case STKTABLE_DT_SESS_CNT:
2909 case STKTABLE_DT_HTTP_REQ_CNT:
2910 case STKTABLE_DT_HTTP_ERR_CNT:
2911 chunk_printf(&msg, "%u", stktable_data_cast(ptr, server_id));
2912 break;
2913 case STKTABLE_DT_CONN_RATE:
2914 case STKTABLE_DT_SESS_RATE:
2915 case STKTABLE_DT_HTTP_REQ_RATE:
2916 case STKTABLE_DT_HTTP_ERR_RATE:
2917 case STKTABLE_DT_BYTES_IN_RATE:
2918 case STKTABLE_DT_BYTES_OUT_RATE:
2919 chunk_printf(&msg, "%d",
2920 read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
2921 s->data_ctx.table.proxy->table.data_arg[dt].u));
2922 break;
2923 case STKTABLE_DT_BYTES_IN_CNT:
2924 case STKTABLE_DT_BYTES_OUT_CNT:
2925 chunk_printf(&msg, "%lld", stktable_data_cast(ptr, bytes_in_cnt));
2926 break;
2927 }
2928 }
2929 chunk_printf(&msg, "\n");
2930
2931 if (buffer_feed_chunk(rep, &msg) >= 0)
2932 return 0;
2933
2934 s->data_ctx.table.entry->ref_cnt--;
2935
2936 eb = ebmb_next(&s->data_ctx.table.entry->key);
2937 if (eb) {
2938 s->data_ctx.table.entry = ebmb_entry(eb, struct stksess, key);
2939 s->data_ctx.table.entry->ref_cnt++;
2940 break;
2941 }
2942
2943 s->data_ctx.table.proxy = s->data_ctx.table.proxy->next;
2944 s->data_state = DATA_ST_INFO;
2945 break;
2946
2947 case DATA_ST_END:
2948 s->data_state = DATA_ST_FIN;
2949 break;
2950 }
2951 }
2952 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01002953}
2954
Willy Tarreaud426a182010-03-05 14:58:26 +01002955/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01002956 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
2957 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
2958 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
2959 * lines are respected within the limit of 70 output chars. Lines that are
2960 * continuation of a previous truncated line begin with "+" instead of " "
2961 * after the offset. The new pointer is returned.
2962 */
Willy Tarreaud426a182010-03-05 14:58:26 +01002963static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
2964 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01002965{
2966 int end;
2967 unsigned char c;
2968
2969 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002970 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01002971 return ptr;
2972
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002973 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01002974
Willy Tarreaud426a182010-03-05 14:58:26 +01002975 while (ptr < len && ptr < bsize) {
2976 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01002977 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002978 if (out->len > end - 2)
2979 break;
2980 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01002981 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01002982 if (out->len > end - 3)
2983 break;
2984 out->str[out->len++] = '\\';
2985 switch (c) {
2986 case '\t': c = 't'; break;
2987 case '\n': c = 'n'; break;
2988 case '\r': c = 'r'; break;
2989 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01002990 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01002991 }
2992 out->str[out->len++] = c;
2993 } else {
2994 if (out->len > end - 5)
2995 break;
2996 out->str[out->len++] = '\\';
2997 out->str[out->len++] = 'x';
2998 out->str[out->len++] = hextab[(c >> 4) & 0xF];
2999 out->str[out->len++] = hextab[c & 0xF];
3000 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003001 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003002 /* we had a line break, let's return now */
3003 out->str[out->len++] = '\n';
3004 *line = ptr;
3005 return ptr;
3006 }
3007 }
3008 /* we have an incomplete line, we return it as-is */
3009 out->str[out->len++] = '\n';
3010 return ptr;
3011}
3012
3013/* This function is called to send output to the response buffer.
3014 * It dumps the errors logged in proxies onto the output buffer <rep>.
3015 * Expects to be called with client socket shut down on input.
3016 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau61b34732009-10-03 23:49:35 +02003017 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003018 */
Willy Tarreau61b34732009-10-03 23:49:35 +02003019int stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003020{
3021 extern const char *monthname[12];
3022 struct chunk msg;
3023
Willy Tarreau61b34732009-10-03 23:49:35 +02003024 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW)))
3025 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003026
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003027 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003028
3029 if (!s->data_ctx.errors.px) {
3030 /* the function had not been called yet, let's prepare the
3031 * buffer for a response.
3032 */
Willy Tarreau74808cb2009-03-04 15:53:18 +01003033 s->data_ctx.errors.px = proxy;
3034 s->data_ctx.errors.buf = 0;
3035 s->data_ctx.errors.bol = 0;
3036 s->data_ctx.errors.ptr = -1;
3037 }
3038
3039 /* we have two inner loops here, one for the proxy, the other one for
3040 * the buffer.
3041 */
3042 while (s->data_ctx.errors.px) {
3043 struct error_snapshot *es;
3044
3045 if (s->data_ctx.errors.buf == 0)
3046 es = &s->data_ctx.errors.px->invalid_req;
3047 else
3048 es = &s->data_ctx.errors.px->invalid_rep;
3049
3050 if (!es->when.tv_sec)
3051 goto next;
3052
3053 if (s->data_ctx.errors.iid >= 0 &&
3054 s->data_ctx.errors.px->uuid != s->data_ctx.errors.iid &&
3055 es->oe->uuid != s->data_ctx.errors.iid)
3056 goto next;
3057
3058 if (s->data_ctx.errors.ptr < 0) {
3059 /* just print headers now */
3060
3061 char pn[INET6_ADDRSTRLEN];
3062 struct tm tm;
3063
3064 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003065 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003066 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003067 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003068
3069
3070 if (es->src.ss_family == AF_INET)
3071 inet_ntop(AF_INET,
3072 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
3073 pn, sizeof(pn));
3074 else
3075 inet_ntop(AF_INET6,
3076 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
3077 pn, sizeof(pn));
3078
3079 switch (s->data_ctx.errors.buf) {
3080 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003081 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003082 " frontend %s (#%d): invalid request\n"
3083 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003084 " request length %d bytes, error at position %d:\n \n",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003085 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
Willy Tarreau3fd6cec2010-03-25 06:45:07 +01003086 pn, es->sid, (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3087 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003088 es->srv ? es->srv->id : "<NONE>",
3089 es->srv ? es->srv->puid : -1,
3090 es->len, es->pos);
3091 break;
3092 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003093 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003094 " backend %s (#%d) : invalid response\n"
3095 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003096 " response length %d bytes, error at position %d:\n \n",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003097 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
3098 pn, es->sid, es->oe->id, es->oe->uuid,
3099 es->srv ? es->srv->id : "<NONE>",
3100 es->srv ? es->srv->puid : -1,
3101 es->len, es->pos);
3102 break;
3103 }
3104
Willy Tarreau61b34732009-10-03 23:49:35 +02003105 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003106 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003107 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003108 }
3109 s->data_ctx.errors.ptr = 0;
3110 s->data_ctx.errors.sid = es->sid;
3111 }
3112
3113 if (s->data_ctx.errors.sid != es->sid) {
3114 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003115 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003116 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau61b34732009-10-03 23:49:35 +02003117 if (buffer_feed_chunk(rep, &msg) >= 0)
3118 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003119 goto next;
3120 }
3121
3122 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau61b34732009-10-03 23:49:35 +02003123 while (s->data_ctx.errors.ptr < es->len && s->data_ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003124 int newptr;
3125 int newline;
3126
3127 newline = s->data_ctx.errors.bol;
Willy Tarreaud426a182010-03-05 14:58:26 +01003128 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 +01003129 if (newptr == s->data_ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02003130 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003131
Willy Tarreau61b34732009-10-03 23:49:35 +02003132 if (buffer_feed_chunk(rep, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003133 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003134 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003135 }
3136 s->data_ctx.errors.ptr = newptr;
3137 s->data_ctx.errors.bol = newline;
3138 };
3139 next:
3140 s->data_ctx.errors.bol = 0;
3141 s->data_ctx.errors.ptr = -1;
3142 s->data_ctx.errors.buf++;
3143 if (s->data_ctx.errors.buf > 1) {
3144 s->data_ctx.errors.buf = 0;
3145 s->data_ctx.errors.px = s->data_ctx.errors.px->next;
3146 }
3147 }
3148
3149 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02003150 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003151}
3152
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003153
Willy Tarreau10522fd2008-07-09 20:12:41 +02003154static struct cfg_kw_list cfg_kws = {{ },{
3155 { CFG_GLOBAL, "stats", stats_parse_global },
3156 { 0, NULL, NULL },
3157}};
3158
3159__attribute__((constructor))
3160static void __dumpstats_module_init(void)
3161{
3162 cfg_register_keywords(&cfg_kws);
3163}
3164
Willy Tarreau91861262007-10-17 17:06:05 +02003165/*
3166 * Local variables:
3167 * c-indent-level: 8
3168 * c-basic-offset: 8
3169 * End:
3170 */