blob: 71e382afdcfd9485f5bbf6615e1c07db92030760 [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;
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;
81 global.stats_sock.handler = process_uxst_stats;
82 global.stats_sock.private = NULL;
83
84 cur_arg = 2;
85 while (*args[cur_arg]) {
86 if (!strcmp(args[cur_arg], "uid")) {
87 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
88 cur_arg += 2;
89 }
90 else if (!strcmp(args[cur_arg], "gid")) {
91 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
92 cur_arg += 2;
93 }
94 else if (!strcmp(args[cur_arg], "mode")) {
95 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
96 cur_arg += 2;
97 }
98 else if (!strcmp(args[cur_arg], "user")) {
99 struct passwd *user;
100 user = getpwnam(args[cur_arg + 1]);
101 if (!user) {
102 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
103 args[cur_arg + 1]);
104 return -1;
105 }
106 global.stats_sock.perm.ux.uid = user->pw_uid;
107 cur_arg += 2;
108 }
109 else if (!strcmp(args[cur_arg], "group")) {
110 struct group *group;
111 group = getgrnam(args[cur_arg + 1]);
112 if (!group) {
113 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
114 args[cur_arg + 1]);
115 return -1;
116 }
117 global.stats_sock.perm.ux.gid = group->gr_gid;
118 cur_arg += 2;
119 }
120 else {
121 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', and 'mode'");
122 return -1;
123 }
124 }
125
126 uxst_add_listener(&global.stats_sock);
127 global.maxsock++;
128 }
129 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100130 unsigned timeout;
131 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
132
133 if (res) {
134 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
135 return -1;
136 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200137
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100138 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200139 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
140 return -1;
141 }
142 __tv_from_ms(&global.stats_timeout, timeout);
143 }
144 else if (!strcmp(args[0], "maxconn")) {
145 int maxconn = atol(args[1]);
146
147 if (maxconn <= 0) {
148 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
149 return -1;
150 }
151 global.maxsock -= global.stats_sock.maxconn;
152 global.stats_sock.maxconn = maxconn;
153 global.maxsock += global.stats_sock.maxconn;
154 }
155 else {
156 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
157 return -1;
158 }
159 return 0;
160}
161
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100162int print_csv_header(struct chunk *msg, int size)
163{
164 return chunk_printf(msg, size,
165 "# pxname,svname,"
166 "qcur,qmax,"
167 "scur,smax,slim,stot,"
168 "bin,bout,"
169 "dreq,dresp,"
170 "ereq,econ,eresp,"
171 "wretr,wredis,"
172 "status,weight,act,bck,"
173 "chkfail,chkdown,lastchg,downtime,qlimit,"
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100174 "pid,iid,sid,throttle,lbtot,tracked,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100175 "\n");
176}
177
Willy Tarreau91861262007-10-17 17:06:05 +0200178/*
179 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau3e76e722007-10-17 18:57:38 +0200180 * s->cli_state == CL_STSHUTR. It *may* make use of informations from <uri>
181 * and <flags>.
182 * It returns 0 if it had to stop writing data and an I/O is needed, 1 if the
183 * dump is finished and the session must be closed, or -1 in case of any error.
184 */
185int stats_dump_raw(struct session *s, struct uri_auth *uri, int flags)
186{
187 struct buffer *rep = s->rep;
188 struct proxy *px;
189 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +0100190 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200191
192 msg.len = 0;
193 msg.str = trash;
194
195 switch (s->data_state) {
196 case DATA_ST_INIT:
197 /* the function had not been called yet, let's prepare the
198 * buffer for a response.
199 */
200 client_retnclose(s, &msg);
201 s->data_state = DATA_ST_HEAD;
202 /* fall through */
203
204 case DATA_ST_HEAD:
Willy Tarreaua8efd362008-01-03 10:19:15 +0100205 if (flags & STAT_SHOW_STAT) {
206 print_csv_header(&msg, sizeof(trash));
207 if (buffer_write_chunk(rep, &msg) != 0)
208 return 0;
209 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200210
211 s->data_state = DATA_ST_INFO;
212 /* fall through */
213
214 case DATA_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +0100215 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200216 memset(&s->data_ctx, 0, sizeof(s->data_ctx));
217
Willy Tarreaua8efd362008-01-03 10:19:15 +0100218 if (flags & STAT_SHOW_INFO) {
219 chunk_printf(&msg, sizeof(trash),
220 "Name: " PRODUCT_NAME "\n"
221 "Version: " HAPROXY_VERSION "\n"
222 "Release_date: " HAPROXY_DATE "\n"
223 "Nbproc: %d\n"
224 "Process_num: %d\n"
225 "Pid: %d\n"
226 "Uptime: %dd %dh%02dm%02ds\n"
227 "Uptime_sec: %d\n"
228 "Memmax_MB: %d\n"
229 "Ulimit-n: %d\n"
230 "Maxsock: %d\n"
231 "Maxconn: %d\n"
232 "CurrConns: %d\n"
233 "",
234 global.nbproc,
235 relative_pid,
236 pid,
237 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
238 up,
239 global.rlimit_memmax,
240 global.rlimit_nofile,
241 global.maxsock,
242 global.maxconn,
243 actconn
244 );
245 if (buffer_write_chunk(rep, &msg) != 0)
246 return 0;
247 }
248
Willy Tarreau3e76e722007-10-17 18:57:38 +0200249 s->data_ctx.stats.px = proxy;
250 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
251 s->data_state = DATA_ST_LIST;
252 /* fall through */
253
254 case DATA_ST_LIST:
255 /* dump proxies */
Willy Tarreaua8efd362008-01-03 10:19:15 +0100256 if (flags & STAT_SHOW_STAT) {
257 while (s->data_ctx.stats.px) {
258 px = s->data_ctx.stats.px;
259 /* skip the disabled proxies and non-networked ones */
260 if (px->state != PR_STSTOPPED &&
261 (px->cap & (PR_CAP_FE | PR_CAP_BE)))
262 if (stats_dump_proxy(s, px, NULL, 0) == 0)
263 return 0;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200264
Willy Tarreaua8efd362008-01-03 10:19:15 +0100265 s->data_ctx.stats.px = px->next;
266 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
267 }
268 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200269 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200270
271 s->data_state = DATA_ST_END;
272 /* fall through */
273
274 case DATA_ST_END:
275 s->data_state = DATA_ST_FIN;
276 return 1;
277
278 case DATA_ST_FIN:
279 return 1;
280
281 default:
282 /* unknown state ! */
283 return -1;
284 }
285}
286
287
288/*
289 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau91861262007-10-17 17:06:05 +0200290 * s->cli_state == CL_STSHUTR. It stops by itself by unsetting the SN_SELF_GEN
291 * flag from the session, which it uses to keep on being called when there is
292 * free space in the buffer, of simply by letting an empty buffer upon return.
293 * It returns 0 if it had to stop writing data and an I/O is needed, 1 if the
294 * dump is finished and the session must be closed, or -1 in case of any error.
295 */
296int stats_dump_http(struct session *s, struct uri_auth *uri, int flags)
297{
298 struct buffer *rep = s->rep;
299 struct proxy *px;
300 struct chunk msg;
301 unsigned int up;
302
303 msg.len = 0;
304 msg.str = trash;
305
306 switch (s->data_state) {
307 case DATA_ST_INIT:
308 /* the function had not been called yet */
309 s->flags |= SN_SELF_GEN; // more data will follow
310
311 chunk_printf(&msg, sizeof(trash),
312 "HTTP/1.0 200 OK\r\n"
313 "Cache-Control: no-cache\r\n"
314 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200315 "Content-Type: %s\r\n",
316 (flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau91861262007-10-17 17:06:05 +0200317
318 if (uri->refresh > 0 && !(s->flags & SN_STAT_NORFRSH))
319 chunk_printf(&msg, sizeof(trash), "Refresh: %d\r\n",
320 uri->refresh);
321
322 chunk_printf(&msg, sizeof(trash), "\r\n");
323
324 s->txn.status = 200;
325 client_retnclose(s, &msg); // send the start of the response.
326 msg.len = 0;
327
328 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
329 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
330 if (!(s->flags & SN_FINST_MASK))
331 s->flags |= SN_FINST_R;
332
333 if (s->txn.meth == HTTP_METH_HEAD) {
334 /* that's all we return in case of HEAD request */
335 s->data_state = DATA_ST_FIN;
336 s->flags &= ~SN_SELF_GEN;
337 return 1;
338 }
339
340 s->data_state = DATA_ST_HEAD; /* let's start producing data */
341 /* fall through */
342
343 case DATA_ST_HEAD:
Willy Tarreau55bb8452007-10-17 18:44:57 +0200344 if (flags & STAT_FMT_HTML) {
345 /* WARNING! This must fit in the first buffer !!! */
346 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200347 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
348 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
349 "<style type=\"text/css\"><!--\n"
350 "body {"
351 " font-family: helvetica, arial;"
352 " font-size: 12px;"
353 " font-weight: normal;"
354 " color: black;"
355 " background: white;"
356 "}\n"
357 "th,td {"
358 " font-size: 0.8em;"
359 " align: center;"
360 "}\n"
361 "h1 {"
362 " font-size: xx-large;"
363 " margin-bottom: 0.5em;"
364 "}\n"
365 "h2 {"
366 " font-family: helvetica, arial;"
367 " font-size: x-large;"
368 " font-weight: bold;"
369 " font-style: italic;"
370 " color: #6020a0;"
371 " margin-top: 0em;"
372 " margin-bottom: 0em;"
373 "}\n"
374 "h3 {"
375 " font-family: helvetica, arial;"
376 " font-size: 16px;"
377 " font-weight: bold;"
378 " color: #b00040;"
379 " background: #e8e8d0;"
380 " margin-top: 0em;"
381 " margin-bottom: 0em;"
382 "}\n"
383 "li {"
384 " margin-top: 0.25em;"
385 " margin-right: 2em;"
386 "}\n"
387 ".hr {margin-top: 0.25em;"
388 " border-color: black;"
389 " border-bottom-style: solid;"
390 "}\n"
391 ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n"
392 ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n"
393 ".total {background: #20D0D0;color: #ffff80;}\n"
394 ".frontend {background: #e8e8d0;}\n"
395 ".backend {background: #e8e8d0;}\n"
396 ".active0 {background: #ff9090;}\n"
397 ".active1 {background: #ffd020;}\n"
398 ".active2 {background: #ffffa0;}\n"
399 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100400 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
401 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
402 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200403 ".backup0 {background: #ff9090;}\n"
404 ".backup1 {background: #ff80ff;}\n"
405 ".backup2 {background: #c060ff;}\n"
406 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100407 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
408 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
409 ".backup6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200410 "table.tbl { border-collapse: collapse; border-style: none;}\n"
411 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray;}\n"
412 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
413 "table.tbl th.empty { border-style: none; empty-cells: hide;}\n"
414 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
415 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
416 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
417 "-->\n"
418 "</style></head>\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200419 } else {
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100420 print_csv_header(&msg, sizeof(trash));
Willy Tarreau55bb8452007-10-17 18:44:57 +0200421 }
Willy Tarreau91861262007-10-17 17:06:05 +0200422 if (buffer_write_chunk(rep, &msg) != 0)
423 return 0;
424
425 s->data_state = DATA_ST_INFO;
426 /* fall through */
427
428 case DATA_ST_INFO:
429 up = (now.tv_sec - start_date.tv_sec);
430
431 /* WARNING! this has to fit the first packet too.
432 * We are around 3.5 kB, add adding entries will
433 * become tricky if we want to support 4kB buffers !
434 */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200435 if (flags & STAT_FMT_HTML) {
436 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200437 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
438 PRODUCT_NAME "%s</a></h1>\n"
439 "<h2>Statistics Report for pid %d</h2>\n"
440 "<hr width=\"100%%\" class=\"hr\">\n"
441 "<h3>&gt; General process information</h3>\n"
442 "<table border=0 cols=4><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100443 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200444 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
445 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
446 "<b>maxsock = </b> %d<br>\n"
447 "<b>maxconn = </b> %d (current conns = %d)<br>\n"
448 "</td><td align=\"center\" nowrap>\n"
449 "<table class=\"lgd\"><tr>\n"
450 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
451 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
452 "</tr><tr>\n"
453 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
454 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
455 "</tr><tr>\n"
456 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
457 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
458 "</tr><tr>\n"
459 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100460 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200461 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100462 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +0200463 "</td>"
464 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
465 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
466 "",
467 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Willy Tarreaua8efd362008-01-03 10:19:15 +0100468 pid, pid,
469 relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +0200470 up / 86400, (up % 86400) / 3600,
471 (up % 3600) / 60, (up % 60),
472 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
473 global.rlimit_memmax ? " MB" : "",
474 global.rlimit_nofile,
475 global.maxsock,
476 global.maxconn,
477 actconn
478 );
479
Willy Tarreau55bb8452007-10-17 18:44:57 +0200480 if (s->flags & SN_STAT_HIDEDWN)
481 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200482 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
483 uri->uri_prefix,
484 "",
485 (s->flags & SN_STAT_NORFRSH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200486 else
487 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200488 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
489 uri->uri_prefix,
490 ";up",
491 (s->flags & SN_STAT_NORFRSH) ? ";norefresh" : "");
492
Willy Tarreau55bb8452007-10-17 18:44:57 +0200493 if (uri->refresh > 0) {
494 if (s->flags & SN_STAT_NORFRSH)
495 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200496 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
497 uri->uri_prefix,
498 (s->flags & SN_STAT_HIDEDWN) ? ";up" : "",
499 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200500 else
501 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200502 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
503 uri->uri_prefix,
504 (s->flags & SN_STAT_HIDEDWN) ? ";up" : "",
505 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200506 }
Willy Tarreau91861262007-10-17 17:06:05 +0200507
Willy Tarreau55bb8452007-10-17 18:44:57 +0200508 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200509 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
510 uri->uri_prefix,
511 (s->flags & SN_STAT_HIDEDWN) ? ";up" : "",
512 (s->flags & SN_STAT_NORFRSH) ? ";norefresh" : "");
513
Willy Tarreau55bb8452007-10-17 18:44:57 +0200514 chunk_printf(&msg, sizeof(trash),
Willy Tarreau5031e6a2007-10-18 11:05:48 +0200515 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
516 uri->uri_prefix,
517 (uri->refresh > 0) ? ";norefresh" : "");
518
519 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200520 "</td>"
521 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
522 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
523 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
524 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
525 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
526 "</ul>"
527 "</td>"
528 "</tr></table>\n"
529 ""
530 );
531
Willy Tarreau55bb8452007-10-17 18:44:57 +0200532 if (buffer_write_chunk(rep, &msg) != 0)
533 return 0;
534 }
Willy Tarreau91861262007-10-17 17:06:05 +0200535
536 memset(&s->data_ctx, 0, sizeof(s->data_ctx));
537
538 s->data_ctx.stats.px = proxy;
539 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
540 s->data_state = DATA_ST_LIST;
541 /* fall through */
542
543 case DATA_ST_LIST:
544 /* dump proxies */
545 while (s->data_ctx.stats.px) {
546 px = s->data_ctx.stats.px;
547 /* skip the disabled proxies and non-networked ones */
548 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
549 if (stats_dump_proxy(s, px, uri, flags) == 0)
550 return 0;
551
552 s->data_ctx.stats.px = px->next;
553 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
554 }
555 /* here, we just have reached the last proxy */
556
557 s->data_state = DATA_ST_END;
558 /* fall through */
559
560 case DATA_ST_END:
Willy Tarreau55bb8452007-10-17 18:44:57 +0200561 if (flags & STAT_FMT_HTML) {
562 chunk_printf(&msg, sizeof(trash), "</body></html>\n");
563 if (buffer_write_chunk(rep, &msg) != 0)
564 return 0;
565 }
Willy Tarreau91861262007-10-17 17:06:05 +0200566
567 s->data_state = DATA_ST_FIN;
568 /* fall through */
569
570 case DATA_ST_FIN:
571 s->flags &= ~SN_SELF_GEN;
572 return 1;
573
574 default:
575 /* unknown state ! */
576 s->flags &= ~SN_SELF_GEN;
577 return -1;
578 }
579}
580
581
582/*
583 * Dumps statistics for a proxy.
584 * Returns 0 if it had to stop dumping data because of lack of buffer space,
585 * ot non-zero if everything completed.
586 */
587int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri, int flags)
588{
589 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100590 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Willy Tarreau91861262007-10-17 17:06:05 +0200591 struct chunk msg;
592
593 msg.len = 0;
594 msg.str = trash;
595
596 switch (s->data_ctx.stats.px_st) {
597 case DATA_ST_PX_INIT:
598 /* we are on a new proxy */
599
600 if (uri && uri->scope) {
601 /* we have a limited scope, we have to check the proxy name */
602 struct stat_scope *scope;
603 int len;
604
605 len = strlen(px->id);
606 scope = uri->scope;
607
608 while (scope) {
609 /* match exact proxy name */
610 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
611 break;
612
613 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +0200614 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +0200615 break;
616 scope = scope->next;
617 }
618
619 /* proxy name not found : don't dump anything */
620 if (scope == NULL)
621 return 1;
622 }
623
624 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
625 /* fall through */
626
627 case DATA_ST_PX_TH:
Willy Tarreau55bb8452007-10-17 18:44:57 +0200628 if (flags & STAT_FMT_HTML) {
629 /* print a new table */
630 chunk_printf(&msg, sizeof(trash),
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100631 "<table cols=\"26\" class=\"tbl\" width=\"100%%\">\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200632 "<tr align=\"center\" class=\"titre\">"
633 "<th colspan=2 class=\"pxname\">%s</th>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100634 "<th colspan=24 class=\"empty\"></th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200635 "</tr>\n"
636 "<tr align=\"center\" class=\"titre\">"
637 "<th rowspan=2></th>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100638 "<th colspan=3>Queue</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200639 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200640 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100641 "<th colspan=8>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200642 "</tr>\n"
643 "<tr align=\"center\" class=\"titre\">"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200644 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100645 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200646 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200647 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200648 "<th>Status</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100649 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
650 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200651 "</tr>",
Willy Tarreau55bb8452007-10-17 18:44:57 +0200652 px->id);
653
654 if (buffer_write_chunk(rep, &msg) != 0)
655 return 0;
656 }
Willy Tarreau91861262007-10-17 17:06:05 +0200657
658 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
659 /* fall through */
660
661 case DATA_ST_PX_FE:
662 /* print the frontend */
663 if (px->cap & PR_CAP_FE) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200664 if (flags & STAT_FMT_HTML) {
665 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200666 /* name, queue */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200667 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=3></td>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100668 /* sessions : current, max, limit, total, lbtot */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200669 "<td align=right>%d</td><td align=right>%d</td>"
670 "<td align=right>%d</td><td align=right>%d</td>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100671 "<td align=right></td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200672 /* bytes : in, out */
673 "<td align=right>%lld</td><td align=right>%lld</td>"
674 /* denied: req, resp */
675 "<td align=right>%d</td><td align=right>%d</td>"
676 /* errors : request, connect, response */
677 "<td align=right>%d</td><td align=right></td><td align=right></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200678 /* warnings: retries, redispatches */
679 "<td align=right></td><td align=right></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200680 /* server status : reflect frontend status */
Willy Tarreau91861262007-10-17 17:06:05 +0200681 "<td align=center>%s</td>"
682 /* rest of server: nothing */
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100683 "<td align=center colspan=7></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +0200684 "",
685 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
686 px->bytes_in, px->bytes_out,
687 px->denied_req, px->denied_resp,
688 px->failed_req,
689 px->state == PR_STRUN ? "OPEN" :
690 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200691 } else {
692 chunk_printf(&msg, sizeof(trash),
693 /* pxid, name, queue cur, queue max, */
694 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100695 /* sessions : current, max, limit, total */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200696 "%d,%d,%d,%d,"
697 /* bytes : in, out */
698 "%lld,%lld,"
699 /* denied: req, resp */
700 "%d,%d,"
701 /* errors : request, connect, response */
702 "%d,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200703 /* warnings: retries, redispatches */
704 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200705 /* server status : reflect frontend status */
706 "%s,"
707 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200708 ",,,,,,,,"
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100709 /* pid, iid, sid, throttle, lbtot, tracked*/
710 "%d,%d,0,,,,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200711 "\n",
712 px->id,
713 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
714 px->bytes_in, px->bytes_out,
715 px->denied_req, px->denied_resp,
716 px->failed_req,
717 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +0100718 px->state == PR_STIDLE ? "FULL" : "STOP",
719 relative_pid, px->uuid);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200720 }
Willy Tarreau91861262007-10-17 17:06:05 +0200721
722 if (buffer_write_chunk(rep, &msg) != 0)
723 return 0;
724 }
725
726 s->data_ctx.stats.sv = px->srv; /* may be NULL */
727 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
728 /* fall through */
729
730 case DATA_ST_PX_SV:
731 /* stats.sv has been initialized above */
732 while (s->data_ctx.stats.sv != NULL) {
Willy Tarreau2ea81932007-11-30 12:04:38 +0100733 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 +0200734
735 sv = s->data_ctx.stats.sv;
736
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100737 if (sv->tracked)
738 svs = sv->tracked;
739 else
740 svs = sv;
741
Willy Tarreau91861262007-10-17 17:06:05 +0200742 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100743 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +0100744 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100745 else if (svs->state & SRV_RUNNING) {
746 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +0200747 sv_state = 3; /* UP */
748 else
749 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +0100750
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100751 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +0100752 sv_state += 2;
753 }
Willy Tarreau91861262007-10-17 17:06:05 +0200754 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100755 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +0200756 sv_state = 1; /* going up */
757 else
758 sv_state = 0; /* DOWN */
759
760 if ((sv_state == 0) && (s->flags & SN_STAT_HIDEDWN)) {
761 /* do not report servers which are DOWN */
762 s->data_ctx.stats.sv = sv->next;
763 continue;
764 }
765
Willy Tarreau55bb8452007-10-17 18:44:57 +0200766 if (flags & STAT_FMT_HTML) {
Willy Tarreau2ea81932007-11-30 12:04:38 +0100767 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
768 "UP %d/%d &darr;", "UP",
769 "NOLB %d/%d &darr;", "NOLB",
770 "<i>no check</i>" };
Willy Tarreau55bb8452007-10-17 18:44:57 +0200771 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200772 /* name */
773 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200774 /* queue : current, max, limit */
775 "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100776 /* sessions : current, max, limit, total, lbtot */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200777 "<td align=right>%d</td><td align=right>%d</td>"
778 "<td align=right>%s</td><td align=right>%d</td>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100779 "<td align=right>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200780 /* bytes : in, out */
781 "<td align=right>%lld</td><td align=right>%lld</td>"
782 /* denied: req, resp */
783 "<td align=right></td><td align=right>%d</td>"
784 /* errors : request, connect, response */
785 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200786 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100787 "<td align=right>%u</td><td align=right>%u</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200788 "",
789 (sv->state & SRV_BACKUP) ? "backup" : "active",
790 sv_state, sv->id,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200791 sv->nbpend, sv->nbpend_max, LIM2A0(sv->maxqueue, "-"),
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100792 sv->cur_sess, sv->cur_sess_max, LIM2A1(sv->maxconn, "-"),
793 sv->cum_sess, sv->cum_lbconn,
Willy Tarreau91861262007-10-17 17:06:05 +0200794 sv->bytes_in, sv->bytes_out,
795 sv->failed_secu,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200796 sv->failed_conns, sv->failed_resp,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100797 sv->retries, sv->redispatches);
Willy Tarreau91861262007-10-17 17:06:05 +0200798
Willy Tarreau55bb8452007-10-17 18:44:57 +0200799 /* status */
800 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200801
802 if (sv->state & SRV_CHECKED)
803 chunk_printf(&msg, sizeof(trash), "%s ",
804 human_time(now.tv_sec - sv->last_change, 1));
805
Willy Tarreau55bb8452007-10-17 18:44:57 +0200806 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200807 srv_hlt_st[sv_state],
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100808 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
809 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Willy Tarreau91861262007-10-17 17:06:05 +0200810
Willy Tarreau55bb8452007-10-17 18:44:57 +0200811 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200812 /* weight */
813 "</td><td>%d</td>"
814 /* act, bck */
815 "<td>%s</td><td>%s</td>"
816 "",
Willy Tarreau5542af62007-12-03 02:04:00 +0100817 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +0200818 (sv->state & SRV_BACKUP) ? "-" : "Y",
819 (sv->state & SRV_BACKUP) ? "Y" : "-");
820
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200821 /* check failures: unique, fatal, down time */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200822 if (sv->state & SRV_CHECKED)
823 chunk_printf(&msg, sizeof(trash),
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200824 "<td align=right>%d</td><td align=right>%d</td>"
825 "<td nowrap align=right>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100826 "",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100827 svs->failed_checks, svs->down_trans,
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200828 human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100829 else if (sv != svs)
830 chunk_printf(&msg, sizeof(trash),
831 "<td nowrap colspan=3>via %s/%s</td>", svs->proxy->id, svs->id );
Willy Tarreau55bb8452007-10-17 18:44:57 +0200832 else
833 chunk_printf(&msg, sizeof(trash),
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100834 "<td colspan=3></td>");
835
836 /* throttle */
837 if ((sv->state & SRV_WARMINGUP) &&
838 now.tv_sec < sv->last_change + sv->slowstart &&
839 now.tv_sec >= sv->last_change) {
840 unsigned int ratio;
841 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
842 chunk_printf(&msg, sizeof(trash),
843 "<td>%d %%</td></tr>\n", ratio);
844 } else {
845 chunk_printf(&msg, sizeof(trash),
846 "<td>-</td></tr>\n");
847 }
Willy Tarreau55bb8452007-10-17 18:44:57 +0200848 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +0100849 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
850 "UP %d/%d,", "UP,",
851 "NOLB %d/%d,", "NOLB,",
852 "no check," };
Willy Tarreau55bb8452007-10-17 18:44:57 +0200853 chunk_printf(&msg, sizeof(trash),
854 /* pxid, name */
855 "%s,%s,"
856 /* queue : current, max */
857 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100858 /* sessions : current, max, limit, total */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200859 "%d,%d,%s,%d,"
860 /* bytes : in, out */
861 "%lld,%lld,"
862 /* denied: req, resp */
863 ",%d,"
864 /* errors : request, connect, response */
865 ",%d,%d,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200866 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100867 "%u,%u,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200868 "",
869 px->id, sv->id,
870 sv->nbpend, sv->nbpend_max,
Willy Tarreaudcd47712007-11-04 23:35:08 +0100871 sv->cur_sess, sv->cur_sess_max, LIM2A0(sv->maxconn, ""), sv->cum_sess,
Willy Tarreau55bb8452007-10-17 18:44:57 +0200872 sv->bytes_in, sv->bytes_out,
873 sv->failed_secu,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200874 sv->failed_conns, sv->failed_resp,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100875 sv->retries, sv->redispatches);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200876
877 /* status */
878 chunk_printf(&msg, sizeof(trash),
879 srv_hlt_st[sv_state],
880 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
881 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
Willy Tarreau91861262007-10-17 17:06:05 +0200882
Willy Tarreau55bb8452007-10-17 18:44:57 +0200883 chunk_printf(&msg, sizeof(trash),
884 /* weight, active, backup */
885 "%d,%d,%d,"
886 "",
Willy Tarreau5542af62007-12-03 02:04:00 +0100887 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +0200888 (sv->state & SRV_BACKUP) ? 0 : 1,
889 (sv->state & SRV_BACKUP) ? 1 : 0);
890
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200891 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200892 if (sv->state & SRV_CHECKED)
893 chunk_printf(&msg, sizeof(trash),
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200894 "%d,%d,%d,%d,",
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200895 sv->failed_checks, sv->down_trans,
896 now.tv_sec - sv->last_change, srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +0200897 else
898 chunk_printf(&msg, sizeof(trash),
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200899 ",,,,");
900
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100901 /* queue limit, pid, iid, sid, */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200902 chunk_printf(&msg, sizeof(trash),
Willy Tarreaudcd47712007-11-04 23:35:08 +0100903 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100904 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +0100905 LIM2A0(sv->maxqueue, ""),
906 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100907
908 /* throttle */
909 if ((sv->state & SRV_WARMINGUP) &&
910 now.tv_sec < sv->last_change + sv->slowstart &&
911 now.tv_sec >= sv->last_change) {
912 unsigned int ratio;
913 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
914 chunk_printf(&msg, sizeof(trash), "%d", ratio);
915 }
916
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100917 /* sessions: lbtot */
918 chunk_printf(&msg, sizeof(trash), ",%d", sv->cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100919
920 /* tracked */
921 if (sv->tracked)
922 chunk_printf(&msg, sizeof(trash), ",%s/%s",
923 sv->tracked->proxy->id, sv->tracked->id);
924 else
925 chunk_printf(&msg, sizeof(trash), ",");
926
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100927 /* ',' then EOL */
928 chunk_printf(&msg, sizeof(trash), ",\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200929 }
Willy Tarreau91861262007-10-17 17:06:05 +0200930 if (buffer_write_chunk(rep, &msg) != 0)
931 return 0;
932
933 s->data_ctx.stats.sv = sv->next;
934 } /* while sv */
935
936 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
937 /* fall through */
938
939 case DATA_ST_PX_BE:
940 /* print the backend */
941 if (px->cap & PR_CAP_BE) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200942 if (flags & STAT_FMT_HTML) {
943 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200944 /* name */
945 "<tr align=center class=\"backend\"><td>Backend</td>"
946 /* queue : current, max */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200947 "<td align=right>%d</td><td align=right>%d</td><td></td>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100948 /* sessions : current, max, limit, total, lbtot */
949 "<td align=right>%d</td><td align=right>%d</td>"
950 "<td align=right>%d</td><td align=right>%d</td>"
951 "<td align=right>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200952 /* bytes : in, out */
953 "<td align=right>%lld</td><td align=right>%lld</td>"
954 /* denied: req, resp */
955 "<td align=right>%d</td><td align=right>%d</td>"
956 /* errors : request, connect, response */
957 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200958 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100959 "<td align=right>%u</td><td align=right>%u</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200960 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +0200961 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200962 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +0200963 * active and backups. */
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200964 "<td align=center nowrap>%s %s</td><td align=center>%d</td>"
965 "<td align=center>%d</td><td align=center>%d</td>",
Willy Tarreau91861262007-10-17 17:06:05 +0200966 px->nbpend /* or px->totpend ? */, px->nbpend_max,
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100967 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn, px->cum_lbconn,
Willy Tarreau91861262007-10-17 17:06:05 +0200968 px->bytes_in, px->bytes_out,
969 px->denied_req, px->denied_resp,
970 px->failed_conns, px->failed_resp,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200971 px->retries, px->redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200972 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +0100973 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
974 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +0100975 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +0100976 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200977
978 chunk_printf(&msg, sizeof(trash),
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100979 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200980 "<td align=center>&nbsp;</td><td align=\"right\">%d</td>"
981 "<td align=\"right\" nowrap>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100982 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200983 "</tr>",
984 px->down_trans,
985 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200986 } else {
987 chunk_printf(&msg, sizeof(trash),
988 /* pxid, name */
989 "%s,BACKEND,"
990 /* queue : current, max */
991 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100992 /* sessions : current, max, limit, total */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200993 "%d,%d,%d,%d,"
994 /* bytes : in, out */
995 "%lld,%lld,"
996 /* denied: req, resp */
997 "%d,%d,"
998 /* errors : request, connect, response */
999 ",%d,%d,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001000 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001001 "%u,%u,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001002 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02001003 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001004 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02001005 * active and backups. */
1006 "%s,"
1007 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001008 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001009 ",%d,%d,%d,,"
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001010 /* pid, iid, sid, throttle, lbtot, tracked,*/
1011 "%d,%d,0,,%d,,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001012 "\n",
1013 px->id,
1014 px->nbpend /* or px->totpend ? */, px->nbpend_max,
1015 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
1016 px->bytes_in, px->bytes_out,
1017 px->denied_req, px->denied_resp,
1018 px->failed_conns, px->failed_resp,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001019 px->retries, px->redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01001020 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01001021 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001022 px->srv_act, px->srv_bck,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001023 px->down_trans, now.tv_sec - px->last_change,
Willy Tarreau20697042007-11-15 23:26:18 +01001024 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001025 relative_pid, px->uuid,
1026 px->cum_lbconn);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001027 }
Willy Tarreau91861262007-10-17 17:06:05 +02001028 if (buffer_write_chunk(rep, &msg) != 0)
1029 return 0;
1030 }
1031
1032 s->data_ctx.stats.px_st = DATA_ST_PX_END;
1033 /* fall through */
1034
1035 case DATA_ST_PX_END:
Willy Tarreau55bb8452007-10-17 18:44:57 +02001036 if (flags & STAT_FMT_HTML) {
1037 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001038
Willy Tarreau55bb8452007-10-17 18:44:57 +02001039 if (buffer_write_chunk(rep, &msg) != 0)
1040 return 0;
1041 }
Willy Tarreau91861262007-10-17 17:06:05 +02001042
1043 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
1044 /* fall through */
1045
1046 case DATA_ST_PX_FIN:
1047 return 1;
1048
1049 default:
1050 /* unknown state, we should put an abort() here ! */
1051 return 1;
1052 }
1053}
1054
1055/*
1056 * Local variables:
1057 * c-indent-level: 8
1058 * c-basic-offset: 8
1059 * End:
1060 */