blob: 4d34c474a11f58d80580ad9fa5b86d25438c3ef1 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
2 * Functions dedicated to statistics output
3 *
4 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
5 *
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
26#include <common/compat.h>
27#include <common/config.h>
28#include <common/debug.h>
29#include <common/memory.h>
30#include <common/mini-clist.h>
31#include <common/standard.h>
32#include <common/time.h>
33#include <common/uri_auth.h>
34#include <common/version.h>
35
36#include <types/client.h>
37#include <types/global.h>
38#include <types/polling.h>
39#include <types/proxy.h>
40#include <types/server.h>
41
42#include <proto/backend.h>
43#include <proto/buffers.h>
44#include <proto/dumpstats.h>
45#include <proto/fd.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020046#include <proto/proto_uxst.h>
Willy Tarreau91861262007-10-17 17:06:05 +020047#include <proto/senddata.h>
48#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020049#include <proto/server.h>
Willy Tarreau91861262007-10-17 17:06:05 +020050
Willy Tarreaufbee7132007-10-18 13:53:22 +020051/* This function parses a "stats" statement in the "global" section. It returns
52 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
53 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
54 * zero included. The trailing '\n' must not be written. The function must be
55 * called with <args> pointing to the first word after "stats".
56 */
57int stats_parse_global(const char **args, char *err, int errlen)
58{
59 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;
79 global.stats_sock.accept = uxst_event_accept;
80 global.stats_sock.handler = process_uxst_stats;
81 global.stats_sock.private = NULL;
82
83 cur_arg = 2;
84 while (*args[cur_arg]) {
85 if (!strcmp(args[cur_arg], "uid")) {
86 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
87 cur_arg += 2;
88 }
89 else if (!strcmp(args[cur_arg], "gid")) {
90 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
91 cur_arg += 2;
92 }
93 else if (!strcmp(args[cur_arg], "mode")) {
94 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
95 cur_arg += 2;
96 }
97 else if (!strcmp(args[cur_arg], "user")) {
98 struct passwd *user;
99 user = getpwnam(args[cur_arg + 1]);
100 if (!user) {
101 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
102 args[cur_arg + 1]);
103 return -1;
104 }
105 global.stats_sock.perm.ux.uid = user->pw_uid;
106 cur_arg += 2;
107 }
108 else if (!strcmp(args[cur_arg], "group")) {
109 struct group *group;
110 group = getgrnam(args[cur_arg + 1]);
111 if (!group) {
112 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
113 args[cur_arg + 1]);
114 return -1;
115 }
116 global.stats_sock.perm.ux.gid = group->gr_gid;
117 cur_arg += 2;
118 }
119 else {
120 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', and 'mode'");
121 return -1;
122 }
123 }
124
125 uxst_add_listener(&global.stats_sock);
126 global.maxsock++;
127 }
128 else if (!strcmp(args[0], "timeout")) {
129 int timeout = atol(args[1]);
130
131 if (timeout <= 0) {
132 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
133 return -1;
134 }
135 __tv_from_ms(&global.stats_timeout, timeout);
136 }
137 else if (!strcmp(args[0], "maxconn")) {
138 int maxconn = atol(args[1]);
139
140 if (maxconn <= 0) {
141 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
142 return -1;
143 }
144 global.maxsock -= global.stats_sock.maxconn;
145 global.stats_sock.maxconn = maxconn;
146 global.maxsock += global.stats_sock.maxconn;
147 }
148 else {
149 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
150 return -1;
151 }
152 return 0;
153}
154
Willy Tarreau91861262007-10-17 17:06:05 +0200155/*
156 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau3e76e722007-10-17 18:57:38 +0200157 * s->cli_state == CL_STSHUTR. It *may* make use of informations from <uri>
158 * and <flags>.
159 * It returns 0 if it had to stop writing data and an I/O is needed, 1 if the
160 * dump is finished and the session must be closed, or -1 in case of any error.
161 */
162int stats_dump_raw(struct session *s, struct uri_auth *uri, int flags)
163{
164 struct buffer *rep = s->rep;
165 struct proxy *px;
166 struct chunk msg;
167
168 msg.len = 0;
169 msg.str = trash;
170
171 switch (s->data_state) {
172 case DATA_ST_INIT:
173 /* the function had not been called yet, let's prepare the
174 * buffer for a response.
175 */
176 client_retnclose(s, &msg);
177 s->data_state = DATA_ST_HEAD;
178 /* fall through */
179
180 case DATA_ST_HEAD:
181 chunk_printf(&msg, sizeof(trash),
182 "# pxname,svname,"
183 "qcur,qmax,"
184 "scur,smax,slim,stot,"
185 "bin,bout,"
186 "dreq,dresp,"
187 "ereq,econ,eresp,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200188 "wretr,wredis,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200189 "status,weight,act,bck,"
190 "chkfail,chkdown,lastchg,downtime,"
Willy Tarreau3e76e722007-10-17 18:57:38 +0200191 "\n");
192
193 if (buffer_write_chunk(rep, &msg) != 0)
194 return 0;
195
196 s->data_state = DATA_ST_INFO;
197 /* fall through */
198
199 case DATA_ST_INFO:
200 memset(&s->data_ctx, 0, sizeof(s->data_ctx));
201
202 s->data_ctx.stats.px = proxy;
203 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
204 s->data_state = DATA_ST_LIST;
205 /* fall through */
206
207 case DATA_ST_LIST:
208 /* dump proxies */
209 while (s->data_ctx.stats.px) {
210 px = s->data_ctx.stats.px;
211 /* skip the disabled proxies and non-networked ones */
212 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
213 if (stats_dump_proxy(s, px, NULL, 0) == 0)
214 return 0;
215
216 s->data_ctx.stats.px = px->next;
217 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
218 }
219 /* here, we just have reached the last proxy */
220
221 s->data_state = DATA_ST_END;
222 /* fall through */
223
224 case DATA_ST_END:
225 s->data_state = DATA_ST_FIN;
226 return 1;
227
228 case DATA_ST_FIN:
229 return 1;
230
231 default:
232 /* unknown state ! */
233 return -1;
234 }
235}
236
237
238/*
239 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau91861262007-10-17 17:06:05 +0200240 * s->cli_state == CL_STSHUTR. It stops by itself by unsetting the SN_SELF_GEN
241 * flag from the session, which it uses to keep on being called when there is
242 * free space in the buffer, of simply by letting an empty buffer upon return.
243 * It returns 0 if it had to stop writing data and an I/O is needed, 1 if the
244 * dump is finished and the session must be closed, or -1 in case of any error.
245 */
246int stats_dump_http(struct session *s, struct uri_auth *uri, int flags)
247{
248 struct buffer *rep = s->rep;
249 struct proxy *px;
250 struct chunk msg;
251 unsigned int up;
252
253 msg.len = 0;
254 msg.str = trash;
255
256 switch (s->data_state) {
257 case DATA_ST_INIT:
258 /* the function had not been called yet */
259 s->flags |= SN_SELF_GEN; // more data will follow
260
261 chunk_printf(&msg, sizeof(trash),
262 "HTTP/1.0 200 OK\r\n"
263 "Cache-Control: no-cache\r\n"
264 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200265 "Content-Type: %s\r\n",
266 (flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau91861262007-10-17 17:06:05 +0200267
268 if (uri->refresh > 0 && !(s->flags & SN_STAT_NORFRSH))
269 chunk_printf(&msg, sizeof(trash), "Refresh: %d\r\n",
270 uri->refresh);
271
272 chunk_printf(&msg, sizeof(trash), "\r\n");
273
274 s->txn.status = 200;
275 client_retnclose(s, &msg); // send the start of the response.
276 msg.len = 0;
277
278 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
279 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
280 if (!(s->flags & SN_FINST_MASK))
281 s->flags |= SN_FINST_R;
282
283 if (s->txn.meth == HTTP_METH_HEAD) {
284 /* that's all we return in case of HEAD request */
285 s->data_state = DATA_ST_FIN;
286 s->flags &= ~SN_SELF_GEN;
287 return 1;
288 }
289
290 s->data_state = DATA_ST_HEAD; /* let's start producing data */
291 /* fall through */
292
293 case DATA_ST_HEAD:
Willy Tarreau55bb8452007-10-17 18:44:57 +0200294 if (flags & STAT_FMT_HTML) {
295 /* WARNING! This must fit in the first buffer !!! */
296 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200297 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
298 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
299 "<style type=\"text/css\"><!--\n"
300 "body {"
301 " font-family: helvetica, arial;"
302 " font-size: 12px;"
303 " font-weight: normal;"
304 " color: black;"
305 " background: white;"
306 "}\n"
307 "th,td {"
308 " font-size: 0.8em;"
309 " align: center;"
310 "}\n"
311 "h1 {"
312 " font-size: xx-large;"
313 " margin-bottom: 0.5em;"
314 "}\n"
315 "h2 {"
316 " font-family: helvetica, arial;"
317 " font-size: x-large;"
318 " font-weight: bold;"
319 " font-style: italic;"
320 " color: #6020a0;"
321 " margin-top: 0em;"
322 " margin-bottom: 0em;"
323 "}\n"
324 "h3 {"
325 " font-family: helvetica, arial;"
326 " font-size: 16px;"
327 " font-weight: bold;"
328 " color: #b00040;"
329 " background: #e8e8d0;"
330 " margin-top: 0em;"
331 " margin-bottom: 0em;"
332 "}\n"
333 "li {"
334 " margin-top: 0.25em;"
335 " margin-right: 2em;"
336 "}\n"
337 ".hr {margin-top: 0.25em;"
338 " border-color: black;"
339 " border-bottom-style: solid;"
340 "}\n"
341 ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n"
342 ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n"
343 ".total {background: #20D0D0;color: #ffff80;}\n"
344 ".frontend {background: #e8e8d0;}\n"
345 ".backend {background: #e8e8d0;}\n"
346 ".active0 {background: #ff9090;}\n"
347 ".active1 {background: #ffd020;}\n"
348 ".active2 {background: #ffffa0;}\n"
349 ".active3 {background: #c0ffc0;}\n"
350 ".active4 {background: #e0e0e0;}\n"
351 ".backup0 {background: #ff9090;}\n"
352 ".backup1 {background: #ff80ff;}\n"
353 ".backup2 {background: #c060ff;}\n"
354 ".backup3 {background: #b0d0ff;}\n"
355 ".backup4 {background: #e0e0e0;}\n"
356 "table.tbl { border-collapse: collapse; border-style: none;}\n"
357 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray;}\n"
358 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
359 "table.tbl th.empty { border-style: none; empty-cells: hide;}\n"
360 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
361 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
362 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
363 "-->\n"
364 "</style></head>\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200365 } else {
366 chunk_printf(&msg, sizeof(trash),
367 "# pxname,svname,"
368 "qcur,qmax,"
369 "scur,smax,slim,stot,"
370 "bin,bout,"
371 "dreq,dresp,"
372 "ereq,econ,eresp,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200373 "wretr,wredis,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200374 "status,weight,act,bck,"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200375 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200376 "\n");
377 }
Willy Tarreau91861262007-10-17 17:06:05 +0200378 if (buffer_write_chunk(rep, &msg) != 0)
379 return 0;
380
381 s->data_state = DATA_ST_INFO;
382 /* fall through */
383
384 case DATA_ST_INFO:
385 up = (now.tv_sec - start_date.tv_sec);
386
387 /* WARNING! this has to fit the first packet too.
388 * We are around 3.5 kB, add adding entries will
389 * become tricky if we want to support 4kB buffers !
390 */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200391 if (flags & STAT_FMT_HTML) {
392 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200393 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
394 PRODUCT_NAME "%s</a></h1>\n"
395 "<h2>Statistics Report for pid %d</h2>\n"
396 "<hr width=\"100%%\" class=\"hr\">\n"
397 "<h3>&gt; General process information</h3>\n"
398 "<table border=0 cols=4><tr><td align=\"left\" nowrap width=\"1%%\">\n"
399 "<p><b>pid = </b> %d (nbproc = %d)<br>\n"
400 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
401 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
402 "<b>maxsock = </b> %d<br>\n"
403 "<b>maxconn = </b> %d (current conns = %d)<br>\n"
404 "</td><td align=\"center\" nowrap>\n"
405 "<table class=\"lgd\"><tr>\n"
406 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
407 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
408 "</tr><tr>\n"
409 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
410 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
411 "</tr><tr>\n"
412 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
413 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
414 "</tr><tr>\n"
415 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
416 "<td class=\"active4\"></td><td class=\"noborder\">not checked </td>"
417 "</tr></table>\n"
418 "</td>"
419 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
420 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
421 "",
422 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
423 pid, pid, global.nbproc,
424 up / 86400, (up % 86400) / 3600,
425 (up % 3600) / 60, (up % 60),
426 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
427 global.rlimit_memmax ? " MB" : "",
428 global.rlimit_nofile,
429 global.maxsock,
430 global.maxconn,
431 actconn
432 );
433
Willy Tarreau55bb8452007-10-17 18:44:57 +0200434 if (s->flags & SN_STAT_HIDEDWN)
435 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200436 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
437 uri->uri_prefix,
438 "",
439 (s->flags & SN_STAT_NORFRSH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200440 else
441 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200442 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
443 uri->uri_prefix,
444 ";up",
445 (s->flags & SN_STAT_NORFRSH) ? ";norefresh" : "");
446
Willy Tarreau55bb8452007-10-17 18:44:57 +0200447 if (uri->refresh > 0) {
448 if (s->flags & SN_STAT_NORFRSH)
449 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200450 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
451 uri->uri_prefix,
452 (s->flags & SN_STAT_HIDEDWN) ? ";up" : "",
453 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200454 else
455 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200456 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
457 uri->uri_prefix,
458 (s->flags & SN_STAT_HIDEDWN) ? ";up" : "",
459 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200460 }
Willy Tarreau91861262007-10-17 17:06:05 +0200461
Willy Tarreau55bb8452007-10-17 18:44:57 +0200462 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200463 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
464 uri->uri_prefix,
465 (s->flags & SN_STAT_HIDEDWN) ? ";up" : "",
466 (s->flags & SN_STAT_NORFRSH) ? ";norefresh" : "");
467
Willy Tarreau55bb8452007-10-17 18:44:57 +0200468 chunk_printf(&msg, sizeof(trash),
Willy Tarreau5031e6a2007-10-18 11:05:48 +0200469 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
470 uri->uri_prefix,
471 (uri->refresh > 0) ? ";norefresh" : "");
472
473 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200474 "</td>"
475 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
476 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
477 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
478 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
479 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
480 "</ul>"
481 "</td>"
482 "</tr></table>\n"
483 ""
484 );
485
Willy Tarreau55bb8452007-10-17 18:44:57 +0200486 if (buffer_write_chunk(rep, &msg) != 0)
487 return 0;
488 }
Willy Tarreau91861262007-10-17 17:06:05 +0200489
490 memset(&s->data_ctx, 0, sizeof(s->data_ctx));
491
492 s->data_ctx.stats.px = proxy;
493 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
494 s->data_state = DATA_ST_LIST;
495 /* fall through */
496
497 case DATA_ST_LIST:
498 /* dump proxies */
499 while (s->data_ctx.stats.px) {
500 px = s->data_ctx.stats.px;
501 /* skip the disabled proxies and non-networked ones */
502 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
503 if (stats_dump_proxy(s, px, uri, flags) == 0)
504 return 0;
505
506 s->data_ctx.stats.px = px->next;
507 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
508 }
509 /* here, we just have reached the last proxy */
510
511 s->data_state = DATA_ST_END;
512 /* fall through */
513
514 case DATA_ST_END:
Willy Tarreau55bb8452007-10-17 18:44:57 +0200515 if (flags & STAT_FMT_HTML) {
516 chunk_printf(&msg, sizeof(trash), "</body></html>\n");
517 if (buffer_write_chunk(rep, &msg) != 0)
518 return 0;
519 }
Willy Tarreau91861262007-10-17 17:06:05 +0200520
521 s->data_state = DATA_ST_FIN;
522 /* fall through */
523
524 case DATA_ST_FIN:
525 s->flags &= ~SN_SELF_GEN;
526 return 1;
527
528 default:
529 /* unknown state ! */
530 s->flags &= ~SN_SELF_GEN;
531 return -1;
532 }
533}
534
535
536/*
537 * Dumps statistics for a proxy.
538 * Returns 0 if it had to stop dumping data because of lack of buffer space,
539 * ot non-zero if everything completed.
540 */
541int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri, int flags)
542{
543 struct buffer *rep = s->rep;
544 struct server *sv;
545 struct chunk msg;
546
547 msg.len = 0;
548 msg.str = trash;
549
550 switch (s->data_ctx.stats.px_st) {
551 case DATA_ST_PX_INIT:
552 /* we are on a new proxy */
553
554 if (uri && uri->scope) {
555 /* we have a limited scope, we have to check the proxy name */
556 struct stat_scope *scope;
557 int len;
558
559 len = strlen(px->id);
560 scope = uri->scope;
561
562 while (scope) {
563 /* match exact proxy name */
564 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
565 break;
566
567 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +0200568 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +0200569 break;
570 scope = scope->next;
571 }
572
573 /* proxy name not found : don't dump anything */
574 if (scope == NULL)
575 return 1;
576 }
577
578 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
579 /* fall through */
580
581 case DATA_ST_PX_TH:
Willy Tarreau55bb8452007-10-17 18:44:57 +0200582 if (flags & STAT_FMT_HTML) {
583 /* print a new table */
584 chunk_printf(&msg, sizeof(trash),
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200585 "<table cols=\"24\" class=\"tbl\" width=\"100%%\">\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200586 "<tr align=\"center\" class=\"titre\">"
587 "<th colspan=2 class=\"pxname\">%s</th>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200588 "<th colspan=21 class=\"empty\"></th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200589 "</tr>\n"
590 "<tr align=\"center\" class=\"titre\">"
591 "<th rowspan=2></th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200592 "<th colspan=3>Queue</th><th colspan=4>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200593 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200594 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200595 "<th colspan=7>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200596 "</tr>\n"
597 "<tr align=\"center\" class=\"titre\">"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200598 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200599 "<th>Limit</th><th>Cumul</th><th>In</th><th>Out</th>"
600 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200601 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200602 "<th>Status</th><th>Wght</th><th>Act</th>"
603 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>\n"
604 "</tr>",
Willy Tarreau55bb8452007-10-17 18:44:57 +0200605 px->id);
606
607 if (buffer_write_chunk(rep, &msg) != 0)
608 return 0;
609 }
Willy Tarreau91861262007-10-17 17:06:05 +0200610
611 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
612 /* fall through */
613
614 case DATA_ST_PX_FE:
615 /* print the frontend */
616 if (px->cap & PR_CAP_FE) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200617 if (flags & STAT_FMT_HTML) {
618 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200619 /* name, queue */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200620 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=3></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200621 /* sessions : current, max, limit, cumul */
622 "<td align=right>%d</td><td align=right>%d</td>"
623 "<td align=right>%d</td><td align=right>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200624 /* bytes : in, out */
625 "<td align=right>%lld</td><td align=right>%lld</td>"
626 /* denied: req, resp */
627 "<td align=right>%d</td><td align=right>%d</td>"
628 /* errors : request, connect, response */
629 "<td align=right>%d</td><td align=right></td><td align=right></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200630 /* warnings: retries, redispatches */
631 "<td align=right></td><td align=right></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200632 /* server status : reflect frontend status */
Willy Tarreau91861262007-10-17 17:06:05 +0200633 "<td align=center>%s</td>"
634 /* rest of server: nothing */
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200635 "<td align=center colspan=6></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +0200636 "",
637 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
638 px->bytes_in, px->bytes_out,
639 px->denied_req, px->denied_resp,
640 px->failed_req,
641 px->state == PR_STRUN ? "OPEN" :
642 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200643 } else {
644 chunk_printf(&msg, sizeof(trash),
645 /* pxid, name, queue cur, queue max, */
646 "%s,FRONTEND,,,"
647 /* sessions : current, max, limit, cumul */
648 "%d,%d,%d,%d,"
649 /* bytes : in, out */
650 "%lld,%lld,"
651 /* denied: req, resp */
652 "%d,%d,"
653 /* errors : request, connect, response */
654 "%d,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200655 /* warnings: retries, redispatches */
656 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200657 /* server status : reflect frontend status */
658 "%s,"
659 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200660 ",,,,,,,,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200661 "\n",
662 px->id,
663 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
664 px->bytes_in, px->bytes_out,
665 px->denied_req, px->denied_resp,
666 px->failed_req,
667 px->state == PR_STRUN ? "OPEN" :
668 px->state == PR_STIDLE ? "FULL" : "STOP");
669 }
Willy Tarreau91861262007-10-17 17:06:05 +0200670
671 if (buffer_write_chunk(rep, &msg) != 0)
672 return 0;
673 }
674
675 s->data_ctx.stats.sv = px->srv; /* may be NULL */
676 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
677 /* fall through */
678
679 case DATA_ST_PX_SV:
680 /* stats.sv has been initialized above */
681 while (s->data_ctx.stats.sv != NULL) {
Willy Tarreau91861262007-10-17 17:06:05 +0200682 int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP, 4=unchecked */
683
684 sv = s->data_ctx.stats.sv;
685
686 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
687 if (!(sv->state & SRV_CHECKED))
688 sv_state = 4;
689 else if (sv->state & SRV_RUNNING)
690 if (sv->health == sv->rise + sv->fall - 1)
691 sv_state = 3; /* UP */
692 else
693 sv_state = 2; /* going down */
694 else
695 if (sv->health)
696 sv_state = 1; /* going up */
697 else
698 sv_state = 0; /* DOWN */
699
700 if ((sv_state == 0) && (s->flags & SN_STAT_HIDEDWN)) {
701 /* do not report servers which are DOWN */
702 s->data_ctx.stats.sv = sv->next;
703 continue;
704 }
705
Willy Tarreau55bb8452007-10-17 18:44:57 +0200706 if (flags & STAT_FMT_HTML) {
707 static char *srv_hlt_st[5] = { "DOWN", "DN %d/%d &uarr;", "UP %d/%d &darr;",
708 "UP", "<i>no check</i>" };
709 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200710 /* name */
711 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200712 /* queue : current, max, limit */
713 "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200714 /* sessions : current, max, limit, cumul */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200715 "<td align=right>%d</td><td align=right>%d</td>"
716 "<td align=right>%s</td><td align=right>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200717 /* bytes : in, out */
718 "<td align=right>%lld</td><td align=right>%lld</td>"
719 /* denied: req, resp */
720 "<td align=right></td><td align=right>%d</td>"
721 /* errors : request, connect, response */
722 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200723 /* warnings: retries, redispatches */
724 "<td align=right>%d</td><td align=right></td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200725 "",
726 (sv->state & SRV_BACKUP) ? "backup" : "active",
727 sv_state, sv->id,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200728 sv->nbpend, sv->nbpend_max, LIM2A0(sv->maxqueue, "-"),
729 sv->cur_sess, sv->cur_sess_max, LIM2A1(sv->maxconn, "-"), sv->cum_sess,
Willy Tarreau91861262007-10-17 17:06:05 +0200730 sv->bytes_in, sv->bytes_out,
731 sv->failed_secu,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200732 sv->failed_conns, sv->failed_resp,
733 sv->retries);
Willy Tarreau91861262007-10-17 17:06:05 +0200734
Willy Tarreau55bb8452007-10-17 18:44:57 +0200735 /* status */
736 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200737
738 if (sv->state & SRV_CHECKED)
739 chunk_printf(&msg, sizeof(trash), "%s ",
740 human_time(now.tv_sec - sv->last_change, 1));
741
Willy Tarreau55bb8452007-10-17 18:44:57 +0200742 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200743 srv_hlt_st[sv_state],
744 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
745 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
746
Willy Tarreau55bb8452007-10-17 18:44:57 +0200747 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200748 /* weight */
749 "</td><td>%d</td>"
750 /* act, bck */
751 "<td>%s</td><td>%s</td>"
752 "",
753 sv->uweight,
754 (sv->state & SRV_BACKUP) ? "-" : "Y",
755 (sv->state & SRV_BACKUP) ? "Y" : "-");
756
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200757 /* check failures: unique, fatal, down time */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200758 if (sv->state & SRV_CHECKED)
759 chunk_printf(&msg, sizeof(trash),
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200760 "<td align=right>%d</td><td align=right>%d</td>"
761 "<td nowrap align=right>%s</td>"
762 "</tr>\n",
763 sv->failed_checks, sv->down_trans,
764 human_time(srv_downtime(sv), 1));
Willy Tarreau55bb8452007-10-17 18:44:57 +0200765 else
766 chunk_printf(&msg, sizeof(trash),
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200767 "<td colspan=3></td></tr>\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200768 } else {
769 static char *srv_hlt_st[5] = { "DOWN,", "DOWN %d/%d,", "UP %d/%d,",
770 "UP,", "no check," };
771 chunk_printf(&msg, sizeof(trash),
772 /* pxid, name */
773 "%s,%s,"
774 /* queue : current, max */
775 "%d,%d,"
776 /* sessions : current, max, limit, cumul */
777 "%d,%d,%s,%d,"
778 /* bytes : in, out */
779 "%lld,%lld,"
780 /* denied: req, resp */
781 ",%d,"
782 /* errors : request, connect, response */
783 ",%d,%d,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200784 /* warnings: retries, redispatches */
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200785 "%d,,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200786 "",
787 px->id, sv->id,
788 sv->nbpend, sv->nbpend_max,
789 sv->cur_sess, sv->cur_sess_max, sv->maxconn ? ultoa(sv->maxconn) : "-", sv->cum_sess,
790 sv->bytes_in, sv->bytes_out,
791 sv->failed_secu,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200792 sv->failed_conns, sv->failed_resp,
793 sv->retries);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200794
795 /* status */
796 chunk_printf(&msg, sizeof(trash),
797 srv_hlt_st[sv_state],
798 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
799 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
Willy Tarreau91861262007-10-17 17:06:05 +0200800
Willy Tarreau55bb8452007-10-17 18:44:57 +0200801 chunk_printf(&msg, sizeof(trash),
802 /* weight, active, backup */
803 "%d,%d,%d,"
804 "",
805 sv->uweight,
806 (sv->state & SRV_BACKUP) ? 0 : 1,
807 (sv->state & SRV_BACKUP) ? 1 : 0);
808
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200809 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200810 if (sv->state & SRV_CHECKED)
811 chunk_printf(&msg, sizeof(trash),
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200812 "%d,%d,%d,%d,",
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200813 sv->failed_checks, sv->down_trans,
814 now.tv_sec - sv->last_change, srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +0200815 else
816 chunk_printf(&msg, sizeof(trash),
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200817 ",,,,");
818
819 /* queue limit and EOL */
820 chunk_printf(&msg, sizeof(trash),
821 "%s,\n",
822 LIM2A0(sv->maxqueue, ""));
Willy Tarreau55bb8452007-10-17 18:44:57 +0200823 }
Willy Tarreau91861262007-10-17 17:06:05 +0200824 if (buffer_write_chunk(rep, &msg) != 0)
825 return 0;
826
827 s->data_ctx.stats.sv = sv->next;
828 } /* while sv */
829
830 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
831 /* fall through */
832
833 case DATA_ST_PX_BE:
834 /* print the backend */
835 if (px->cap & PR_CAP_BE) {
836 int gcd = 1;
837
838 if (px->map_state & PR_MAP_RECALC)
839 recalc_server_map(px);
840
841 /* The GCD which was computed causes the total effective
842 * weight to appear lower than all weights. Let's
843 * recompute it.
844 */
845 if (px->srv && px->srv->eweight)
846 gcd = px->srv->uweight / px->srv->eweight;
847
Willy Tarreau55bb8452007-10-17 18:44:57 +0200848 if (flags & STAT_FMT_HTML) {
849 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200850 /* name */
851 "<tr align=center class=\"backend\"><td>Backend</td>"
852 /* queue : current, max */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200853 "<td align=right>%d</td><td align=right>%d</td><td></td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200854 /* sessions : current, max, limit, cumul. */
855 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
856 /* bytes : in, out */
857 "<td align=right>%lld</td><td align=right>%lld</td>"
858 /* denied: req, resp */
859 "<td align=right>%d</td><td align=right>%d</td>"
860 /* errors : request, connect, response */
861 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200862 /* warnings: retries, redispatches */
863 "<td align=right>%d</td><td align=right>%d</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200864 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +0200865 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200866 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +0200867 * active and backups. */
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200868 "<td align=center nowrap>%s %s</td><td align=center>%d</td>"
869 "<td align=center>%d</td><td align=center>%d</td>",
Willy Tarreau91861262007-10-17 17:06:05 +0200870 px->nbpend /* or px->totpend ? */, px->nbpend_max,
871 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
872 px->bytes_in, px->bytes_out,
873 px->denied_req, px->denied_resp,
874 px->failed_conns, px->failed_resp,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200875 px->retries, px->redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200876 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau91861262007-10-17 17:06:05 +0200877 (px->srv_map_sz > 0 || !px->srv) ? "UP" : "DOWN",
878 px->srv_map_sz * gcd, px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200879
880 chunk_printf(&msg, sizeof(trash),
881 /* rest of backend: nothing, down transformations, total downtime */
882 "<td align=center>&nbsp;</td><td align=\"right\">%d</td>"
883 "<td align=\"right\" nowrap>%s</td>"
884 "</tr>",
885 px->down_trans,
886 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200887 } else {
888 chunk_printf(&msg, sizeof(trash),
889 /* pxid, name */
890 "%s,BACKEND,"
891 /* queue : current, max */
892 "%d,%d,"
893 /* sessions : current, max, limit, cumul */
894 "%d,%d,%d,%d,"
895 /* bytes : in, out */
896 "%lld,%lld,"
897 /* denied: req, resp */
898 "%d,%d,"
899 /* errors : request, connect, response */
900 ",%d,%d,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200901 /* warnings: retries, redispatches */
902 "%d,%d,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200903 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +0200904 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200905 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +0200906 * active and backups. */
907 "%s,"
908 "%d,%d,%d,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200909 /* rest of backend: nothing, down transformations,
910 * last change, total downtime. */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200911 ",%d,%d,%d,,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200912 "\n",
913 px->id,
914 px->nbpend /* or px->totpend ? */, px->nbpend_max,
915 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
916 px->bytes_in, px->bytes_out,
917 px->denied_req, px->denied_resp,
918 px->failed_conns, px->failed_resp,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200919 px->retries, px->redispatches,
Willy Tarreau55bb8452007-10-17 18:44:57 +0200920 (px->srv_map_sz > 0 || !px->srv) ? "UP" : "DOWN",
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200921 px->srv_map_sz * gcd, px->srv_act, px->srv_bck,
922 px->down_trans, now.tv_sec - px->last_change,
923 px->srv?be_downtime(px):0);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200924 }
Willy Tarreau91861262007-10-17 17:06:05 +0200925 if (buffer_write_chunk(rep, &msg) != 0)
926 return 0;
927 }
928
929 s->data_ctx.stats.px_st = DATA_ST_PX_END;
930 /* fall through */
931
932 case DATA_ST_PX_END:
Willy Tarreau55bb8452007-10-17 18:44:57 +0200933 if (flags & STAT_FMT_HTML) {
934 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +0200935
Willy Tarreau55bb8452007-10-17 18:44:57 +0200936 if (buffer_write_chunk(rep, &msg) != 0)
937 return 0;
938 }
Willy Tarreau91861262007-10-17 17:06:05 +0200939
940 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
941 /* fall through */
942
943 case DATA_ST_PX_FIN:
944 return 1;
945
946 default:
947 /* unknown state, we should put an abort() here ! */
948 return 1;
949 }
950}
951
952/*
953 * Local variables:
954 * c-indent-level: 8
955 * c-basic-offset: 8
956 * End:
957 */