blob: 215e1490237ed989db07d17b86ff61e48c24429e [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
2 * Functions dedicated to statistics output
3 *
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004 * Copyright 2000-2008 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 Tarreaufbee7132007-10-18 13:53:22 +020044#include <proto/proto_uxst.h>
Willy Tarreau91861262007-10-17 17:06:05 +020045#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020046#include <proto/server.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010047#include <proto/stream_interface.h>
Willy Tarreau91861262007-10-17 17:06:05 +020048
Willy Tarreaufbee7132007-10-18 13:53:22 +020049/* This function parses a "stats" statement in the "global" section. It returns
50 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
51 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
52 * zero included. The trailing '\n' must not be written. The function must be
53 * called with <args> pointing to the first word after "stats".
54 */
Willy Tarreau10522fd2008-07-09 20:12:41 +020055static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
56 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +020057{
Willy Tarreau10522fd2008-07-09 20:12:41 +020058 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +020059 if (!strcmp(args[0], "socket")) {
60 struct sockaddr_un su;
61 int cur_arg;
62
63 if (*args[1] == 0) {
64 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
65 return -1;
66 }
67
68 if (global.stats_sock.state != LI_NEW) {
69 snprintf(err, errlen, "'stats socket' already specified in global section");
70 return -1;
71 }
72
73 su.sun_family = AF_UNIX;
74 strncpy(su.sun_path, args[1], sizeof(su.sun_path));
75 su.sun_path[sizeof(su.sun_path) - 1] = 0;
76 memcpy(&global.stats_sock.addr, &su, sizeof(su)); // guaranteed to fit
77
78 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +010079 global.stats_sock.options = LI_O_NONE;
Willy Tarreaufbee7132007-10-18 13:53:22 +020080 global.stats_sock.accept = uxst_event_accept;
Willy Tarreaub1356cf2008-12-07 16:06:43 +010081 global.stats_sock.handler = uxst_process_session;
82 global.stats_sock.analysers = AN_REQ_UNIX_STATS;
Willy Tarreaufbee7132007-10-18 13:53:22 +020083 global.stats_sock.private = NULL;
84
85 cur_arg = 2;
86 while (*args[cur_arg]) {
87 if (!strcmp(args[cur_arg], "uid")) {
88 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
89 cur_arg += 2;
90 }
91 else if (!strcmp(args[cur_arg], "gid")) {
92 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
93 cur_arg += 2;
94 }
95 else if (!strcmp(args[cur_arg], "mode")) {
96 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
97 cur_arg += 2;
98 }
99 else if (!strcmp(args[cur_arg], "user")) {
100 struct passwd *user;
101 user = getpwnam(args[cur_arg + 1]);
102 if (!user) {
103 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
104 args[cur_arg + 1]);
105 return -1;
106 }
107 global.stats_sock.perm.ux.uid = user->pw_uid;
108 cur_arg += 2;
109 }
110 else if (!strcmp(args[cur_arg], "group")) {
111 struct group *group;
112 group = getgrnam(args[cur_arg + 1]);
113 if (!group) {
114 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
115 args[cur_arg + 1]);
116 return -1;
117 }
118 global.stats_sock.perm.ux.gid = group->gr_gid;
119 cur_arg += 2;
120 }
121 else {
122 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', and 'mode'");
123 return -1;
124 }
125 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100126
Willy Tarreaufbee7132007-10-18 13:53:22 +0200127 uxst_add_listener(&global.stats_sock);
128 global.maxsock++;
129 }
130 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100131 unsigned timeout;
132 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
133
134 if (res) {
135 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
136 return -1;
137 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200138
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100139 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200140 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
141 return -1;
142 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200143 global.stats_timeout = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200144 }
145 else if (!strcmp(args[0], "maxconn")) {
146 int maxconn = atol(args[1]);
147
148 if (maxconn <= 0) {
149 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
150 return -1;
151 }
152 global.maxsock -= global.stats_sock.maxconn;
153 global.stats_sock.maxconn = maxconn;
154 global.maxsock += global.stats_sock.maxconn;
155 }
156 else {
157 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
158 return -1;
159 }
160 return 0;
161}
162
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100163int print_csv_header(struct chunk *msg, int size)
164{
165 return chunk_printf(msg, size,
166 "# pxname,svname,"
167 "qcur,qmax,"
168 "scur,smax,slim,stot,"
169 "bin,bout,"
170 "dreq,dresp,"
171 "ereq,econ,eresp,"
172 "wretr,wredis,"
173 "status,weight,act,bck,"
174 "chkfail,chkdown,lastchg,downtime,qlimit,"
Krzysztof Piotr Oledzkif58a9622008-02-23 01:19:10 +0100175 "pid,iid,sid,throttle,lbtot,tracked,type,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100176 "\n");
177}
178
Willy Tarreau91861262007-10-17 17:06:05 +0200179/*
180 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200181 * client socket shut down on input. It *may* make use of informations from
182 * <uri>. s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200183 * It returns 0 if it had to stop writing data and an I/O is needed, 1 if the
184 * dump is finished and the session must be closed, or -1 in case of any error.
185 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100186int stats_dump_raw(struct session *s, struct uri_auth *uri)
Willy Tarreau3e76e722007-10-17 18:57:38 +0200187{
188 struct buffer *rep = s->rep;
189 struct proxy *px;
190 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +0100191 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200192
193 msg.len = 0;
194 msg.str = trash;
195
196 switch (s->data_state) {
197 case DATA_ST_INIT:
198 /* the function had not been called yet, let's prepare the
199 * buffer for a response.
200 */
Willy Tarreaudded32d2008-11-30 19:48:07 +0100201 stream_int_retnclose(rep->cons, &msg);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200202 s->data_state = DATA_ST_HEAD;
203 /* fall through */
204
205 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100206 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100207 print_csv_header(&msg, sizeof(trash));
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200208 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100209 return 0;
210 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200211
212 s->data_state = DATA_ST_INFO;
213 /* fall through */
214
215 case DATA_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +0100216 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100217 if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100218 chunk_printf(&msg, sizeof(trash),
219 "Name: " PRODUCT_NAME "\n"
220 "Version: " HAPROXY_VERSION "\n"
221 "Release_date: " HAPROXY_DATE "\n"
222 "Nbproc: %d\n"
223 "Process_num: %d\n"
224 "Pid: %d\n"
225 "Uptime: %dd %dh%02dm%02ds\n"
226 "Uptime_sec: %d\n"
227 "Memmax_MB: %d\n"
228 "Ulimit-n: %d\n"
229 "Maxsock: %d\n"
230 "Maxconn: %d\n"
231 "CurrConns: %d\n"
232 "",
233 global.nbproc,
234 relative_pid,
235 pid,
236 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
237 up,
238 global.rlimit_memmax,
239 global.rlimit_nofile,
240 global.maxsock,
241 global.maxconn,
242 actconn
243 );
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200244 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100245 return 0;
246 }
247
Willy Tarreau3e76e722007-10-17 18:57:38 +0200248 s->data_ctx.stats.px = proxy;
249 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100250
251 s->data_ctx.stats.sv = NULL;
252 s->data_ctx.stats.sv_st = 0;
253
Willy Tarreau3e76e722007-10-17 18:57:38 +0200254 s->data_state = DATA_ST_LIST;
255 /* fall through */
256
257 case DATA_ST_LIST:
258 /* dump proxies */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100259 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100260 while (s->data_ctx.stats.px) {
261 px = s->data_ctx.stats.px;
262 /* skip the disabled proxies and non-networked ones */
263 if (px->state != PR_STSTOPPED &&
264 (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100265 if (stats_dump_proxy(s, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100266 return 0;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200267
Willy Tarreaua8efd362008-01-03 10:19:15 +0100268 s->data_ctx.stats.px = px->next;
269 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
270 }
271 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200272 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200273
274 s->data_state = DATA_ST_END;
275 /* fall through */
276
277 case DATA_ST_END:
278 s->data_state = DATA_ST_FIN;
279 return 1;
280
281 case DATA_ST_FIN:
282 return 1;
283
284 default:
285 /* unknown state ! */
286 return -1;
287 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100288}
289
290
291/* This function is called to send output to the response buffer. It simply
292 * calls stats_dump_raw(), and releases the buffer's hijack bit when the dump
293 * is finished. It always returns 0.
294 */
295int stats_dump_raw_to_buffer(struct session *s, struct buffer *req)
296{
297 if (s->ana_state != STATS_ST_REP)
298 return 0;
299
300 if (stats_dump_raw(s, NULL) != 0) {
301 buffer_stop_hijack(s->rep);
302 s->ana_state = STATS_ST_CLOSE;
303 }
304 return 0;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200305}
306
307
308/*
309 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200310 * client socket shut down on input. It stops by itself by unsetting the
Willy Tarreau72b179a2008-08-28 16:01:32 +0200311 * BF_HIJACK flag from the buffer, which it uses to keep on being called
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200312 * when there is free space in the buffer, of simply by letting an empty buffer
313 * upon return.s->data_ctx must have been zeroed before the first call, and the
314 * flags set. It returns 0 if it had to stop writing data and an I/O is needed,
315 * 1 if the dump is finished and the session must be closed, or -1 in case of
316 * any error.
Willy Tarreau91861262007-10-17 17:06:05 +0200317 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100318int stats_dump_http(struct session *s, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200319{
320 struct buffer *rep = s->rep;
321 struct proxy *px;
322 struct chunk msg;
323 unsigned int up;
324
325 msg.len = 0;
326 msg.str = trash;
327
328 switch (s->data_state) {
329 case DATA_ST_INIT:
330 /* the function had not been called yet */
Willy Tarreau72b179a2008-08-28 16:01:32 +0200331 buffer_start_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200332
333 chunk_printf(&msg, sizeof(trash),
334 "HTTP/1.0 200 OK\r\n"
335 "Cache-Control: no-cache\r\n"
336 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200337 "Content-Type: %s\r\n",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100338 (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +0200339
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100340 if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau91861262007-10-17 17:06:05 +0200341 chunk_printf(&msg, sizeof(trash), "Refresh: %d\r\n",
342 uri->refresh);
343
344 chunk_printf(&msg, sizeof(trash), "\r\n");
345
346 s->txn.status = 200;
Willy Tarreaudded32d2008-11-30 19:48:07 +0100347 stream_int_retnclose(rep->cons, &msg); // send the start of the response.
Willy Tarreau91861262007-10-17 17:06:05 +0200348 msg.len = 0;
349
350 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
351 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
352 if (!(s->flags & SN_FINST_MASK))
353 s->flags |= SN_FINST_R;
354
355 if (s->txn.meth == HTTP_METH_HEAD) {
356 /* that's all we return in case of HEAD request */
357 s->data_state = DATA_ST_FIN;
Willy Tarreau72b179a2008-08-28 16:01:32 +0200358 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200359 return 1;
360 }
361
362 s->data_state = DATA_ST_HEAD; /* let's start producing data */
363 /* fall through */
364
365 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100366 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200367 /* WARNING! This must fit in the first buffer !!! */
368 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200369 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
370 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
371 "<style type=\"text/css\"><!--\n"
372 "body {"
373 " font-family: helvetica, arial;"
374 " font-size: 12px;"
375 " font-weight: normal;"
376 " color: black;"
377 " background: white;"
378 "}\n"
379 "th,td {"
380 " font-size: 0.8em;"
381 " align: center;"
382 "}\n"
383 "h1 {"
384 " font-size: xx-large;"
385 " margin-bottom: 0.5em;"
386 "}\n"
387 "h2 {"
388 " font-family: helvetica, arial;"
389 " font-size: x-large;"
390 " font-weight: bold;"
391 " font-style: italic;"
392 " color: #6020a0;"
393 " margin-top: 0em;"
394 " margin-bottom: 0em;"
395 "}\n"
396 "h3 {"
397 " font-family: helvetica, arial;"
398 " font-size: 16px;"
399 " font-weight: bold;"
400 " color: #b00040;"
401 " background: #e8e8d0;"
402 " margin-top: 0em;"
403 " margin-bottom: 0em;"
404 "}\n"
405 "li {"
406 " margin-top: 0.25em;"
407 " margin-right: 2em;"
408 "}\n"
409 ".hr {margin-top: 0.25em;"
410 " border-color: black;"
411 " border-bottom-style: solid;"
412 "}\n"
413 ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n"
414 ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n"
415 ".total {background: #20D0D0;color: #ffff80;}\n"
416 ".frontend {background: #e8e8d0;}\n"
417 ".backend {background: #e8e8d0;}\n"
418 ".active0 {background: #ff9090;}\n"
419 ".active1 {background: #ffd020;}\n"
420 ".active2 {background: #ffffa0;}\n"
421 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100422 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
423 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
424 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200425 ".backup0 {background: #ff9090;}\n"
426 ".backup1 {background: #ff80ff;}\n"
427 ".backup2 {background: #c060ff;}\n"
428 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100429 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
430 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
431 ".backup6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200432 "table.tbl { border-collapse: collapse; border-style: none;}\n"
433 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray;}\n"
434 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
435 "table.tbl th.empty { border-style: none; empty-cells: hide;}\n"
436 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
437 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
438 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
439 "-->\n"
440 "</style></head>\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200441 } else {
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100442 print_csv_header(&msg, sizeof(trash));
Willy Tarreau55bb8452007-10-17 18:44:57 +0200443 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200444 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200445 return 0;
446
447 s->data_state = DATA_ST_INFO;
448 /* fall through */
449
450 case DATA_ST_INFO:
451 up = (now.tv_sec - start_date.tv_sec);
452
453 /* WARNING! this has to fit the first packet too.
454 * We are around 3.5 kB, add adding entries will
455 * become tricky if we want to support 4kB buffers !
456 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100457 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200458 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200459 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
460 PRODUCT_NAME "%s</a></h1>\n"
461 "<h2>Statistics Report for pid %d</h2>\n"
462 "<hr width=\"100%%\" class=\"hr\">\n"
463 "<h3>&gt; General process information</h3>\n"
464 "<table border=0 cols=4><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100465 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200466 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
467 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
468 "<b>maxsock = </b> %d<br>\n"
469 "<b>maxconn = </b> %d (current conns = %d)<br>\n"
470 "</td><td align=\"center\" nowrap>\n"
471 "<table class=\"lgd\"><tr>\n"
472 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
473 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
474 "</tr><tr>\n"
475 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
476 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
477 "</tr><tr>\n"
478 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
479 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
480 "</tr><tr>\n"
481 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100482 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200483 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100484 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +0200485 "</td>"
486 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
487 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
488 "",
489 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Willy Tarreaua8efd362008-01-03 10:19:15 +0100490 pid, pid,
491 relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +0200492 up / 86400, (up % 86400) / 3600,
493 (up % 3600) / 60, (up % 60),
494 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
495 global.rlimit_memmax ? " MB" : "",
496 global.rlimit_nofile,
497 global.maxsock,
498 global.maxconn,
499 actconn
500 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100501
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100502 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200503 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200504 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
505 uri->uri_prefix,
506 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100507 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200508 else
509 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200510 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
511 uri->uri_prefix,
512 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100513 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200514
Willy Tarreau55bb8452007-10-17 18:44:57 +0200515 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100516 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200517 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200518 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
519 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100520 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200521 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200522 else
523 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200524 "<li><a href=\"%s%s%s\">Disable 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 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200528 }
Willy Tarreau91861262007-10-17 17:06:05 +0200529
Willy Tarreau55bb8452007-10-17 18:44:57 +0200530 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200531 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
532 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100533 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
534 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200535
Willy Tarreau55bb8452007-10-17 18:44:57 +0200536 chunk_printf(&msg, sizeof(trash),
Willy Tarreau5031e6a2007-10-18 11:05:48 +0200537 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
538 uri->uri_prefix,
539 (uri->refresh > 0) ? ";norefresh" : "");
540
541 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200542 "</td>"
543 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
544 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
545 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
546 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
547 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
548 "</ul>"
549 "</td>"
550 "</tr></table>\n"
551 ""
552 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100553
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200554 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200555 return 0;
556 }
Willy Tarreau91861262007-10-17 17:06:05 +0200557
Willy Tarreau91861262007-10-17 17:06:05 +0200558 s->data_ctx.stats.px = proxy;
559 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
560 s->data_state = DATA_ST_LIST;
561 /* fall through */
562
563 case DATA_ST_LIST:
564 /* dump proxies */
565 while (s->data_ctx.stats.px) {
566 px = s->data_ctx.stats.px;
567 /* skip the disabled proxies and non-networked ones */
568 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100569 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200570 return 0;
571
572 s->data_ctx.stats.px = px->next;
573 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
574 }
575 /* here, we just have reached the last proxy */
576
577 s->data_state = DATA_ST_END;
578 /* fall through */
579
580 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100581 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200582 chunk_printf(&msg, sizeof(trash), "</body></html>\n");
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200583 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200584 return 0;
585 }
Willy Tarreau91861262007-10-17 17:06:05 +0200586
587 s->data_state = DATA_ST_FIN;
588 /* fall through */
589
590 case DATA_ST_FIN:
Willy Tarreau72b179a2008-08-28 16:01:32 +0200591 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200592 return 1;
593
594 default:
595 /* unknown state ! */
Willy Tarreau72b179a2008-08-28 16:01:32 +0200596 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200597 return -1;
598 }
599}
600
601
602/*
603 * Dumps statistics for a proxy.
604 * Returns 0 if it had to stop dumping data because of lack of buffer space,
605 * ot non-zero if everything completed.
606 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100607int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200608{
609 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100610 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Willy Tarreau91861262007-10-17 17:06:05 +0200611 struct chunk msg;
612
613 msg.len = 0;
614 msg.str = trash;
615
616 switch (s->data_ctx.stats.px_st) {
617 case DATA_ST_PX_INIT:
618 /* we are on a new proxy */
619
620 if (uri && uri->scope) {
621 /* we have a limited scope, we have to check the proxy name */
622 struct stat_scope *scope;
623 int len;
624
625 len = strlen(px->id);
626 scope = uri->scope;
627
628 while (scope) {
629 /* match exact proxy name */
630 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
631 break;
632
633 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +0200634 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +0200635 break;
636 scope = scope->next;
637 }
638
639 /* proxy name not found : don't dump anything */
640 if (scope == NULL)
641 return 1;
642 }
643
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100644 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100645 (px->uuid != s->data_ctx.stats.iid))
646 return 1;
647
Willy Tarreau91861262007-10-17 17:06:05 +0200648 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
649 /* fall through */
650
651 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100652 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200653 /* print a new table */
654 chunk_printf(&msg, sizeof(trash),
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100655 "<table cols=\"26\" class=\"tbl\" width=\"100%%\">\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200656 "<tr align=\"center\" class=\"titre\">"
657 "<th colspan=2 class=\"pxname\">%s</th>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100658 "<th colspan=24 class=\"empty\"></th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200659 "</tr>\n"
660 "<tr align=\"center\" class=\"titre\">"
661 "<th rowspan=2></th>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100662 "<th colspan=3>Queue</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200663 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200664 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100665 "<th colspan=8>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200666 "</tr>\n"
667 "<tr align=\"center\" class=\"titre\">"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200668 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100669 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200670 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200671 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200672 "<th>Status</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100673 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
674 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200675 "</tr>",
Willy Tarreau55bb8452007-10-17 18:44:57 +0200676 px->id);
677
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200678 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200679 return 0;
680 }
Willy Tarreau91861262007-10-17 17:06:05 +0200681
682 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
683 /* fall through */
684
685 case DATA_ST_PX_FE:
686 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100687 if ((px->cap & PR_CAP_FE) &&
688 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
689 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200690 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200691 /* name, queue */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200692 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=3></td>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100693 /* sessions : current, max, limit, total, lbtot */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200694 "<td align=right>%d</td><td align=right>%d</td>"
695 "<td align=right>%d</td><td align=right>%d</td>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100696 "<td align=right></td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200697 /* bytes : in, out */
698 "<td align=right>%lld</td><td align=right>%lld</td>"
699 /* denied: req, resp */
700 "<td align=right>%d</td><td align=right>%d</td>"
701 /* errors : request, connect, response */
702 "<td align=right>%d</td><td align=right></td><td align=right></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200703 /* warnings: retries, redispatches */
704 "<td align=right></td><td align=right></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200705 /* server status : reflect frontend status */
Willy Tarreau91861262007-10-17 17:06:05 +0200706 "<td align=center>%s</td>"
707 /* rest of server: nothing */
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100708 "<td align=center colspan=7></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +0200709 "",
710 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
711 px->bytes_in, px->bytes_out,
712 px->denied_req, px->denied_resp,
713 px->failed_req,
714 px->state == PR_STRUN ? "OPEN" :
715 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200716 } else {
717 chunk_printf(&msg, sizeof(trash),
718 /* pxid, name, queue cur, queue max, */
719 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100720 /* sessions : current, max, limit, total */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200721 "%d,%d,%d,%d,"
722 /* bytes : in, out */
723 "%lld,%lld,"
724 /* denied: req, resp */
725 "%d,%d,"
726 /* errors : request, connect, response */
727 "%d,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200728 /* warnings: retries, redispatches */
729 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200730 /* server status : reflect frontend status */
731 "%s,"
732 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200733 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100734 /* pid, iid, sid, throttle, lbtot, tracked, type */
735 "%d,%d,0,,,,%d,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200736 "\n",
737 px->id,
738 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
739 px->bytes_in, px->bytes_out,
740 px->denied_req, px->denied_resp,
741 px->failed_req,
742 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +0100743 px->state == PR_STIDLE ? "FULL" : "STOP",
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100744 relative_pid, px->uuid, STATS_TYPE_FE);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200745 }
Willy Tarreau91861262007-10-17 17:06:05 +0200746
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200747 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200748 return 0;
749 }
750
751 s->data_ctx.stats.sv = px->srv; /* may be NULL */
752 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
753 /* fall through */
754
755 case DATA_ST_PX_SV:
756 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100757 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
758
Willy Tarreau2ea81932007-11-30 12:04:38 +0100759 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 +0200760
761 sv = s->data_ctx.stats.sv;
762
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100763 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100764 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
765 break;
766
767 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
768 continue;
769 }
770
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100771 if (sv->tracked)
772 svs = sv->tracked;
773 else
774 svs = sv;
775
Willy Tarreau91861262007-10-17 17:06:05 +0200776 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100777 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +0100778 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100779 else if (svs->state & SRV_RUNNING) {
780 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +0200781 sv_state = 3; /* UP */
782 else
783 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +0100784
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100785 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +0100786 sv_state += 2;
787 }
Willy Tarreau91861262007-10-17 17:06:05 +0200788 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100789 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +0200790 sv_state = 1; /* going up */
791 else
792 sv_state = 0; /* DOWN */
793
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100794 if ((sv_state == 0) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +0200795 /* do not report servers which are DOWN */
796 s->data_ctx.stats.sv = sv->next;
797 continue;
798 }
799
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100800 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +0100801 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
802 "UP %d/%d &darr;", "UP",
803 "NOLB %d/%d &darr;", "NOLB",
804 "<i>no check</i>" };
Willy Tarreau55bb8452007-10-17 18:44:57 +0200805 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200806 /* name */
807 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200808 /* queue : current, max, limit */
809 "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100810 /* sessions : current, max, limit, total, lbtot */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200811 "<td align=right>%d</td><td align=right>%d</td>"
812 "<td align=right>%s</td><td align=right>%d</td>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100813 "<td align=right>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200814 /* bytes : in, out */
815 "<td align=right>%lld</td><td align=right>%lld</td>"
816 /* denied: req, resp */
817 "<td align=right></td><td align=right>%d</td>"
818 /* errors : request, connect, response */
819 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200820 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100821 "<td align=right>%u</td><td align=right>%u</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200822 "",
823 (sv->state & SRV_BACKUP) ? "backup" : "active",
824 sv_state, sv->id,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200825 sv->nbpend, sv->nbpend_max, LIM2A0(sv->maxqueue, "-"),
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100826 sv->cur_sess, sv->cur_sess_max, LIM2A1(sv->maxconn, "-"),
827 sv->cum_sess, sv->cum_lbconn,
Willy Tarreau91861262007-10-17 17:06:05 +0200828 sv->bytes_in, sv->bytes_out,
829 sv->failed_secu,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200830 sv->failed_conns, sv->failed_resp,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100831 sv->retries, sv->redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100832
Willy Tarreau55bb8452007-10-17 18:44:57 +0200833 /* status */
834 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200835
836 if (sv->state & SRV_CHECKED)
837 chunk_printf(&msg, sizeof(trash), "%s ",
838 human_time(now.tv_sec - sv->last_change, 1));
839
Willy Tarreau55bb8452007-10-17 18:44:57 +0200840 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200841 srv_hlt_st[sv_state],
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100842 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
843 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Willy Tarreau91861262007-10-17 17:06:05 +0200844
Willy Tarreau55bb8452007-10-17 18:44:57 +0200845 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200846 /* weight */
847 "</td><td>%d</td>"
848 /* act, bck */
849 "<td>%s</td><td>%s</td>"
850 "",
Willy Tarreau5542af62007-12-03 02:04:00 +0100851 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +0200852 (sv->state & SRV_BACKUP) ? "-" : "Y",
853 (sv->state & SRV_BACKUP) ? "Y" : "-");
854
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200855 /* check failures: unique, fatal, down time */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200856 if (sv->state & SRV_CHECKED)
857 chunk_printf(&msg, sizeof(trash),
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200858 "<td align=right>%d</td><td align=right>%d</td>"
859 "<td nowrap align=right>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100860 "",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100861 svs->failed_checks, svs->down_trans,
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200862 human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100863 else if (sv != svs)
864 chunk_printf(&msg, sizeof(trash),
865 "<td nowrap colspan=3>via %s/%s</td>", svs->proxy->id, svs->id );
Willy Tarreau55bb8452007-10-17 18:44:57 +0200866 else
867 chunk_printf(&msg, sizeof(trash),
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100868 "<td colspan=3></td>");
869
870 /* throttle */
871 if ((sv->state & SRV_WARMINGUP) &&
872 now.tv_sec < sv->last_change + sv->slowstart &&
873 now.tv_sec >= sv->last_change) {
874 unsigned int ratio;
875 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
876 chunk_printf(&msg, sizeof(trash),
877 "<td>%d %%</td></tr>\n", ratio);
878 } else {
879 chunk_printf(&msg, sizeof(trash),
880 "<td>-</td></tr>\n");
881 }
Willy Tarreau55bb8452007-10-17 18:44:57 +0200882 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +0100883 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
884 "UP %d/%d,", "UP,",
885 "NOLB %d/%d,", "NOLB,",
886 "no check," };
Willy Tarreau55bb8452007-10-17 18:44:57 +0200887 chunk_printf(&msg, sizeof(trash),
888 /* pxid, name */
889 "%s,%s,"
890 /* queue : current, max */
891 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100892 /* sessions : current, max, limit, total */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200893 "%d,%d,%s,%d,"
894 /* bytes : in, out */
895 "%lld,%lld,"
896 /* denied: req, resp */
897 ",%d,"
898 /* errors : request, connect, response */
899 ",%d,%d,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200900 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100901 "%u,%u,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200902 "",
903 px->id, sv->id,
904 sv->nbpend, sv->nbpend_max,
Willy Tarreaudcd47712007-11-04 23:35:08 +0100905 sv->cur_sess, sv->cur_sess_max, LIM2A0(sv->maxconn, ""), sv->cum_sess,
Willy Tarreau55bb8452007-10-17 18:44:57 +0200906 sv->bytes_in, sv->bytes_out,
907 sv->failed_secu,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200908 sv->failed_conns, sv->failed_resp,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100909 sv->retries, sv->redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100910
Willy Tarreau55bb8452007-10-17 18:44:57 +0200911 /* status */
912 chunk_printf(&msg, sizeof(trash),
913 srv_hlt_st[sv_state],
914 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
915 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
Willy Tarreau91861262007-10-17 17:06:05 +0200916
Willy Tarreau55bb8452007-10-17 18:44:57 +0200917 chunk_printf(&msg, sizeof(trash),
918 /* weight, active, backup */
919 "%d,%d,%d,"
920 "",
Willy Tarreau5542af62007-12-03 02:04:00 +0100921 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +0200922 (sv->state & SRV_BACKUP) ? 0 : 1,
923 (sv->state & SRV_BACKUP) ? 1 : 0);
924
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200925 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200926 if (sv->state & SRV_CHECKED)
927 chunk_printf(&msg, sizeof(trash),
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200928 "%d,%d,%d,%d,",
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200929 sv->failed_checks, sv->down_trans,
930 now.tv_sec - sv->last_change, srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +0200931 else
932 chunk_printf(&msg, sizeof(trash),
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200933 ",,,,");
934
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100935 /* queue limit, pid, iid, sid, */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200936 chunk_printf(&msg, sizeof(trash),
Willy Tarreaudcd47712007-11-04 23:35:08 +0100937 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100938 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +0100939 LIM2A0(sv->maxqueue, ""),
940 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100941
942 /* throttle */
943 if ((sv->state & SRV_WARMINGUP) &&
944 now.tv_sec < sv->last_change + sv->slowstart &&
945 now.tv_sec >= sv->last_change) {
946 unsigned int ratio;
947 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100948 chunk_printf(&msg, sizeof(trash), "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100949 }
950
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100951 /* sessions: lbtot */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100952 chunk_printf(&msg, sizeof(trash), ",%d,", sv->cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100953
954 /* tracked */
955 if (sv->tracked)
Krzysztof Piotr Oledzkif58a9622008-02-23 01:19:10 +0100956 chunk_printf(&msg, sizeof(trash), "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100957 sv->tracked->proxy->id, sv->tracked->id);
958 else
959 chunk_printf(&msg, sizeof(trash), ",");
960
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100961 /* type, then EOL */
962 chunk_printf(&msg, sizeof(trash), "%d,\n", STATS_TYPE_SV);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200963 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200964 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200965 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100966 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +0200967
968 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
969 /* fall through */
970
971 case DATA_ST_PX_BE:
972 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100973 if ((px->cap & PR_CAP_BE) &&
974 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
975 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200976 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200977 /* name */
978 "<tr align=center class=\"backend\"><td>Backend</td>"
979 /* queue : current, max */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200980 "<td align=right>%d</td><td align=right>%d</td><td></td>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100981 /* sessions : current, max, limit, total, lbtot */
982 "<td align=right>%d</td><td align=right>%d</td>"
983 "<td align=right>%d</td><td align=right>%d</td>"
984 "<td align=right>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200985 /* bytes : in, out */
986 "<td align=right>%lld</td><td align=right>%lld</td>"
987 /* denied: req, resp */
988 "<td align=right>%d</td><td align=right>%d</td>"
989 /* errors : request, connect, response */
990 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200991 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100992 "<td align=right>%u</td><td align=right>%u</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200993 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +0200994 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200995 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +0200996 * active and backups. */
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200997 "<td align=center nowrap>%s %s</td><td align=center>%d</td>"
998 "<td align=center>%d</td><td align=center>%d</td>",
Willy Tarreau91861262007-10-17 17:06:05 +0200999 px->nbpend /* or px->totpend ? */, px->nbpend_max,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001000 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn, px->cum_lbconn,
Willy Tarreau91861262007-10-17 17:06:05 +02001001 px->bytes_in, px->bytes_out,
1002 px->denied_req, px->denied_resp,
1003 px->failed_conns, px->failed_resp,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001004 px->retries, px->redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001005 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01001006 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
1007 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01001008 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001009 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001010
1011 chunk_printf(&msg, sizeof(trash),
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001012 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001013 "<td align=center>&nbsp;</td><td align=\"right\">%d</td>"
1014 "<td align=\"right\" nowrap>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001015 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001016 "</tr>",
1017 px->down_trans,
1018 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001019 } else {
1020 chunk_printf(&msg, sizeof(trash),
1021 /* pxid, name */
1022 "%s,BACKEND,"
1023 /* queue : current, max */
1024 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001025 /* sessions : current, max, limit, total */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001026 "%d,%d,%d,%d,"
1027 /* bytes : in, out */
1028 "%lld,%lld,"
1029 /* denied: req, resp */
1030 "%d,%d,"
1031 /* errors : request, connect, response */
1032 ",%d,%d,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001033 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001034 "%u,%u,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001035 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02001036 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001037 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02001038 * active and backups. */
1039 "%s,"
1040 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001041 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001042 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001043 /* pid, iid, sid, throttle, lbtot, tracked, type */
1044 "%d,%d,0,,%d,,%d,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001045 "\n",
1046 px->id,
1047 px->nbpend /* or px->totpend ? */, px->nbpend_max,
1048 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
1049 px->bytes_in, px->bytes_out,
1050 px->denied_req, px->denied_resp,
1051 px->failed_conns, px->failed_resp,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001052 px->retries, px->redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01001053 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01001054 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001055 px->srv_act, px->srv_bck,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001056 px->down_trans, now.tv_sec - px->last_change,
Willy Tarreau20697042007-11-15 23:26:18 +01001057 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001058 relative_pid, px->uuid,
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001059 px->cum_lbconn, STATS_TYPE_BE);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001060 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001061 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001062 return 0;
1063 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001064
Willy Tarreau91861262007-10-17 17:06:05 +02001065 s->data_ctx.stats.px_st = DATA_ST_PX_END;
1066 /* fall through */
1067
1068 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001069 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001070 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001071
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001072 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001073 return 0;
1074 }
Willy Tarreau91861262007-10-17 17:06:05 +02001075
1076 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
1077 /* fall through */
1078
1079 case DATA_ST_PX_FIN:
1080 return 1;
1081
1082 default:
1083 /* unknown state, we should put an abort() here ! */
1084 return 1;
1085 }
1086}
1087
Willy Tarreau10522fd2008-07-09 20:12:41 +02001088static struct cfg_kw_list cfg_kws = {{ },{
1089 { CFG_GLOBAL, "stats", stats_parse_global },
1090 { 0, NULL, NULL },
1091}};
1092
1093__attribute__((constructor))
1094static void __dumpstats_module_init(void)
1095{
1096 cfg_register_keywords(&cfg_kws);
1097}
1098
Willy Tarreau91861262007-10-17 17:06:05 +02001099/*
1100 * Local variables:
1101 * c-indent-level: 8
1102 * c-basic-offset: 8
1103 * End:
1104 */