blob: bba8c2ce76608c064b8e70cc1a53ad2b8e40ce59 [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>
19#include <time.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020020#include <pwd.h>
21#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020022
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
27#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>
33#include <common/time.h>
34#include <common/uri_auth.h>
35#include <common/version.h>
36
37#include <types/client.h>
38#include <types/global.h>
39#include <types/polling.h>
40#include <types/proxy.h>
41#include <types/server.h>
42
43#include <proto/backend.h>
44#include <proto/buffers.h>
45#include <proto/dumpstats.h>
46#include <proto/fd.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020047#include <proto/proto_uxst.h>
Willy Tarreau91861262007-10-17 17:06:05 +020048#include <proto/senddata.h>
49#include <proto/session.h>
50
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,"
188 "weight,act,bck,"
189 "chkfail,chkdown"
190 "\n");
191
192 if (buffer_write_chunk(rep, &msg) != 0)
193 return 0;
194
195 s->data_state = DATA_ST_INFO;
196 /* fall through */
197
198 case DATA_ST_INFO:
199 memset(&s->data_ctx, 0, sizeof(s->data_ctx));
200
201 s->data_ctx.stats.px = proxy;
202 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
203 s->data_state = DATA_ST_LIST;
204 /* fall through */
205
206 case DATA_ST_LIST:
207 /* dump proxies */
208 while (s->data_ctx.stats.px) {
209 px = s->data_ctx.stats.px;
210 /* skip the disabled proxies and non-networked ones */
211 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
212 if (stats_dump_proxy(s, px, NULL, 0) == 0)
213 return 0;
214
215 s->data_ctx.stats.px = px->next;
216 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
217 }
218 /* here, we just have reached the last proxy */
219
220 s->data_state = DATA_ST_END;
221 /* fall through */
222
223 case DATA_ST_END:
224 s->data_state = DATA_ST_FIN;
225 return 1;
226
227 case DATA_ST_FIN:
228 return 1;
229
230 default:
231 /* unknown state ! */
232 return -1;
233 }
234}
235
236
237/*
238 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau91861262007-10-17 17:06:05 +0200239 * s->cli_state == CL_STSHUTR. It stops by itself by unsetting the SN_SELF_GEN
240 * flag from the session, which it uses to keep on being called when there is
241 * free space in the buffer, of simply by letting an empty buffer upon return.
242 * It returns 0 if it had to stop writing data and an I/O is needed, 1 if the
243 * dump is finished and the session must be closed, or -1 in case of any error.
244 */
245int stats_dump_http(struct session *s, struct uri_auth *uri, int flags)
246{
247 struct buffer *rep = s->rep;
248 struct proxy *px;
249 struct chunk msg;
250 unsigned int up;
251
252 msg.len = 0;
253 msg.str = trash;
254
255 switch (s->data_state) {
256 case DATA_ST_INIT:
257 /* the function had not been called yet */
258 s->flags |= SN_SELF_GEN; // more data will follow
259
260 chunk_printf(&msg, sizeof(trash),
261 "HTTP/1.0 200 OK\r\n"
262 "Cache-Control: no-cache\r\n"
263 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200264 "Content-Type: %s\r\n",
265 (flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau91861262007-10-17 17:06:05 +0200266
267 if (uri->refresh > 0 && !(s->flags & SN_STAT_NORFRSH))
268 chunk_printf(&msg, sizeof(trash), "Refresh: %d\r\n",
269 uri->refresh);
270
271 chunk_printf(&msg, sizeof(trash), "\r\n");
272
273 s->txn.status = 200;
274 client_retnclose(s, &msg); // send the start of the response.
275 msg.len = 0;
276
277 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
278 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
279 if (!(s->flags & SN_FINST_MASK))
280 s->flags |= SN_FINST_R;
281
282 if (s->txn.meth == HTTP_METH_HEAD) {
283 /* that's all we return in case of HEAD request */
284 s->data_state = DATA_ST_FIN;
285 s->flags &= ~SN_SELF_GEN;
286 return 1;
287 }
288
289 s->data_state = DATA_ST_HEAD; /* let's start producing data */
290 /* fall through */
291
292 case DATA_ST_HEAD:
Willy Tarreau55bb8452007-10-17 18:44:57 +0200293 if (flags & STAT_FMT_HTML) {
294 /* WARNING! This must fit in the first buffer !!! */
295 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200296 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
297 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
298 "<style type=\"text/css\"><!--\n"
299 "body {"
300 " font-family: helvetica, arial;"
301 " font-size: 12px;"
302 " font-weight: normal;"
303 " color: black;"
304 " background: white;"
305 "}\n"
306 "th,td {"
307 " font-size: 0.8em;"
308 " align: center;"
309 "}\n"
310 "h1 {"
311 " font-size: xx-large;"
312 " margin-bottom: 0.5em;"
313 "}\n"
314 "h2 {"
315 " font-family: helvetica, arial;"
316 " font-size: x-large;"
317 " font-weight: bold;"
318 " font-style: italic;"
319 " color: #6020a0;"
320 " margin-top: 0em;"
321 " margin-bottom: 0em;"
322 "}\n"
323 "h3 {"
324 " font-family: helvetica, arial;"
325 " font-size: 16px;"
326 " font-weight: bold;"
327 " color: #b00040;"
328 " background: #e8e8d0;"
329 " margin-top: 0em;"
330 " margin-bottom: 0em;"
331 "}\n"
332 "li {"
333 " margin-top: 0.25em;"
334 " margin-right: 2em;"
335 "}\n"
336 ".hr {margin-top: 0.25em;"
337 " border-color: black;"
338 " border-bottom-style: solid;"
339 "}\n"
340 ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n"
341 ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n"
342 ".total {background: #20D0D0;color: #ffff80;}\n"
343 ".frontend {background: #e8e8d0;}\n"
344 ".backend {background: #e8e8d0;}\n"
345 ".active0 {background: #ff9090;}\n"
346 ".active1 {background: #ffd020;}\n"
347 ".active2 {background: #ffffa0;}\n"
348 ".active3 {background: #c0ffc0;}\n"
349 ".active4 {background: #e0e0e0;}\n"
350 ".backup0 {background: #ff9090;}\n"
351 ".backup1 {background: #ff80ff;}\n"
352 ".backup2 {background: #c060ff;}\n"
353 ".backup3 {background: #b0d0ff;}\n"
354 ".backup4 {background: #e0e0e0;}\n"
355 "table.tbl { border-collapse: collapse; border-style: none;}\n"
356 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray;}\n"
357 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
358 "table.tbl th.empty { border-style: none; empty-cells: hide;}\n"
359 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
360 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
361 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
362 "-->\n"
363 "</style></head>\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200364 } else {
365 chunk_printf(&msg, sizeof(trash),
366 "# pxname,svname,"
367 "qcur,qmax,"
368 "scur,smax,slim,stot,"
369 "bin,bout,"
370 "dreq,dresp,"
371 "ereq,econ,eresp,"
372 "weight,act,bck,"
373 "chkfail,chkdown"
374 "\n");
375 }
Willy Tarreau91861262007-10-17 17:06:05 +0200376 if (buffer_write_chunk(rep, &msg) != 0)
377 return 0;
378
379 s->data_state = DATA_ST_INFO;
380 /* fall through */
381
382 case DATA_ST_INFO:
383 up = (now.tv_sec - start_date.tv_sec);
384
385 /* WARNING! this has to fit the first packet too.
386 * We are around 3.5 kB, add adding entries will
387 * become tricky if we want to support 4kB buffers !
388 */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200389 if (flags & STAT_FMT_HTML) {
390 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200391 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
392 PRODUCT_NAME "%s</a></h1>\n"
393 "<h2>Statistics Report for pid %d</h2>\n"
394 "<hr width=\"100%%\" class=\"hr\">\n"
395 "<h3>&gt; General process information</h3>\n"
396 "<table border=0 cols=4><tr><td align=\"left\" nowrap width=\"1%%\">\n"
397 "<p><b>pid = </b> %d (nbproc = %d)<br>\n"
398 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
399 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
400 "<b>maxsock = </b> %d<br>\n"
401 "<b>maxconn = </b> %d (current conns = %d)<br>\n"
402 "</td><td align=\"center\" nowrap>\n"
403 "<table class=\"lgd\"><tr>\n"
404 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
405 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
406 "</tr><tr>\n"
407 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
408 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
409 "</tr><tr>\n"
410 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
411 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
412 "</tr><tr>\n"
413 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
414 "<td class=\"active4\"></td><td class=\"noborder\">not checked </td>"
415 "</tr></table>\n"
416 "</td>"
417 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
418 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
419 "",
420 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
421 pid, pid, global.nbproc,
422 up / 86400, (up % 86400) / 3600,
423 (up % 3600) / 60, (up % 60),
424 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
425 global.rlimit_memmax ? " MB" : "",
426 global.rlimit_nofile,
427 global.maxsock,
428 global.maxconn,
429 actconn
430 );
431
Willy Tarreau55bb8452007-10-17 18:44:57 +0200432 if (s->flags & SN_STAT_HIDEDWN)
433 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200434 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
435 uri->uri_prefix,
436 "",
437 (s->flags & SN_STAT_NORFRSH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200438 else
439 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200440 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
441 uri->uri_prefix,
442 ";up",
443 (s->flags & SN_STAT_NORFRSH) ? ";norefresh" : "");
444
Willy Tarreau55bb8452007-10-17 18:44:57 +0200445 if (uri->refresh > 0) {
446 if (s->flags & SN_STAT_NORFRSH)
447 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200448 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
449 uri->uri_prefix,
450 (s->flags & SN_STAT_HIDEDWN) ? ";up" : "",
451 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200452 else
453 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200454 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
455 uri->uri_prefix,
456 (s->flags & SN_STAT_HIDEDWN) ? ";up" : "",
457 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200458 }
Willy Tarreau91861262007-10-17 17:06:05 +0200459
Willy Tarreau55bb8452007-10-17 18:44:57 +0200460 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200461 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
462 uri->uri_prefix,
463 (s->flags & SN_STAT_HIDEDWN) ? ";up" : "",
464 (s->flags & SN_STAT_NORFRSH) ? ";norefresh" : "");
465
Willy Tarreau55bb8452007-10-17 18:44:57 +0200466 chunk_printf(&msg, sizeof(trash),
Willy Tarreau5031e6a2007-10-18 11:05:48 +0200467 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
468 uri->uri_prefix,
469 (uri->refresh > 0) ? ";norefresh" : "");
470
471 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200472 "</td>"
473 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
474 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
475 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
476 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
477 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
478 "</ul>"
479 "</td>"
480 "</tr></table>\n"
481 ""
482 );
483
Willy Tarreau55bb8452007-10-17 18:44:57 +0200484 if (buffer_write_chunk(rep, &msg) != 0)
485 return 0;
486 }
Willy Tarreau91861262007-10-17 17:06:05 +0200487
488 memset(&s->data_ctx, 0, sizeof(s->data_ctx));
489
490 s->data_ctx.stats.px = proxy;
491 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
492 s->data_state = DATA_ST_LIST;
493 /* fall through */
494
495 case DATA_ST_LIST:
496 /* dump proxies */
497 while (s->data_ctx.stats.px) {
498 px = s->data_ctx.stats.px;
499 /* skip the disabled proxies and non-networked ones */
500 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
501 if (stats_dump_proxy(s, px, uri, flags) == 0)
502 return 0;
503
504 s->data_ctx.stats.px = px->next;
505 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
506 }
507 /* here, we just have reached the last proxy */
508
509 s->data_state = DATA_ST_END;
510 /* fall through */
511
512 case DATA_ST_END:
Willy Tarreau55bb8452007-10-17 18:44:57 +0200513 if (flags & STAT_FMT_HTML) {
514 chunk_printf(&msg, sizeof(trash), "</body></html>\n");
515 if (buffer_write_chunk(rep, &msg) != 0)
516 return 0;
517 }
Willy Tarreau91861262007-10-17 17:06:05 +0200518
519 s->data_state = DATA_ST_FIN;
520 /* fall through */
521
522 case DATA_ST_FIN:
523 s->flags &= ~SN_SELF_GEN;
524 return 1;
525
526 default:
527 /* unknown state ! */
528 s->flags &= ~SN_SELF_GEN;
529 return -1;
530 }
531}
532
533
534/*
535 * Dumps statistics for a proxy.
536 * Returns 0 if it had to stop dumping data because of lack of buffer space,
537 * ot non-zero if everything completed.
538 */
539int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri, int flags)
540{
541 struct buffer *rep = s->rep;
542 struct server *sv;
543 struct chunk msg;
544
545 msg.len = 0;
546 msg.str = trash;
547
548 switch (s->data_ctx.stats.px_st) {
549 case DATA_ST_PX_INIT:
550 /* we are on a new proxy */
551
552 if (uri && uri->scope) {
553 /* we have a limited scope, we have to check the proxy name */
554 struct stat_scope *scope;
555 int len;
556
557 len = strlen(px->id);
558 scope = uri->scope;
559
560 while (scope) {
561 /* match exact proxy name */
562 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
563 break;
564
565 /* match '.' which means 'self' proxy */
566 if (!strcmp(scope->px_id, ".") && px == s->fe)
567 break;
568 scope = scope->next;
569 }
570
571 /* proxy name not found : don't dump anything */
572 if (scope == NULL)
573 return 1;
574 }
575
576 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
577 /* fall through */
578
579 case DATA_ST_PX_TH:
Willy Tarreau55bb8452007-10-17 18:44:57 +0200580 if (flags & STAT_FMT_HTML) {
581 /* print a new table */
582 chunk_printf(&msg, sizeof(trash),
583 "<table cols=\"20\" class=\"tbl\" width=\"100%%\">\n"
584 "<tr align=\"center\" class=\"titre\">"
585 "<th colspan=2 class=\"pxname\">%s</th>"
586 "<th colspan=18 class=\"empty\"></th>"
587 "</tr>\n"
588 "<tr align=\"center\" class=\"titre\">"
589 "<th rowspan=2></th>"
590 "<th colspan=2>Queue</th><th colspan=4>Sessions</th>"
591 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
592 "<th colspan=3>Errors</th><th colspan=6>Server</th>"
593 "</tr>\n"
594 "<tr align=\"center\" class=\"titre\">"
595 "<th>Cur</th><th>Max</th><th>Cur</th><th>Max</th>"
596 "<th>Limit</th><th>Cumul</th><th>In</th><th>Out</th>"
597 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
598 "<th>Resp</th><th>Status</th><th>Weight</th><th>Act</th>"
599 "<th>Bck</th><th>Check</th><th>Down</th></tr>\n"
600 "",
601 px->id);
602
603 if (buffer_write_chunk(rep, &msg) != 0)
604 return 0;
605 }
Willy Tarreau91861262007-10-17 17:06:05 +0200606
607 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
608 /* fall through */
609
610 case DATA_ST_PX_FE:
611 /* print the frontend */
612 if (px->cap & PR_CAP_FE) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200613 if (flags & STAT_FMT_HTML) {
614 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200615 /* name, queue */
616 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=2></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200617 /* sessions : current, max, limit, cumul */
618 "<td align=right>%d</td><td align=right>%d</td>"
619 "<td align=right>%d</td><td align=right>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200620 /* bytes : in, out */
621 "<td align=right>%lld</td><td align=right>%lld</td>"
622 /* denied: req, resp */
623 "<td align=right>%d</td><td align=right>%d</td>"
624 /* errors : request, connect, response */
625 "<td align=right>%d</td><td align=right></td><td align=right></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200626 /* server status : reflect frontend status */
Willy Tarreau91861262007-10-17 17:06:05 +0200627 "<td align=center>%s</td>"
628 /* rest of server: nothing */
629 "<td align=center colspan=5></td></tr>"
630 "",
631 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
632 px->bytes_in, px->bytes_out,
633 px->denied_req, px->denied_resp,
634 px->failed_req,
635 px->state == PR_STRUN ? "OPEN" :
636 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200637 } else {
638 chunk_printf(&msg, sizeof(trash),
639 /* pxid, name, queue cur, queue max, */
640 "%s,FRONTEND,,,"
641 /* sessions : current, max, limit, cumul */
642 "%d,%d,%d,%d,"
643 /* bytes : in, out */
644 "%lld,%lld,"
645 /* denied: req, resp */
646 "%d,%d,"
647 /* errors : request, connect, response */
648 "%d,,,"
649 /* server status : reflect frontend status */
650 "%s,"
651 /* rest of server: nothing */
652 ",,,,,"
653 "\n",
654 px->id,
655 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
656 px->bytes_in, px->bytes_out,
657 px->denied_req, px->denied_resp,
658 px->failed_req,
659 px->state == PR_STRUN ? "OPEN" :
660 px->state == PR_STIDLE ? "FULL" : "STOP");
661 }
Willy Tarreau91861262007-10-17 17:06:05 +0200662
663 if (buffer_write_chunk(rep, &msg) != 0)
664 return 0;
665 }
666
667 s->data_ctx.stats.sv = px->srv; /* may be NULL */
668 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
669 /* fall through */
670
671 case DATA_ST_PX_SV:
672 /* stats.sv has been initialized above */
673 while (s->data_ctx.stats.sv != NULL) {
Willy Tarreau91861262007-10-17 17:06:05 +0200674 int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP, 4=unchecked */
675
676 sv = s->data_ctx.stats.sv;
677
678 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
679 if (!(sv->state & SRV_CHECKED))
680 sv_state = 4;
681 else if (sv->state & SRV_RUNNING)
682 if (sv->health == sv->rise + sv->fall - 1)
683 sv_state = 3; /* UP */
684 else
685 sv_state = 2; /* going down */
686 else
687 if (sv->health)
688 sv_state = 1; /* going up */
689 else
690 sv_state = 0; /* DOWN */
691
692 if ((sv_state == 0) && (s->flags & SN_STAT_HIDEDWN)) {
693 /* do not report servers which are DOWN */
694 s->data_ctx.stats.sv = sv->next;
695 continue;
696 }
697
Willy Tarreau55bb8452007-10-17 18:44:57 +0200698 if (flags & STAT_FMT_HTML) {
699 static char *srv_hlt_st[5] = { "DOWN", "DN %d/%d &uarr;", "UP %d/%d &darr;",
700 "UP", "<i>no check</i>" };
701 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200702 /* name */
703 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
704 /* queue : current, max */
705 "<td align=right>%d</td><td align=right>%d</td>"
706 /* sessions : current, max, limit, cumul */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200707 "<td align=right>%d</td><td align=right>%d</td>"
708 "<td align=right>%s</td><td align=right>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200709 /* bytes : in, out */
710 "<td align=right>%lld</td><td align=right>%lld</td>"
711 /* denied: req, resp */
712 "<td align=right></td><td align=right>%d</td>"
713 /* errors : request, connect, response */
714 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
715 "",
716 (sv->state & SRV_BACKUP) ? "backup" : "active",
717 sv_state, sv->id,
718 sv->nbpend, sv->nbpend_max,
719 sv->cur_sess, sv->cur_sess_max, sv->maxconn ? ultoa(sv->maxconn) : "-", sv->cum_sess,
720 sv->bytes_in, sv->bytes_out,
721 sv->failed_secu,
722 sv->failed_conns, sv->failed_resp);
723
Willy Tarreau55bb8452007-10-17 18:44:57 +0200724 /* status */
725 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
726 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200727 srv_hlt_st[sv_state],
728 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
729 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
730
Willy Tarreau55bb8452007-10-17 18:44:57 +0200731 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200732 /* weight */
733 "</td><td>%d</td>"
734 /* act, bck */
735 "<td>%s</td><td>%s</td>"
736 "",
737 sv->uweight,
738 (sv->state & SRV_BACKUP) ? "-" : "Y",
739 (sv->state & SRV_BACKUP) ? "Y" : "-");
740
Willy Tarreau55bb8452007-10-17 18:44:57 +0200741 /* check failures : unique, fatal */
742 if (sv->state & SRV_CHECKED)
743 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200744 "<td align=right>%d</td><td align=right>%d</td></tr>\n",
745 sv->failed_checks, sv->down_trans);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200746 else
747 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200748 "<td colspan=2></td></tr>\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200749 } else {
750 static char *srv_hlt_st[5] = { "DOWN,", "DOWN %d/%d,", "UP %d/%d,",
751 "UP,", "no check," };
752 chunk_printf(&msg, sizeof(trash),
753 /* pxid, name */
754 "%s,%s,"
755 /* queue : current, max */
756 "%d,%d,"
757 /* sessions : current, max, limit, cumul */
758 "%d,%d,%s,%d,"
759 /* bytes : in, out */
760 "%lld,%lld,"
761 /* denied: req, resp */
762 ",%d,"
763 /* errors : request, connect, response */
764 ",%d,%d,"
765 "",
766 px->id, sv->id,
767 sv->nbpend, sv->nbpend_max,
768 sv->cur_sess, sv->cur_sess_max, sv->maxconn ? ultoa(sv->maxconn) : "-", sv->cum_sess,
769 sv->bytes_in, sv->bytes_out,
770 sv->failed_secu,
771 sv->failed_conns, sv->failed_resp);
772
773 /* status */
774 chunk_printf(&msg, sizeof(trash),
775 srv_hlt_st[sv_state],
776 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
777 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
Willy Tarreau91861262007-10-17 17:06:05 +0200778
Willy Tarreau55bb8452007-10-17 18:44:57 +0200779 chunk_printf(&msg, sizeof(trash),
780 /* weight, active, backup */
781 "%d,%d,%d,"
782 "",
783 sv->uweight,
784 (sv->state & SRV_BACKUP) ? 0 : 1,
785 (sv->state & SRV_BACKUP) ? 1 : 0);
786
787 /* check failures : unique, fatal */
788 if (sv->state & SRV_CHECKED)
789 chunk_printf(&msg, sizeof(trash),
790 "%d,%d,\n",
791 sv->failed_checks, sv->down_trans);
792 else
793 chunk_printf(&msg, sizeof(trash),
794 ",,\n");
795 }
Willy Tarreau91861262007-10-17 17:06:05 +0200796 if (buffer_write_chunk(rep, &msg) != 0)
797 return 0;
798
799 s->data_ctx.stats.sv = sv->next;
800 } /* while sv */
801
802 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
803 /* fall through */
804
805 case DATA_ST_PX_BE:
806 /* print the backend */
807 if (px->cap & PR_CAP_BE) {
808 int gcd = 1;
809
810 if (px->map_state & PR_MAP_RECALC)
811 recalc_server_map(px);
812
813 /* The GCD which was computed causes the total effective
814 * weight to appear lower than all weights. Let's
815 * recompute it.
816 */
817 if (px->srv && px->srv->eweight)
818 gcd = px->srv->uweight / px->srv->eweight;
819
Willy Tarreau55bb8452007-10-17 18:44:57 +0200820 if (flags & STAT_FMT_HTML) {
821 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200822 /* name */
823 "<tr align=center class=\"backend\"><td>Backend</td>"
824 /* queue : current, max */
825 "<td align=right>%d</td><td align=right>%d</td>"
826 /* sessions : current, max, limit, cumul. */
827 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
828 /* bytes : in, out */
829 "<td align=right>%lld</td><td align=right>%lld</td>"
830 /* denied: req, resp */
831 "<td align=right>%d</td><td align=right>%d</td>"
832 /* errors : request, connect, response */
833 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
834 /* server status : reflect backend status (up/down) : we display UP
835 * if the backend has known working servers or if it has no server at
836 * all (eg: for stats). Tthen we display the total weight, number of
837 * active and backups. */
838 "<td align=center>%s</td><td align=center>%d</td>"
839 "<td align=center>%d</td><td align=center>%d</td>"
840 /* rest of server: nothing */
841 "<td align=center colspan=2></td></tr>"
842 "",
843 px->nbpend /* or px->totpend ? */, px->nbpend_max,
844 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
845 px->bytes_in, px->bytes_out,
846 px->denied_req, px->denied_resp,
847 px->failed_conns, px->failed_resp,
848 (px->srv_map_sz > 0 || !px->srv) ? "UP" : "DOWN",
849 px->srv_map_sz * gcd, px->srv_act, px->srv_bck);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200850 } else {
851 chunk_printf(&msg, sizeof(trash),
852 /* pxid, name */
853 "%s,BACKEND,"
854 /* queue : current, max */
855 "%d,%d,"
856 /* sessions : current, max, limit, cumul */
857 "%d,%d,%d,%d,"
858 /* bytes : in, out */
859 "%lld,%lld,"
860 /* denied: req, resp */
861 "%d,%d,"
862 /* errors : request, connect, response */
863 ",%d,%d,"
864 /* server status : reflect backend status (up/down) : we display UP
865 * if the backend has known working servers or if it has no server at
866 * all (eg: for stats). Tthen we display the total weight, number of
867 * active and backups. */
868 "%s,"
869 "%d,%d,%d,"
870 /* rest of server: nothing */
871 ",,"
872 "\n",
873 px->id,
874 px->nbpend /* or px->totpend ? */, px->nbpend_max,
875 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
876 px->bytes_in, px->bytes_out,
877 px->denied_req, px->denied_resp,
878 px->failed_conns, px->failed_resp,
879 (px->srv_map_sz > 0 || !px->srv) ? "UP" : "DOWN",
880 px->srv_map_sz * gcd, px->srv_act, px->srv_bck);
881 }
Willy Tarreau91861262007-10-17 17:06:05 +0200882 if (buffer_write_chunk(rep, &msg) != 0)
883 return 0;
884 }
885
886 s->data_ctx.stats.px_st = DATA_ST_PX_END;
887 /* fall through */
888
889 case DATA_ST_PX_END:
Willy Tarreau55bb8452007-10-17 18:44:57 +0200890 if (flags & STAT_FMT_HTML) {
891 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +0200892
Willy Tarreau55bb8452007-10-17 18:44:57 +0200893 if (buffer_write_chunk(rep, &msg) != 0)
894 return 0;
895 }
Willy Tarreau91861262007-10-17 17:06:05 +0200896
897 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
898 /* fall through */
899
900 case DATA_ST_PX_FIN:
901 return 1;
902
903 default:
904 /* unknown state, we should put an abort() here ! */
905 return 1;
906 }
907}
908
909/*
910 * Local variables:
911 * c-indent-level: 8
912 * c-basic-offset: 8
913 * End:
914 */