blob: 97443a122ff83bb59b2c0ad7babcb52a863977e5 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
2 * Functions dedicated to statistics output
3 *
Willy Tarreaua206fa92009-01-25 14:02:00 +01004 * Copyright 2000-2009 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 Tarreau7f062c42009-03-05 18:43:00 +010044#include <proto/freq_ctr.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010045#include <proto/pipe.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020046#include <proto/proto_uxst.h>
Willy Tarreau91861262007-10-17 17:06:05 +020047#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020048#include <proto/server.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010049#include <proto/stream_interface.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 */
Willy Tarreau10522fd2008-07-09 20:12:41 +020057static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
58 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +020059{
Willy Tarreau10522fd2008-07-09 20:12:41 +020060 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +020061 if (!strcmp(args[0], "socket")) {
62 struct sockaddr_un su;
63 int cur_arg;
64
65 if (*args[1] == 0) {
66 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
67 return -1;
68 }
69
70 if (global.stats_sock.state != LI_NEW) {
71 snprintf(err, errlen, "'stats socket' already specified in global section");
72 return -1;
73 }
74
75 su.sun_family = AF_UNIX;
76 strncpy(su.sun_path, args[1], sizeof(su.sun_path));
77 su.sun_path[sizeof(su.sun_path) - 1] = 0;
78 memcpy(&global.stats_sock.addr, &su, sizeof(su)); // guaranteed to fit
79
80 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +010081 global.stats_sock.options = LI_O_NONE;
Willy Tarreaufbee7132007-10-18 13:53:22 +020082 global.stats_sock.accept = uxst_event_accept;
Willy Tarreaub1356cf2008-12-07 16:06:43 +010083 global.stats_sock.handler = uxst_process_session;
84 global.stats_sock.analysers = AN_REQ_UNIX_STATS;
Willy Tarreaufbee7132007-10-18 13:53:22 +020085 global.stats_sock.private = NULL;
86
87 cur_arg = 2;
88 while (*args[cur_arg]) {
89 if (!strcmp(args[cur_arg], "uid")) {
90 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
91 cur_arg += 2;
92 }
93 else if (!strcmp(args[cur_arg], "gid")) {
94 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
95 cur_arg += 2;
96 }
97 else if (!strcmp(args[cur_arg], "mode")) {
98 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
99 cur_arg += 2;
100 }
101 else if (!strcmp(args[cur_arg], "user")) {
102 struct passwd *user;
103 user = getpwnam(args[cur_arg + 1]);
104 if (!user) {
105 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
106 args[cur_arg + 1]);
107 return -1;
108 }
109 global.stats_sock.perm.ux.uid = user->pw_uid;
110 cur_arg += 2;
111 }
112 else if (!strcmp(args[cur_arg], "group")) {
113 struct group *group;
114 group = getgrnam(args[cur_arg + 1]);
115 if (!group) {
116 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
117 args[cur_arg + 1]);
118 return -1;
119 }
120 global.stats_sock.perm.ux.gid = group->gr_gid;
121 cur_arg += 2;
122 }
123 else {
124 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', and 'mode'");
125 return -1;
126 }
127 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100128
Willy Tarreaufbee7132007-10-18 13:53:22 +0200129 uxst_add_listener(&global.stats_sock);
130 global.maxsock++;
131 }
132 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100133 unsigned timeout;
134 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
135
136 if (res) {
137 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
138 return -1;
139 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200140
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100141 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200142 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
143 return -1;
144 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200145 global.stats_timeout = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200146 }
147 else if (!strcmp(args[0], "maxconn")) {
148 int maxconn = atol(args[1]);
149
150 if (maxconn <= 0) {
151 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
152 return -1;
153 }
154 global.maxsock -= global.stats_sock.maxconn;
155 global.stats_sock.maxconn = maxconn;
156 global.maxsock += global.stats_sock.maxconn;
157 }
158 else {
159 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
160 return -1;
161 }
162 return 0;
163}
164
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100165int print_csv_header(struct chunk *msg, int size)
166{
167 return chunk_printf(msg, size,
168 "# pxname,svname,"
169 "qcur,qmax,"
170 "scur,smax,slim,stot,"
171 "bin,bout,"
172 "dreq,dresp,"
173 "ereq,econ,eresp,"
174 "wretr,wredis,"
175 "status,weight,act,bck,"
176 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100177 "pid,iid,sid,throttle,lbtot,tracked,type,rate,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100178 "\n");
179}
180
Willy Tarreau91861262007-10-17 17:06:05 +0200181/*
182 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200183 * client socket shut down on input. It *may* make use of informations from
184 * <uri>. s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200185 * It returns 0 if it had to stop writing data and an I/O is needed, 1 if the
186 * dump is finished and the session must be closed, or -1 in case of any error.
Willy Tarreau0a464892008-12-07 18:30:00 +0100187 * It automatically clears the HIJACK bit from the response buffer.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200188 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100189int stats_dump_raw(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau3e76e722007-10-17 18:57:38 +0200190{
Willy Tarreau3e76e722007-10-17 18:57:38 +0200191 struct proxy *px;
192 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +0100193 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200194
195 msg.len = 0;
196 msg.str = trash;
197
198 switch (s->data_state) {
199 case DATA_ST_INIT:
200 /* the function had not been called yet, let's prepare the
201 * buffer for a response.
202 */
Willy Tarreaudded32d2008-11-30 19:48:07 +0100203 stream_int_retnclose(rep->cons, &msg);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200204 s->data_state = DATA_ST_HEAD;
205 /* fall through */
206
207 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100208 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100209 print_csv_header(&msg, sizeof(trash));
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200210 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100211 return 0;
212 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200213
214 s->data_state = DATA_ST_INFO;
215 /* fall through */
216
217 case DATA_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +0100218 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100219 if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100220 chunk_printf(&msg, sizeof(trash),
221 "Name: " PRODUCT_NAME "\n"
222 "Version: " HAPROXY_VERSION "\n"
223 "Release_date: " HAPROXY_DATE "\n"
224 "Nbproc: %d\n"
225 "Process_num: %d\n"
226 "Pid: %d\n"
227 "Uptime: %dd %dh%02dm%02ds\n"
228 "Uptime_sec: %d\n"
229 "Memmax_MB: %d\n"
230 "Ulimit-n: %d\n"
231 "Maxsock: %d\n"
232 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100233 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100234 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100235 "PipesUsed: %d\n"
236 "PipesFree: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100237 "",
238 global.nbproc,
239 relative_pid,
240 pid,
241 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
242 up,
243 global.rlimit_memmax,
244 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100245 global.maxsock, global.maxconn, global.maxpipes,
246 actconn, pipes_used, pipes_free
Willy Tarreaua8efd362008-01-03 10:19:15 +0100247 );
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200248 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100249 return 0;
250 }
251
Willy Tarreau3e76e722007-10-17 18:57:38 +0200252 s->data_ctx.stats.px = proxy;
253 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100254
255 s->data_ctx.stats.sv = NULL;
256 s->data_ctx.stats.sv_st = 0;
257
Willy Tarreau3e76e722007-10-17 18:57:38 +0200258 s->data_state = DATA_ST_LIST;
259 /* fall through */
260
261 case DATA_ST_LIST:
262 /* dump proxies */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100263 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100264 while (s->data_ctx.stats.px) {
265 px = s->data_ctx.stats.px;
266 /* skip the disabled proxies and non-networked ones */
267 if (px->state != PR_STSTOPPED &&
268 (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100269 if (stats_dump_proxy(s, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100270 return 0;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200271
Willy Tarreaua8efd362008-01-03 10:19:15 +0100272 s->data_ctx.stats.px = px->next;
273 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
274 }
275 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200276 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200277
278 s->data_state = DATA_ST_END;
279 /* fall through */
280
281 case DATA_ST_END:
282 s->data_state = DATA_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +0100283 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200284
285 case DATA_ST_FIN:
Willy Tarreau0a464892008-12-07 18:30:00 +0100286 buffer_stop_hijack(rep);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200287 return 1;
288
289 default:
290 /* unknown state ! */
Willy Tarreau0a464892008-12-07 18:30:00 +0100291 buffer_stop_hijack(rep);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200292 return -1;
293 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100294}
295
296
297/* This function is called to send output to the response buffer. It simply
298 * calls stats_dump_raw(), and releases the buffer's hijack bit when the dump
Willy Tarreau01bf8672008-12-07 18:03:29 +0100299 * is finished.
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100300 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100301void stats_dump_raw_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100302{
303 if (s->ana_state != STATS_ST_REP)
Willy Tarreau01bf8672008-12-07 18:03:29 +0100304 return;
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100305
Willy Tarreau0a464892008-12-07 18:30:00 +0100306 if (stats_dump_raw(s, rep, NULL) != 0)
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100307 s->ana_state = STATS_ST_CLOSE;
Willy Tarreau01bf8672008-12-07 18:03:29 +0100308 return;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200309}
310
311
312/*
313 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200314 * client socket shut down on input. It stops by itself by unsetting the
Willy Tarreau72b179a2008-08-28 16:01:32 +0200315 * BF_HIJACK flag from the buffer, which it uses to keep on being called
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200316 * when there is free space in the buffer, of simply by letting an empty buffer
317 * upon return.s->data_ctx must have been zeroed before the first call, and the
318 * flags set. It returns 0 if it had to stop writing data and an I/O is needed,
319 * 1 if the dump is finished and the session must be closed, or -1 in case of
320 * any error.
Willy Tarreau91861262007-10-17 17:06:05 +0200321 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100322int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200323{
Willy Tarreau91861262007-10-17 17:06:05 +0200324 struct proxy *px;
325 struct chunk msg;
326 unsigned int up;
327
328 msg.len = 0;
329 msg.str = trash;
330
331 switch (s->data_state) {
332 case DATA_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +0200333 chunk_printf(&msg, sizeof(trash),
334 "HTTP/1.0 200 OK\r\n"
335 "Cache-Control: no-cache\r\n"
336 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200337 "Content-Type: %s\r\n",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100338 (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +0200339
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100340 if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau91861262007-10-17 17:06:05 +0200341 chunk_printf(&msg, sizeof(trash), "Refresh: %d\r\n",
342 uri->refresh);
343
344 chunk_printf(&msg, sizeof(trash), "\r\n");
345
346 s->txn.status = 200;
Willy Tarreaudded32d2008-11-30 19:48:07 +0100347 stream_int_retnclose(rep->cons, &msg); // send the start of the response.
Willy Tarreau91861262007-10-17 17:06:05 +0200348 msg.len = 0;
349
350 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
351 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
352 if (!(s->flags & SN_FINST_MASK))
353 s->flags |= SN_FINST_R;
354
355 if (s->txn.meth == HTTP_METH_HEAD) {
356 /* that's all we return in case of HEAD request */
357 s->data_state = DATA_ST_FIN;
Willy Tarreau72b179a2008-08-28 16:01:32 +0200358 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200359 return 1;
360 }
361
362 s->data_state = DATA_ST_HEAD; /* let's start producing data */
363 /* fall through */
364
365 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100366 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200367 /* WARNING! This must fit in the first buffer !!! */
368 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200369 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
370 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
371 "<style type=\"text/css\"><!--\n"
372 "body {"
373 " font-family: helvetica, arial;"
374 " font-size: 12px;"
375 " font-weight: normal;"
376 " color: black;"
377 " background: white;"
378 "}\n"
379 "th,td {"
380 " font-size: 0.8em;"
381 " align: center;"
382 "}\n"
383 "h1 {"
384 " font-size: xx-large;"
385 " margin-bottom: 0.5em;"
386 "}\n"
387 "h2 {"
388 " font-family: helvetica, arial;"
389 " font-size: x-large;"
390 " font-weight: bold;"
391 " font-style: italic;"
392 " color: #6020a0;"
393 " margin-top: 0em;"
394 " margin-bottom: 0em;"
395 "}\n"
396 "h3 {"
397 " font-family: helvetica, arial;"
398 " font-size: 16px;"
399 " font-weight: bold;"
400 " color: #b00040;"
401 " background: #e8e8d0;"
402 " margin-top: 0em;"
403 " margin-bottom: 0em;"
404 "}\n"
405 "li {"
406 " margin-top: 0.25em;"
407 " margin-right: 2em;"
408 "}\n"
409 ".hr {margin-top: 0.25em;"
410 " border-color: black;"
411 " border-bottom-style: solid;"
412 "}\n"
413 ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n"
414 ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n"
415 ".total {background: #20D0D0;color: #ffff80;}\n"
416 ".frontend {background: #e8e8d0;}\n"
417 ".backend {background: #e8e8d0;}\n"
418 ".active0 {background: #ff9090;}\n"
419 ".active1 {background: #ffd020;}\n"
420 ".active2 {background: #ffffa0;}\n"
421 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100422 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
423 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
424 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200425 ".backup0 {background: #ff9090;}\n"
426 ".backup1 {background: #ff80ff;}\n"
427 ".backup2 {background: #c060ff;}\n"
428 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100429 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
430 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
431 ".backup6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200432 "table.tbl { border-collapse: collapse; border-style: none;}\n"
433 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray;}\n"
434 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
435 "table.tbl th.empty { border-style: none; empty-cells: hide;}\n"
436 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
437 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
438 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
439 "-->\n"
440 "</style></head>\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200441 } else {
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100442 print_csv_header(&msg, sizeof(trash));
Willy Tarreau55bb8452007-10-17 18:44:57 +0200443 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200444 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200445 return 0;
446
447 s->data_state = DATA_ST_INFO;
448 /* fall through */
449
450 case DATA_ST_INFO:
451 up = (now.tv_sec - start_date.tv_sec);
452
453 /* WARNING! this has to fit the first packet too.
454 * We are around 3.5 kB, add adding entries will
455 * become tricky if we want to support 4kB buffers !
456 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100457 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200458 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200459 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
460 PRODUCT_NAME "%s</a></h1>\n"
461 "<h2>Statistics Report for pid %d</h2>\n"
462 "<hr width=\"100%%\" class=\"hr\">\n"
463 "<h3>&gt; General process information</h3>\n"
464 "<table border=0 cols=4><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100465 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200466 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
467 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100468 "<b>maxsock = </b> %d ; <b>maxconn = </b> %d ; <b>maxpipes = </b> %d<br>\n"
469 "current conns = %d ; current pipes = %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200470 "</td><td align=\"center\" nowrap>\n"
471 "<table class=\"lgd\"><tr>\n"
472 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
473 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
474 "</tr><tr>\n"
475 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
476 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
477 "</tr><tr>\n"
478 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
479 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
480 "</tr><tr>\n"
481 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100482 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200483 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100484 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +0200485 "</td>"
486 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
487 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
488 "",
489 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Willy Tarreaua8efd362008-01-03 10:19:15 +0100490 pid, pid,
491 relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +0200492 up / 86400, (up % 86400) / 3600,
493 (up % 3600) / 60, (up % 60),
494 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
495 global.rlimit_memmax ? " MB" : "",
496 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100497 global.maxsock, global.maxconn, global.maxpipes,
498 actconn, pipes_used, pipes_used+pipes_free
Willy Tarreau91861262007-10-17 17:06:05 +0200499 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100500
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100501 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200502 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200503 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
504 uri->uri_prefix,
505 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100506 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200507 else
508 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200509 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
510 uri->uri_prefix,
511 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100512 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200513
Willy Tarreau55bb8452007-10-17 18:44:57 +0200514 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100515 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200516 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200517 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
518 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100519 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200520 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200521 else
522 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200523 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
524 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100525 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200526 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200527 }
Willy Tarreau91861262007-10-17 17:06:05 +0200528
Willy Tarreau55bb8452007-10-17 18:44:57 +0200529 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200530 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
531 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100532 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
533 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200534
Willy Tarreau55bb8452007-10-17 18:44:57 +0200535 chunk_printf(&msg, sizeof(trash),
Willy Tarreau5031e6a2007-10-18 11:05:48 +0200536 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
537 uri->uri_prefix,
538 (uri->refresh > 0) ? ";norefresh" : "");
539
540 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200541 "</td>"
542 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
543 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
544 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
545 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
546 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
547 "</ul>"
548 "</td>"
549 "</tr></table>\n"
550 ""
551 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100552
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200553 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200554 return 0;
555 }
Willy Tarreau91861262007-10-17 17:06:05 +0200556
Willy Tarreau91861262007-10-17 17:06:05 +0200557 s->data_ctx.stats.px = proxy;
558 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
559 s->data_state = DATA_ST_LIST;
560 /* fall through */
561
562 case DATA_ST_LIST:
563 /* dump proxies */
564 while (s->data_ctx.stats.px) {
565 px = s->data_ctx.stats.px;
566 /* skip the disabled proxies and non-networked ones */
567 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100568 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200569 return 0;
570
571 s->data_ctx.stats.px = px->next;
572 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
573 }
574 /* here, we just have reached the last proxy */
575
576 s->data_state = DATA_ST_END;
577 /* fall through */
578
579 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100580 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200581 chunk_printf(&msg, sizeof(trash), "</body></html>\n");
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200582 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200583 return 0;
584 }
Willy Tarreau91861262007-10-17 17:06:05 +0200585
586 s->data_state = DATA_ST_FIN;
587 /* fall through */
588
589 case DATA_ST_FIN:
Willy Tarreau72b179a2008-08-28 16:01:32 +0200590 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200591 return 1;
592
593 default:
594 /* unknown state ! */
Willy Tarreau72b179a2008-08-28 16:01:32 +0200595 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200596 return -1;
597 }
598}
599
600
601/*
602 * Dumps statistics for a proxy.
603 * Returns 0 if it had to stop dumping data because of lack of buffer space,
604 * ot non-zero if everything completed.
605 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100606int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200607{
608 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100609 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Willy Tarreau91861262007-10-17 17:06:05 +0200610 struct chunk msg;
611
612 msg.len = 0;
613 msg.str = trash;
614
615 switch (s->data_ctx.stats.px_st) {
616 case DATA_ST_PX_INIT:
617 /* we are on a new proxy */
618
619 if (uri && uri->scope) {
620 /* we have a limited scope, we have to check the proxy name */
621 struct stat_scope *scope;
622 int len;
623
624 len = strlen(px->id);
625 scope = uri->scope;
626
627 while (scope) {
628 /* match exact proxy name */
629 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
630 break;
631
632 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +0200633 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +0200634 break;
635 scope = scope->next;
636 }
637
638 /* proxy name not found : don't dump anything */
639 if (scope == NULL)
640 return 1;
641 }
642
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100643 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100644 (px->uuid != s->data_ctx.stats.iid))
645 return 1;
646
Willy Tarreau91861262007-10-17 17:06:05 +0200647 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
648 /* fall through */
649
650 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100651 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200652 /* print a new table */
653 chunk_printf(&msg, sizeof(trash),
Willy Tarreau7f062c42009-03-05 18:43:00 +0100654 "<table cols=\"27\" class=\"tbl\" width=\"100%%\">\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200655 "<tr align=\"center\" class=\"titre\">"
656 "<th colspan=2 class=\"pxname\">%s</th>"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100657 "<th colspan=25 class=\"empty\"></th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200658 "</tr>\n"
659 "<tr align=\"center\" class=\"titre\">"
660 "<th rowspan=2></th>"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100661 "<th colspan=3>Queue</th><th colspan=6>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200662 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200663 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100664 "<th colspan=8>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200665 "</tr>\n"
666 "<tr align=\"center\" class=\"titre\">"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200667 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100668 "<th>Limit</th><th>Rate</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200669 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200670 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200671 "<th>Status</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100672 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
673 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200674 "</tr>",
Willy Tarreau55bb8452007-10-17 18:44:57 +0200675 px->id);
676
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200677 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200678 return 0;
679 }
Willy Tarreau91861262007-10-17 17:06:05 +0200680
681 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
682 /* fall through */
683
684 case DATA_ST_PX_FE:
685 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100686 if ((px->cap & PR_CAP_FE) &&
687 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
688 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200689 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200690 /* name, queue */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200691 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=3></td>"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100692 /* sessions : current, max, limit, rate, total, lbtot */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200693 "<td align=right>%d</td><td align=right>%d</td>"
694 "<td align=right>%d</td><td align=right>%d</td>"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100695 "<td align=right>%d</td><td align=right></td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200696 /* bytes : in, out */
697 "<td align=right>%lld</td><td align=right>%lld</td>"
698 /* denied: req, resp */
699 "<td align=right>%d</td><td align=right>%d</td>"
700 /* errors : request, connect, response */
701 "<td align=right>%d</td><td align=right></td><td align=right></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200702 /* warnings: retries, redispatches */
703 "<td align=right></td><td align=right></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200704 /* server status : reflect frontend status */
Willy Tarreau91861262007-10-17 17:06:05 +0200705 "<td align=center>%s</td>"
706 /* rest of server: nothing */
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100707 "<td align=center colspan=7></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +0200708 "",
Willy Tarreau7f062c42009-03-05 18:43:00 +0100709 px->feconn, px->feconn_max, px->maxconn,
710 read_freq_ctr(&px->fe_sess_per_sec), px->cum_feconn,
Willy Tarreau91861262007-10-17 17:06:05 +0200711 px->bytes_in, px->bytes_out,
712 px->denied_req, px->denied_resp,
713 px->failed_req,
714 px->state == PR_STRUN ? "OPEN" :
715 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200716 } else {
717 chunk_printf(&msg, sizeof(trash),
718 /* pxid, name, queue cur, queue max, */
719 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100720 /* sessions : current, max, limit, total */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200721 "%d,%d,%d,%d,"
722 /* bytes : in, out */
723 "%lld,%lld,"
724 /* denied: req, resp */
725 "%d,%d,"
726 /* errors : request, connect, response */
727 "%d,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200728 /* warnings: retries, redispatches */
729 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200730 /* server status : reflect frontend status */
731 "%s,"
732 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200733 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100734 /* pid, iid, sid, throttle, lbtot, tracked, type */
735 "%d,%d,0,,,,%d,"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100736 /* rate */
737 "%u,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200738 "\n",
739 px->id,
740 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
741 px->bytes_in, px->bytes_out,
742 px->denied_req, px->denied_resp,
743 px->failed_req,
744 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +0100745 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +0100746 relative_pid, px->uuid, STATS_TYPE_FE,
747 read_freq_ctr(&px->fe_sess_per_sec));
Willy Tarreau55bb8452007-10-17 18:44:57 +0200748 }
Willy Tarreau91861262007-10-17 17:06:05 +0200749
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200750 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200751 return 0;
752 }
753
754 s->data_ctx.stats.sv = px->srv; /* may be NULL */
755 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
756 /* fall through */
757
758 case DATA_ST_PX_SV:
759 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100760 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
761
Willy Tarreau2ea81932007-11-30 12:04:38 +0100762 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 +0200763
764 sv = s->data_ctx.stats.sv;
765
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100766 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100767 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
768 break;
769
770 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
771 continue;
772 }
773
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100774 if (sv->tracked)
775 svs = sv->tracked;
776 else
777 svs = sv;
778
Willy Tarreau91861262007-10-17 17:06:05 +0200779 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100780 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +0100781 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100782 else if (svs->state & SRV_RUNNING) {
783 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +0200784 sv_state = 3; /* UP */
785 else
786 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +0100787
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100788 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +0100789 sv_state += 2;
790 }
Willy Tarreau91861262007-10-17 17:06:05 +0200791 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100792 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +0200793 sv_state = 1; /* going up */
794 else
795 sv_state = 0; /* DOWN */
796
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100797 if ((sv_state == 0) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +0200798 /* do not report servers which are DOWN */
799 s->data_ctx.stats.sv = sv->next;
800 continue;
801 }
802
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100803 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +0100804 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
805 "UP %d/%d &darr;", "UP",
806 "NOLB %d/%d &darr;", "NOLB",
807 "<i>no check</i>" };
Willy Tarreau55bb8452007-10-17 18:44:57 +0200808 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200809 /* name */
810 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200811 /* queue : current, max, limit */
812 "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td>"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100813 /* sessions : current, max, limit, rate, total, lbtot */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200814 "<td align=right>%d</td><td align=right>%d</td>"
815 "<td align=right>%s</td><td align=right>%d</td>"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100816 "<td align=right>%d</td><td align=right>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200817 /* bytes : in, out */
818 "<td align=right>%lld</td><td align=right>%lld</td>"
819 /* denied: req, resp */
820 "<td align=right></td><td align=right>%d</td>"
821 /* errors : request, connect, response */
822 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200823 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100824 "<td align=right>%u</td><td align=right>%u</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200825 "",
826 (sv->state & SRV_BACKUP) ? "backup" : "active",
827 sv_state, sv->id,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200828 sv->nbpend, sv->nbpend_max, LIM2A0(sv->maxqueue, "-"),
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100829 sv->cur_sess, sv->cur_sess_max, LIM2A1(sv->maxconn, "-"),
Willy Tarreau7f062c42009-03-05 18:43:00 +0100830 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100831 sv->cum_sess, sv->cum_lbconn,
Willy Tarreau91861262007-10-17 17:06:05 +0200832 sv->bytes_in, sv->bytes_out,
833 sv->failed_secu,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200834 sv->failed_conns, sv->failed_resp,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100835 sv->retries, sv->redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100836
Willy Tarreau55bb8452007-10-17 18:44:57 +0200837 /* status */
838 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200839
840 if (sv->state & SRV_CHECKED)
841 chunk_printf(&msg, sizeof(trash), "%s ",
842 human_time(now.tv_sec - sv->last_change, 1));
843
Willy Tarreau55bb8452007-10-17 18:44:57 +0200844 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200845 srv_hlt_st[sv_state],
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100846 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
847 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Willy Tarreau91861262007-10-17 17:06:05 +0200848
Willy Tarreau55bb8452007-10-17 18:44:57 +0200849 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200850 /* weight */
851 "</td><td>%d</td>"
852 /* act, bck */
853 "<td>%s</td><td>%s</td>"
854 "",
Willy Tarreau5542af62007-12-03 02:04:00 +0100855 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +0200856 (sv->state & SRV_BACKUP) ? "-" : "Y",
857 (sv->state & SRV_BACKUP) ? "Y" : "-");
858
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200859 /* check failures: unique, fatal, down time */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200860 if (sv->state & SRV_CHECKED)
861 chunk_printf(&msg, sizeof(trash),
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200862 "<td align=right>%d</td><td align=right>%d</td>"
863 "<td nowrap align=right>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100864 "",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100865 svs->failed_checks, svs->down_trans,
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200866 human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100867 else if (sv != svs)
868 chunk_printf(&msg, sizeof(trash),
869 "<td nowrap colspan=3>via %s/%s</td>", svs->proxy->id, svs->id );
Willy Tarreau55bb8452007-10-17 18:44:57 +0200870 else
871 chunk_printf(&msg, sizeof(trash),
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100872 "<td colspan=3></td>");
873
874 /* throttle */
875 if ((sv->state & SRV_WARMINGUP) &&
876 now.tv_sec < sv->last_change + sv->slowstart &&
877 now.tv_sec >= sv->last_change) {
878 unsigned int ratio;
879 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
880 chunk_printf(&msg, sizeof(trash),
881 "<td>%d %%</td></tr>\n", ratio);
882 } else {
883 chunk_printf(&msg, sizeof(trash),
884 "<td>-</td></tr>\n");
885 }
Willy Tarreau55bb8452007-10-17 18:44:57 +0200886 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +0100887 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
888 "UP %d/%d,", "UP,",
889 "NOLB %d/%d,", "NOLB,",
890 "no check," };
Willy Tarreau55bb8452007-10-17 18:44:57 +0200891 chunk_printf(&msg, sizeof(trash),
892 /* pxid, name */
893 "%s,%s,"
894 /* queue : current, max */
895 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100896 /* sessions : current, max, limit, total */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200897 "%d,%d,%s,%d,"
898 /* bytes : in, out */
899 "%lld,%lld,"
900 /* denied: req, resp */
901 ",%d,"
902 /* errors : request, connect, response */
903 ",%d,%d,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200904 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100905 "%u,%u,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200906 "",
907 px->id, sv->id,
908 sv->nbpend, sv->nbpend_max,
Willy Tarreaudcd47712007-11-04 23:35:08 +0100909 sv->cur_sess, sv->cur_sess_max, LIM2A0(sv->maxconn, ""), sv->cum_sess,
Willy Tarreau55bb8452007-10-17 18:44:57 +0200910 sv->bytes_in, sv->bytes_out,
911 sv->failed_secu,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200912 sv->failed_conns, sv->failed_resp,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100913 sv->retries, sv->redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100914
Willy Tarreau55bb8452007-10-17 18:44:57 +0200915 /* status */
916 chunk_printf(&msg, sizeof(trash),
917 srv_hlt_st[sv_state],
918 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
919 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
Willy Tarreau91861262007-10-17 17:06:05 +0200920
Willy Tarreau55bb8452007-10-17 18:44:57 +0200921 chunk_printf(&msg, sizeof(trash),
922 /* weight, active, backup */
923 "%d,%d,%d,"
924 "",
Willy Tarreau5542af62007-12-03 02:04:00 +0100925 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +0200926 (sv->state & SRV_BACKUP) ? 0 : 1,
927 (sv->state & SRV_BACKUP) ? 1 : 0);
928
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200929 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200930 if (sv->state & SRV_CHECKED)
931 chunk_printf(&msg, sizeof(trash),
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200932 "%d,%d,%d,%d,",
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200933 sv->failed_checks, sv->down_trans,
934 now.tv_sec - sv->last_change, srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +0200935 else
936 chunk_printf(&msg, sizeof(trash),
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200937 ",,,,");
938
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100939 /* queue limit, pid, iid, sid, */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200940 chunk_printf(&msg, sizeof(trash),
Willy Tarreaudcd47712007-11-04 23:35:08 +0100941 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100942 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +0100943 LIM2A0(sv->maxqueue, ""),
944 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100945
946 /* throttle */
947 if ((sv->state & SRV_WARMINGUP) &&
948 now.tv_sec < sv->last_change + sv->slowstart &&
949 now.tv_sec >= sv->last_change) {
950 unsigned int ratio;
951 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100952 chunk_printf(&msg, sizeof(trash), "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100953 }
954
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100955 /* sessions: lbtot */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100956 chunk_printf(&msg, sizeof(trash), ",%d,", sv->cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100957
958 /* tracked */
959 if (sv->tracked)
Krzysztof Piotr Oledzkif58a9622008-02-23 01:19:10 +0100960 chunk_printf(&msg, sizeof(trash), "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100961 sv->tracked->proxy->id, sv->tracked->id);
962 else
963 chunk_printf(&msg, sizeof(trash), ",");
964
Willy Tarreau7f062c42009-03-05 18:43:00 +0100965 /* type */
966 chunk_printf(&msg, sizeof(trash), "%d,", STATS_TYPE_SV);
967
968 /* rate */
969 chunk_printf(&msg, sizeof(trash), "%u,", read_freq_ctr(&sv->sess_per_sec));
970
971 /* finish with EOL */
972 chunk_printf(&msg, sizeof(trash), "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200973 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200974 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200975 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100976 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +0200977
978 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
979 /* fall through */
980
981 case DATA_ST_PX_BE:
982 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100983 if ((px->cap & PR_CAP_BE) &&
984 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
985 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200986 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200987 /* name */
988 "<tr align=center class=\"backend\"><td>Backend</td>"
989 /* queue : current, max */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200990 "<td align=right>%d</td><td align=right>%d</td><td></td>"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100991 /* sessions : current, max, limit, rate, total, lbtot */
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100992 "<td align=right>%d</td><td align=right>%d</td>"
993 "<td align=right>%d</td><td align=right>%d</td>"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100994 "<td align=right>%d</td><td align=right>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200995 /* bytes : in, out */
996 "<td align=right>%lld</td><td align=right>%lld</td>"
997 /* denied: req, resp */
998 "<td align=right>%d</td><td align=right>%d</td>"
999 /* errors : request, connect, response */
1000 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001001 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001002 "<td align=right>%u</td><td align=right>%u</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001003 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02001004 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001005 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02001006 * active and backups. */
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001007 "<td align=center nowrap>%s %s</td><td align=center>%d</td>"
1008 "<td align=center>%d</td><td align=center>%d</td>",
Willy Tarreau91861262007-10-17 17:06:05 +02001009 px->nbpend /* or px->totpend ? */, px->nbpend_max,
Willy Tarreau7f062c42009-03-05 18:43:00 +01001010 px->beconn, px->beconn_max, px->fullconn,
1011 read_freq_ctr(&px->be_sess_per_sec),
1012 px->cum_beconn, px->cum_lbconn,
Willy Tarreau91861262007-10-17 17:06:05 +02001013 px->bytes_in, px->bytes_out,
1014 px->denied_req, px->denied_resp,
1015 px->failed_conns, px->failed_resp,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001016 px->retries, px->redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001017 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01001018 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
1019 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01001020 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001021 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001022
1023 chunk_printf(&msg, sizeof(trash),
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001024 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001025 "<td align=center>&nbsp;</td><td align=\"right\">%d</td>"
1026 "<td align=\"right\" nowrap>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001027 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001028 "</tr>",
1029 px->down_trans,
1030 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001031 } else {
1032 chunk_printf(&msg, sizeof(trash),
1033 /* pxid, name */
1034 "%s,BACKEND,"
1035 /* queue : current, max */
1036 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001037 /* sessions : current, max, limit, total */
Willy Tarreau55bb8452007-10-17 18:44:57 +02001038 "%d,%d,%d,%d,"
1039 /* bytes : in, out */
1040 "%lld,%lld,"
1041 /* denied: req, resp */
1042 "%d,%d,"
1043 /* errors : request, connect, response */
1044 ",%d,%d,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001045 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001046 "%u,%u,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001047 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02001048 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001049 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02001050 * active and backups. */
1051 "%s,"
1052 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001053 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001054 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001055 /* pid, iid, sid, throttle, lbtot, tracked, type */
1056 "%d,%d,0,,%d,,%d,"
Willy Tarreau7f062c42009-03-05 18:43:00 +01001057 /* rate */
1058 "%u,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001059 "\n",
1060 px->id,
1061 px->nbpend /* or px->totpend ? */, px->nbpend_max,
1062 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
1063 px->bytes_in, px->bytes_out,
1064 px->denied_req, px->denied_resp,
1065 px->failed_conns, px->failed_resp,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001066 px->retries, px->redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01001067 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01001068 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001069 px->srv_act, px->srv_bck,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001070 px->down_trans, now.tv_sec - px->last_change,
Willy Tarreau20697042007-11-15 23:26:18 +01001071 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001072 relative_pid, px->uuid,
Willy Tarreau7f062c42009-03-05 18:43:00 +01001073 px->cum_lbconn, STATS_TYPE_BE,
1074 read_freq_ctr(&px->be_sess_per_sec));
Willy Tarreau55bb8452007-10-17 18:44:57 +02001075 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001076 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001077 return 0;
1078 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001079
Willy Tarreau91861262007-10-17 17:06:05 +02001080 s->data_ctx.stats.px_st = DATA_ST_PX_END;
1081 /* fall through */
1082
1083 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001084 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001085 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001086
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001087 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001088 return 0;
1089 }
Willy Tarreau91861262007-10-17 17:06:05 +02001090
1091 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
1092 /* fall through */
1093
1094 case DATA_ST_PX_FIN:
1095 return 1;
1096
1097 default:
1098 /* unknown state, we should put an abort() here ! */
1099 return 1;
1100 }
1101}
1102
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001103
1104/* This function is called to send output to the response buffer.
1105 * It dumps the sessions states onto the output buffer <rep>.
1106 * Expects to be called with client socket shut down on input.
1107 * s->data_ctx must have been zeroed first, and the flags properly set.
1108 * It automatically clears the HIJACK bit from the response buffer.
1109 */
1110void stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
1111{
1112 struct chunk msg;
1113
1114 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
1115 /* If we're forced to shut down, we might have to remove our
1116 * reference to the last session being dumped.
1117 */
1118 if (s->data_state == DATA_ST_LIST) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001119 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001120 LIST_DEL(&s->data_ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001121 LIST_INIT(&s->data_ctx.sess.bref.users);
1122 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001123 }
1124 s->data_state = DATA_ST_FIN;
1125 buffer_stop_hijack(rep);
1126 s->ana_state = STATS_ST_CLOSE;
1127 return;
1128 }
1129
1130 if (s->ana_state != STATS_ST_REP)
1131 return;
1132
1133 msg.len = 0;
1134 msg.str = trash;
1135
1136 switch (s->data_state) {
1137 case DATA_ST_INIT:
1138 /* the function had not been called yet, let's prepare the
1139 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001140 * pointer to the first in the global list. When a target
1141 * session is being destroyed, it is responsible for updating
1142 * this pointer. We know we have reached the end when this
1143 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001144 */
1145 stream_int_retnclose(rep->cons, &msg);
1146 LIST_INIT(&s->data_ctx.sess.bref.users);
1147 s->data_ctx.sess.bref.ref = sessions.n;
1148 s->data_state = DATA_ST_LIST;
1149 /* fall through */
1150
1151 case DATA_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001152 /* first, let's detach the back-ref from a possible previous session */
1153 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
1154 LIST_DEL(&s->data_ctx.sess.bref.users);
1155 LIST_INIT(&s->data_ctx.sess.bref.users);
1156 }
1157
1158 /* and start from where we stopped */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001159 while (s->data_ctx.sess.bref.ref != &sessions) {
1160 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1161 struct session *curr_sess;
1162
1163 curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list);
1164
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001165 chunk_printf(&msg, sizeof(trash),
1166 "%p: proto=%s",
1167 curr_sess,
1168 curr_sess->listener->proto->name);
1169
1170 switch (curr_sess->listener->proto->sock_family) {
1171 case AF_INET:
1172 inet_ntop(AF_INET,
1173 (const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr,
1174 pn, sizeof(pn));
1175
1176 chunk_printf(&msg, sizeof(trash),
1177 " src=%s:%d fe=%s be=%s srv=%s",
1178 pn,
1179 ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port),
1180 curr_sess->fe->id,
1181 curr_sess->be->id,
1182 curr_sess->srv ? curr_sess->srv->id : "<none>"
1183 );
1184 break;
1185 case AF_INET6:
1186 inet_ntop(AF_INET6,
1187 (const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr,
1188 pn, sizeof(pn));
1189
1190 chunk_printf(&msg, sizeof(trash),
1191 " src=%s:%d fe=%s be=%s srv=%s",
1192 pn,
1193 ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port),
1194 curr_sess->fe->id,
1195 curr_sess->be->id,
1196 curr_sess->srv ? curr_sess->srv->id : "<none>"
1197 );
1198
1199 break;
1200 case AF_UNIX:
1201 /* no more information to print right now */
1202 break;
1203 }
1204
1205 chunk_printf(&msg, sizeof(trash),
Willy Tarreau8a5c6262008-12-08 00:16:21 +01001206 " si=(%d,%d) as=%d ts=%02x age=%s",
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001207 curr_sess->si[0].state, curr_sess->si[1].state,
Willy Tarreau8a5c6262008-12-08 00:16:21 +01001208 curr_sess->ana_state, curr_sess->task->state,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001209 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1));
1210
Willy Tarreau8a5c6262008-12-08 00:16:21 +01001211 if (curr_sess->task->state & TASK_IN_RUNQUEUE)
1212 chunk_printf(&msg, sizeof(trash), " run(nice=%d)\n", curr_sess->task->nice);
1213 else
1214 chunk_printf(&msg, sizeof(trash),
1215 " exp=%s\n",
1216 curr_sess->task->expire ?
1217 human_time(TICKS_TO_MS(tick_remain(now_ms, curr_sess->task->expire)),
1218 TICKS_TO_MS(1000))
1219 : "never");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001220
1221 if (buffer_write_chunk(rep, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001222 /* let's try again later from this session. We add ourselves into
1223 * this session's users so that it can remove us upon termination.
1224 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001225 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
1226 return;
1227 }
1228
1229 s->data_ctx.sess.bref.ref = curr_sess->list.n;
1230 }
1231 s->data_state = DATA_ST_FIN;
1232 /* fall through */
1233
1234 default:
1235 s->data_state = DATA_ST_FIN;
1236 buffer_stop_hijack(rep);
1237 s->ana_state = STATS_ST_CLOSE;
1238 return;
1239 }
1240}
1241
Willy Tarreau74808cb2009-03-04 15:53:18 +01001242/* print a line of error buffer (limited to 70 bytes) to <out>. The format is :
1243 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
1244 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
1245 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
1246 * lines are respected within the limit of 70 output chars. Lines that are
1247 * continuation of a previous truncated line begin with "+" instead of " "
1248 * after the offset. The new pointer is returned.
1249 */
1250static int dump_error_line(struct chunk *out, int size,
1251 struct error_snapshot *err, int *line, int ptr)
1252{
1253 int end;
1254 unsigned char c;
1255
1256 end = out->len + 80;
1257 if (end > size)
1258 return ptr;
1259
1260 chunk_printf(out, size, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
1261
1262 while (ptr < err->len) {
1263 c = err->buf[ptr];
1264 if (isprint(c)) {
1265 if (out->len > end - 2)
1266 break;
1267 out->str[out->len++] = c;
1268 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e') {
1269 if (out->len > end - 3)
1270 break;
1271 out->str[out->len++] = '\\';
1272 switch (c) {
1273 case '\t': c = 't'; break;
1274 case '\n': c = 'n'; break;
1275 case '\r': c = 'r'; break;
1276 case '\e': c = 'e'; break;
1277 }
1278 out->str[out->len++] = c;
1279 } else {
1280 if (out->len > end - 5)
1281 break;
1282 out->str[out->len++] = '\\';
1283 out->str[out->len++] = 'x';
1284 out->str[out->len++] = hextab[(c >> 4) & 0xF];
1285 out->str[out->len++] = hextab[c & 0xF];
1286 }
1287 if (err->buf[ptr++] == '\n') {
1288 /* we had a line break, let's return now */
1289 out->str[out->len++] = '\n';
1290 *line = ptr;
1291 return ptr;
1292 }
1293 }
1294 /* we have an incomplete line, we return it as-is */
1295 out->str[out->len++] = '\n';
1296 return ptr;
1297}
1298
1299/* This function is called to send output to the response buffer.
1300 * It dumps the errors logged in proxies onto the output buffer <rep>.
1301 * Expects to be called with client socket shut down on input.
1302 * s->data_ctx must have been zeroed first, and the flags properly set.
1303 * It automatically clears the HIJACK bit from the response buffer.
1304 */
1305void stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
1306{
1307 extern const char *monthname[12];
1308 struct chunk msg;
1309
1310 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
1311 s->data_state = DATA_ST_FIN;
1312 buffer_stop_hijack(rep);
1313 s->ana_state = STATS_ST_CLOSE;
1314 return;
1315 }
1316
1317 if (s->ana_state != STATS_ST_REP)
1318 return;
1319
1320 msg.len = 0;
1321 msg.str = trash;
1322
1323 if (!s->data_ctx.errors.px) {
1324 /* the function had not been called yet, let's prepare the
1325 * buffer for a response.
1326 */
1327 stream_int_retnclose(rep->cons, &msg);
1328 s->data_ctx.errors.px = proxy;
1329 s->data_ctx.errors.buf = 0;
1330 s->data_ctx.errors.bol = 0;
1331 s->data_ctx.errors.ptr = -1;
1332 }
1333
1334 /* we have two inner loops here, one for the proxy, the other one for
1335 * the buffer.
1336 */
1337 while (s->data_ctx.errors.px) {
1338 struct error_snapshot *es;
1339
1340 if (s->data_ctx.errors.buf == 0)
1341 es = &s->data_ctx.errors.px->invalid_req;
1342 else
1343 es = &s->data_ctx.errors.px->invalid_rep;
1344
1345 if (!es->when.tv_sec)
1346 goto next;
1347
1348 if (s->data_ctx.errors.iid >= 0 &&
1349 s->data_ctx.errors.px->uuid != s->data_ctx.errors.iid &&
1350 es->oe->uuid != s->data_ctx.errors.iid)
1351 goto next;
1352
1353 if (s->data_ctx.errors.ptr < 0) {
1354 /* just print headers now */
1355
1356 char pn[INET6_ADDRSTRLEN];
1357 struct tm tm;
1358
1359 get_localtime(es->when.tv_sec, &tm);
1360 chunk_printf(&msg, sizeof(trash), "\n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
1361 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
1362 tm.tm_hour, tm.tm_min, tm.tm_sec, es->when.tv_usec/1000);
1363
1364
1365 if (es->src.ss_family == AF_INET)
1366 inet_ntop(AF_INET,
1367 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
1368 pn, sizeof(pn));
1369 else
1370 inet_ntop(AF_INET6,
1371 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
1372 pn, sizeof(pn));
1373
1374 switch (s->data_ctx.errors.buf) {
1375 case 0:
1376 chunk_printf(&msg, sizeof(trash),
1377 " frontend %s (#%d): invalid request\n"
1378 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
1379 " request length %d bytes, error at position %d:\n\n",
1380 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
1381 pn, es->sid, es->oe->id, es->oe->uuid,
1382 es->srv ? es->srv->id : "<NONE>",
1383 es->srv ? es->srv->puid : -1,
1384 es->len, es->pos);
1385 break;
1386 case 1:
1387 chunk_printf(&msg, sizeof(trash),
1388 " backend %s (#%d) : invalid response\n"
1389 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
1390 " response length %d bytes, error at position %d:\n\n",
1391 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
1392 pn, es->sid, es->oe->id, es->oe->uuid,
1393 es->srv ? es->srv->id : "<NONE>",
1394 es->srv ? es->srv->puid : -1,
1395 es->len, es->pos);
1396 break;
1397 }
1398
1399 if (buffer_write_chunk(rep, &msg) >= 0) {
1400 /* Socket buffer full. Let's try again later from the same point */
1401 return;
1402 }
1403 s->data_ctx.errors.ptr = 0;
1404 s->data_ctx.errors.sid = es->sid;
1405 }
1406
1407 if (s->data_ctx.errors.sid != es->sid) {
1408 /* the snapshot changed while we were dumping it */
1409 chunk_printf(&msg, sizeof(trash),
1410 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
1411 if (buffer_write_chunk(rep, &msg) >= 0)
1412 return;
1413 goto next;
1414 }
1415
1416 /* OK, ptr >= 0, so we have to dump the current line */
1417 while (s->data_ctx.errors.ptr < es->len) {
1418 int newptr;
1419 int newline;
1420
1421 newline = s->data_ctx.errors.bol;
1422 newptr = dump_error_line(&msg, sizeof(trash), es, &newline, s->data_ctx.errors.ptr);
1423 if (newptr == s->data_ctx.errors.ptr)
1424 return;
1425
1426 if (buffer_write_chunk(rep, &msg) >= 0) {
1427 /* Socket buffer full. Let's try again later from the same point */
1428 return;
1429 }
1430 s->data_ctx.errors.ptr = newptr;
1431 s->data_ctx.errors.bol = newline;
1432 };
1433 next:
1434 s->data_ctx.errors.bol = 0;
1435 s->data_ctx.errors.ptr = -1;
1436 s->data_ctx.errors.buf++;
1437 if (s->data_ctx.errors.buf > 1) {
1438 s->data_ctx.errors.buf = 0;
1439 s->data_ctx.errors.px = s->data_ctx.errors.px->next;
1440 }
1441 }
1442
1443 /* dump complete */
1444 buffer_stop_hijack(rep);
1445 s->ana_state = STATS_ST_CLOSE;
1446}
1447
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001448
Willy Tarreau10522fd2008-07-09 20:12:41 +02001449static struct cfg_kw_list cfg_kws = {{ },{
1450 { CFG_GLOBAL, "stats", stats_parse_global },
1451 { 0, NULL, NULL },
1452}};
1453
1454__attribute__((constructor))
1455static void __dumpstats_module_init(void)
1456{
1457 cfg_register_keywords(&cfg_kws);
1458}
1459
Willy Tarreau91861262007-10-17 17:06:05 +02001460/*
1461 * Local variables:
1462 * c-indent-level: 8
1463 * c-basic-offset: 8
1464 * End:
1465 */