blob: ddadddd1d69e53c59945f81482c21d142fe1cbea [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 Oledzkif58a9622008-02-23 01:19:10 +0100174 "pid,iid,sid,throttle,lbtot,tracked,type,"
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 Tarreau39f7e6d2008-03-17 21:38:24 +0100180 * s->cli_state == CL_STSHUTR. It *may* make use of informations from <uri>.
181 * s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200182 * 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 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100185int stats_dump_raw(struct session *s, struct uri_auth *uri)
Willy Tarreau3e76e722007-10-17 18:57:38 +0200186{
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 Tarreau39f7e6d2008-03-17 21:38:24 +0100205 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100206 print_csv_header(&msg, sizeof(trash));
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200207 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100208 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 Tarreau39f7e6d2008-03-17 21:38:24 +0100216 if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100217 chunk_printf(&msg, sizeof(trash),
218 "Name: " PRODUCT_NAME "\n"
219 "Version: " HAPROXY_VERSION "\n"
220 "Release_date: " HAPROXY_DATE "\n"
221 "Nbproc: %d\n"
222 "Process_num: %d\n"
223 "Pid: %d\n"
224 "Uptime: %dd %dh%02dm%02ds\n"
225 "Uptime_sec: %d\n"
226 "Memmax_MB: %d\n"
227 "Ulimit-n: %d\n"
228 "Maxsock: %d\n"
229 "Maxconn: %d\n"
230 "CurrConns: %d\n"
231 "",
232 global.nbproc,
233 relative_pid,
234 pid,
235 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
236 up,
237 global.rlimit_memmax,
238 global.rlimit_nofile,
239 global.maxsock,
240 global.maxconn,
241 actconn
242 );
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200243 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100244 return 0;
245 }
246
Willy Tarreau3e76e722007-10-17 18:57:38 +0200247 s->data_ctx.stats.px = proxy;
248 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100249
250 s->data_ctx.stats.sv = NULL;
251 s->data_ctx.stats.sv_st = 0;
252
Willy Tarreau3e76e722007-10-17 18:57:38 +0200253 s->data_state = DATA_ST_LIST;
254 /* fall through */
255
256 case DATA_ST_LIST:
257 /* dump proxies */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100258 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100259 while (s->data_ctx.stats.px) {
260 px = s->data_ctx.stats.px;
261 /* skip the disabled proxies and non-networked ones */
262 if (px->state != PR_STSTOPPED &&
263 (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100264 if (stats_dump_proxy(s, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100265 return 0;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200266
Willy Tarreaua8efd362008-01-03 10:19:15 +0100267 s->data_ctx.stats.px = px->next;
268 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
269 }
270 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200271 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200272
273 s->data_state = DATA_ST_END;
274 /* fall through */
275
276 case DATA_ST_END:
277 s->data_state = DATA_ST_FIN;
278 return 1;
279
280 case DATA_ST_FIN:
281 return 1;
282
283 default:
284 /* unknown state ! */
285 return -1;
286 }
287}
288
289
290/*
291 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau91861262007-10-17 17:06:05 +0200292 * s->cli_state == CL_STSHUTR. It stops by itself by unsetting the SN_SELF_GEN
293 * flag from the session, which it uses to keep on being called when there is
294 * free space in the buffer, of simply by letting an empty buffer upon return.
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100295 * s->data_ctx must have been zeroed before the first call, and the flags set.
Willy Tarreau91861262007-10-17 17:06:05 +0200296 * It returns 0 if it had to stop writing data and an I/O is needed, 1 if the
297 * dump is finished and the session must be closed, or -1 in case of any error.
298 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100299int stats_dump_http(struct session *s, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200300{
301 struct buffer *rep = s->rep;
302 struct proxy *px;
303 struct chunk msg;
304 unsigned int up;
305
306 msg.len = 0;
307 msg.str = trash;
308
309 switch (s->data_state) {
310 case DATA_ST_INIT:
311 /* the function had not been called yet */
312 s->flags |= SN_SELF_GEN; // more data will follow
313
314 chunk_printf(&msg, sizeof(trash),
315 "HTTP/1.0 200 OK\r\n"
316 "Cache-Control: no-cache\r\n"
317 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200318 "Content-Type: %s\r\n",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100319 (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +0200320
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100321 if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau91861262007-10-17 17:06:05 +0200322 chunk_printf(&msg, sizeof(trash), "Refresh: %d\r\n",
323 uri->refresh);
324
325 chunk_printf(&msg, sizeof(trash), "\r\n");
326
327 s->txn.status = 200;
328 client_retnclose(s, &msg); // send the start of the response.
329 msg.len = 0;
330
331 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
332 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
333 if (!(s->flags & SN_FINST_MASK))
334 s->flags |= SN_FINST_R;
335
336 if (s->txn.meth == HTTP_METH_HEAD) {
337 /* that's all we return in case of HEAD request */
338 s->data_state = DATA_ST_FIN;
339 s->flags &= ~SN_SELF_GEN;
340 return 1;
341 }
342
343 s->data_state = DATA_ST_HEAD; /* let's start producing data */
344 /* fall through */
345
346 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100347 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200348 /* WARNING! This must fit in the first buffer !!! */
349 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200350 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
351 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
352 "<style type=\"text/css\"><!--\n"
353 "body {"
354 " font-family: helvetica, arial;"
355 " font-size: 12px;"
356 " font-weight: normal;"
357 " color: black;"
358 " background: white;"
359 "}\n"
360 "th,td {"
361 " font-size: 0.8em;"
362 " align: center;"
363 "}\n"
364 "h1 {"
365 " font-size: xx-large;"
366 " margin-bottom: 0.5em;"
367 "}\n"
368 "h2 {"
369 " font-family: helvetica, arial;"
370 " font-size: x-large;"
371 " font-weight: bold;"
372 " font-style: italic;"
373 " color: #6020a0;"
374 " margin-top: 0em;"
375 " margin-bottom: 0em;"
376 "}\n"
377 "h3 {"
378 " font-family: helvetica, arial;"
379 " font-size: 16px;"
380 " font-weight: bold;"
381 " color: #b00040;"
382 " background: #e8e8d0;"
383 " margin-top: 0em;"
384 " margin-bottom: 0em;"
385 "}\n"
386 "li {"
387 " margin-top: 0.25em;"
388 " margin-right: 2em;"
389 "}\n"
390 ".hr {margin-top: 0.25em;"
391 " border-color: black;"
392 " border-bottom-style: solid;"
393 "}\n"
394 ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n"
395 ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n"
396 ".total {background: #20D0D0;color: #ffff80;}\n"
397 ".frontend {background: #e8e8d0;}\n"
398 ".backend {background: #e8e8d0;}\n"
399 ".active0 {background: #ff9090;}\n"
400 ".active1 {background: #ffd020;}\n"
401 ".active2 {background: #ffffa0;}\n"
402 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100403 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
404 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
405 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200406 ".backup0 {background: #ff9090;}\n"
407 ".backup1 {background: #ff80ff;}\n"
408 ".backup2 {background: #c060ff;}\n"
409 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100410 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
411 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
412 ".backup6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200413 "table.tbl { border-collapse: collapse; border-style: none;}\n"
414 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray;}\n"
415 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
416 "table.tbl th.empty { border-style: none; empty-cells: hide;}\n"
417 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
418 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
419 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
420 "-->\n"
421 "</style></head>\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200422 } else {
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100423 print_csv_header(&msg, sizeof(trash));
Willy Tarreau55bb8452007-10-17 18:44:57 +0200424 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200425 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200426 return 0;
427
428 s->data_state = DATA_ST_INFO;
429 /* fall through */
430
431 case DATA_ST_INFO:
432 up = (now.tv_sec - start_date.tv_sec);
433
434 /* WARNING! this has to fit the first packet too.
435 * We are around 3.5 kB, add adding entries will
436 * become tricky if we want to support 4kB buffers !
437 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100438 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200439 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200440 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
441 PRODUCT_NAME "%s</a></h1>\n"
442 "<h2>Statistics Report for pid %d</h2>\n"
443 "<hr width=\"100%%\" class=\"hr\">\n"
444 "<h3>&gt; General process information</h3>\n"
445 "<table border=0 cols=4><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100446 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200447 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
448 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
449 "<b>maxsock = </b> %d<br>\n"
450 "<b>maxconn = </b> %d (current conns = %d)<br>\n"
451 "</td><td align=\"center\" nowrap>\n"
452 "<table class=\"lgd\"><tr>\n"
453 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
454 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
455 "</tr><tr>\n"
456 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
457 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
458 "</tr><tr>\n"
459 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
460 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
461 "</tr><tr>\n"
462 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100463 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200464 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100465 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +0200466 "</td>"
467 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
468 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
469 "",
470 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Willy Tarreaua8efd362008-01-03 10:19:15 +0100471 pid, pid,
472 relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +0200473 up / 86400, (up % 86400) / 3600,
474 (up % 3600) / 60, (up % 60),
475 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
476 global.rlimit_memmax ? " MB" : "",
477 global.rlimit_nofile,
478 global.maxsock,
479 global.maxconn,
480 actconn
481 );
482
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100483 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200484 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200485 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
486 uri->uri_prefix,
487 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100488 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200489 else
490 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200491 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
492 uri->uri_prefix,
493 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100494 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200495
Willy Tarreau55bb8452007-10-17 18:44:57 +0200496 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100497 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200498 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200499 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
500 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100501 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200502 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200503 else
504 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200505 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
506 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100507 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200508 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200509 }
Willy Tarreau91861262007-10-17 17:06:05 +0200510
Willy Tarreau55bb8452007-10-17 18:44:57 +0200511 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200512 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
513 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100514 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
515 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200516
Willy Tarreau55bb8452007-10-17 18:44:57 +0200517 chunk_printf(&msg, sizeof(trash),
Willy Tarreau5031e6a2007-10-18 11:05:48 +0200518 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
519 uri->uri_prefix,
520 (uri->refresh > 0) ? ";norefresh" : "");
521
522 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200523 "</td>"
524 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
525 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
526 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
527 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
528 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
529 "</ul>"
530 "</td>"
531 "</tr></table>\n"
532 ""
533 );
534
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200535 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200536 return 0;
537 }
Willy Tarreau91861262007-10-17 17:06:05 +0200538
Willy Tarreau91861262007-10-17 17:06:05 +0200539 s->data_ctx.stats.px = proxy;
540 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
541 s->data_state = DATA_ST_LIST;
542 /* fall through */
543
544 case DATA_ST_LIST:
545 /* dump proxies */
546 while (s->data_ctx.stats.px) {
547 px = s->data_ctx.stats.px;
548 /* skip the disabled proxies and non-networked ones */
549 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100550 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200551 return 0;
552
553 s->data_ctx.stats.px = px->next;
554 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
555 }
556 /* here, we just have reached the last proxy */
557
558 s->data_state = DATA_ST_END;
559 /* fall through */
560
561 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100562 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200563 chunk_printf(&msg, sizeof(trash), "</body></html>\n");
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200564 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200565 return 0;
566 }
Willy Tarreau91861262007-10-17 17:06:05 +0200567
568 s->data_state = DATA_ST_FIN;
569 /* fall through */
570
571 case DATA_ST_FIN:
572 s->flags &= ~SN_SELF_GEN;
573 return 1;
574
575 default:
576 /* unknown state ! */
577 s->flags &= ~SN_SELF_GEN;
578 return -1;
579 }
580}
581
582
583/*
584 * Dumps statistics for a proxy.
585 * Returns 0 if it had to stop dumping data because of lack of buffer space,
586 * ot non-zero if everything completed.
587 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100588int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200589{
590 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100591 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Willy Tarreau91861262007-10-17 17:06:05 +0200592 struct chunk msg;
593
594 msg.len = 0;
595 msg.str = trash;
596
597 switch (s->data_ctx.stats.px_st) {
598 case DATA_ST_PX_INIT:
599 /* we are on a new proxy */
600
601 if (uri && uri->scope) {
602 /* we have a limited scope, we have to check the proxy name */
603 struct stat_scope *scope;
604 int len;
605
606 len = strlen(px->id);
607 scope = uri->scope;
608
609 while (scope) {
610 /* match exact proxy name */
611 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
612 break;
613
614 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +0200615 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +0200616 break;
617 scope = scope->next;
618 }
619
620 /* proxy name not found : don't dump anything */
621 if (scope == NULL)
622 return 1;
623 }
624
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100625 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100626 (px->uuid != s->data_ctx.stats.iid))
627 return 1;
628
Willy Tarreau91861262007-10-17 17:06:05 +0200629 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
630 /* fall through */
631
632 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100633 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200634 /* print a new table */
635 chunk_printf(&msg, sizeof(trash),
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100636 "<table cols=\"26\" class=\"tbl\" width=\"100%%\">\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200637 "<tr align=\"center\" class=\"titre\">"
638 "<th colspan=2 class=\"pxname\">%s</th>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100639 "<th colspan=24 class=\"empty\"></th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200640 "</tr>\n"
641 "<tr align=\"center\" class=\"titre\">"
642 "<th rowspan=2></th>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100643 "<th colspan=3>Queue</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200644 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200645 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100646 "<th colspan=8>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200647 "</tr>\n"
648 "<tr align=\"center\" class=\"titre\">"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200649 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100650 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200651 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200652 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200653 "<th>Status</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100654 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
655 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200656 "</tr>",
Willy Tarreau55bb8452007-10-17 18:44:57 +0200657 px->id);
658
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200659 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200660 return 0;
661 }
Willy Tarreau91861262007-10-17 17:06:05 +0200662
663 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
664 /* fall through */
665
666 case DATA_ST_PX_FE:
667 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100668 if ((px->cap & PR_CAP_FE) &&
669 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
670 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200671 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200672 /* name, queue */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200673 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=3></td>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100674 /* sessions : current, max, limit, total, lbtot */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200675 "<td align=right>%d</td><td align=right>%d</td>"
676 "<td align=right>%d</td><td align=right>%d</td>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100677 "<td align=right></td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200678 /* bytes : in, out */
679 "<td align=right>%lld</td><td align=right>%lld</td>"
680 /* denied: req, resp */
681 "<td align=right>%d</td><td align=right>%d</td>"
682 /* errors : request, connect, response */
683 "<td align=right>%d</td><td align=right></td><td align=right></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200684 /* warnings: retries, redispatches */
685 "<td align=right></td><td align=right></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200686 /* server status : reflect frontend status */
Willy Tarreau91861262007-10-17 17:06:05 +0200687 "<td align=center>%s</td>"
688 /* rest of server: nothing */
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100689 "<td align=center colspan=7></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +0200690 "",
691 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
692 px->bytes_in, px->bytes_out,
693 px->denied_req, px->denied_resp,
694 px->failed_req,
695 px->state == PR_STRUN ? "OPEN" :
696 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200697 } else {
698 chunk_printf(&msg, sizeof(trash),
699 /* pxid, name, queue cur, queue max, */
700 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100701 /* sessions : current, max, limit, total */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200702 "%d,%d,%d,%d,"
703 /* bytes : in, out */
704 "%lld,%lld,"
705 /* denied: req, resp */
706 "%d,%d,"
707 /* errors : request, connect, response */
708 "%d,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200709 /* warnings: retries, redispatches */
710 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200711 /* server status : reflect frontend status */
712 "%s,"
713 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200714 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100715 /* pid, iid, sid, throttle, lbtot, tracked, type */
716 "%d,%d,0,,,,%d,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200717 "\n",
718 px->id,
719 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
720 px->bytes_in, px->bytes_out,
721 px->denied_req, px->denied_resp,
722 px->failed_req,
723 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +0100724 px->state == PR_STIDLE ? "FULL" : "STOP",
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100725 relative_pid, px->uuid, STATS_TYPE_FE);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200726 }
Willy Tarreau91861262007-10-17 17:06:05 +0200727
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200728 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200729 return 0;
730 }
731
732 s->data_ctx.stats.sv = px->srv; /* may be NULL */
733 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
734 /* fall through */
735
736 case DATA_ST_PX_SV:
737 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100738 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
739
Willy Tarreau2ea81932007-11-30 12:04:38 +0100740 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 +0200741
742 sv = s->data_ctx.stats.sv;
743
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100744 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100745 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
746 break;
747
748 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
749 continue;
750 }
751
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100752 if (sv->tracked)
753 svs = sv->tracked;
754 else
755 svs = sv;
756
Willy Tarreau91861262007-10-17 17:06:05 +0200757 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100758 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +0100759 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100760 else if (svs->state & SRV_RUNNING) {
761 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +0200762 sv_state = 3; /* UP */
763 else
764 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +0100765
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100766 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +0100767 sv_state += 2;
768 }
Willy Tarreau91861262007-10-17 17:06:05 +0200769 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100770 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +0200771 sv_state = 1; /* going up */
772 else
773 sv_state = 0; /* DOWN */
774
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100775 if ((sv_state == 0) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +0200776 /* do not report servers which are DOWN */
777 s->data_ctx.stats.sv = sv->next;
778 continue;
779 }
780
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100781 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +0100782 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
783 "UP %d/%d &darr;", "UP",
784 "NOLB %d/%d &darr;", "NOLB",
785 "<i>no check</i>" };
Willy Tarreau55bb8452007-10-17 18:44:57 +0200786 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200787 /* name */
788 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200789 /* queue : current, max, limit */
790 "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100791 /* sessions : current, max, limit, total, lbtot */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200792 "<td align=right>%d</td><td align=right>%d</td>"
793 "<td align=right>%s</td><td align=right>%d</td>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100794 "<td align=right>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200795 /* bytes : in, out */
796 "<td align=right>%lld</td><td align=right>%lld</td>"
797 /* denied: req, resp */
798 "<td align=right></td><td align=right>%d</td>"
799 /* errors : request, connect, response */
800 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200801 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100802 "<td align=right>%u</td><td align=right>%u</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200803 "",
804 (sv->state & SRV_BACKUP) ? "backup" : "active",
805 sv_state, sv->id,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200806 sv->nbpend, sv->nbpend_max, LIM2A0(sv->maxqueue, "-"),
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100807 sv->cur_sess, sv->cur_sess_max, LIM2A1(sv->maxconn, "-"),
808 sv->cum_sess, sv->cum_lbconn,
Willy Tarreau91861262007-10-17 17:06:05 +0200809 sv->bytes_in, sv->bytes_out,
810 sv->failed_secu,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200811 sv->failed_conns, sv->failed_resp,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100812 sv->retries, sv->redispatches);
Willy Tarreau91861262007-10-17 17:06:05 +0200813
Willy Tarreau55bb8452007-10-17 18:44:57 +0200814 /* status */
815 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200816
817 if (sv->state & SRV_CHECKED)
818 chunk_printf(&msg, sizeof(trash), "%s ",
819 human_time(now.tv_sec - sv->last_change, 1));
820
Willy Tarreau55bb8452007-10-17 18:44:57 +0200821 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200822 srv_hlt_st[sv_state],
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100823 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
824 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Willy Tarreau91861262007-10-17 17:06:05 +0200825
Willy Tarreau55bb8452007-10-17 18:44:57 +0200826 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200827 /* weight */
828 "</td><td>%d</td>"
829 /* act, bck */
830 "<td>%s</td><td>%s</td>"
831 "",
Willy Tarreau5542af62007-12-03 02:04:00 +0100832 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +0200833 (sv->state & SRV_BACKUP) ? "-" : "Y",
834 (sv->state & SRV_BACKUP) ? "Y" : "-");
835
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200836 /* check failures: unique, fatal, down time */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200837 if (sv->state & SRV_CHECKED)
838 chunk_printf(&msg, sizeof(trash),
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200839 "<td align=right>%d</td><td align=right>%d</td>"
840 "<td nowrap align=right>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100841 "",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100842 svs->failed_checks, svs->down_trans,
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200843 human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100844 else if (sv != svs)
845 chunk_printf(&msg, sizeof(trash),
846 "<td nowrap colspan=3>via %s/%s</td>", svs->proxy->id, svs->id );
Willy Tarreau55bb8452007-10-17 18:44:57 +0200847 else
848 chunk_printf(&msg, sizeof(trash),
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100849 "<td colspan=3></td>");
850
851 /* throttle */
852 if ((sv->state & SRV_WARMINGUP) &&
853 now.tv_sec < sv->last_change + sv->slowstart &&
854 now.tv_sec >= sv->last_change) {
855 unsigned int ratio;
856 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
857 chunk_printf(&msg, sizeof(trash),
858 "<td>%d %%</td></tr>\n", ratio);
859 } else {
860 chunk_printf(&msg, sizeof(trash),
861 "<td>-</td></tr>\n");
862 }
Willy Tarreau55bb8452007-10-17 18:44:57 +0200863 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +0100864 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
865 "UP %d/%d,", "UP,",
866 "NOLB %d/%d,", "NOLB,",
867 "no check," };
Willy Tarreau55bb8452007-10-17 18:44:57 +0200868 chunk_printf(&msg, sizeof(trash),
869 /* pxid, name */
870 "%s,%s,"
871 /* queue : current, max */
872 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100873 /* sessions : current, max, limit, total */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200874 "%d,%d,%s,%d,"
875 /* bytes : in, out */
876 "%lld,%lld,"
877 /* denied: req, resp */
878 ",%d,"
879 /* errors : request, connect, response */
880 ",%d,%d,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200881 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100882 "%u,%u,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200883 "",
884 px->id, sv->id,
885 sv->nbpend, sv->nbpend_max,
Willy Tarreaudcd47712007-11-04 23:35:08 +0100886 sv->cur_sess, sv->cur_sess_max, LIM2A0(sv->maxconn, ""), sv->cum_sess,
Willy Tarreau55bb8452007-10-17 18:44:57 +0200887 sv->bytes_in, sv->bytes_out,
888 sv->failed_secu,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200889 sv->failed_conns, sv->failed_resp,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100890 sv->retries, sv->redispatches);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200891
892 /* status */
893 chunk_printf(&msg, sizeof(trash),
894 srv_hlt_st[sv_state],
895 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
896 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
Willy Tarreau91861262007-10-17 17:06:05 +0200897
Willy Tarreau55bb8452007-10-17 18:44:57 +0200898 chunk_printf(&msg, sizeof(trash),
899 /* weight, active, backup */
900 "%d,%d,%d,"
901 "",
Willy Tarreau5542af62007-12-03 02:04:00 +0100902 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +0200903 (sv->state & SRV_BACKUP) ? 0 : 1,
904 (sv->state & SRV_BACKUP) ? 1 : 0);
905
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200906 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200907 if (sv->state & SRV_CHECKED)
908 chunk_printf(&msg, sizeof(trash),
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200909 "%d,%d,%d,%d,",
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200910 sv->failed_checks, sv->down_trans,
911 now.tv_sec - sv->last_change, srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +0200912 else
913 chunk_printf(&msg, sizeof(trash),
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200914 ",,,,");
915
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100916 /* queue limit, pid, iid, sid, */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200917 chunk_printf(&msg, sizeof(trash),
Willy Tarreaudcd47712007-11-04 23:35:08 +0100918 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100919 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +0100920 LIM2A0(sv->maxqueue, ""),
921 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100922
923 /* throttle */
924 if ((sv->state & SRV_WARMINGUP) &&
925 now.tv_sec < sv->last_change + sv->slowstart &&
926 now.tv_sec >= sv->last_change) {
927 unsigned int ratio;
928 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100929 chunk_printf(&msg, sizeof(trash), "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100930 }
931
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100932 /* sessions: lbtot */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100933 chunk_printf(&msg, sizeof(trash), ",%d,", sv->cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100934
935 /* tracked */
936 if (sv->tracked)
Krzysztof Piotr Oledzkif58a9622008-02-23 01:19:10 +0100937 chunk_printf(&msg, sizeof(trash), "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100938 sv->tracked->proxy->id, sv->tracked->id);
939 else
940 chunk_printf(&msg, sizeof(trash), ",");
941
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100942 /* type, then EOL */
943 chunk_printf(&msg, sizeof(trash), "%d,\n", STATS_TYPE_SV);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200944 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200945 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200946 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100947 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +0200948
949 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
950 /* fall through */
951
952 case DATA_ST_PX_BE:
953 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100954 if ((px->cap & PR_CAP_BE) &&
955 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
956 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200957 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200958 /* name */
959 "<tr align=center class=\"backend\"><td>Backend</td>"
960 /* queue : current, max */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200961 "<td align=right>%d</td><td align=right>%d</td><td></td>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100962 /* sessions : current, max, limit, total, lbtot */
963 "<td align=right>%d</td><td align=right>%d</td>"
964 "<td align=right>%d</td><td align=right>%d</td>"
965 "<td align=right>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200966 /* bytes : in, out */
967 "<td align=right>%lld</td><td align=right>%lld</td>"
968 /* denied: req, resp */
969 "<td align=right>%d</td><td align=right>%d</td>"
970 /* errors : request, connect, response */
971 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200972 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100973 "<td align=right>%u</td><td align=right>%u</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200974 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +0200975 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200976 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +0200977 * active and backups. */
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200978 "<td align=center nowrap>%s %s</td><td align=center>%d</td>"
979 "<td align=center>%d</td><td align=center>%d</td>",
Willy Tarreau91861262007-10-17 17:06:05 +0200980 px->nbpend /* or px->totpend ? */, px->nbpend_max,
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100981 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn, px->cum_lbconn,
Willy Tarreau91861262007-10-17 17:06:05 +0200982 px->bytes_in, px->bytes_out,
983 px->denied_req, px->denied_resp,
984 px->failed_conns, px->failed_resp,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200985 px->retries, px->redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200986 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +0100987 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
988 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +0100989 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +0100990 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200991
992 chunk_printf(&msg, sizeof(trash),
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100993 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200994 "<td align=center>&nbsp;</td><td align=\"right\">%d</td>"
995 "<td align=\"right\" nowrap>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100996 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200997 "</tr>",
998 px->down_trans,
999 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001000 } else {
1001 chunk_printf(&msg, sizeof(trash),
1002 /* pxid, name */
1003 "%s,BACKEND,"
1004 /* queue : current, max */
1005 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001006 /* sessions : current, max, limit, total */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001007 "%d,%d,%d,%d,"
1008 /* bytes : in, out */
1009 "%lld,%lld,"
1010 /* denied: req, resp */
1011 "%d,%d,"
1012 /* errors : request, connect, response */
1013 ",%d,%d,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001014 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001015 "%u,%u,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001016 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02001017 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001018 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02001019 * active and backups. */
1020 "%s,"
1021 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001022 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001023 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001024 /* pid, iid, sid, throttle, lbtot, tracked, type */
1025 "%d,%d,0,,%d,,%d,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001026 "\n",
1027 px->id,
1028 px->nbpend /* or px->totpend ? */, px->nbpend_max,
1029 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
1030 px->bytes_in, px->bytes_out,
1031 px->denied_req, px->denied_resp,
1032 px->failed_conns, px->failed_resp,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001033 px->retries, px->redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01001034 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01001035 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001036 px->srv_act, px->srv_bck,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001037 px->down_trans, now.tv_sec - px->last_change,
Willy Tarreau20697042007-11-15 23:26:18 +01001038 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001039 relative_pid, px->uuid,
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001040 px->cum_lbconn, STATS_TYPE_BE);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001041 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001042 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001043 return 0;
1044 }
1045
1046 s->data_ctx.stats.px_st = DATA_ST_PX_END;
1047 /* fall through */
1048
1049 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001050 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001051 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001052
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001053 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001054 return 0;
1055 }
Willy Tarreau91861262007-10-17 17:06:05 +02001056
1057 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
1058 /* fall through */
1059
1060 case DATA_ST_PX_FIN:
1061 return 1;
1062
1063 default:
1064 /* unknown state, we should put an abort() here ! */
1065 return 1;
1066 }
1067}
1068
1069/*
1070 * Local variables:
1071 * c-indent-level: 8
1072 * c-basic-offset: 8
1073 * End:
1074 */