blob: 34b808191b078967f253d146b0b643908499a5a6 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
2 * Functions dedicated to statistics output
3 *
Willy Tarreaua206fa92009-01-25 14:02:00 +01004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Willy Tarreau91861262007-10-17 17:06:05 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020019#include <pwd.h>
20#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau10522fd2008-07-09 20:12:41 +020026#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020027#include <common/compat.h>
28#include <common/config.h>
29#include <common/debug.h>
30#include <common/memory.h>
31#include <common/mini-clist.h>
32#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020033#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020034#include <common/time.h>
35#include <common/uri_auth.h>
36#include <common/version.h>
37
Willy Tarreau91861262007-10-17 17:06:05 +020038#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020039
40#include <proto/backend.h>
41#include <proto/buffers.h>
42#include <proto/dumpstats.h>
43#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010044#include <proto/freq_ctr.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010045#include <proto/pipe.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020046#include <proto/proto_uxst.h>
Willy Tarreau91861262007-10-17 17:06:05 +020047#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020048#include <proto/server.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010049#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010050#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020051
Willy Tarreaufbee7132007-10-18 13:53:22 +020052/* This function parses a "stats" statement in the "global" section. It returns
53 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
54 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
55 * zero included. The trailing '\n' must not be written. The function must be
56 * called with <args> pointing to the first word after "stats".
57 */
Willy Tarreau10522fd2008-07-09 20:12:41 +020058static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
59 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +020060{
Willy Tarreau10522fd2008-07-09 20:12:41 +020061 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +020062 if (!strcmp(args[0], "socket")) {
63 struct sockaddr_un su;
64 int cur_arg;
65
66 if (*args[1] == 0) {
67 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
68 return -1;
69 }
70
71 if (global.stats_sock.state != LI_NEW) {
72 snprintf(err, errlen, "'stats socket' already specified in global section");
73 return -1;
74 }
75
76 su.sun_family = AF_UNIX;
77 strncpy(su.sun_path, args[1], sizeof(su.sun_path));
78 su.sun_path[sizeof(su.sun_path) - 1] = 0;
79 memcpy(&global.stats_sock.addr, &su, sizeof(su)); // guaranteed to fit
80
81 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +010082 global.stats_sock.options = LI_O_NONE;
Willy Tarreaufbee7132007-10-18 13:53:22 +020083 global.stats_sock.accept = uxst_event_accept;
Willy Tarreaub1356cf2008-12-07 16:06:43 +010084 global.stats_sock.handler = uxst_process_session;
85 global.stats_sock.analysers = AN_REQ_UNIX_STATS;
Willy Tarreaufbee7132007-10-18 13:53:22 +020086 global.stats_sock.private = NULL;
87
88 cur_arg = 2;
89 while (*args[cur_arg]) {
90 if (!strcmp(args[cur_arg], "uid")) {
91 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
92 cur_arg += 2;
93 }
94 else if (!strcmp(args[cur_arg], "gid")) {
95 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
96 cur_arg += 2;
97 }
98 else if (!strcmp(args[cur_arg], "mode")) {
99 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
100 cur_arg += 2;
101 }
102 else if (!strcmp(args[cur_arg], "user")) {
103 struct passwd *user;
104 user = getpwnam(args[cur_arg + 1]);
105 if (!user) {
106 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
107 args[cur_arg + 1]);
108 return -1;
109 }
110 global.stats_sock.perm.ux.uid = user->pw_uid;
111 cur_arg += 2;
112 }
113 else if (!strcmp(args[cur_arg], "group")) {
114 struct group *group;
115 group = getgrnam(args[cur_arg + 1]);
116 if (!group) {
117 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
118 args[cur_arg + 1]);
119 return -1;
120 }
121 global.stats_sock.perm.ux.gid = group->gr_gid;
122 cur_arg += 2;
123 }
124 else {
125 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', and 'mode'");
126 return -1;
127 }
128 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100129
Willy Tarreaufbee7132007-10-18 13:53:22 +0200130 uxst_add_listener(&global.stats_sock);
131 global.maxsock++;
132 }
133 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100134 unsigned timeout;
135 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
136
137 if (res) {
138 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
139 return -1;
140 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200141
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100142 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200143 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
144 return -1;
145 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200146 global.stats_timeout = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200147 }
148 else if (!strcmp(args[0], "maxconn")) {
149 int maxconn = atol(args[1]);
150
151 if (maxconn <= 0) {
152 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
153 return -1;
154 }
155 global.maxsock -= global.stats_sock.maxconn;
156 global.stats_sock.maxconn = maxconn;
157 global.maxsock += global.stats_sock.maxconn;
158 }
159 else {
160 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
161 return -1;
162 }
163 return 0;
164}
165
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100166int print_csv_header(struct chunk *msg, int size)
167{
168 return chunk_printf(msg, size,
169 "# pxname,svname,"
170 "qcur,qmax,"
171 "scur,smax,slim,stot,"
172 "bin,bout,"
173 "dreq,dresp,"
174 "ereq,econ,eresp,"
175 "wretr,wredis,"
176 "status,weight,act,bck,"
177 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100178 "pid,iid,sid,throttle,lbtot,tracked,type,rate,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100179 "\n");
180}
181
Willy Tarreau91861262007-10-17 17:06:05 +0200182/*
183 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200184 * client socket shut down on input. It *may* make use of informations from
185 * <uri>. s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200186 * It returns 0 if it had to stop writing data and an I/O is needed, 1 if the
187 * dump is finished and the session must be closed, or -1 in case of any error.
Willy Tarreau0a464892008-12-07 18:30:00 +0100188 * It automatically clears the HIJACK bit from the response buffer.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200189 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100190int stats_dump_raw(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau3e76e722007-10-17 18:57:38 +0200191{
Willy Tarreau3e76e722007-10-17 18:57:38 +0200192 struct proxy *px;
193 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +0100194 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200195
196 msg.len = 0;
197 msg.str = trash;
198
199 switch (s->data_state) {
200 case DATA_ST_INIT:
201 /* the function had not been called yet, let's prepare the
202 * buffer for a response.
203 */
Willy Tarreaudded32d2008-11-30 19:48:07 +0100204 stream_int_retnclose(rep->cons, &msg);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200205 s->data_state = DATA_ST_HEAD;
206 /* fall through */
207
208 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100209 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100210 print_csv_header(&msg, sizeof(trash));
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200211 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100212 return 0;
213 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200214
215 s->data_state = DATA_ST_INFO;
216 /* fall through */
217
218 case DATA_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +0100219 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100220 if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100221 chunk_printf(&msg, sizeof(trash),
222 "Name: " PRODUCT_NAME "\n"
223 "Version: " HAPROXY_VERSION "\n"
224 "Release_date: " HAPROXY_DATE "\n"
225 "Nbproc: %d\n"
226 "Process_num: %d\n"
227 "Pid: %d\n"
228 "Uptime: %dd %dh%02dm%02ds\n"
229 "Uptime_sec: %d\n"
230 "Memmax_MB: %d\n"
231 "Ulimit-n: %d\n"
232 "Maxsock: %d\n"
233 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100234 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100235 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100236 "PipesUsed: %d\n"
237 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100238 "Tasks: %d\n"
239 "Run_queue: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100240 "",
241 global.nbproc,
242 relative_pid,
243 pid,
244 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
245 up,
246 global.rlimit_memmax,
247 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100248 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100249 actconn, pipes_used, pipes_free,
250 nb_tasks_cur, run_queue_cur
Willy Tarreaua8efd362008-01-03 10:19:15 +0100251 );
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200252 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100253 return 0;
254 }
255
Willy Tarreau3e76e722007-10-17 18:57:38 +0200256 s->data_ctx.stats.px = proxy;
257 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100258
259 s->data_ctx.stats.sv = NULL;
260 s->data_ctx.stats.sv_st = 0;
261
Willy Tarreau3e76e722007-10-17 18:57:38 +0200262 s->data_state = DATA_ST_LIST;
263 /* fall through */
264
265 case DATA_ST_LIST:
266 /* dump proxies */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100267 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100268 while (s->data_ctx.stats.px) {
269 px = s->data_ctx.stats.px;
270 /* skip the disabled proxies and non-networked ones */
271 if (px->state != PR_STSTOPPED &&
272 (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100273 if (stats_dump_proxy(s, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100274 return 0;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200275
Willy Tarreaua8efd362008-01-03 10:19:15 +0100276 s->data_ctx.stats.px = px->next;
277 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
278 }
279 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200280 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200281
282 s->data_state = DATA_ST_END;
283 /* fall through */
284
285 case DATA_ST_END:
286 s->data_state = DATA_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +0100287 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200288
289 case DATA_ST_FIN:
Willy Tarreau0a464892008-12-07 18:30:00 +0100290 buffer_stop_hijack(rep);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200291 return 1;
292
293 default:
294 /* unknown state ! */
Willy Tarreau0a464892008-12-07 18:30:00 +0100295 buffer_stop_hijack(rep);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200296 return -1;
297 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100298}
299
300
301/* This function is called to send output to the response buffer. It simply
302 * calls stats_dump_raw(), and releases the buffer's hijack bit when the dump
Willy Tarreau01bf8672008-12-07 18:03:29 +0100303 * is finished.
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100304 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100305void stats_dump_raw_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100306{
307 if (s->ana_state != STATS_ST_REP)
Willy Tarreau01bf8672008-12-07 18:03:29 +0100308 return;
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100309
Willy Tarreau0a464892008-12-07 18:30:00 +0100310 if (stats_dump_raw(s, rep, NULL) != 0)
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100311 s->ana_state = STATS_ST_CLOSE;
Willy Tarreau01bf8672008-12-07 18:03:29 +0100312 return;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200313}
314
315
316/*
317 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200318 * client socket shut down on input. It stops by itself by unsetting the
Willy Tarreau72b179a2008-08-28 16:01:32 +0200319 * BF_HIJACK flag from the buffer, which it uses to keep on being called
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200320 * when there is free space in the buffer, of simply by letting an empty buffer
321 * upon return.s->data_ctx must have been zeroed before the first call, and the
322 * flags set. It returns 0 if it had to stop writing data and an I/O is needed,
323 * 1 if the dump is finished and the session must be closed, or -1 in case of
324 * any error.
Willy Tarreau91861262007-10-17 17:06:05 +0200325 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100326int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200327{
Willy Tarreau91861262007-10-17 17:06:05 +0200328 struct proxy *px;
329 struct chunk msg;
330 unsigned int up;
331
332 msg.len = 0;
333 msg.str = trash;
334
335 switch (s->data_state) {
336 case DATA_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +0200337 chunk_printf(&msg, sizeof(trash),
338 "HTTP/1.0 200 OK\r\n"
339 "Cache-Control: no-cache\r\n"
340 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200341 "Content-Type: %s\r\n",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100342 (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +0200343
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100344 if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau91861262007-10-17 17:06:05 +0200345 chunk_printf(&msg, sizeof(trash), "Refresh: %d\r\n",
346 uri->refresh);
347
348 chunk_printf(&msg, sizeof(trash), "\r\n");
349
350 s->txn.status = 200;
Willy Tarreaudded32d2008-11-30 19:48:07 +0100351 stream_int_retnclose(rep->cons, &msg); // send the start of the response.
Willy Tarreau91861262007-10-17 17:06:05 +0200352 msg.len = 0;
353
354 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
355 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
356 if (!(s->flags & SN_FINST_MASK))
357 s->flags |= SN_FINST_R;
358
359 if (s->txn.meth == HTTP_METH_HEAD) {
360 /* that's all we return in case of HEAD request */
361 s->data_state = DATA_ST_FIN;
Willy Tarreau72b179a2008-08-28 16:01:32 +0200362 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200363 return 1;
364 }
365
366 s->data_state = DATA_ST_HEAD; /* let's start producing data */
367 /* fall through */
368
369 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100370 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200371 /* WARNING! This must fit in the first buffer !!! */
372 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200373 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
374 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
375 "<style type=\"text/css\"><!--\n"
376 "body {"
377 " font-family: helvetica, arial;"
378 " font-size: 12px;"
379 " font-weight: normal;"
380 " color: black;"
381 " background: white;"
382 "}\n"
383 "th,td {"
384 " font-size: 0.8em;"
385 " align: center;"
386 "}\n"
387 "h1 {"
388 " font-size: xx-large;"
389 " margin-bottom: 0.5em;"
390 "}\n"
391 "h2 {"
392 " font-family: helvetica, arial;"
393 " font-size: x-large;"
394 " font-weight: bold;"
395 " font-style: italic;"
396 " color: #6020a0;"
397 " margin-top: 0em;"
398 " margin-bottom: 0em;"
399 "}\n"
400 "h3 {"
401 " font-family: helvetica, arial;"
402 " font-size: 16px;"
403 " font-weight: bold;"
404 " color: #b00040;"
405 " background: #e8e8d0;"
406 " margin-top: 0em;"
407 " margin-bottom: 0em;"
408 "}\n"
409 "li {"
410 " margin-top: 0.25em;"
411 " margin-right: 2em;"
412 "}\n"
413 ".hr {margin-top: 0.25em;"
414 " border-color: black;"
415 " border-bottom-style: solid;"
416 "}\n"
417 ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n"
418 ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n"
419 ".total {background: #20D0D0;color: #ffff80;}\n"
420 ".frontend {background: #e8e8d0;}\n"
421 ".backend {background: #e8e8d0;}\n"
422 ".active0 {background: #ff9090;}\n"
423 ".active1 {background: #ffd020;}\n"
424 ".active2 {background: #ffffa0;}\n"
425 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100426 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
427 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
428 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200429 ".backup0 {background: #ff9090;}\n"
430 ".backup1 {background: #ff80ff;}\n"
431 ".backup2 {background: #c060ff;}\n"
432 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100433 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
434 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
435 ".backup6 {background: #e0e0e0;}\n"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200436 ".rls {letter-spacing: 0.3em;}\n" /* right letter spacing (used for grouping digits) */
Willy Tarreau91861262007-10-17 17:06:05 +0200437 "table.tbl { border-collapse: collapse; border-style: none;}\n"
438 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray;}\n"
439 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
440 "table.tbl th.empty { border-style: none; empty-cells: hide;}\n"
441 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
442 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
443 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
444 "-->\n"
445 "</style></head>\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200446 } else {
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100447 print_csv_header(&msg, sizeof(trash));
Willy Tarreau55bb8452007-10-17 18:44:57 +0200448 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200449 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200450 return 0;
451
452 s->data_state = DATA_ST_INFO;
453 /* fall through */
454
455 case DATA_ST_INFO:
456 up = (now.tv_sec - start_date.tv_sec);
457
458 /* WARNING! this has to fit the first packet too.
459 * We are around 3.5 kB, add adding entries will
460 * become tricky if we want to support 4kB buffers !
461 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100462 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200463 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200464 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
465 PRODUCT_NAME "%s</a></h1>\n"
466 "<h2>Statistics Report for pid %d</h2>\n"
467 "<hr width=\"100%%\" class=\"hr\">\n"
468 "<h3>&gt; General process information</h3>\n"
469 "<table border=0 cols=4><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100470 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200471 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
472 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100473 "<b>maxsock = </b> %d ; <b>maxconn = </b> %d ; <b>maxpipes = </b> %d<br>\n"
474 "current conns = %d ; current pipes = %d/%d<br>\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100475 "Running tasks : %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200476 "</td><td align=\"center\" nowrap>\n"
477 "<table class=\"lgd\"><tr>\n"
478 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
479 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
480 "</tr><tr>\n"
481 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
482 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
483 "</tr><tr>\n"
484 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
485 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
486 "</tr><tr>\n"
487 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100488 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200489 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100490 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +0200491 "</td>"
492 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
493 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
494 "",
495 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Willy Tarreaua8efd362008-01-03 10:19:15 +0100496 pid, pid,
497 relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +0200498 up / 86400, (up % 86400) / 3600,
499 (up % 3600) / 60, (up % 60),
500 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
501 global.rlimit_memmax ? " MB" : "",
502 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100503 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100504 actconn, pipes_used, pipes_used+pipes_free,
505 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +0200506 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100507
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100508 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200509 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200510 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
511 uri->uri_prefix,
512 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100513 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200514 else
515 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200516 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
517 uri->uri_prefix,
518 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100519 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200520
Willy Tarreau55bb8452007-10-17 18:44:57 +0200521 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100522 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200523 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200524 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
525 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100526 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200527 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200528 else
529 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200530 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
531 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100532 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200533 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200534 }
Willy Tarreau91861262007-10-17 17:06:05 +0200535
Willy Tarreau55bb8452007-10-17 18:44:57 +0200536 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200537 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
538 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100539 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
540 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200541
Willy Tarreau55bb8452007-10-17 18:44:57 +0200542 chunk_printf(&msg, sizeof(trash),
Willy Tarreau5031e6a2007-10-18 11:05:48 +0200543 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
544 uri->uri_prefix,
545 (uri->refresh > 0) ? ";norefresh" : "");
546
547 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200548 "</td>"
549 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
550 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
551 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
552 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
553 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
554 "</ul>"
555 "</td>"
556 "</tr></table>\n"
557 ""
558 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100559
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200560 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200561 return 0;
562 }
Willy Tarreau91861262007-10-17 17:06:05 +0200563
Willy Tarreau91861262007-10-17 17:06:05 +0200564 s->data_ctx.stats.px = proxy;
565 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
566 s->data_state = DATA_ST_LIST;
567 /* fall through */
568
569 case DATA_ST_LIST:
570 /* dump proxies */
571 while (s->data_ctx.stats.px) {
572 px = s->data_ctx.stats.px;
573 /* skip the disabled proxies and non-networked ones */
574 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100575 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200576 return 0;
577
578 s->data_ctx.stats.px = px->next;
579 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
580 }
581 /* here, we just have reached the last proxy */
582
583 s->data_state = DATA_ST_END;
584 /* fall through */
585
586 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100587 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200588 chunk_printf(&msg, sizeof(trash), "</body></html>\n");
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200589 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200590 return 0;
591 }
Willy Tarreau91861262007-10-17 17:06:05 +0200592
593 s->data_state = DATA_ST_FIN;
594 /* fall through */
595
596 case DATA_ST_FIN:
Willy Tarreau72b179a2008-08-28 16:01:32 +0200597 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200598 return 1;
599
600 default:
601 /* unknown state ! */
Willy Tarreau72b179a2008-08-28 16:01:32 +0200602 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200603 return -1;
604 }
605}
606
607
608/*
609 * Dumps statistics for a proxy.
610 * Returns 0 if it had to stop dumping data because of lack of buffer space,
611 * ot non-zero if everything completed.
612 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100613int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200614{
615 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100616 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Willy Tarreau91861262007-10-17 17:06:05 +0200617 struct chunk msg;
618
619 msg.len = 0;
620 msg.str = trash;
621
622 switch (s->data_ctx.stats.px_st) {
623 case DATA_ST_PX_INIT:
624 /* we are on a new proxy */
625
626 if (uri && uri->scope) {
627 /* we have a limited scope, we have to check the proxy name */
628 struct stat_scope *scope;
629 int len;
630
631 len = strlen(px->id);
632 scope = uri->scope;
633
634 while (scope) {
635 /* match exact proxy name */
636 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
637 break;
638
639 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +0200640 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +0200641 break;
642 scope = scope->next;
643 }
644
645 /* proxy name not found : don't dump anything */
646 if (scope == NULL)
647 return 1;
648 }
649
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100650 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100651 (px->uuid != s->data_ctx.stats.iid))
652 return 1;
653
Willy Tarreau91861262007-10-17 17:06:05 +0200654 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
655 /* fall through */
656
657 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100658 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200659 /* print a new table */
660 chunk_printf(&msg, sizeof(trash),
Willy Tarreau7f062c42009-03-05 18:43:00 +0100661 "<table cols=\"27\" class=\"tbl\" width=\"100%%\">\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200662 "<tr align=\"center\" class=\"titre\">"
663 "<th colspan=2 class=\"pxname\">%s</th>"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100664 "<th colspan=25 class=\"empty\"></th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200665 "</tr>\n"
666 "<tr align=\"center\" class=\"titre\">"
667 "<th rowspan=2></th>"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100668 "<th colspan=3>Queue</th><th colspan=6>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200669 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200670 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100671 "<th colspan=8>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200672 "</tr>\n"
673 "<tr align=\"center\" class=\"titre\">"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200674 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100675 "<th>Limit</th><th>Rate</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200676 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200677 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200678 "<th>Status</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100679 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
680 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200681 "</tr>",
Willy Tarreau55bb8452007-10-17 18:44:57 +0200682 px->id);
683
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200684 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200685 return 0;
686 }
Willy Tarreau91861262007-10-17 17:06:05 +0200687
688 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
689 /* fall through */
690
691 case DATA_ST_PX_FE:
692 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100693 if ((px->cap & PR_CAP_FE) &&
694 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
695 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200696 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200697 /* name, queue */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200698 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=3></td>"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100699 /* sessions : current, max, limit, rate, total, lbtot */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200700 "<td align=right>%s</td><td align=right>%s</td>"
701 "<td align=right>%s</td><td align=right>%s</td>"
702 "<td align=right>%s</td><td align=right></td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200703 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200704 "<td align=right>%s</td><td align=right>%s</td>"
705 "",
706 U2H0(px->feconn), U2H1(px->feconn_max), U2H2(px->maxconn),
707 U2H3(read_freq_ctr(&px->fe_sess_per_sec)), U2H4(px->cum_feconn),
708 U2H5(px->bytes_in), U2H6(px->bytes_out));
709
710 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200711 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200712 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200713 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200714 "<td align=right>%s</td><td align=right></td><td align=right></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200715 /* warnings: retries, redispatches */
716 "<td align=right></td><td align=right></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200717 /* server status : reflect frontend status */
Willy Tarreau91861262007-10-17 17:06:05 +0200718 "<td align=center>%s</td>"
719 /* rest of server: nothing */
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100720 "<td align=center colspan=7></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +0200721 "",
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200722 U2H0(px->denied_req), U2H1(px->denied_resp),
723 U2H2(px->failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +0200724 px->state == PR_STRUN ? "OPEN" :
725 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200726 } else {
727 chunk_printf(&msg, sizeof(trash),
728 /* pxid, name, queue cur, queue max, */
729 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100730 /* sessions : current, max, limit, total */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200731 "%d,%d,%d,%d,"
732 /* bytes : in, out */
733 "%lld,%lld,"
734 /* denied: req, resp */
735 "%d,%d,"
736 /* errors : request, connect, response */
737 "%d,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200738 /* warnings: retries, redispatches */
739 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200740 /* server status : reflect frontend status */
741 "%s,"
742 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200743 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100744 /* pid, iid, sid, throttle, lbtot, tracked, type */
745 "%d,%d,0,,,,%d,"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100746 /* rate */
747 "%u,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200748 "\n",
749 px->id,
750 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
751 px->bytes_in, px->bytes_out,
752 px->denied_req, px->denied_resp,
753 px->failed_req,
754 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +0100755 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +0100756 relative_pid, px->uuid, STATS_TYPE_FE,
757 read_freq_ctr(&px->fe_sess_per_sec));
Willy Tarreau55bb8452007-10-17 18:44:57 +0200758 }
Willy Tarreau91861262007-10-17 17:06:05 +0200759
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200760 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200761 return 0;
762 }
763
764 s->data_ctx.stats.sv = px->srv; /* may be NULL */
765 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
766 /* fall through */
767
768 case DATA_ST_PX_SV:
769 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100770 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
771
Willy Tarreau2ea81932007-11-30 12:04:38 +0100772 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 +0200773
774 sv = s->data_ctx.stats.sv;
775
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100776 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100777 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
778 break;
779
780 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
781 continue;
782 }
783
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100784 if (sv->tracked)
785 svs = sv->tracked;
786 else
787 svs = sv;
788
Willy Tarreau91861262007-10-17 17:06:05 +0200789 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100790 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +0100791 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100792 else if (svs->state & SRV_RUNNING) {
793 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +0200794 sv_state = 3; /* UP */
795 else
796 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +0100797
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100798 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +0100799 sv_state += 2;
800 }
Willy Tarreau91861262007-10-17 17:06:05 +0200801 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100802 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +0200803 sv_state = 1; /* going up */
804 else
805 sv_state = 0; /* DOWN */
806
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100807 if ((sv_state == 0) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +0200808 /* do not report servers which are DOWN */
809 s->data_ctx.stats.sv = sv->next;
810 continue;
811 }
812
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100813 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +0100814 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
815 "UP %d/%d &darr;", "UP",
816 "NOLB %d/%d &darr;", "NOLB",
817 "<i>no check</i>" };
Willy Tarreau55bb8452007-10-17 18:44:57 +0200818 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200819 /* name */
820 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200821 /* queue : current, max, limit */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200822 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100823 /* sessions : current, max, limit, rate, total, lbtot */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200824 "<td align=right>%s</td><td align=right>%s</td>"
825 "<td align=right>%s</td><td align=right>%s</td>"
826 "<td align=right>%s</td><td align=right>%s</td>"
827 "",
828 (sv->state & SRV_BACKUP) ? "backup" : "active",
829 sv_state, sv->id,
830 U2H0(sv->nbpend), U2H1(sv->nbpend_max), LIM2A2(sv->maxqueue, "-"),
831 U2H3(sv->cur_sess), U2H4(sv->cur_sess_max), LIM2A5(sv->maxconn, "-"),
832 U2H6(read_freq_ctr(&sv->sess_per_sec)),
833 U2H7(sv->cum_sess), U2H8(sv->cum_lbconn));
834
835 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200836 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200837 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200838 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200839 "<td align=right></td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200840 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200841 "<td align=right></td><td align=right>%s</td><td align=right>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200842 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100843 "<td align=right>%u</td><td align=right>%u</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200844 "",
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200845 U2H0(sv->bytes_in), U2H1(sv->bytes_out),
846 U2H2(sv->failed_secu),
847 U2H3(sv->failed_conns), U2H4(sv->failed_resp),
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100848 sv->retries, sv->redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100849
Willy Tarreau55bb8452007-10-17 18:44:57 +0200850 /* status */
851 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200852
853 if (sv->state & SRV_CHECKED)
854 chunk_printf(&msg, sizeof(trash), "%s ",
855 human_time(now.tv_sec - sv->last_change, 1));
856
Willy Tarreau55bb8452007-10-17 18:44:57 +0200857 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200858 srv_hlt_st[sv_state],
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100859 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
860 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Willy Tarreau91861262007-10-17 17:06:05 +0200861
Willy Tarreau55bb8452007-10-17 18:44:57 +0200862 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200863 /* weight */
864 "</td><td>%d</td>"
865 /* act, bck */
866 "<td>%s</td><td>%s</td>"
867 "",
Willy Tarreau5542af62007-12-03 02:04:00 +0100868 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +0200869 (sv->state & SRV_BACKUP) ? "-" : "Y",
870 (sv->state & SRV_BACKUP) ? "Y" : "-");
871
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200872 /* check failures: unique, fatal, down time */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200873 if (sv->state & SRV_CHECKED)
874 chunk_printf(&msg, sizeof(trash),
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200875 "<td align=right>%d</td><td align=right>%d</td>"
876 "<td nowrap align=right>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100877 "",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100878 svs->failed_checks, svs->down_trans,
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200879 human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100880 else if (sv != svs)
881 chunk_printf(&msg, sizeof(trash),
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200882 "<td nowrap colspan=3>via %s/%s</td>", svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200883 else
884 chunk_printf(&msg, sizeof(trash),
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100885 "<td colspan=3></td>");
886
887 /* throttle */
888 if ((sv->state & SRV_WARMINGUP) &&
889 now.tv_sec < sv->last_change + sv->slowstart &&
890 now.tv_sec >= sv->last_change) {
891 unsigned int ratio;
892 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
893 chunk_printf(&msg, sizeof(trash),
894 "<td>%d %%</td></tr>\n", ratio);
895 } else {
896 chunk_printf(&msg, sizeof(trash),
897 "<td>-</td></tr>\n");
898 }
Willy Tarreau55bb8452007-10-17 18:44:57 +0200899 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +0100900 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
901 "UP %d/%d,", "UP,",
902 "NOLB %d/%d,", "NOLB,",
903 "no check," };
Willy Tarreau55bb8452007-10-17 18:44:57 +0200904 chunk_printf(&msg, sizeof(trash),
905 /* pxid, name */
906 "%s,%s,"
907 /* queue : current, max */
908 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100909 /* sessions : current, max, limit, total */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200910 "%d,%d,%s,%d,"
911 /* bytes : in, out */
912 "%lld,%lld,"
913 /* denied: req, resp */
914 ",%d,"
915 /* errors : request, connect, response */
916 ",%d,%d,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200917 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100918 "%u,%u,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200919 "",
920 px->id, sv->id,
921 sv->nbpend, sv->nbpend_max,
Willy Tarreaudcd47712007-11-04 23:35:08 +0100922 sv->cur_sess, sv->cur_sess_max, LIM2A0(sv->maxconn, ""), sv->cum_sess,
Willy Tarreau55bb8452007-10-17 18:44:57 +0200923 sv->bytes_in, sv->bytes_out,
924 sv->failed_secu,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200925 sv->failed_conns, sv->failed_resp,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100926 sv->retries, sv->redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100927
Willy Tarreau55bb8452007-10-17 18:44:57 +0200928 /* status */
929 chunk_printf(&msg, sizeof(trash),
930 srv_hlt_st[sv_state],
931 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
932 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
Willy Tarreau91861262007-10-17 17:06:05 +0200933
Willy Tarreau55bb8452007-10-17 18:44:57 +0200934 chunk_printf(&msg, sizeof(trash),
935 /* weight, active, backup */
936 "%d,%d,%d,"
937 "",
Willy Tarreau5542af62007-12-03 02:04:00 +0100938 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +0200939 (sv->state & SRV_BACKUP) ? 0 : 1,
940 (sv->state & SRV_BACKUP) ? 1 : 0);
941
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200942 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200943 if (sv->state & SRV_CHECKED)
944 chunk_printf(&msg, sizeof(trash),
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200945 "%d,%d,%d,%d,",
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200946 sv->failed_checks, sv->down_trans,
947 now.tv_sec - sv->last_change, srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +0200948 else
949 chunk_printf(&msg, sizeof(trash),
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200950 ",,,,");
951
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100952 /* queue limit, pid, iid, sid, */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200953 chunk_printf(&msg, sizeof(trash),
Willy Tarreaudcd47712007-11-04 23:35:08 +0100954 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100955 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +0100956 LIM2A0(sv->maxqueue, ""),
957 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100958
959 /* throttle */
960 if ((sv->state & SRV_WARMINGUP) &&
961 now.tv_sec < sv->last_change + sv->slowstart &&
962 now.tv_sec >= sv->last_change) {
963 unsigned int ratio;
964 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100965 chunk_printf(&msg, sizeof(trash), "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100966 }
967
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100968 /* sessions: lbtot */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100969 chunk_printf(&msg, sizeof(trash), ",%d,", sv->cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100970
971 /* tracked */
972 if (sv->tracked)
Krzysztof Piotr Oledzkif58a9622008-02-23 01:19:10 +0100973 chunk_printf(&msg, sizeof(trash), "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100974 sv->tracked->proxy->id, sv->tracked->id);
975 else
976 chunk_printf(&msg, sizeof(trash), ",");
977
Willy Tarreau7f062c42009-03-05 18:43:00 +0100978 /* type */
979 chunk_printf(&msg, sizeof(trash), "%d,", STATS_TYPE_SV);
980
981 /* rate */
982 chunk_printf(&msg, sizeof(trash), "%u,", read_freq_ctr(&sv->sess_per_sec));
983
984 /* finish with EOL */
985 chunk_printf(&msg, sizeof(trash), "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200986 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200987 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200988 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100989 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +0200990
991 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
992 /* fall through */
993
994 case DATA_ST_PX_BE:
995 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100996 if ((px->cap & PR_CAP_BE) &&
997 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
998 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200999 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +02001000 /* name */
1001 "<tr align=center class=\"backend\"><td>Backend</td>"
1002 /* queue : current, max */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001003 "<td align=right>%s</td><td align=right>%s</td><td></td>"
Willy Tarreau7f062c42009-03-05 18:43:00 +01001004 /* sessions : current, max, limit, rate, total, lbtot */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001005 "<td align=right>%s</td><td align=right>%s</td>"
1006 "<td align=right>%s</td><td align=right>%s</td>"
1007 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001008 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001009 "<td align=right>%s</td><td align=right>%s</td>"
1010 "",
1011 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->nbpend_max),
1012 U2H2(px->beconn), U2H3(px->beconn_max), U2H4(px->fullconn),
1013 U2H5(read_freq_ctr(&px->be_sess_per_sec)),
1014 U2H6(px->cum_beconn), U2H7(px->cum_lbconn),
1015 U2H8(px->bytes_in), U2H9(px->bytes_out));
1016
1017 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +02001018 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001019 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001020 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001021 "<td align=right></td><td align=right>%s</td><td align=right>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001022 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001023 "<td align=right>%u</td><td align=right>%u</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001024 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02001025 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001026 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02001027 * active and backups. */
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001028 "<td align=center nowrap>%s %s</td><td align=center>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001029 "<td align=center>%d</td><td align=center>%d</td>"
1030 "",
1031 U2H0(px->denied_req), U2H1(px->denied_resp),
1032 U2H2(px->failed_conns), U2H3(px->failed_resp),
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001033 px->retries, px->redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001034 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01001035 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
1036 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01001037 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001038 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001039
1040 chunk_printf(&msg, sizeof(trash),
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001041 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001042 "<td align=center>&nbsp;</td><td align=\"right\">%d</td>"
1043 "<td align=\"right\" nowrap>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001044 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001045 "</tr>",
1046 px->down_trans,
1047 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001048 } else {
1049 chunk_printf(&msg, sizeof(trash),
1050 /* pxid, name */
1051 "%s,BACKEND,"
1052 /* queue : current, max */
1053 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001054 /* sessions : current, max, limit, total */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001055 "%d,%d,%d,%d,"
1056 /* bytes : in, out */
1057 "%lld,%lld,"
1058 /* denied: req, resp */
1059 "%d,%d,"
1060 /* errors : request, connect, response */
1061 ",%d,%d,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001062 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001063 "%u,%u,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001064 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02001065 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001066 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02001067 * active and backups. */
1068 "%s,"
1069 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001070 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001071 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001072 /* pid, iid, sid, throttle, lbtot, tracked, type */
1073 "%d,%d,0,,%d,,%d,"
Willy Tarreau7f062c42009-03-05 18:43:00 +01001074 /* rate */
1075 "%u,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001076 "\n",
1077 px->id,
1078 px->nbpend /* or px->totpend ? */, px->nbpend_max,
1079 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
1080 px->bytes_in, px->bytes_out,
1081 px->denied_req, px->denied_resp,
1082 px->failed_conns, px->failed_resp,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001083 px->retries, px->redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01001084 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01001085 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001086 px->srv_act, px->srv_bck,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001087 px->down_trans, now.tv_sec - px->last_change,
Willy Tarreau20697042007-11-15 23:26:18 +01001088 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001089 relative_pid, px->uuid,
Willy Tarreau7f062c42009-03-05 18:43:00 +01001090 px->cum_lbconn, STATS_TYPE_BE,
1091 read_freq_ctr(&px->be_sess_per_sec));
Willy Tarreau55bb8452007-10-17 18:44:57 +02001092 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001093 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001094 return 0;
1095 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001096
Willy Tarreau91861262007-10-17 17:06:05 +02001097 s->data_ctx.stats.px_st = DATA_ST_PX_END;
1098 /* fall through */
1099
1100 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001101 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001102 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001103
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001104 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001105 return 0;
1106 }
Willy Tarreau91861262007-10-17 17:06:05 +02001107
1108 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
1109 /* fall through */
1110
1111 case DATA_ST_PX_FIN:
1112 return 1;
1113
1114 default:
1115 /* unknown state, we should put an abort() here ! */
1116 return 1;
1117 }
1118}
1119
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001120
1121/* This function is called to send output to the response buffer.
1122 * It dumps the sessions states onto the output buffer <rep>.
1123 * Expects to be called with client socket shut down on input.
1124 * s->data_ctx must have been zeroed first, and the flags properly set.
1125 * It automatically clears the HIJACK bit from the response buffer.
1126 */
1127void stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
1128{
1129 struct chunk msg;
1130
1131 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
1132 /* If we're forced to shut down, we might have to remove our
1133 * reference to the last session being dumped.
1134 */
1135 if (s->data_state == DATA_ST_LIST) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001136 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001137 LIST_DEL(&s->data_ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001138 LIST_INIT(&s->data_ctx.sess.bref.users);
1139 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001140 }
1141 s->data_state = DATA_ST_FIN;
1142 buffer_stop_hijack(rep);
1143 s->ana_state = STATS_ST_CLOSE;
1144 return;
1145 }
1146
1147 if (s->ana_state != STATS_ST_REP)
1148 return;
1149
1150 msg.len = 0;
1151 msg.str = trash;
1152
1153 switch (s->data_state) {
1154 case DATA_ST_INIT:
1155 /* the function had not been called yet, let's prepare the
1156 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001157 * pointer to the first in the global list. When a target
1158 * session is being destroyed, it is responsible for updating
1159 * this pointer. We know we have reached the end when this
1160 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001161 */
1162 stream_int_retnclose(rep->cons, &msg);
1163 LIST_INIT(&s->data_ctx.sess.bref.users);
1164 s->data_ctx.sess.bref.ref = sessions.n;
1165 s->data_state = DATA_ST_LIST;
1166 /* fall through */
1167
1168 case DATA_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001169 /* first, let's detach the back-ref from a possible previous session */
1170 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
1171 LIST_DEL(&s->data_ctx.sess.bref.users);
1172 LIST_INIT(&s->data_ctx.sess.bref.users);
1173 }
1174
1175 /* and start from where we stopped */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001176 while (s->data_ctx.sess.bref.ref != &sessions) {
1177 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1178 struct session *curr_sess;
1179
1180 curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list);
1181
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001182 chunk_printf(&msg, sizeof(trash),
1183 "%p: proto=%s",
1184 curr_sess,
1185 curr_sess->listener->proto->name);
1186
1187 switch (curr_sess->listener->proto->sock_family) {
1188 case AF_INET:
1189 inet_ntop(AF_INET,
1190 (const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr,
1191 pn, sizeof(pn));
1192
1193 chunk_printf(&msg, sizeof(trash),
1194 " src=%s:%d fe=%s be=%s srv=%s",
1195 pn,
1196 ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port),
1197 curr_sess->fe->id,
1198 curr_sess->be->id,
1199 curr_sess->srv ? curr_sess->srv->id : "<none>"
1200 );
1201 break;
1202 case AF_INET6:
1203 inet_ntop(AF_INET6,
1204 (const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr,
1205 pn, sizeof(pn));
1206
1207 chunk_printf(&msg, sizeof(trash),
1208 " src=%s:%d fe=%s be=%s srv=%s",
1209 pn,
1210 ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port),
1211 curr_sess->fe->id,
1212 curr_sess->be->id,
1213 curr_sess->srv ? curr_sess->srv->id : "<none>"
1214 );
1215
1216 break;
1217 case AF_UNIX:
1218 /* no more information to print right now */
1219 break;
1220 }
1221
1222 chunk_printf(&msg, sizeof(trash),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001223 " as=%d ts=%02x age=%s calls=%d",
Willy Tarreau8a5c6262008-12-08 00:16:21 +01001224 curr_sess->ana_state, curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01001225 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
1226 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001227
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001228 chunk_printf(&msg, sizeof(trash),
1229 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
1230 curr_sess->req->flags,
1231 curr_sess->req->l,
1232 curr_sess->req->analysers,
1233 curr_sess->req->rex ?
1234 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
1235 TICKS_TO_MS(1000)) : "");
1236
1237 chunk_printf(&msg, sizeof(trash),
1238 ",wx=%s",
1239 curr_sess->req->wex ?
1240 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
1241 TICKS_TO_MS(1000)) : "");
1242
1243 chunk_printf(&msg, sizeof(trash),
1244 ",ax=%s]",
1245 curr_sess->req->analyse_exp ?
1246 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
1247 TICKS_TO_MS(1000)) : "");
1248
1249 chunk_printf(&msg, sizeof(trash),
1250 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
1251 curr_sess->rep->flags,
1252 curr_sess->rep->l,
1253 curr_sess->rep->analysers,
1254 curr_sess->rep->rex ?
1255 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
1256 TICKS_TO_MS(1000)) : "");
1257
1258 chunk_printf(&msg, sizeof(trash),
1259 ",wx=%s",
1260 curr_sess->rep->wex ?
1261 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
1262 TICKS_TO_MS(1000)) : "");
1263
1264 chunk_printf(&msg, sizeof(trash),
1265 ",ax=%s]",
1266 curr_sess->rep->analyse_exp ?
1267 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
1268 TICKS_TO_MS(1000)) : "");
1269
1270 chunk_printf(&msg, sizeof(trash),
1271 " s0=[%d,%1xh,fd=%d,ex=%s]",
1272 curr_sess->si[0].state,
1273 curr_sess->si[0].flags,
1274 curr_sess->si[0].fd,
1275 curr_sess->si[0].exp ?
1276 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
1277 TICKS_TO_MS(1000)) : "");
1278
1279 chunk_printf(&msg, sizeof(trash),
1280 " s1=[%d,%1xh,fd=%d,ex=%s]",
1281 curr_sess->si[1].state,
1282 curr_sess->si[1].flags,
1283 curr_sess->si[1].fd,
1284 curr_sess->si[1].exp ?
1285 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
1286 TICKS_TO_MS(1000)) : "");
1287
1288 chunk_printf(&msg, sizeof(trash),
1289 " exp=%s",
1290 curr_sess->task->expire ?
1291 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
1292 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01001293 if (task_in_rq(curr_sess->task))
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001294 chunk_printf(&msg, sizeof(trash), " run(nice=%d)", curr_sess->task->nice);
1295
1296 chunk_printf(&msg, sizeof(trash), "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001297
1298 if (buffer_write_chunk(rep, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001299 /* let's try again later from this session. We add ourselves into
1300 * this session's users so that it can remove us upon termination.
1301 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001302 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
1303 return;
1304 }
1305
1306 s->data_ctx.sess.bref.ref = curr_sess->list.n;
1307 }
1308 s->data_state = DATA_ST_FIN;
1309 /* fall through */
1310
1311 default:
1312 s->data_state = DATA_ST_FIN;
1313 buffer_stop_hijack(rep);
1314 s->ana_state = STATS_ST_CLOSE;
1315 return;
1316 }
1317}
1318
Willy Tarreau74808cb2009-03-04 15:53:18 +01001319/* print a line of error buffer (limited to 70 bytes) to <out>. The format is :
1320 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
1321 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
1322 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
1323 * lines are respected within the limit of 70 output chars. Lines that are
1324 * continuation of a previous truncated line begin with "+" instead of " "
1325 * after the offset. The new pointer is returned.
1326 */
1327static int dump_error_line(struct chunk *out, int size,
1328 struct error_snapshot *err, int *line, int ptr)
1329{
1330 int end;
1331 unsigned char c;
1332
1333 end = out->len + 80;
1334 if (end > size)
1335 return ptr;
1336
1337 chunk_printf(out, size, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
1338
1339 while (ptr < err->len) {
1340 c = err->buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01001341 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001342 if (out->len > end - 2)
1343 break;
1344 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01001345 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001346 if (out->len > end - 3)
1347 break;
1348 out->str[out->len++] = '\\';
1349 switch (c) {
1350 case '\t': c = 't'; break;
1351 case '\n': c = 'n'; break;
1352 case '\r': c = 'r'; break;
1353 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01001354 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001355 }
1356 out->str[out->len++] = c;
1357 } else {
1358 if (out->len > end - 5)
1359 break;
1360 out->str[out->len++] = '\\';
1361 out->str[out->len++] = 'x';
1362 out->str[out->len++] = hextab[(c >> 4) & 0xF];
1363 out->str[out->len++] = hextab[c & 0xF];
1364 }
1365 if (err->buf[ptr++] == '\n') {
1366 /* we had a line break, let's return now */
1367 out->str[out->len++] = '\n';
1368 *line = ptr;
1369 return ptr;
1370 }
1371 }
1372 /* we have an incomplete line, we return it as-is */
1373 out->str[out->len++] = '\n';
1374 return ptr;
1375}
1376
1377/* This function is called to send output to the response buffer.
1378 * It dumps the errors logged in proxies onto the output buffer <rep>.
1379 * Expects to be called with client socket shut down on input.
1380 * s->data_ctx must have been zeroed first, and the flags properly set.
1381 * It automatically clears the HIJACK bit from the response buffer.
1382 */
1383void stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
1384{
1385 extern const char *monthname[12];
1386 struct chunk msg;
1387
1388 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
1389 s->data_state = DATA_ST_FIN;
1390 buffer_stop_hijack(rep);
1391 s->ana_state = STATS_ST_CLOSE;
1392 return;
1393 }
1394
1395 if (s->ana_state != STATS_ST_REP)
1396 return;
1397
1398 msg.len = 0;
1399 msg.str = trash;
1400
1401 if (!s->data_ctx.errors.px) {
1402 /* the function had not been called yet, let's prepare the
1403 * buffer for a response.
1404 */
1405 stream_int_retnclose(rep->cons, &msg);
1406 s->data_ctx.errors.px = proxy;
1407 s->data_ctx.errors.buf = 0;
1408 s->data_ctx.errors.bol = 0;
1409 s->data_ctx.errors.ptr = -1;
1410 }
1411
1412 /* we have two inner loops here, one for the proxy, the other one for
1413 * the buffer.
1414 */
1415 while (s->data_ctx.errors.px) {
1416 struct error_snapshot *es;
1417
1418 if (s->data_ctx.errors.buf == 0)
1419 es = &s->data_ctx.errors.px->invalid_req;
1420 else
1421 es = &s->data_ctx.errors.px->invalid_rep;
1422
1423 if (!es->when.tv_sec)
1424 goto next;
1425
1426 if (s->data_ctx.errors.iid >= 0 &&
1427 s->data_ctx.errors.px->uuid != s->data_ctx.errors.iid &&
1428 es->oe->uuid != s->data_ctx.errors.iid)
1429 goto next;
1430
1431 if (s->data_ctx.errors.ptr < 0) {
1432 /* just print headers now */
1433
1434 char pn[INET6_ADDRSTRLEN];
1435 struct tm tm;
1436
1437 get_localtime(es->when.tv_sec, &tm);
1438 chunk_printf(&msg, sizeof(trash), "\n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
1439 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
1440 tm.tm_hour, tm.tm_min, tm.tm_sec, es->when.tv_usec/1000);
1441
1442
1443 if (es->src.ss_family == AF_INET)
1444 inet_ntop(AF_INET,
1445 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
1446 pn, sizeof(pn));
1447 else
1448 inet_ntop(AF_INET6,
1449 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
1450 pn, sizeof(pn));
1451
1452 switch (s->data_ctx.errors.buf) {
1453 case 0:
1454 chunk_printf(&msg, sizeof(trash),
1455 " frontend %s (#%d): invalid request\n"
1456 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
1457 " request length %d bytes, error at position %d:\n\n",
1458 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
1459 pn, es->sid, es->oe->id, es->oe->uuid,
1460 es->srv ? es->srv->id : "<NONE>",
1461 es->srv ? es->srv->puid : -1,
1462 es->len, es->pos);
1463 break;
1464 case 1:
1465 chunk_printf(&msg, sizeof(trash),
1466 " backend %s (#%d) : invalid response\n"
1467 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
1468 " response length %d bytes, error at position %d:\n\n",
1469 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
1470 pn, es->sid, es->oe->id, es->oe->uuid,
1471 es->srv ? es->srv->id : "<NONE>",
1472 es->srv ? es->srv->puid : -1,
1473 es->len, es->pos);
1474 break;
1475 }
1476
1477 if (buffer_write_chunk(rep, &msg) >= 0) {
1478 /* Socket buffer full. Let's try again later from the same point */
1479 return;
1480 }
1481 s->data_ctx.errors.ptr = 0;
1482 s->data_ctx.errors.sid = es->sid;
1483 }
1484
1485 if (s->data_ctx.errors.sid != es->sid) {
1486 /* the snapshot changed while we were dumping it */
1487 chunk_printf(&msg, sizeof(trash),
1488 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
1489 if (buffer_write_chunk(rep, &msg) >= 0)
1490 return;
1491 goto next;
1492 }
1493
1494 /* OK, ptr >= 0, so we have to dump the current line */
1495 while (s->data_ctx.errors.ptr < es->len) {
1496 int newptr;
1497 int newline;
1498
1499 newline = s->data_ctx.errors.bol;
1500 newptr = dump_error_line(&msg, sizeof(trash), es, &newline, s->data_ctx.errors.ptr);
1501 if (newptr == s->data_ctx.errors.ptr)
1502 return;
1503
1504 if (buffer_write_chunk(rep, &msg) >= 0) {
1505 /* Socket buffer full. Let's try again later from the same point */
1506 return;
1507 }
1508 s->data_ctx.errors.ptr = newptr;
1509 s->data_ctx.errors.bol = newline;
1510 };
1511 next:
1512 s->data_ctx.errors.bol = 0;
1513 s->data_ctx.errors.ptr = -1;
1514 s->data_ctx.errors.buf++;
1515 if (s->data_ctx.errors.buf > 1) {
1516 s->data_ctx.errors.buf = 0;
1517 s->data_ctx.errors.px = s->data_ctx.errors.px->next;
1518 }
1519 }
1520
1521 /* dump complete */
1522 buffer_stop_hijack(rep);
1523 s->ana_state = STATS_ST_CLOSE;
1524}
1525
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001526
Willy Tarreau10522fd2008-07-09 20:12:41 +02001527static struct cfg_kw_list cfg_kws = {{ },{
1528 { CFG_GLOBAL, "stats", stats_parse_global },
1529 { 0, NULL, NULL },
1530}};
1531
1532__attribute__((constructor))
1533static void __dumpstats_module_init(void)
1534{
1535 cfg_register_keywords(&cfg_kws);
1536}
1537
Willy Tarreau91861262007-10-17 17:06:05 +02001538/*
1539 * Local variables:
1540 * c-indent-level: 8
1541 * c-basic-offset: 8
1542 * End:
1543 */