blob: 30cb0edf3b8c1f43fb9b7279682ba8d56408a015 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
2 * Functions dedicated to statistics output
3 *
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreau91861262007-10-17 17:06:05 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020019#include <pwd.h>
20#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau10522fd2008-07-09 20:12:41 +020026#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020027#include <common/compat.h>
28#include <common/config.h>
29#include <common/debug.h>
30#include <common/memory.h>
31#include <common/mini-clist.h>
32#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020033#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020034#include <common/time.h>
35#include <common/uri_auth.h>
36#include <common/version.h>
37
Willy Tarreau91861262007-10-17 17:06:05 +020038#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020039
40#include <proto/backend.h>
41#include <proto/buffers.h>
42#include <proto/dumpstats.h>
43#include <proto/fd.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020044#include <proto/proto_uxst.h>
Willy Tarreau91861262007-10-17 17:06:05 +020045#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020046#include <proto/server.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010047#include <proto/stream_interface.h>
Willy Tarreau91861262007-10-17 17:06:05 +020048
Willy Tarreaufbee7132007-10-18 13:53:22 +020049/* This function parses a "stats" statement in the "global" section. It returns
50 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
51 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
52 * zero included. The trailing '\n' must not be written. The function must be
53 * called with <args> pointing to the first word after "stats".
54 */
Willy Tarreau10522fd2008-07-09 20:12:41 +020055static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
56 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +020057{
Willy Tarreau10522fd2008-07-09 20:12:41 +020058 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +020059 if (!strcmp(args[0], "socket")) {
60 struct sockaddr_un su;
61 int cur_arg;
62
63 if (*args[1] == 0) {
64 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
65 return -1;
66 }
67
68 if (global.stats_sock.state != LI_NEW) {
69 snprintf(err, errlen, "'stats socket' already specified in global section");
70 return -1;
71 }
72
73 su.sun_family = AF_UNIX;
74 strncpy(su.sun_path, args[1], sizeof(su.sun_path));
75 su.sun_path[sizeof(su.sun_path) - 1] = 0;
76 memcpy(&global.stats_sock.addr, &su, sizeof(su)); // guaranteed to fit
77
78 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +010079 global.stats_sock.options = LI_O_NONE;
Willy Tarreaufbee7132007-10-18 13:53:22 +020080 global.stats_sock.accept = uxst_event_accept;
Willy Tarreaub1356cf2008-12-07 16:06:43 +010081 global.stats_sock.handler = uxst_process_session;
82 global.stats_sock.analysers = AN_REQ_UNIX_STATS;
Willy Tarreaufbee7132007-10-18 13:53:22 +020083 global.stats_sock.private = NULL;
84
85 cur_arg = 2;
86 while (*args[cur_arg]) {
87 if (!strcmp(args[cur_arg], "uid")) {
88 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
89 cur_arg += 2;
90 }
91 else if (!strcmp(args[cur_arg], "gid")) {
92 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
93 cur_arg += 2;
94 }
95 else if (!strcmp(args[cur_arg], "mode")) {
96 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
97 cur_arg += 2;
98 }
99 else if (!strcmp(args[cur_arg], "user")) {
100 struct passwd *user;
101 user = getpwnam(args[cur_arg + 1]);
102 if (!user) {
103 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
104 args[cur_arg + 1]);
105 return -1;
106 }
107 global.stats_sock.perm.ux.uid = user->pw_uid;
108 cur_arg += 2;
109 }
110 else if (!strcmp(args[cur_arg], "group")) {
111 struct group *group;
112 group = getgrnam(args[cur_arg + 1]);
113 if (!group) {
114 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
115 args[cur_arg + 1]);
116 return -1;
117 }
118 global.stats_sock.perm.ux.gid = group->gr_gid;
119 cur_arg += 2;
120 }
121 else {
122 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', and 'mode'");
123 return -1;
124 }
125 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100126
Willy Tarreaufbee7132007-10-18 13:53:22 +0200127 uxst_add_listener(&global.stats_sock);
128 global.maxsock++;
129 }
130 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100131 unsigned timeout;
132 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
133
134 if (res) {
135 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
136 return -1;
137 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200138
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100139 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200140 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
141 return -1;
142 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200143 global.stats_timeout = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200144 }
145 else if (!strcmp(args[0], "maxconn")) {
146 int maxconn = atol(args[1]);
147
148 if (maxconn <= 0) {
149 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
150 return -1;
151 }
152 global.maxsock -= global.stats_sock.maxconn;
153 global.stats_sock.maxconn = maxconn;
154 global.maxsock += global.stats_sock.maxconn;
155 }
156 else {
157 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
158 return -1;
159 }
160 return 0;
161}
162
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100163int print_csv_header(struct chunk *msg, int size)
164{
165 return chunk_printf(msg, size,
166 "# pxname,svname,"
167 "qcur,qmax,"
168 "scur,smax,slim,stot,"
169 "bin,bout,"
170 "dreq,dresp,"
171 "ereq,econ,eresp,"
172 "wretr,wredis,"
173 "status,weight,act,bck,"
174 "chkfail,chkdown,lastchg,downtime,qlimit,"
Krzysztof Piotr Oledzkif58a9622008-02-23 01:19:10 +0100175 "pid,iid,sid,throttle,lbtot,tracked,type,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100176 "\n");
177}
178
Willy Tarreau91861262007-10-17 17:06:05 +0200179/*
180 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200181 * client socket shut down on input. It *may* make use of informations from
182 * <uri>. s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200183 * It returns 0 if it had to stop writing data and an I/O is needed, 1 if the
184 * dump is finished and the session must be closed, or -1 in case of any error.
Willy Tarreau0a464892008-12-07 18:30:00 +0100185 * It automatically clears the HIJACK bit from the response buffer.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200186 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100187int stats_dump_raw(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau3e76e722007-10-17 18:57:38 +0200188{
Willy Tarreau3e76e722007-10-17 18:57:38 +0200189 struct proxy *px;
190 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +0100191 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200192
193 msg.len = 0;
194 msg.str = trash;
195
196 switch (s->data_state) {
197 case DATA_ST_INIT:
198 /* the function had not been called yet, let's prepare the
199 * buffer for a response.
200 */
Willy Tarreaudded32d2008-11-30 19:48:07 +0100201 stream_int_retnclose(rep->cons, &msg);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200202 s->data_state = DATA_ST_HEAD;
203 /* fall through */
204
205 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100206 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100207 print_csv_header(&msg, sizeof(trash));
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200208 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100209 return 0;
210 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200211
212 s->data_state = DATA_ST_INFO;
213 /* fall through */
214
215 case DATA_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +0100216 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100217 if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100218 chunk_printf(&msg, sizeof(trash),
219 "Name: " PRODUCT_NAME "\n"
220 "Version: " HAPROXY_VERSION "\n"
221 "Release_date: " HAPROXY_DATE "\n"
222 "Nbproc: %d\n"
223 "Process_num: %d\n"
224 "Pid: %d\n"
225 "Uptime: %dd %dh%02dm%02ds\n"
226 "Uptime_sec: %d\n"
227 "Memmax_MB: %d\n"
228 "Ulimit-n: %d\n"
229 "Maxsock: %d\n"
230 "Maxconn: %d\n"
231 "CurrConns: %d\n"
232 "",
233 global.nbproc,
234 relative_pid,
235 pid,
236 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
237 up,
238 global.rlimit_memmax,
239 global.rlimit_nofile,
240 global.maxsock,
241 global.maxconn,
242 actconn
243 );
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200244 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100245 return 0;
246 }
247
Willy Tarreau3e76e722007-10-17 18:57:38 +0200248 s->data_ctx.stats.px = proxy;
249 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100250
251 s->data_ctx.stats.sv = NULL;
252 s->data_ctx.stats.sv_st = 0;
253
Willy Tarreau3e76e722007-10-17 18:57:38 +0200254 s->data_state = DATA_ST_LIST;
255 /* fall through */
256
257 case DATA_ST_LIST:
258 /* dump proxies */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100259 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100260 while (s->data_ctx.stats.px) {
261 px = s->data_ctx.stats.px;
262 /* skip the disabled proxies and non-networked ones */
263 if (px->state != PR_STSTOPPED &&
264 (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100265 if (stats_dump_proxy(s, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100266 return 0;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200267
Willy Tarreaua8efd362008-01-03 10:19:15 +0100268 s->data_ctx.stats.px = px->next;
269 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
270 }
271 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200272 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200273
274 s->data_state = DATA_ST_END;
275 /* fall through */
276
277 case DATA_ST_END:
278 s->data_state = DATA_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +0100279 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200280
281 case DATA_ST_FIN:
Willy Tarreau0a464892008-12-07 18:30:00 +0100282 buffer_stop_hijack(rep);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200283 return 1;
284
285 default:
286 /* unknown state ! */
Willy Tarreau0a464892008-12-07 18:30:00 +0100287 buffer_stop_hijack(rep);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200288 return -1;
289 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100290}
291
292
293/* This function is called to send output to the response buffer. It simply
294 * calls stats_dump_raw(), and releases the buffer's hijack bit when the dump
Willy Tarreau01bf8672008-12-07 18:03:29 +0100295 * is finished.
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100296 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100297void stats_dump_raw_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100298{
299 if (s->ana_state != STATS_ST_REP)
Willy Tarreau01bf8672008-12-07 18:03:29 +0100300 return;
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100301
Willy Tarreau0a464892008-12-07 18:30:00 +0100302 if (stats_dump_raw(s, rep, NULL) != 0)
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100303 s->ana_state = STATS_ST_CLOSE;
Willy Tarreau01bf8672008-12-07 18:03:29 +0100304 return;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200305}
306
307
308/*
309 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200310 * client socket shut down on input. It stops by itself by unsetting the
Willy Tarreau72b179a2008-08-28 16:01:32 +0200311 * BF_HIJACK flag from the buffer, which it uses to keep on being called
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200312 * when there is free space in the buffer, of simply by letting an empty buffer
313 * upon return.s->data_ctx must have been zeroed before the first call, and the
314 * flags set. It returns 0 if it had to stop writing data and an I/O is needed,
315 * 1 if the dump is finished and the session must be closed, or -1 in case of
316 * any error.
Willy Tarreau91861262007-10-17 17:06:05 +0200317 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100318int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200319{
Willy Tarreau91861262007-10-17 17:06:05 +0200320 struct proxy *px;
321 struct chunk msg;
322 unsigned int up;
323
324 msg.len = 0;
325 msg.str = trash;
326
327 switch (s->data_state) {
328 case DATA_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +0200329 chunk_printf(&msg, sizeof(trash),
330 "HTTP/1.0 200 OK\r\n"
331 "Cache-Control: no-cache\r\n"
332 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200333 "Content-Type: %s\r\n",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100334 (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +0200335
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100336 if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau91861262007-10-17 17:06:05 +0200337 chunk_printf(&msg, sizeof(trash), "Refresh: %d\r\n",
338 uri->refresh);
339
340 chunk_printf(&msg, sizeof(trash), "\r\n");
341
342 s->txn.status = 200;
Willy Tarreaudded32d2008-11-30 19:48:07 +0100343 stream_int_retnclose(rep->cons, &msg); // send the start of the response.
Willy Tarreau91861262007-10-17 17:06:05 +0200344 msg.len = 0;
345
346 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
347 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
348 if (!(s->flags & SN_FINST_MASK))
349 s->flags |= SN_FINST_R;
350
351 if (s->txn.meth == HTTP_METH_HEAD) {
352 /* that's all we return in case of HEAD request */
353 s->data_state = DATA_ST_FIN;
Willy Tarreau72b179a2008-08-28 16:01:32 +0200354 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200355 return 1;
356 }
357
358 s->data_state = DATA_ST_HEAD; /* let's start producing data */
359 /* fall through */
360
361 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100362 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200363 /* WARNING! This must fit in the first buffer !!! */
364 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200365 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
366 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
367 "<style type=\"text/css\"><!--\n"
368 "body {"
369 " font-family: helvetica, arial;"
370 " font-size: 12px;"
371 " font-weight: normal;"
372 " color: black;"
373 " background: white;"
374 "}\n"
375 "th,td {"
376 " font-size: 0.8em;"
377 " align: center;"
378 "}\n"
379 "h1 {"
380 " font-size: xx-large;"
381 " margin-bottom: 0.5em;"
382 "}\n"
383 "h2 {"
384 " font-family: helvetica, arial;"
385 " font-size: x-large;"
386 " font-weight: bold;"
387 " font-style: italic;"
388 " color: #6020a0;"
389 " margin-top: 0em;"
390 " margin-bottom: 0em;"
391 "}\n"
392 "h3 {"
393 " font-family: helvetica, arial;"
394 " font-size: 16px;"
395 " font-weight: bold;"
396 " color: #b00040;"
397 " background: #e8e8d0;"
398 " margin-top: 0em;"
399 " margin-bottom: 0em;"
400 "}\n"
401 "li {"
402 " margin-top: 0.25em;"
403 " margin-right: 2em;"
404 "}\n"
405 ".hr {margin-top: 0.25em;"
406 " border-color: black;"
407 " border-bottom-style: solid;"
408 "}\n"
409 ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n"
410 ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n"
411 ".total {background: #20D0D0;color: #ffff80;}\n"
412 ".frontend {background: #e8e8d0;}\n"
413 ".backend {background: #e8e8d0;}\n"
414 ".active0 {background: #ff9090;}\n"
415 ".active1 {background: #ffd020;}\n"
416 ".active2 {background: #ffffa0;}\n"
417 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100418 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
419 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
420 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200421 ".backup0 {background: #ff9090;}\n"
422 ".backup1 {background: #ff80ff;}\n"
423 ".backup2 {background: #c060ff;}\n"
424 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100425 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
426 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
427 ".backup6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200428 "table.tbl { border-collapse: collapse; border-style: none;}\n"
429 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray;}\n"
430 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
431 "table.tbl th.empty { border-style: none; empty-cells: hide;}\n"
432 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
433 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
434 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
435 "-->\n"
436 "</style></head>\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200437 } else {
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100438 print_csv_header(&msg, sizeof(trash));
Willy Tarreau55bb8452007-10-17 18:44:57 +0200439 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200440 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200441 return 0;
442
443 s->data_state = DATA_ST_INFO;
444 /* fall through */
445
446 case DATA_ST_INFO:
447 up = (now.tv_sec - start_date.tv_sec);
448
449 /* WARNING! this has to fit the first packet too.
450 * We are around 3.5 kB, add adding entries will
451 * become tricky if we want to support 4kB buffers !
452 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100453 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200454 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200455 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
456 PRODUCT_NAME "%s</a></h1>\n"
457 "<h2>Statistics Report for pid %d</h2>\n"
458 "<hr width=\"100%%\" class=\"hr\">\n"
459 "<h3>&gt; General process information</h3>\n"
460 "<table border=0 cols=4><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100461 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200462 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
463 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
464 "<b>maxsock = </b> %d<br>\n"
465 "<b>maxconn = </b> %d (current conns = %d)<br>\n"
466 "</td><td align=\"center\" nowrap>\n"
467 "<table class=\"lgd\"><tr>\n"
468 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
469 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
470 "</tr><tr>\n"
471 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
472 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
473 "</tr><tr>\n"
474 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
475 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
476 "</tr><tr>\n"
477 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100478 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200479 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100480 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +0200481 "</td>"
482 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
483 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
484 "",
485 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Willy Tarreaua8efd362008-01-03 10:19:15 +0100486 pid, pid,
487 relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +0200488 up / 86400, (up % 86400) / 3600,
489 (up % 3600) / 60, (up % 60),
490 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
491 global.rlimit_memmax ? " MB" : "",
492 global.rlimit_nofile,
493 global.maxsock,
494 global.maxconn,
495 actconn
496 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100497
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100498 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200499 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200500 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
501 uri->uri_prefix,
502 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100503 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200504 else
505 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200506 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
507 uri->uri_prefix,
508 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100509 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200510
Willy Tarreau55bb8452007-10-17 18:44:57 +0200511 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100512 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200513 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200514 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
515 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100516 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200517 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200518 else
519 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200520 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
521 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100522 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200523 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200524 }
Willy Tarreau91861262007-10-17 17:06:05 +0200525
Willy Tarreau55bb8452007-10-17 18:44:57 +0200526 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200527 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
528 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100529 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
530 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200531
Willy Tarreau55bb8452007-10-17 18:44:57 +0200532 chunk_printf(&msg, sizeof(trash),
Willy Tarreau5031e6a2007-10-18 11:05:48 +0200533 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
534 uri->uri_prefix,
535 (uri->refresh > 0) ? ";norefresh" : "");
536
537 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200538 "</td>"
539 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
540 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
541 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
542 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
543 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
544 "</ul>"
545 "</td>"
546 "</tr></table>\n"
547 ""
548 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100549
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200550 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200551 return 0;
552 }
Willy Tarreau91861262007-10-17 17:06:05 +0200553
Willy Tarreau91861262007-10-17 17:06:05 +0200554 s->data_ctx.stats.px = proxy;
555 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
556 s->data_state = DATA_ST_LIST;
557 /* fall through */
558
559 case DATA_ST_LIST:
560 /* dump proxies */
561 while (s->data_ctx.stats.px) {
562 px = s->data_ctx.stats.px;
563 /* skip the disabled proxies and non-networked ones */
564 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100565 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200566 return 0;
567
568 s->data_ctx.stats.px = px->next;
569 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
570 }
571 /* here, we just have reached the last proxy */
572
573 s->data_state = DATA_ST_END;
574 /* fall through */
575
576 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100577 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200578 chunk_printf(&msg, sizeof(trash), "</body></html>\n");
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200579 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200580 return 0;
581 }
Willy Tarreau91861262007-10-17 17:06:05 +0200582
583 s->data_state = DATA_ST_FIN;
584 /* fall through */
585
586 case DATA_ST_FIN:
Willy Tarreau72b179a2008-08-28 16:01:32 +0200587 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200588 return 1;
589
590 default:
591 /* unknown state ! */
Willy Tarreau72b179a2008-08-28 16:01:32 +0200592 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200593 return -1;
594 }
595}
596
597
598/*
599 * Dumps statistics for a proxy.
600 * Returns 0 if it had to stop dumping data because of lack of buffer space,
601 * ot non-zero if everything completed.
602 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100603int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200604{
605 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100606 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Willy Tarreau91861262007-10-17 17:06:05 +0200607 struct chunk msg;
608
609 msg.len = 0;
610 msg.str = trash;
611
612 switch (s->data_ctx.stats.px_st) {
613 case DATA_ST_PX_INIT:
614 /* we are on a new proxy */
615
616 if (uri && uri->scope) {
617 /* we have a limited scope, we have to check the proxy name */
618 struct stat_scope *scope;
619 int len;
620
621 len = strlen(px->id);
622 scope = uri->scope;
623
624 while (scope) {
625 /* match exact proxy name */
626 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
627 break;
628
629 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +0200630 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +0200631 break;
632 scope = scope->next;
633 }
634
635 /* proxy name not found : don't dump anything */
636 if (scope == NULL)
637 return 1;
638 }
639
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100640 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100641 (px->uuid != s->data_ctx.stats.iid))
642 return 1;
643
Willy Tarreau91861262007-10-17 17:06:05 +0200644 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
645 /* fall through */
646
647 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100648 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200649 /* print a new table */
650 chunk_printf(&msg, sizeof(trash),
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100651 "<table cols=\"26\" class=\"tbl\" width=\"100%%\">\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200652 "<tr align=\"center\" class=\"titre\">"
653 "<th colspan=2 class=\"pxname\">%s</th>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100654 "<th colspan=24 class=\"empty\"></th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200655 "</tr>\n"
656 "<tr align=\"center\" class=\"titre\">"
657 "<th rowspan=2></th>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100658 "<th colspan=3>Queue</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200659 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200660 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100661 "<th colspan=8>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200662 "</tr>\n"
663 "<tr align=\"center\" class=\"titre\">"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200664 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100665 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200666 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200667 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200668 "<th>Status</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100669 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
670 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200671 "</tr>",
Willy Tarreau55bb8452007-10-17 18:44:57 +0200672 px->id);
673
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200674 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200675 return 0;
676 }
Willy Tarreau91861262007-10-17 17:06:05 +0200677
678 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
679 /* fall through */
680
681 case DATA_ST_PX_FE:
682 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100683 if ((px->cap & PR_CAP_FE) &&
684 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
685 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200686 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200687 /* name, queue */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200688 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=3></td>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100689 /* sessions : current, max, limit, total, lbtot */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200690 "<td align=right>%d</td><td align=right>%d</td>"
691 "<td align=right>%d</td><td align=right>%d</td>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100692 "<td align=right></td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200693 /* bytes : in, out */
694 "<td align=right>%lld</td><td align=right>%lld</td>"
695 /* denied: req, resp */
696 "<td align=right>%d</td><td align=right>%d</td>"
697 /* errors : request, connect, response */
698 "<td align=right>%d</td><td align=right></td><td align=right></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200699 /* warnings: retries, redispatches */
700 "<td align=right></td><td align=right></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200701 /* server status : reflect frontend status */
Willy Tarreau91861262007-10-17 17:06:05 +0200702 "<td align=center>%s</td>"
703 /* rest of server: nothing */
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100704 "<td align=center colspan=7></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +0200705 "",
706 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
707 px->bytes_in, px->bytes_out,
708 px->denied_req, px->denied_resp,
709 px->failed_req,
710 px->state == PR_STRUN ? "OPEN" :
711 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200712 } else {
713 chunk_printf(&msg, sizeof(trash),
714 /* pxid, name, queue cur, queue max, */
715 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100716 /* sessions : current, max, limit, total */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200717 "%d,%d,%d,%d,"
718 /* bytes : in, out */
719 "%lld,%lld,"
720 /* denied: req, resp */
721 "%d,%d,"
722 /* errors : request, connect, response */
723 "%d,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200724 /* warnings: retries, redispatches */
725 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200726 /* server status : reflect frontend status */
727 "%s,"
728 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200729 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100730 /* pid, iid, sid, throttle, lbtot, tracked, type */
731 "%d,%d,0,,,,%d,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200732 "\n",
733 px->id,
734 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
735 px->bytes_in, px->bytes_out,
736 px->denied_req, px->denied_resp,
737 px->failed_req,
738 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +0100739 px->state == PR_STIDLE ? "FULL" : "STOP",
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100740 relative_pid, px->uuid, STATS_TYPE_FE);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200741 }
Willy Tarreau91861262007-10-17 17:06:05 +0200742
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200743 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200744 return 0;
745 }
746
747 s->data_ctx.stats.sv = px->srv; /* may be NULL */
748 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
749 /* fall through */
750
751 case DATA_ST_PX_SV:
752 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100753 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
754
Willy Tarreau2ea81932007-11-30 12:04:38 +0100755 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 +0200756
757 sv = s->data_ctx.stats.sv;
758
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100759 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100760 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
761 break;
762
763 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
764 continue;
765 }
766
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100767 if (sv->tracked)
768 svs = sv->tracked;
769 else
770 svs = sv;
771
Willy Tarreau91861262007-10-17 17:06:05 +0200772 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100773 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +0100774 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100775 else if (svs->state & SRV_RUNNING) {
776 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +0200777 sv_state = 3; /* UP */
778 else
779 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +0100780
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100781 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +0100782 sv_state += 2;
783 }
Willy Tarreau91861262007-10-17 17:06:05 +0200784 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100785 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +0200786 sv_state = 1; /* going up */
787 else
788 sv_state = 0; /* DOWN */
789
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100790 if ((sv_state == 0) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +0200791 /* do not report servers which are DOWN */
792 s->data_ctx.stats.sv = sv->next;
793 continue;
794 }
795
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100796 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +0100797 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
798 "UP %d/%d &darr;", "UP",
799 "NOLB %d/%d &darr;", "NOLB",
800 "<i>no check</i>" };
Willy Tarreau55bb8452007-10-17 18:44:57 +0200801 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200802 /* name */
803 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200804 /* queue : current, max, limit */
805 "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100806 /* sessions : current, max, limit, total, lbtot */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200807 "<td align=right>%d</td><td align=right>%d</td>"
808 "<td align=right>%s</td><td align=right>%d</td>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100809 "<td align=right>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200810 /* bytes : in, out */
811 "<td align=right>%lld</td><td align=right>%lld</td>"
812 /* denied: req, resp */
813 "<td align=right></td><td align=right>%d</td>"
814 /* errors : request, connect, response */
815 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200816 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100817 "<td align=right>%u</td><td align=right>%u</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200818 "",
819 (sv->state & SRV_BACKUP) ? "backup" : "active",
820 sv_state, sv->id,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200821 sv->nbpend, sv->nbpend_max, LIM2A0(sv->maxqueue, "-"),
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100822 sv->cur_sess, sv->cur_sess_max, LIM2A1(sv->maxconn, "-"),
823 sv->cum_sess, sv->cum_lbconn,
Willy Tarreau91861262007-10-17 17:06:05 +0200824 sv->bytes_in, sv->bytes_out,
825 sv->failed_secu,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200826 sv->failed_conns, sv->failed_resp,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100827 sv->retries, sv->redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100828
Willy Tarreau55bb8452007-10-17 18:44:57 +0200829 /* status */
830 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200831
832 if (sv->state & SRV_CHECKED)
833 chunk_printf(&msg, sizeof(trash), "%s ",
834 human_time(now.tv_sec - sv->last_change, 1));
835
Willy Tarreau55bb8452007-10-17 18:44:57 +0200836 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200837 srv_hlt_st[sv_state],
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100838 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
839 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Willy Tarreau91861262007-10-17 17:06:05 +0200840
Willy Tarreau55bb8452007-10-17 18:44:57 +0200841 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200842 /* weight */
843 "</td><td>%d</td>"
844 /* act, bck */
845 "<td>%s</td><td>%s</td>"
846 "",
Willy Tarreau5542af62007-12-03 02:04:00 +0100847 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +0200848 (sv->state & SRV_BACKUP) ? "-" : "Y",
849 (sv->state & SRV_BACKUP) ? "Y" : "-");
850
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200851 /* check failures: unique, fatal, down time */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200852 if (sv->state & SRV_CHECKED)
853 chunk_printf(&msg, sizeof(trash),
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200854 "<td align=right>%d</td><td align=right>%d</td>"
855 "<td nowrap align=right>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100856 "",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100857 svs->failed_checks, svs->down_trans,
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200858 human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100859 else if (sv != svs)
860 chunk_printf(&msg, sizeof(trash),
861 "<td nowrap colspan=3>via %s/%s</td>", svs->proxy->id, svs->id );
Willy Tarreau55bb8452007-10-17 18:44:57 +0200862 else
863 chunk_printf(&msg, sizeof(trash),
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100864 "<td colspan=3></td>");
865
866 /* throttle */
867 if ((sv->state & SRV_WARMINGUP) &&
868 now.tv_sec < sv->last_change + sv->slowstart &&
869 now.tv_sec >= sv->last_change) {
870 unsigned int ratio;
871 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
872 chunk_printf(&msg, sizeof(trash),
873 "<td>%d %%</td></tr>\n", ratio);
874 } else {
875 chunk_printf(&msg, sizeof(trash),
876 "<td>-</td></tr>\n");
877 }
Willy Tarreau55bb8452007-10-17 18:44:57 +0200878 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +0100879 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
880 "UP %d/%d,", "UP,",
881 "NOLB %d/%d,", "NOLB,",
882 "no check," };
Willy Tarreau55bb8452007-10-17 18:44:57 +0200883 chunk_printf(&msg, sizeof(trash),
884 /* pxid, name */
885 "%s,%s,"
886 /* queue : current, max */
887 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100888 /* sessions : current, max, limit, total */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200889 "%d,%d,%s,%d,"
890 /* bytes : in, out */
891 "%lld,%lld,"
892 /* denied: req, resp */
893 ",%d,"
894 /* errors : request, connect, response */
895 ",%d,%d,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200896 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100897 "%u,%u,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200898 "",
899 px->id, sv->id,
900 sv->nbpend, sv->nbpend_max,
Willy Tarreaudcd47712007-11-04 23:35:08 +0100901 sv->cur_sess, sv->cur_sess_max, LIM2A0(sv->maxconn, ""), sv->cum_sess,
Willy Tarreau55bb8452007-10-17 18:44:57 +0200902 sv->bytes_in, sv->bytes_out,
903 sv->failed_secu,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200904 sv->failed_conns, sv->failed_resp,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100905 sv->retries, sv->redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100906
Willy Tarreau55bb8452007-10-17 18:44:57 +0200907 /* status */
908 chunk_printf(&msg, sizeof(trash),
909 srv_hlt_st[sv_state],
910 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
911 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
Willy Tarreau91861262007-10-17 17:06:05 +0200912
Willy Tarreau55bb8452007-10-17 18:44:57 +0200913 chunk_printf(&msg, sizeof(trash),
914 /* weight, active, backup */
915 "%d,%d,%d,"
916 "",
Willy Tarreau5542af62007-12-03 02:04:00 +0100917 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +0200918 (sv->state & SRV_BACKUP) ? 0 : 1,
919 (sv->state & SRV_BACKUP) ? 1 : 0);
920
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200921 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200922 if (sv->state & SRV_CHECKED)
923 chunk_printf(&msg, sizeof(trash),
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200924 "%d,%d,%d,%d,",
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200925 sv->failed_checks, sv->down_trans,
926 now.tv_sec - sv->last_change, srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +0200927 else
928 chunk_printf(&msg, sizeof(trash),
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200929 ",,,,");
930
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100931 /* queue limit, pid, iid, sid, */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200932 chunk_printf(&msg, sizeof(trash),
Willy Tarreaudcd47712007-11-04 23:35:08 +0100933 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100934 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +0100935 LIM2A0(sv->maxqueue, ""),
936 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100937
938 /* throttle */
939 if ((sv->state & SRV_WARMINGUP) &&
940 now.tv_sec < sv->last_change + sv->slowstart &&
941 now.tv_sec >= sv->last_change) {
942 unsigned int ratio;
943 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100944 chunk_printf(&msg, sizeof(trash), "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100945 }
946
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100947 /* sessions: lbtot */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100948 chunk_printf(&msg, sizeof(trash), ",%d,", sv->cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100949
950 /* tracked */
951 if (sv->tracked)
Krzysztof Piotr Oledzkif58a9622008-02-23 01:19:10 +0100952 chunk_printf(&msg, sizeof(trash), "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100953 sv->tracked->proxy->id, sv->tracked->id);
954 else
955 chunk_printf(&msg, sizeof(trash), ",");
956
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100957 /* type, then EOL */
958 chunk_printf(&msg, sizeof(trash), "%d,\n", STATS_TYPE_SV);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200959 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200960 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200961 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100962 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +0200963
964 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
965 /* fall through */
966
967 case DATA_ST_PX_BE:
968 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100969 if ((px->cap & PR_CAP_BE) &&
970 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
971 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200972 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200973 /* name */
974 "<tr align=center class=\"backend\"><td>Backend</td>"
975 /* queue : current, max */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200976 "<td align=right>%d</td><td align=right>%d</td><td></td>"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100977 /* sessions : current, max, limit, total, lbtot */
978 "<td align=right>%d</td><td align=right>%d</td>"
979 "<td align=right>%d</td><td align=right>%d</td>"
980 "<td align=right>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200981 /* bytes : in, out */
982 "<td align=right>%lld</td><td align=right>%lld</td>"
983 /* denied: req, resp */
984 "<td align=right>%d</td><td align=right>%d</td>"
985 /* errors : request, connect, response */
986 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200987 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100988 "<td align=right>%u</td><td align=right>%u</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200989 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +0200990 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200991 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +0200992 * active and backups. */
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200993 "<td align=center nowrap>%s %s</td><td align=center>%d</td>"
994 "<td align=center>%d</td><td align=center>%d</td>",
Willy Tarreau91861262007-10-17 17:06:05 +0200995 px->nbpend /* or px->totpend ? */, px->nbpend_max,
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100996 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn, px->cum_lbconn,
Willy Tarreau91861262007-10-17 17:06:05 +0200997 px->bytes_in, px->bytes_out,
998 px->denied_req, px->denied_resp,
999 px->failed_conns, px->failed_resp,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001000 px->retries, px->redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001001 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01001002 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
1003 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01001004 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001005 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001006
1007 chunk_printf(&msg, sizeof(trash),
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001008 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001009 "<td align=center>&nbsp;</td><td align=\"right\">%d</td>"
1010 "<td align=\"right\" nowrap>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001011 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001012 "</tr>",
1013 px->down_trans,
1014 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001015 } else {
1016 chunk_printf(&msg, sizeof(trash),
1017 /* pxid, name */
1018 "%s,BACKEND,"
1019 /* queue : current, max */
1020 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001021 /* sessions : current, max, limit, total */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001022 "%d,%d,%d,%d,"
1023 /* bytes : in, out */
1024 "%lld,%lld,"
1025 /* denied: req, resp */
1026 "%d,%d,"
1027 /* errors : request, connect, response */
1028 ",%d,%d,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001029 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001030 "%u,%u,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001031 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02001032 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001033 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02001034 * active and backups. */
1035 "%s,"
1036 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001037 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001038 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001039 /* pid, iid, sid, throttle, lbtot, tracked, type */
1040 "%d,%d,0,,%d,,%d,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001041 "\n",
1042 px->id,
1043 px->nbpend /* or px->totpend ? */, px->nbpend_max,
1044 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
1045 px->bytes_in, px->bytes_out,
1046 px->denied_req, px->denied_resp,
1047 px->failed_conns, px->failed_resp,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001048 px->retries, px->redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01001049 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01001050 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001051 px->srv_act, px->srv_bck,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001052 px->down_trans, now.tv_sec - px->last_change,
Willy Tarreau20697042007-11-15 23:26:18 +01001053 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001054 relative_pid, px->uuid,
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001055 px->cum_lbconn, STATS_TYPE_BE);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001056 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001057 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001058 return 0;
1059 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001060
Willy Tarreau91861262007-10-17 17:06:05 +02001061 s->data_ctx.stats.px_st = DATA_ST_PX_END;
1062 /* fall through */
1063
1064 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001065 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001066 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001067
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001068 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001069 return 0;
1070 }
Willy Tarreau91861262007-10-17 17:06:05 +02001071
1072 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
1073 /* fall through */
1074
1075 case DATA_ST_PX_FIN:
1076 return 1;
1077
1078 default:
1079 /* unknown state, we should put an abort() here ! */
1080 return 1;
1081 }
1082}
1083
Willy Tarreau10522fd2008-07-09 20:12:41 +02001084static struct cfg_kw_list cfg_kws = {{ },{
1085 { CFG_GLOBAL, "stats", stats_parse_global },
1086 { 0, NULL, NULL },
1087}};
1088
1089__attribute__((constructor))
1090static void __dumpstats_module_init(void)
1091{
1092 cfg_register_keywords(&cfg_kws);
1093}
1094
Willy Tarreau91861262007-10-17 17:06:05 +02001095/*
1096 * Local variables:
1097 * c-indent-level: 8
1098 * c-basic-offset: 8
1099 * End:
1100 */