blob: ea6f4460d9098975e1ba9ac19c7b8f461737b3b8 [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 Tarreau4726f532009-03-07 17:25:21 +010050#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020051
Willy Tarreaufbee7132007-10-18 13:53:22 +020052/* This function parses a "stats" statement in the "global" section. It returns
53 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
54 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
55 * zero included. The trailing '\n' must not be written. The function must be
56 * called with <args> pointing to the first word after "stats".
57 */
Willy Tarreau10522fd2008-07-09 20:12:41 +020058static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
59 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +020060{
Willy Tarreau10522fd2008-07-09 20:12:41 +020061 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +020062 if (!strcmp(args[0], "socket")) {
63 struct sockaddr_un su;
64 int cur_arg;
65
66 if (*args[1] == 0) {
67 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
68 return -1;
69 }
70
71 if (global.stats_sock.state != LI_NEW) {
72 snprintf(err, errlen, "'stats socket' already specified in global section");
73 return -1;
74 }
75
76 su.sun_family = AF_UNIX;
77 strncpy(su.sun_path, args[1], sizeof(su.sun_path));
78 su.sun_path[sizeof(su.sun_path) - 1] = 0;
79 memcpy(&global.stats_sock.addr, &su, sizeof(su)); // guaranteed to fit
80
81 global.stats_sock.state = LI_INIT;
Willy Tarreau6fb42e02007-10-28 17:02:33 +010082 global.stats_sock.options = LI_O_NONE;
Willy Tarreaufbee7132007-10-18 13:53:22 +020083 global.stats_sock.accept = uxst_event_accept;
Willy Tarreaub1356cf2008-12-07 16:06:43 +010084 global.stats_sock.handler = uxst_process_session;
85 global.stats_sock.analysers = AN_REQ_UNIX_STATS;
Willy Tarreaufbee7132007-10-18 13:53:22 +020086 global.stats_sock.private = NULL;
87
88 cur_arg = 2;
89 while (*args[cur_arg]) {
90 if (!strcmp(args[cur_arg], "uid")) {
91 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
92 cur_arg += 2;
93 }
94 else if (!strcmp(args[cur_arg], "gid")) {
95 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
96 cur_arg += 2;
97 }
98 else if (!strcmp(args[cur_arg], "mode")) {
99 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
100 cur_arg += 2;
101 }
102 else if (!strcmp(args[cur_arg], "user")) {
103 struct passwd *user;
104 user = getpwnam(args[cur_arg + 1]);
105 if (!user) {
106 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
107 args[cur_arg + 1]);
108 return -1;
109 }
110 global.stats_sock.perm.ux.uid = user->pw_uid;
111 cur_arg += 2;
112 }
113 else if (!strcmp(args[cur_arg], "group")) {
114 struct group *group;
115 group = getgrnam(args[cur_arg + 1]);
116 if (!group) {
117 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
118 args[cur_arg + 1]);
119 return -1;
120 }
121 global.stats_sock.perm.ux.gid = group->gr_gid;
122 cur_arg += 2;
123 }
124 else {
125 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', and 'mode'");
126 return -1;
127 }
128 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100129
Willy Tarreaufbee7132007-10-18 13:53:22 +0200130 uxst_add_listener(&global.stats_sock);
131 global.maxsock++;
132 }
133 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100134 unsigned timeout;
135 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
136
137 if (res) {
138 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
139 return -1;
140 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200141
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100142 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200143 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
144 return -1;
145 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200146 global.stats_timeout = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200147 }
148 else if (!strcmp(args[0], "maxconn")) {
149 int maxconn = atol(args[1]);
150
151 if (maxconn <= 0) {
152 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
153 return -1;
154 }
155 global.maxsock -= global.stats_sock.maxconn;
156 global.stats_sock.maxconn = maxconn;
157 global.maxsock += global.stats_sock.maxconn;
158 }
159 else {
160 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
161 return -1;
162 }
163 return 0;
164}
165
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100166int print_csv_header(struct chunk *msg, int size)
167{
168 return chunk_printf(msg, size,
169 "# pxname,svname,"
170 "qcur,qmax,"
171 "scur,smax,slim,stot,"
172 "bin,bout,"
173 "dreq,dresp,"
174 "ereq,econ,eresp,"
175 "wretr,wredis,"
176 "status,weight,act,bck,"
177 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200178 "pid,iid,sid,throttle,lbtot,tracked,type,"
179 "rate,rate_lim,rate_max,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100180 "\n");
181}
182
Willy Tarreau91861262007-10-17 17:06:05 +0200183/*
184 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200185 * client socket shut down on input. It *may* make use of informations from
186 * <uri>. s->data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200187 * It returns 0 if it had to stop writing data and an I/O is needed, 1 if the
188 * dump is finished and the session must be closed, or -1 in case of any error.
Willy Tarreau0a464892008-12-07 18:30:00 +0100189 * It automatically clears the HIJACK bit from the response buffer.
Willy Tarreau3e76e722007-10-17 18:57:38 +0200190 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100191int stats_dump_raw(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau3e76e722007-10-17 18:57:38 +0200192{
Willy Tarreau3e76e722007-10-17 18:57:38 +0200193 struct proxy *px;
194 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +0100195 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200196
197 msg.len = 0;
198 msg.str = trash;
199
200 switch (s->data_state) {
201 case DATA_ST_INIT:
202 /* the function had not been called yet, let's prepare the
203 * buffer for a response.
204 */
Willy Tarreaudded32d2008-11-30 19:48:07 +0100205 stream_int_retnclose(rep->cons, &msg);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200206 s->data_state = DATA_ST_HEAD;
207 /* fall through */
208
209 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100210 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100211 print_csv_header(&msg, sizeof(trash));
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200212 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100213 return 0;
214 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200215
216 s->data_state = DATA_ST_INFO;
217 /* fall through */
218
219 case DATA_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +0100220 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100221 if (s->data_ctx.stats.flags & STAT_SHOW_INFO) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100222 chunk_printf(&msg, sizeof(trash),
223 "Name: " PRODUCT_NAME "\n"
224 "Version: " HAPROXY_VERSION "\n"
225 "Release_date: " HAPROXY_DATE "\n"
226 "Nbproc: %d\n"
227 "Process_num: %d\n"
228 "Pid: %d\n"
229 "Uptime: %dd %dh%02dm%02ds\n"
230 "Uptime_sec: %d\n"
231 "Memmax_MB: %d\n"
232 "Ulimit-n: %d\n"
233 "Maxsock: %d\n"
234 "Maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100235 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100236 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100237 "PipesUsed: %d\n"
238 "PipesFree: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100239 "Tasks: %d\n"
240 "Run_queue: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100241 "",
242 global.nbproc,
243 relative_pid,
244 pid,
245 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
246 up,
247 global.rlimit_memmax,
248 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100249 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100250 actconn, pipes_used, pipes_free,
251 nb_tasks_cur, run_queue_cur
Willy Tarreaua8efd362008-01-03 10:19:15 +0100252 );
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200253 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100254 return 0;
255 }
256
Willy Tarreau3e76e722007-10-17 18:57:38 +0200257 s->data_ctx.stats.px = proxy;
258 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100259
260 s->data_ctx.stats.sv = NULL;
261 s->data_ctx.stats.sv_st = 0;
262
Willy Tarreau3e76e722007-10-17 18:57:38 +0200263 s->data_state = DATA_ST_LIST;
264 /* fall through */
265
266 case DATA_ST_LIST:
267 /* dump proxies */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100268 if (s->data_ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreaua8efd362008-01-03 10:19:15 +0100269 while (s->data_ctx.stats.px) {
270 px = s->data_ctx.stats.px;
271 /* skip the disabled proxies and non-networked ones */
272 if (px->state != PR_STSTOPPED &&
273 (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100274 if (stats_dump_proxy(s, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +0100275 return 0;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200276
Willy Tarreaua8efd362008-01-03 10:19:15 +0100277 s->data_ctx.stats.px = px->next;
278 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
279 }
280 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200281 }
Willy Tarreau3e76e722007-10-17 18:57:38 +0200282
283 s->data_state = DATA_ST_END;
284 /* fall through */
285
286 case DATA_ST_END:
287 s->data_state = DATA_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +0100288 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +0200289
290 case DATA_ST_FIN:
Willy Tarreau0a464892008-12-07 18:30:00 +0100291 buffer_stop_hijack(rep);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200292 return 1;
293
294 default:
295 /* unknown state ! */
Willy Tarreau0a464892008-12-07 18:30:00 +0100296 buffer_stop_hijack(rep);
Willy Tarreau3e76e722007-10-17 18:57:38 +0200297 return -1;
298 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100299}
300
301
302/* This function is called to send output to the response buffer. It simply
303 * calls stats_dump_raw(), and releases the buffer's hijack bit when the dump
Willy Tarreau01bf8672008-12-07 18:03:29 +0100304 * is finished.
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100305 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100306void stats_dump_raw_to_buffer(struct session *s, struct buffer *rep)
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100307{
308 if (s->ana_state != STATS_ST_REP)
Willy Tarreau01bf8672008-12-07 18:03:29 +0100309 return;
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100310
Willy Tarreau0a464892008-12-07 18:30:00 +0100311 if (stats_dump_raw(s, rep, NULL) != 0)
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100312 s->ana_state = STATS_ST_CLOSE;
Willy Tarreau01bf8672008-12-07 18:03:29 +0100313 return;
Willy Tarreau3e76e722007-10-17 18:57:38 +0200314}
315
316
317/*
318 * Produces statistics data for the session <s>. Expects to be called with
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200319 * client socket shut down on input. It stops by itself by unsetting the
Willy Tarreau72b179a2008-08-28 16:01:32 +0200320 * BF_HIJACK flag from the buffer, which it uses to keep on being called
Willy Tarreau1ae3a052008-08-16 10:56:30 +0200321 * when there is free space in the buffer, of simply by letting an empty buffer
322 * upon return.s->data_ctx must have been zeroed before the first call, and the
323 * flags set. It returns 0 if it had to stop writing data and an I/O is needed,
324 * 1 if the dump is finished and the session must be closed, or -1 in case of
325 * any error.
Willy Tarreau91861262007-10-17 17:06:05 +0200326 */
Willy Tarreau0a464892008-12-07 18:30:00 +0100327int stats_dump_http(struct session *s, struct buffer *rep, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200328{
Willy Tarreau91861262007-10-17 17:06:05 +0200329 struct proxy *px;
330 struct chunk msg;
331 unsigned int up;
332
333 msg.len = 0;
334 msg.str = trash;
335
336 switch (s->data_state) {
337 case DATA_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +0200338 chunk_printf(&msg, sizeof(trash),
339 "HTTP/1.0 200 OK\r\n"
340 "Cache-Control: no-cache\r\n"
341 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200342 "Content-Type: %s\r\n",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100343 (s->data_ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +0200344
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100345 if (uri->refresh > 0 && !(s->data_ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau91861262007-10-17 17:06:05 +0200346 chunk_printf(&msg, sizeof(trash), "Refresh: %d\r\n",
347 uri->refresh);
348
349 chunk_printf(&msg, sizeof(trash), "\r\n");
350
351 s->txn.status = 200;
Willy Tarreaudded32d2008-11-30 19:48:07 +0100352 stream_int_retnclose(rep->cons, &msg); // send the start of the response.
Willy Tarreau91861262007-10-17 17:06:05 +0200353 msg.len = 0;
354
355 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
356 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
357 if (!(s->flags & SN_FINST_MASK))
358 s->flags |= SN_FINST_R;
359
360 if (s->txn.meth == HTTP_METH_HEAD) {
361 /* that's all we return in case of HEAD request */
362 s->data_state = DATA_ST_FIN;
Willy Tarreau72b179a2008-08-28 16:01:32 +0200363 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200364 return 1;
365 }
366
367 s->data_state = DATA_ST_HEAD; /* let's start producing data */
368 /* fall through */
369
370 case DATA_ST_HEAD:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100371 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200372 /* WARNING! This must fit in the first buffer !!! */
373 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200374 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
375 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
376 "<style type=\"text/css\"><!--\n"
377 "body {"
378 " font-family: helvetica, arial;"
379 " font-size: 12px;"
380 " font-weight: normal;"
381 " color: black;"
382 " background: white;"
383 "}\n"
384 "th,td {"
385 " font-size: 0.8em;"
386 " align: center;"
387 "}\n"
388 "h1 {"
389 " font-size: xx-large;"
390 " margin-bottom: 0.5em;"
391 "}\n"
392 "h2 {"
393 " font-family: helvetica, arial;"
394 " font-size: x-large;"
395 " font-weight: bold;"
396 " font-style: italic;"
397 " color: #6020a0;"
398 " margin-top: 0em;"
399 " margin-bottom: 0em;"
400 "}\n"
401 "h3 {"
402 " font-family: helvetica, arial;"
403 " font-size: 16px;"
404 " font-weight: bold;"
405 " color: #b00040;"
406 " background: #e8e8d0;"
407 " margin-top: 0em;"
408 " margin-bottom: 0em;"
409 "}\n"
410 "li {"
411 " margin-top: 0.25em;"
412 " margin-right: 2em;"
413 "}\n"
414 ".hr {margin-top: 0.25em;"
415 " border-color: black;"
416 " border-bottom-style: solid;"
417 "}\n"
418 ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n"
419 ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n"
420 ".total {background: #20D0D0;color: #ffff80;}\n"
421 ".frontend {background: #e8e8d0;}\n"
422 ".backend {background: #e8e8d0;}\n"
423 ".active0 {background: #ff9090;}\n"
424 ".active1 {background: #ffd020;}\n"
425 ".active2 {background: #ffffa0;}\n"
426 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100427 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
428 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
429 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200430 ".backup0 {background: #ff9090;}\n"
431 ".backup1 {background: #ff80ff;}\n"
432 ".backup2 {background: #c060ff;}\n"
433 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100434 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
435 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
436 ".backup6 {background: #e0e0e0;}\n"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200437 ".rls {letter-spacing: 0.3em;}\n" /* right letter spacing (used for grouping digits) */
Willy Tarreau91861262007-10-17 17:06:05 +0200438 "table.tbl { border-collapse: collapse; border-style: none;}\n"
439 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray;}\n"
440 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
441 "table.tbl th.empty { border-style: none; empty-cells: hide;}\n"
442 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
443 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
444 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
445 "-->\n"
446 "</style></head>\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200447 } else {
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100448 print_csv_header(&msg, sizeof(trash));
Willy Tarreau55bb8452007-10-17 18:44:57 +0200449 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200450 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200451 return 0;
452
453 s->data_state = DATA_ST_INFO;
454 /* fall through */
455
456 case DATA_ST_INFO:
457 up = (now.tv_sec - start_date.tv_sec);
458
459 /* WARNING! this has to fit the first packet too.
460 * We are around 3.5 kB, add adding entries will
461 * become tricky if we want to support 4kB buffers !
462 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100463 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200464 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200465 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
466 PRODUCT_NAME "%s</a></h1>\n"
467 "<h2>Statistics Report for pid %d</h2>\n"
468 "<hr width=\"100%%\" class=\"hr\">\n"
469 "<h3>&gt; General process information</h3>\n"
470 "<table border=0 cols=4><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +0100471 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200472 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
473 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +0100474 "<b>maxsock = </b> %d ; <b>maxconn = </b> %d ; <b>maxpipes = </b> %d<br>\n"
475 "current conns = %d ; current pipes = %d/%d<br>\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100476 "Running tasks : %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +0200477 "</td><td align=\"center\" nowrap>\n"
478 "<table class=\"lgd\"><tr>\n"
479 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
480 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
481 "</tr><tr>\n"
482 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
483 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
484 "</tr><tr>\n"
485 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
486 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
487 "</tr><tr>\n"
488 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100489 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200490 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +0100491 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +0200492 "</td>"
493 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
494 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
495 "",
496 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Willy Tarreaua8efd362008-01-03 10:19:15 +0100497 pid, pid,
498 relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +0200499 up / 86400, (up % 86400) / 3600,
500 (up % 3600) / 60, (up % 60),
501 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
502 global.rlimit_memmax ? " MB" : "",
503 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +0100504 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +0100505 actconn, pipes_used, pipes_used+pipes_free,
506 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +0200507 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100508
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100509 if (s->data_ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200510 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200511 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
512 uri->uri_prefix,
513 "",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100514 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200515 else
516 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200517 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
518 uri->uri_prefix,
519 ";up",
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100520 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200521
Willy Tarreau55bb8452007-10-17 18:44:57 +0200522 if (uri->refresh > 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100523 if (s->data_ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200524 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200525 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
526 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100527 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200528 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200529 else
530 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200531 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
532 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100533 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +0200534 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200535 }
Willy Tarreau91861262007-10-17 17:06:05 +0200536
Willy Tarreau55bb8452007-10-17 18:44:57 +0200537 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200538 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
539 uri->uri_prefix,
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100540 (s->data_ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
541 (s->data_ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +0200542
Willy Tarreau55bb8452007-10-17 18:44:57 +0200543 chunk_printf(&msg, sizeof(trash),
Willy Tarreau5031e6a2007-10-18 11:05:48 +0200544 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
545 uri->uri_prefix,
546 (uri->refresh > 0) ? ";norefresh" : "");
547
548 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200549 "</td>"
550 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
551 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
552 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
553 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
554 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
555 "</ul>"
556 "</td>"
557 "</tr></table>\n"
558 ""
559 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100560
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200561 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200562 return 0;
563 }
Willy Tarreau91861262007-10-17 17:06:05 +0200564
Willy Tarreau91861262007-10-17 17:06:05 +0200565 s->data_ctx.stats.px = proxy;
566 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
567 s->data_state = DATA_ST_LIST;
568 /* fall through */
569
570 case DATA_ST_LIST:
571 /* dump proxies */
572 while (s->data_ctx.stats.px) {
573 px = s->data_ctx.stats.px;
574 /* skip the disabled proxies and non-networked ones */
575 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100576 if (stats_dump_proxy(s, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200577 return 0;
578
579 s->data_ctx.stats.px = px->next;
580 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
581 }
582 /* here, we just have reached the last proxy */
583
584 s->data_state = DATA_ST_END;
585 /* fall through */
586
587 case DATA_ST_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100588 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200589 chunk_printf(&msg, sizeof(trash), "</body></html>\n");
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200590 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200591 return 0;
592 }
Willy Tarreau91861262007-10-17 17:06:05 +0200593
594 s->data_state = DATA_ST_FIN;
595 /* fall through */
596
597 case DATA_ST_FIN:
Willy Tarreau72b179a2008-08-28 16:01:32 +0200598 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200599 return 1;
600
601 default:
602 /* unknown state ! */
Willy Tarreau72b179a2008-08-28 16:01:32 +0200603 buffer_stop_hijack(rep);
Willy Tarreau91861262007-10-17 17:06:05 +0200604 return -1;
605 }
606}
607
608
609/*
610 * Dumps statistics for a proxy.
611 * Returns 0 if it had to stop dumping data because of lack of buffer space,
612 * ot non-zero if everything completed.
613 */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100614int stats_dump_proxy(struct session *s, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +0200615{
616 struct buffer *rep = s->rep;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100617 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Willy Tarreau91861262007-10-17 17:06:05 +0200618 struct chunk msg;
619
620 msg.len = 0;
621 msg.str = trash;
622
623 switch (s->data_ctx.stats.px_st) {
624 case DATA_ST_PX_INIT:
625 /* we are on a new proxy */
626
627 if (uri && uri->scope) {
628 /* we have a limited scope, we have to check the proxy name */
629 struct stat_scope *scope;
630 int len;
631
632 len = strlen(px->id);
633 scope = uri->scope;
634
635 while (scope) {
636 /* match exact proxy name */
637 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
638 break;
639
640 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +0200641 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +0200642 break;
643 scope = scope->next;
644 }
645
646 /* proxy name not found : don't dump anything */
647 if (scope == NULL)
648 return 1;
649 }
650
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100651 if ((s->data_ctx.stats.flags & STAT_BOUND) && (s->data_ctx.stats.iid != -1) &&
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100652 (px->uuid != s->data_ctx.stats.iid))
653 return 1;
654
Willy Tarreau91861262007-10-17 17:06:05 +0200655 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
656 /* fall through */
657
658 case DATA_ST_PX_TH:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100659 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200660 /* print a new table */
661 chunk_printf(&msg, sizeof(trash),
Willy Tarreau7f062c42009-03-05 18:43:00 +0100662 "<table cols=\"27\" class=\"tbl\" width=\"100%%\">\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200663 "<tr align=\"center\" class=\"titre\">"
664 "<th colspan=2 class=\"pxname\">%s</th>"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100665 "<th colspan=25 class=\"empty\"></th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200666 "</tr>\n"
667 "<tr align=\"center\" class=\"titre\">"
668 "<th rowspan=2></th>"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100669 "<th colspan=3>Queue</th><th colspan=6>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200670 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200671 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100672 "<th colspan=8>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200673 "</tr>\n"
674 "<tr align=\"center\" class=\"titre\">"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200675 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100676 "<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 +0200677 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200678 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200679 "<th>Status</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100680 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
681 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200682 "</tr>",
Willy Tarreau55bb8452007-10-17 18:44:57 +0200683 px->id);
684
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200685 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200686 return 0;
687 }
Willy Tarreau91861262007-10-17 17:06:05 +0200688
689 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
690 /* fall through */
691
692 case DATA_ST_PX_FE:
693 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100694 if ((px->cap & PR_CAP_FE) &&
695 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
696 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200697 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200698 /* name, queue */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200699 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=3></td>"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100700 /* sessions : current, max, limit, rate, total, lbtot */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200701 "<td align=right>%s</td><td align=right>%s</td>"
702 "<td align=right>%s</td><td align=right>%s</td>"
703 "<td align=right>%s</td><td align=right></td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200704 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200705 "<td align=right>%s</td><td align=right>%s</td>"
706 "",
707 U2H0(px->feconn), U2H1(px->feconn_max), U2H2(px->maxconn),
708 U2H3(read_freq_ctr(&px->fe_sess_per_sec)), U2H4(px->cum_feconn),
709 U2H5(px->bytes_in), U2H6(px->bytes_out));
710
711 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200712 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200713 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200714 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200715 "<td align=right>%s</td><td align=right></td><td align=right></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200716 /* warnings: retries, redispatches */
717 "<td align=right></td><td align=right></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200718 /* server status : reflect frontend status */
Willy Tarreau91861262007-10-17 17:06:05 +0200719 "<td align=center>%s</td>"
720 /* rest of server: nothing */
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100721 "<td align=center colspan=7></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +0200722 "",
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200723 U2H0(px->denied_req), U2H1(px->denied_resp),
724 U2H2(px->failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +0200725 px->state == PR_STRUN ? "OPEN" :
726 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200727 } else {
728 chunk_printf(&msg, sizeof(trash),
729 /* pxid, name, queue cur, queue max, */
730 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100731 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200732 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200733 /* bytes : in, out */
734 "%lld,%lld,"
735 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200736 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200737 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200738 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200739 /* warnings: retries, redispatches */
740 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200741 /* server status : reflect frontend status */
742 "%s,"
743 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200744 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100745 /* pid, iid, sid, throttle, lbtot, tracked, type */
746 "%d,%d,0,,,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200747 /* rate, rate_lim, rate_max, */
748 "%u,%u,%u,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200749 "\n",
750 px->id,
751 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
752 px->bytes_in, px->bytes_out,
753 px->denied_req, px->denied_resp,
754 px->failed_req,
755 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +0100756 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +0100757 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200758 read_freq_ctr(&px->fe_sess_per_sec),
759 px->fe_sps_lim, px->fe_sps_max);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200760 }
Willy Tarreau91861262007-10-17 17:06:05 +0200761
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200762 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200763 return 0;
764 }
765
766 s->data_ctx.stats.sv = px->srv; /* may be NULL */
767 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
768 /* fall through */
769
770 case DATA_ST_PX_SV:
771 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100772 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
773
Willy Tarreau2ea81932007-11-30 12:04:38 +0100774 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 +0200775
776 sv = s->data_ctx.stats.sv;
777
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100778 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100779 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
780 break;
781
782 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
783 continue;
784 }
785
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100786 if (sv->tracked)
787 svs = sv->tracked;
788 else
789 svs = sv;
790
Willy Tarreau91861262007-10-17 17:06:05 +0200791 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100792 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +0100793 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100794 else if (svs->state & SRV_RUNNING) {
795 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +0200796 sv_state = 3; /* UP */
797 else
798 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +0100799
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100800 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +0100801 sv_state += 2;
802 }
Willy Tarreau91861262007-10-17 17:06:05 +0200803 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100804 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +0200805 sv_state = 1; /* going up */
806 else
807 sv_state = 0; /* DOWN */
808
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100809 if ((sv_state == 0) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +0200810 /* do not report servers which are DOWN */
811 s->data_ctx.stats.sv = sv->next;
812 continue;
813 }
814
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100815 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +0100816 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
817 "UP %d/%d &darr;", "UP",
818 "NOLB %d/%d &darr;", "NOLB",
819 "<i>no check</i>" };
Willy Tarreau55bb8452007-10-17 18:44:57 +0200820 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200821 /* name */
822 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200823 /* queue : current, max, limit */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200824 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau7f062c42009-03-05 18:43:00 +0100825 /* sessions : current, max, limit, rate, total, lbtot */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200826 "<td align=right>%s</td><td align=right>%s</td>"
827 "<td align=right>%s</td><td align=right>%s</td>"
828 "<td align=right>%s</td><td align=right>%s</td>"
829 "",
830 (sv->state & SRV_BACKUP) ? "backup" : "active",
831 sv_state, sv->id,
832 U2H0(sv->nbpend), U2H1(sv->nbpend_max), LIM2A2(sv->maxqueue, "-"),
833 U2H3(sv->cur_sess), U2H4(sv->cur_sess_max), LIM2A5(sv->maxconn, "-"),
834 U2H6(read_freq_ctr(&sv->sess_per_sec)),
835 U2H7(sv->cum_sess), U2H8(sv->cum_lbconn));
836
837 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200838 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200839 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200840 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200841 "<td align=right></td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200842 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200843 "<td align=right></td><td align=right>%s</td><td align=right>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200844 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200845 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200846 "",
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200847 U2H0(sv->bytes_in), U2H1(sv->bytes_out),
848 U2H2(sv->failed_secu),
849 U2H3(sv->failed_conns), U2H4(sv->failed_resp),
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100850 sv->retries, sv->redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100851
Willy Tarreau55bb8452007-10-17 18:44:57 +0200852 /* status */
853 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200854
855 if (sv->state & SRV_CHECKED)
856 chunk_printf(&msg, sizeof(trash), "%s ",
857 human_time(now.tv_sec - sv->last_change, 1));
858
Willy Tarreau55bb8452007-10-17 18:44:57 +0200859 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200860 srv_hlt_st[sv_state],
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100861 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
862 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Willy Tarreau91861262007-10-17 17:06:05 +0200863
Willy Tarreau55bb8452007-10-17 18:44:57 +0200864 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200865 /* weight */
866 "</td><td>%d</td>"
867 /* act, bck */
868 "<td>%s</td><td>%s</td>"
869 "",
Willy Tarreau5542af62007-12-03 02:04:00 +0100870 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +0200871 (sv->state & SRV_BACKUP) ? "-" : "Y",
872 (sv->state & SRV_BACKUP) ? "Y" : "-");
873
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200874 /* check failures: unique, fatal, down time */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200875 if (sv->state & SRV_CHECKED)
876 chunk_printf(&msg, sizeof(trash),
Willy Tarreau3b88d442009-04-11 20:44:08 +0200877 "<td align=right>%lld</td><td align=right>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200878 "<td nowrap align=right>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100879 "",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100880 svs->failed_checks, svs->down_trans,
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200881 human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100882 else if (sv != svs)
883 chunk_printf(&msg, sizeof(trash),
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200884 "<td nowrap colspan=3>via %s/%s</td>", svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200885 else
886 chunk_printf(&msg, sizeof(trash),
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100887 "<td colspan=3></td>");
888
889 /* throttle */
890 if ((sv->state & SRV_WARMINGUP) &&
891 now.tv_sec < sv->last_change + sv->slowstart &&
892 now.tv_sec >= sv->last_change) {
893 unsigned int ratio;
894 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
895 chunk_printf(&msg, sizeof(trash),
896 "<td>%d %%</td></tr>\n", ratio);
897 } else {
898 chunk_printf(&msg, sizeof(trash),
899 "<td>-</td></tr>\n");
900 }
Willy Tarreau55bb8452007-10-17 18:44:57 +0200901 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +0100902 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
903 "UP %d/%d,", "UP,",
904 "NOLB %d/%d,", "NOLB,",
905 "no check," };
Willy Tarreau55bb8452007-10-17 18:44:57 +0200906 chunk_printf(&msg, sizeof(trash),
907 /* pxid, name */
908 "%s,%s,"
909 /* queue : current, max */
910 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100911 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200912 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200913 /* bytes : in, out */
914 "%lld,%lld,"
915 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200916 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200917 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200918 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200919 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200920 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200921 "",
922 px->id, sv->id,
923 sv->nbpend, sv->nbpend_max,
Willy Tarreaudcd47712007-11-04 23:35:08 +0100924 sv->cur_sess, sv->cur_sess_max, LIM2A0(sv->maxconn, ""), sv->cum_sess,
Willy Tarreau55bb8452007-10-17 18:44:57 +0200925 sv->bytes_in, sv->bytes_out,
926 sv->failed_secu,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200927 sv->failed_conns, sv->failed_resp,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100928 sv->retries, sv->redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100929
Willy Tarreau55bb8452007-10-17 18:44:57 +0200930 /* status */
931 chunk_printf(&msg, sizeof(trash),
932 srv_hlt_st[sv_state],
933 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
934 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
Willy Tarreau91861262007-10-17 17:06:05 +0200935
Willy Tarreau55bb8452007-10-17 18:44:57 +0200936 chunk_printf(&msg, sizeof(trash),
937 /* weight, active, backup */
938 "%d,%d,%d,"
939 "",
Willy Tarreau5542af62007-12-03 02:04:00 +0100940 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +0200941 (sv->state & SRV_BACKUP) ? 0 : 1,
942 (sv->state & SRV_BACKUP) ? 1 : 0);
943
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200944 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200945 if (sv->state & SRV_CHECKED)
946 chunk_printf(&msg, sizeof(trash),
Willy Tarreau3b88d442009-04-11 20:44:08 +0200947 "%lld,%lld,%d,%d,",
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200948 sv->failed_checks, sv->down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +0200949 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +0200950 else
951 chunk_printf(&msg, sizeof(trash),
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200952 ",,,,");
953
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100954 /* queue limit, pid, iid, sid, */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200955 chunk_printf(&msg, sizeof(trash),
Willy Tarreaudcd47712007-11-04 23:35:08 +0100956 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100957 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +0100958 LIM2A0(sv->maxqueue, ""),
959 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100960
961 /* throttle */
962 if ((sv->state & SRV_WARMINGUP) &&
963 now.tv_sec < sv->last_change + sv->slowstart &&
964 now.tv_sec >= sv->last_change) {
965 unsigned int ratio;
966 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100967 chunk_printf(&msg, sizeof(trash), "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100968 }
969
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100970 /* sessions: lbtot */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200971 chunk_printf(&msg, sizeof(trash), ",%lld,", sv->cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100972
973 /* tracked */
974 if (sv->tracked)
Krzysztof Piotr Oledzkif58a9622008-02-23 01:19:10 +0100975 chunk_printf(&msg, sizeof(trash), "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100976 sv->tracked->proxy->id, sv->tracked->id);
977 else
978 chunk_printf(&msg, sizeof(trash), ",");
979
Willy Tarreau7f062c42009-03-05 18:43:00 +0100980 /* type */
981 chunk_printf(&msg, sizeof(trash), "%d,", STATS_TYPE_SV);
982
983 /* rate */
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200984 chunk_printf(&msg, sizeof(trash), "%u,,%u,",
985 read_freq_ctr(&sv->sess_per_sec),
986 sv->sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +0100987
988 /* finish with EOL */
989 chunk_printf(&msg, sizeof(trash), "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200990 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200991 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200992 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100993 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +0200994
995 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
996 /* fall through */
997
998 case DATA_ST_PX_BE:
999 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001000 if ((px->cap & PR_CAP_BE) &&
1001 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
1002 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001003 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +02001004 /* name */
1005 "<tr align=center class=\"backend\"><td>Backend</td>"
1006 /* queue : current, max */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001007 "<td align=right>%s</td><td align=right>%s</td><td></td>"
Willy Tarreau7f062c42009-03-05 18:43:00 +01001008 /* sessions : current, max, limit, rate, total, lbtot */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001009 "<td align=right>%s</td><td align=right>%s</td>"
1010 "<td align=right>%s</td><td align=right>%s</td>"
1011 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001012 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001013 "<td align=right>%s</td><td align=right>%s</td>"
1014 "",
1015 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->nbpend_max),
1016 U2H2(px->beconn), U2H3(px->beconn_max), U2H4(px->fullconn),
1017 U2H5(read_freq_ctr(&px->be_sess_per_sec)),
1018 U2H6(px->cum_beconn), U2H7(px->cum_lbconn),
1019 U2H8(px->bytes_in), U2H9(px->bytes_out));
1020
1021 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +02001022 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001023 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001024 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001025 "<td align=right></td><td align=right>%s</td><td align=right>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001026 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001027 "<td align=right>%lld</td><td align=right>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001028 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02001029 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001030 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02001031 * active and backups. */
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001032 "<td align=center nowrap>%s %s</td><td align=center>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001033 "<td align=center>%d</td><td align=center>%d</td>"
1034 "",
1035 U2H0(px->denied_req), U2H1(px->denied_resp),
1036 U2H2(px->failed_conns), U2H3(px->failed_resp),
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001037 px->retries, px->redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001038 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01001039 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
1040 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01001041 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001042 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001043
1044 chunk_printf(&msg, sizeof(trash),
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001045 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001046 "<td align=center>&nbsp;</td><td align=\"right\">%d</td>"
1047 "<td align=\"right\" nowrap>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001048 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001049 "</tr>",
1050 px->down_trans,
1051 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001052 } else {
1053 chunk_printf(&msg, sizeof(trash),
1054 /* pxid, name */
1055 "%s,BACKEND,"
1056 /* queue : current, max */
1057 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001058 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001059 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001060 /* bytes : in, out */
1061 "%lld,%lld,"
1062 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001063 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001064 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001065 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001066 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001067 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001068 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02001069 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001070 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02001071 * active and backups. */
1072 "%s,"
1073 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001074 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001075 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001076 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001077 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001078 /* rate, rate_lim, rate_max, */
1079 "%u,,%u,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001080 "\n",
1081 px->id,
1082 px->nbpend /* or px->totpend ? */, px->nbpend_max,
1083 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
1084 px->bytes_in, px->bytes_out,
1085 px->denied_req, px->denied_resp,
1086 px->failed_conns, px->failed_resp,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001087 px->retries, px->redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01001088 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01001089 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001090 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001091 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01001092 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001093 relative_pid, px->uuid,
Willy Tarreau7f062c42009-03-05 18:43:00 +01001094 px->cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001095 read_freq_ctr(&px->be_sess_per_sec),
1096 px->be_sps_max);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001097 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001098 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001099 return 0;
1100 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001101
Willy Tarreau91861262007-10-17 17:06:05 +02001102 s->data_ctx.stats.px_st = DATA_ST_PX_END;
1103 /* fall through */
1104
1105 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001106 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001107 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001108
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001109 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001110 return 0;
1111 }
Willy Tarreau91861262007-10-17 17:06:05 +02001112
1113 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
1114 /* fall through */
1115
1116 case DATA_ST_PX_FIN:
1117 return 1;
1118
1119 default:
1120 /* unknown state, we should put an abort() here ! */
1121 return 1;
1122 }
1123}
1124
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001125
1126/* This function is called to send output to the response buffer.
1127 * It dumps the sessions states onto the output buffer <rep>.
1128 * Expects to be called with client socket shut down on input.
1129 * s->data_ctx must have been zeroed first, and the flags properly set.
1130 * It automatically clears the HIJACK bit from the response buffer.
1131 */
1132void stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
1133{
1134 struct chunk msg;
1135
1136 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
1137 /* If we're forced to shut down, we might have to remove our
1138 * reference to the last session being dumped.
1139 */
1140 if (s->data_state == DATA_ST_LIST) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001141 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001142 LIST_DEL(&s->data_ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001143 LIST_INIT(&s->data_ctx.sess.bref.users);
1144 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001145 }
1146 s->data_state = DATA_ST_FIN;
1147 buffer_stop_hijack(rep);
1148 s->ana_state = STATS_ST_CLOSE;
1149 return;
1150 }
1151
1152 if (s->ana_state != STATS_ST_REP)
1153 return;
1154
1155 msg.len = 0;
1156 msg.str = trash;
1157
1158 switch (s->data_state) {
1159 case DATA_ST_INIT:
1160 /* the function had not been called yet, let's prepare the
1161 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001162 * pointer to the first in the global list. When a target
1163 * session is being destroyed, it is responsible for updating
1164 * this pointer. We know we have reached the end when this
1165 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001166 */
1167 stream_int_retnclose(rep->cons, &msg);
1168 LIST_INIT(&s->data_ctx.sess.bref.users);
1169 s->data_ctx.sess.bref.ref = sessions.n;
1170 s->data_state = DATA_ST_LIST;
1171 /* fall through */
1172
1173 case DATA_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001174 /* first, let's detach the back-ref from a possible previous session */
1175 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
1176 LIST_DEL(&s->data_ctx.sess.bref.users);
1177 LIST_INIT(&s->data_ctx.sess.bref.users);
1178 }
1179
1180 /* and start from where we stopped */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001181 while (s->data_ctx.sess.bref.ref != &sessions) {
1182 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1183 struct session *curr_sess;
1184
1185 curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list);
1186
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001187 chunk_printf(&msg, sizeof(trash),
1188 "%p: proto=%s",
1189 curr_sess,
1190 curr_sess->listener->proto->name);
1191
1192 switch (curr_sess->listener->proto->sock_family) {
1193 case AF_INET:
1194 inet_ntop(AF_INET,
1195 (const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr,
1196 pn, sizeof(pn));
1197
1198 chunk_printf(&msg, sizeof(trash),
1199 " src=%s:%d fe=%s be=%s srv=%s",
1200 pn,
1201 ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port),
1202 curr_sess->fe->id,
1203 curr_sess->be->id,
1204 curr_sess->srv ? curr_sess->srv->id : "<none>"
1205 );
1206 break;
1207 case AF_INET6:
1208 inet_ntop(AF_INET6,
1209 (const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr,
1210 pn, sizeof(pn));
1211
1212 chunk_printf(&msg, sizeof(trash),
1213 " src=%s:%d fe=%s be=%s srv=%s",
1214 pn,
1215 ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port),
1216 curr_sess->fe->id,
1217 curr_sess->be->id,
1218 curr_sess->srv ? curr_sess->srv->id : "<none>"
1219 );
1220
1221 break;
1222 case AF_UNIX:
1223 /* no more information to print right now */
1224 break;
1225 }
1226
1227 chunk_printf(&msg, sizeof(trash),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001228 " as=%d ts=%02x age=%s calls=%d",
Willy Tarreau8a5c6262008-12-08 00:16:21 +01001229 curr_sess->ana_state, curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01001230 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
1231 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001232
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001233 chunk_printf(&msg, sizeof(trash),
1234 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
1235 curr_sess->req->flags,
1236 curr_sess->req->l,
1237 curr_sess->req->analysers,
1238 curr_sess->req->rex ?
1239 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
1240 TICKS_TO_MS(1000)) : "");
1241
1242 chunk_printf(&msg, sizeof(trash),
1243 ",wx=%s",
1244 curr_sess->req->wex ?
1245 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
1246 TICKS_TO_MS(1000)) : "");
1247
1248 chunk_printf(&msg, sizeof(trash),
1249 ",ax=%s]",
1250 curr_sess->req->analyse_exp ?
1251 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
1252 TICKS_TO_MS(1000)) : "");
1253
1254 chunk_printf(&msg, sizeof(trash),
1255 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
1256 curr_sess->rep->flags,
1257 curr_sess->rep->l,
1258 curr_sess->rep->analysers,
1259 curr_sess->rep->rex ?
1260 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
1261 TICKS_TO_MS(1000)) : "");
1262
1263 chunk_printf(&msg, sizeof(trash),
1264 ",wx=%s",
1265 curr_sess->rep->wex ?
1266 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
1267 TICKS_TO_MS(1000)) : "");
1268
1269 chunk_printf(&msg, sizeof(trash),
1270 ",ax=%s]",
1271 curr_sess->rep->analyse_exp ?
1272 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
1273 TICKS_TO_MS(1000)) : "");
1274
1275 chunk_printf(&msg, sizeof(trash),
1276 " s0=[%d,%1xh,fd=%d,ex=%s]",
1277 curr_sess->si[0].state,
1278 curr_sess->si[0].flags,
1279 curr_sess->si[0].fd,
1280 curr_sess->si[0].exp ?
1281 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
1282 TICKS_TO_MS(1000)) : "");
1283
1284 chunk_printf(&msg, sizeof(trash),
1285 " s1=[%d,%1xh,fd=%d,ex=%s]",
1286 curr_sess->si[1].state,
1287 curr_sess->si[1].flags,
1288 curr_sess->si[1].fd,
1289 curr_sess->si[1].exp ?
1290 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
1291 TICKS_TO_MS(1000)) : "");
1292
1293 chunk_printf(&msg, sizeof(trash),
1294 " exp=%s",
1295 curr_sess->task->expire ?
1296 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
1297 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01001298 if (task_in_rq(curr_sess->task))
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001299 chunk_printf(&msg, sizeof(trash), " run(nice=%d)", curr_sess->task->nice);
1300
1301 chunk_printf(&msg, sizeof(trash), "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001302
1303 if (buffer_write_chunk(rep, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001304 /* let's try again later from this session. We add ourselves into
1305 * this session's users so that it can remove us upon termination.
1306 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001307 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
1308 return;
1309 }
1310
1311 s->data_ctx.sess.bref.ref = curr_sess->list.n;
1312 }
1313 s->data_state = DATA_ST_FIN;
1314 /* fall through */
1315
1316 default:
1317 s->data_state = DATA_ST_FIN;
1318 buffer_stop_hijack(rep);
1319 s->ana_state = STATS_ST_CLOSE;
1320 return;
1321 }
1322}
1323
Willy Tarreau74808cb2009-03-04 15:53:18 +01001324/* print a line of error buffer (limited to 70 bytes) to <out>. The format is :
1325 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
1326 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
1327 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
1328 * lines are respected within the limit of 70 output chars. Lines that are
1329 * continuation of a previous truncated line begin with "+" instead of " "
1330 * after the offset. The new pointer is returned.
1331 */
1332static int dump_error_line(struct chunk *out, int size,
1333 struct error_snapshot *err, int *line, int ptr)
1334{
1335 int end;
1336 unsigned char c;
1337
1338 end = out->len + 80;
1339 if (end > size)
1340 return ptr;
1341
1342 chunk_printf(out, size, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
1343
1344 while (ptr < err->len) {
1345 c = err->buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01001346 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001347 if (out->len > end - 2)
1348 break;
1349 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01001350 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001351 if (out->len > end - 3)
1352 break;
1353 out->str[out->len++] = '\\';
1354 switch (c) {
1355 case '\t': c = 't'; break;
1356 case '\n': c = 'n'; break;
1357 case '\r': c = 'r'; break;
1358 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01001359 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001360 }
1361 out->str[out->len++] = c;
1362 } else {
1363 if (out->len > end - 5)
1364 break;
1365 out->str[out->len++] = '\\';
1366 out->str[out->len++] = 'x';
1367 out->str[out->len++] = hextab[(c >> 4) & 0xF];
1368 out->str[out->len++] = hextab[c & 0xF];
1369 }
1370 if (err->buf[ptr++] == '\n') {
1371 /* we had a line break, let's return now */
1372 out->str[out->len++] = '\n';
1373 *line = ptr;
1374 return ptr;
1375 }
1376 }
1377 /* we have an incomplete line, we return it as-is */
1378 out->str[out->len++] = '\n';
1379 return ptr;
1380}
1381
1382/* This function is called to send output to the response buffer.
1383 * It dumps the errors logged in proxies onto the output buffer <rep>.
1384 * Expects to be called with client socket shut down on input.
1385 * s->data_ctx must have been zeroed first, and the flags properly set.
1386 * It automatically clears the HIJACK bit from the response buffer.
1387 */
1388void stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
1389{
1390 extern const char *monthname[12];
1391 struct chunk msg;
1392
1393 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
1394 s->data_state = DATA_ST_FIN;
1395 buffer_stop_hijack(rep);
1396 s->ana_state = STATS_ST_CLOSE;
1397 return;
1398 }
1399
1400 if (s->ana_state != STATS_ST_REP)
1401 return;
1402
1403 msg.len = 0;
1404 msg.str = trash;
1405
1406 if (!s->data_ctx.errors.px) {
1407 /* the function had not been called yet, let's prepare the
1408 * buffer for a response.
1409 */
1410 stream_int_retnclose(rep->cons, &msg);
1411 s->data_ctx.errors.px = proxy;
1412 s->data_ctx.errors.buf = 0;
1413 s->data_ctx.errors.bol = 0;
1414 s->data_ctx.errors.ptr = -1;
1415 }
1416
1417 /* we have two inner loops here, one for the proxy, the other one for
1418 * the buffer.
1419 */
1420 while (s->data_ctx.errors.px) {
1421 struct error_snapshot *es;
1422
1423 if (s->data_ctx.errors.buf == 0)
1424 es = &s->data_ctx.errors.px->invalid_req;
1425 else
1426 es = &s->data_ctx.errors.px->invalid_rep;
1427
1428 if (!es->when.tv_sec)
1429 goto next;
1430
1431 if (s->data_ctx.errors.iid >= 0 &&
1432 s->data_ctx.errors.px->uuid != s->data_ctx.errors.iid &&
1433 es->oe->uuid != s->data_ctx.errors.iid)
1434 goto next;
1435
1436 if (s->data_ctx.errors.ptr < 0) {
1437 /* just print headers now */
1438
1439 char pn[INET6_ADDRSTRLEN];
1440 struct tm tm;
1441
1442 get_localtime(es->when.tv_sec, &tm);
1443 chunk_printf(&msg, sizeof(trash), "\n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
1444 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001445 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01001446
1447
1448 if (es->src.ss_family == AF_INET)
1449 inet_ntop(AF_INET,
1450 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
1451 pn, sizeof(pn));
1452 else
1453 inet_ntop(AF_INET6,
1454 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
1455 pn, sizeof(pn));
1456
1457 switch (s->data_ctx.errors.buf) {
1458 case 0:
1459 chunk_printf(&msg, sizeof(trash),
1460 " frontend %s (#%d): invalid request\n"
1461 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
1462 " request length %d bytes, error at position %d:\n\n",
1463 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
1464 pn, es->sid, es->oe->id, es->oe->uuid,
1465 es->srv ? es->srv->id : "<NONE>",
1466 es->srv ? es->srv->puid : -1,
1467 es->len, es->pos);
1468 break;
1469 case 1:
1470 chunk_printf(&msg, sizeof(trash),
1471 " backend %s (#%d) : invalid response\n"
1472 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
1473 " response length %d bytes, error at position %d:\n\n",
1474 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
1475 pn, es->sid, es->oe->id, es->oe->uuid,
1476 es->srv ? es->srv->id : "<NONE>",
1477 es->srv ? es->srv->puid : -1,
1478 es->len, es->pos);
1479 break;
1480 }
1481
1482 if (buffer_write_chunk(rep, &msg) >= 0) {
1483 /* Socket buffer full. Let's try again later from the same point */
1484 return;
1485 }
1486 s->data_ctx.errors.ptr = 0;
1487 s->data_ctx.errors.sid = es->sid;
1488 }
1489
1490 if (s->data_ctx.errors.sid != es->sid) {
1491 /* the snapshot changed while we were dumping it */
1492 chunk_printf(&msg, sizeof(trash),
1493 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
1494 if (buffer_write_chunk(rep, &msg) >= 0)
1495 return;
1496 goto next;
1497 }
1498
1499 /* OK, ptr >= 0, so we have to dump the current line */
1500 while (s->data_ctx.errors.ptr < es->len) {
1501 int newptr;
1502 int newline;
1503
1504 newline = s->data_ctx.errors.bol;
1505 newptr = dump_error_line(&msg, sizeof(trash), es, &newline, s->data_ctx.errors.ptr);
1506 if (newptr == s->data_ctx.errors.ptr)
1507 return;
1508
1509 if (buffer_write_chunk(rep, &msg) >= 0) {
1510 /* Socket buffer full. Let's try again later from the same point */
1511 return;
1512 }
1513 s->data_ctx.errors.ptr = newptr;
1514 s->data_ctx.errors.bol = newline;
1515 };
1516 next:
1517 s->data_ctx.errors.bol = 0;
1518 s->data_ctx.errors.ptr = -1;
1519 s->data_ctx.errors.buf++;
1520 if (s->data_ctx.errors.buf > 1) {
1521 s->data_ctx.errors.buf = 0;
1522 s->data_ctx.errors.px = s->data_ctx.errors.px->next;
1523 }
1524 }
1525
1526 /* dump complete */
1527 buffer_stop_hijack(rep);
1528 s->ana_state = STATS_ST_CLOSE;
1529}
1530
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001531
Willy Tarreau10522fd2008-07-09 20:12:41 +02001532static struct cfg_kw_list cfg_kws = {{ },{
1533 { CFG_GLOBAL, "stats", stats_parse_global },
1534 { 0, NULL, NULL },
1535}};
1536
1537__attribute__((constructor))
1538static void __dumpstats_module_init(void)
1539{
1540 cfg_register_keywords(&cfg_kws);
1541}
1542
Willy Tarreau91861262007-10-17 17:06:05 +02001543/*
1544 * Local variables:
1545 * c-indent-level: 8
1546 * c-basic-offset: 8
1547 * End:
1548 */