blob: 44a924ad21c11a0ebfe5b5565ed45c26c9e164df [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 {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +0200378 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +0200379 " font-size: 12px;"
380 " font-weight: normal;"
381 " color: black;"
382 " background: white;"
383 "}\n"
384 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +0200385 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +0200386 " 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 Tarreaua3e49422009-05-10 19:19:41 +0200662 "<table cols=\"29\" 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 Tarreaua3e49422009-05-10 19:19:41 +0200665 "<th colspan=27 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 Tarreaua3e49422009-05-10 19:19:41 +0200669 "<th colspan=3>Queue</th>"
670 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200671 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200672 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100673 "<th colspan=8>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200674 "</tr>\n"
675 "<tr align=\"center\" class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +0200676 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200677 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +0200678 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200679 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200680 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200681 "<th>Status</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100682 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
683 "<th>Thrtle</th>\n"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200684 "</tr>",
Willy Tarreau55bb8452007-10-17 18:44:57 +0200685 px->id);
686
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200687 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +0200688 return 0;
689 }
Willy Tarreau91861262007-10-17 17:06:05 +0200690
691 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
692 /* fall through */
693
694 case DATA_ST_PX_FE:
695 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100696 if ((px->cap & PR_CAP_FE) &&
697 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_FE)))) {
698 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +0200699 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200700 /* name, queue */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200701 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=3></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +0200702 /* sessions rate : current, max, limit */
703 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
704 /* sessions : current, max, limit, total, lbtot */
705 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200706 "<td align=right>%s</td><td align=right></td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200707 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200708 "<td align=right>%s</td><td align=right>%s</td>"
709 "",
Willy Tarreaua3e49422009-05-10 19:19:41 +0200710 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
711 U2H1(px->fe_sps_max), LIM2A2(px->fe_sps_lim, "-"),
712 U2H3(px->feconn), U2H4(px->feconn_max), U2H5(px->maxconn),
713 U2H6(px->cum_feconn), U2H7(px->bytes_in), U2H8(px->bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200714
715 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200716 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200717 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200718 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200719 "<td align=right>%s</td><td align=right></td><td align=right></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200720 /* warnings: retries, redispatches */
721 "<td align=right></td><td align=right></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200722 /* server status : reflect frontend status */
Willy Tarreau91861262007-10-17 17:06:05 +0200723 "<td align=center>%s</td>"
724 /* rest of server: nothing */
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100725 "<td align=center colspan=7></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +0200726 "",
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200727 U2H0(px->denied_req), U2H1(px->denied_resp),
728 U2H2(px->failed_req),
Willy Tarreau91861262007-10-17 17:06:05 +0200729 px->state == PR_STRUN ? "OPEN" :
730 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200731 } else {
732 chunk_printf(&msg, sizeof(trash),
733 /* pxid, name, queue cur, queue max, */
734 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100735 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200736 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200737 /* bytes : in, out */
738 "%lld,%lld,"
739 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200740 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200741 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200742 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200743 /* warnings: retries, redispatches */
744 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200745 /* server status : reflect frontend status */
746 "%s,"
747 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200748 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100749 /* pid, iid, sid, throttle, lbtot, tracked, type */
750 "%d,%d,0,,,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200751 /* rate, rate_lim, rate_max, */
752 "%u,%u,%u,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200753 "\n",
754 px->id,
755 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
756 px->bytes_in, px->bytes_out,
757 px->denied_req, px->denied_resp,
758 px->failed_req,
759 px->state == PR_STRUN ? "OPEN" :
Willy Tarreaudcd47712007-11-04 23:35:08 +0100760 px->state == PR_STIDLE ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +0100761 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200762 read_freq_ctr(&px->fe_sess_per_sec),
763 px->fe_sps_lim, px->fe_sps_max);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200764 }
Willy Tarreau91861262007-10-17 17:06:05 +0200765
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200766 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200767 return 0;
768 }
769
770 s->data_ctx.stats.sv = px->srv; /* may be NULL */
771 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
772 /* fall through */
773
774 case DATA_ST_PX_SV:
775 /* stats.sv has been initialized above */
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100776 for (; s->data_ctx.stats.sv != NULL; s->data_ctx.stats.sv = sv->next) {
777
Willy Tarreau2ea81932007-11-30 12:04:38 +0100778 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 +0200779
780 sv = s->data_ctx.stats.sv;
781
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100782 if (s->data_ctx.stats.flags & STAT_BOUND) {
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100783 if (!(s->data_ctx.stats.type & (1 << STATS_TYPE_SV)))
784 break;
785
786 if (s->data_ctx.stats.sid != -1 && sv->puid != s->data_ctx.stats.sid)
787 continue;
788 }
789
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100790 if (sv->tracked)
791 svs = sv->tracked;
792 else
793 svs = sv;
794
Willy Tarreau91861262007-10-17 17:06:05 +0200795 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100796 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +0100797 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100798 else if (svs->state & SRV_RUNNING) {
799 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +0200800 sv_state = 3; /* UP */
801 else
802 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +0100803
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100804 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +0100805 sv_state += 2;
806 }
Willy Tarreau91861262007-10-17 17:06:05 +0200807 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100808 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +0200809 sv_state = 1; /* going up */
810 else
811 sv_state = 0; /* DOWN */
812
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100813 if ((sv_state == 0) && (s->data_ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +0200814 /* do not report servers which are DOWN */
815 s->data_ctx.stats.sv = sv->next;
816 continue;
817 }
818
Willy Tarreau39f7e6d2008-03-17 21:38:24 +0100819 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +0100820 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
821 "UP %d/%d &darr;", "UP",
822 "NOLB %d/%d &darr;", "NOLB",
823 "<i>no check</i>" };
Willy Tarreau55bb8452007-10-17 18:44:57 +0200824 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200825 /* name */
826 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200827 /* queue : current, max, limit */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200828 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +0200829 /* sessions rate : current, max, limit */
830 "<td align=right>%s</td><td align=right>%s</td><td align=right></td>"
831 /* sessions : current, max, limit, total, lbtot */
832 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200833 "<td align=right>%s</td><td align=right>%s</td>"
834 "",
835 (sv->state & SRV_BACKUP) ? "backup" : "active",
836 sv_state, sv->id,
837 U2H0(sv->nbpend), U2H1(sv->nbpend_max), LIM2A2(sv->maxqueue, "-"),
Willy Tarreaua3e49422009-05-10 19:19:41 +0200838 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->sps_max),
839 U2H5(sv->cur_sess), U2H6(sv->cur_sess_max), LIM2A7(sv->maxconn, "-"),
840 U2H8(sv->cum_sess), U2H9(sv->cum_lbconn));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200841
842 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200843 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200844 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200845 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200846 "<td align=right></td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200847 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200848 "<td align=right></td><td align=right>%s</td><td align=right>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200849 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200850 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +0200851 "",
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200852 U2H0(sv->bytes_in), U2H1(sv->bytes_out),
853 U2H2(sv->failed_secu),
854 U2H3(sv->failed_conns), U2H4(sv->failed_resp),
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100855 sv->retries, sv->redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100856
Willy Tarreau55bb8452007-10-17 18:44:57 +0200857 /* status */
858 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200859
860 if (sv->state & SRV_CHECKED)
861 chunk_printf(&msg, sizeof(trash), "%s ",
862 human_time(now.tv_sec - sv->last_change, 1));
863
Willy Tarreau55bb8452007-10-17 18:44:57 +0200864 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200865 srv_hlt_st[sv_state],
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100866 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
867 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Willy Tarreau91861262007-10-17 17:06:05 +0200868
Willy Tarreau55bb8452007-10-17 18:44:57 +0200869 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +0200870 /* weight */
871 "</td><td>%d</td>"
872 /* act, bck */
873 "<td>%s</td><td>%s</td>"
874 "",
Willy Tarreau5542af62007-12-03 02:04:00 +0100875 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +0200876 (sv->state & SRV_BACKUP) ? "-" : "Y",
877 (sv->state & SRV_BACKUP) ? "Y" : "-");
878
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200879 /* check failures: unique, fatal, down time */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200880 if (sv->state & SRV_CHECKED)
881 chunk_printf(&msg, sizeof(trash),
Willy Tarreau3b88d442009-04-11 20:44:08 +0200882 "<td align=right>%lld</td><td align=right>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200883 "<td nowrap align=right>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100884 "",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100885 svs->failed_checks, svs->down_trans,
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200886 human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100887 else if (sv != svs)
888 chunk_printf(&msg, sizeof(trash),
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +0200889 "<td nowrap colspan=3>via %s/%s</td>", svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +0200890 else
891 chunk_printf(&msg, sizeof(trash),
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100892 "<td colspan=3></td>");
893
894 /* throttle */
895 if ((sv->state & SRV_WARMINGUP) &&
896 now.tv_sec < sv->last_change + sv->slowstart &&
897 now.tv_sec >= sv->last_change) {
898 unsigned int ratio;
899 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
900 chunk_printf(&msg, sizeof(trash),
901 "<td>%d %%</td></tr>\n", ratio);
902 } else {
903 chunk_printf(&msg, sizeof(trash),
904 "<td>-</td></tr>\n");
905 }
Willy Tarreau55bb8452007-10-17 18:44:57 +0200906 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +0100907 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
908 "UP %d/%d,", "UP,",
909 "NOLB %d/%d,", "NOLB,",
910 "no check," };
Willy Tarreau55bb8452007-10-17 18:44:57 +0200911 chunk_printf(&msg, sizeof(trash),
912 /* pxid, name */
913 "%s,%s,"
914 /* queue : current, max */
915 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100916 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200917 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200918 /* bytes : in, out */
919 "%lld,%lld,"
920 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200921 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200922 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200923 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200924 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200925 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +0200926 "",
927 px->id, sv->id,
928 sv->nbpend, sv->nbpend_max,
Willy Tarreaudcd47712007-11-04 23:35:08 +0100929 sv->cur_sess, sv->cur_sess_max, LIM2A0(sv->maxconn, ""), sv->cum_sess,
Willy Tarreau55bb8452007-10-17 18:44:57 +0200930 sv->bytes_in, sv->bytes_out,
931 sv->failed_secu,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +0200932 sv->failed_conns, sv->failed_resp,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100933 sv->retries, sv->redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100934
Willy Tarreau55bb8452007-10-17 18:44:57 +0200935 /* status */
936 chunk_printf(&msg, sizeof(trash),
937 srv_hlt_st[sv_state],
938 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
939 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
Willy Tarreau91861262007-10-17 17:06:05 +0200940
Willy Tarreau55bb8452007-10-17 18:44:57 +0200941 chunk_printf(&msg, sizeof(trash),
942 /* weight, active, backup */
943 "%d,%d,%d,"
944 "",
Willy Tarreau5542af62007-12-03 02:04:00 +0100945 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +0200946 (sv->state & SRV_BACKUP) ? 0 : 1,
947 (sv->state & SRV_BACKUP) ? 1 : 0);
948
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200949 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +0200950 if (sv->state & SRV_CHECKED)
951 chunk_printf(&msg, sizeof(trash),
Willy Tarreau3b88d442009-04-11 20:44:08 +0200952 "%lld,%lld,%d,%d,",
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200953 sv->failed_checks, sv->down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +0200954 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +0200955 else
956 chunk_printf(&msg, sizeof(trash),
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200957 ",,,,");
958
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100959 /* queue limit, pid, iid, sid, */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +0200960 chunk_printf(&msg, sizeof(trash),
Willy Tarreaudcd47712007-11-04 23:35:08 +0100961 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100962 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +0100963 LIM2A0(sv->maxqueue, ""),
964 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100965
966 /* throttle */
967 if ((sv->state & SRV_WARMINGUP) &&
968 now.tv_sec < sv->last_change + sv->slowstart &&
969 now.tv_sec >= sv->last_change) {
970 unsigned int ratio;
971 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100972 chunk_printf(&msg, sizeof(trash), "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100973 }
974
Willy Tarreauddbb82f2007-12-05 10:34:49 +0100975 /* sessions: lbtot */
Willy Tarreau3b88d442009-04-11 20:44:08 +0200976 chunk_printf(&msg, sizeof(trash), ",%lld,", sv->cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100977
978 /* tracked */
979 if (sv->tracked)
Krzysztof Piotr Oledzkif58a9622008-02-23 01:19:10 +0100980 chunk_printf(&msg, sizeof(trash), "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100981 sv->tracked->proxy->id, sv->tracked->id);
982 else
983 chunk_printf(&msg, sizeof(trash), ",");
984
Willy Tarreau7f062c42009-03-05 18:43:00 +0100985 /* type */
986 chunk_printf(&msg, sizeof(trash), "%d,", STATS_TYPE_SV);
987
988 /* rate */
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200989 chunk_printf(&msg, sizeof(trash), "%u,,%u,",
990 read_freq_ctr(&sv->sess_per_sec),
991 sv->sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +0100992
993 /* finish with EOL */
994 chunk_printf(&msg, sizeof(trash), "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +0200995 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200996 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +0200997 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +0100998 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +0200999
1000 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
1001 /* fall through */
1002
1003 case DATA_ST_PX_BE:
1004 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001005 if ((px->cap & PR_CAP_BE) &&
1006 (!(s->data_ctx.stats.flags & STAT_BOUND) || (s->data_ctx.stats.type & (1 << STATS_TYPE_BE)))) {
1007 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001008 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +02001009 /* name */
1010 "<tr align=center class=\"backend\"><td>Backend</td>"
1011 /* queue : current, max */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001012 "<td align=right>%s</td><td align=right>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02001013 /* sessions rate : current, max, limit */
1014 "<td align=right>%s</td><td align=right>%s</td><td align=right></td>"
1015 "",
1016 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->nbpend_max),
1017 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_sps_max));
1018
1019 chunk_printf(&msg, sizeof(trash),
1020 /* sessions : current, max, limit, total, lbtot */
1021 "<td align=right>%s</td><td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001022 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001023 /* bytes : in, out */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001024 "<td align=right>%s</td><td align=right>%s</td>"
1025 "",
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001026 U2H2(px->beconn), U2H3(px->beconn_max), U2H4(px->fullconn),
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001027 U2H6(px->cum_beconn), U2H7(px->cum_lbconn),
1028 U2H8(px->bytes_in), U2H9(px->bytes_out));
1029
1030 chunk_printf(&msg, sizeof(trash),
Willy Tarreau91861262007-10-17 17:06:05 +02001031 /* denied: req, resp */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001032 "<td align=right>%s</td><td align=right>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001033 /* errors : request, connect, response */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001034 "<td align=right></td><td align=right>%s</td><td align=right>%s</td>\n"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001035 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001036 "<td align=right>%lld</td><td align=right>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001037 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02001038 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001039 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02001040 * active and backups. */
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001041 "<td align=center nowrap>%s %s</td><td align=center>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02001042 "<td align=center>%d</td><td align=center>%d</td>"
1043 "",
1044 U2H0(px->denied_req), U2H1(px->denied_resp),
1045 U2H2(px->failed_conns), U2H3(px->failed_resp),
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001046 px->retries, px->redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001047 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01001048 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
1049 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01001050 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001051 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001052
1053 chunk_printf(&msg, sizeof(trash),
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001054 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001055 "<td align=center>&nbsp;</td><td align=\"right\">%d</td>"
1056 "<td align=\"right\" nowrap>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001057 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001058 "</tr>",
1059 px->down_trans,
1060 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001061 } else {
1062 chunk_printf(&msg, sizeof(trash),
1063 /* pxid, name */
1064 "%s,BACKEND,"
1065 /* queue : current, max */
1066 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001067 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001068 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001069 /* bytes : in, out */
1070 "%lld,%lld,"
1071 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001072 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001073 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001074 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001075 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001076 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001077 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02001078 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02001079 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02001080 * active and backups. */
1081 "%s,"
1082 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01001083 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02001084 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01001085 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02001086 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001087 /* rate, rate_lim, rate_max, */
1088 "%u,,%u,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001089 "\n",
1090 px->id,
1091 px->nbpend /* or px->totpend ? */, px->nbpend_max,
1092 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
1093 px->bytes_in, px->bytes_out,
1094 px->denied_req, px->denied_resp,
1095 px->failed_conns, px->failed_resp,
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02001096 px->retries, px->redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01001097 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01001098 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01001099 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001100 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01001101 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01001102 relative_pid, px->uuid,
Willy Tarreau7f062c42009-03-05 18:43:00 +01001103 px->cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02001104 read_freq_ctr(&px->be_sess_per_sec),
1105 px->be_sps_max);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001106 }
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001107 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001108 return 0;
1109 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001110
Willy Tarreau91861262007-10-17 17:06:05 +02001111 s->data_ctx.stats.px_st = DATA_ST_PX_END;
1112 /* fall through */
1113
1114 case DATA_ST_PX_END:
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01001115 if (!(s->data_ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02001116 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001117
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +02001118 if (buffer_write_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02001119 return 0;
1120 }
Willy Tarreau91861262007-10-17 17:06:05 +02001121
1122 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
1123 /* fall through */
1124
1125 case DATA_ST_PX_FIN:
1126 return 1;
1127
1128 default:
1129 /* unknown state, we should put an abort() here ! */
1130 return 1;
1131 }
1132}
1133
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001134
1135/* This function is called to send output to the response buffer.
1136 * It dumps the sessions states onto the output buffer <rep>.
1137 * Expects to be called with client socket shut down on input.
1138 * s->data_ctx must have been zeroed first, and the flags properly set.
1139 * It automatically clears the HIJACK bit from the response buffer.
1140 */
1141void stats_dump_sess_to_buffer(struct session *s, struct buffer *rep)
1142{
1143 struct chunk msg;
1144
1145 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
1146 /* If we're forced to shut down, we might have to remove our
1147 * reference to the last session being dumped.
1148 */
1149 if (s->data_state == DATA_ST_LIST) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001150 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001151 LIST_DEL(&s->data_ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001152 LIST_INIT(&s->data_ctx.sess.bref.users);
1153 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001154 }
1155 s->data_state = DATA_ST_FIN;
1156 buffer_stop_hijack(rep);
1157 s->ana_state = STATS_ST_CLOSE;
1158 return;
1159 }
1160
1161 if (s->ana_state != STATS_ST_REP)
1162 return;
1163
1164 msg.len = 0;
1165 msg.str = trash;
1166
1167 switch (s->data_state) {
1168 case DATA_ST_INIT:
1169 /* the function had not been called yet, let's prepare the
1170 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001171 * pointer to the first in the global list. When a target
1172 * session is being destroyed, it is responsible for updating
1173 * this pointer. We know we have reached the end when this
1174 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001175 */
1176 stream_int_retnclose(rep->cons, &msg);
1177 LIST_INIT(&s->data_ctx.sess.bref.users);
1178 s->data_ctx.sess.bref.ref = sessions.n;
1179 s->data_state = DATA_ST_LIST;
1180 /* fall through */
1181
1182 case DATA_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001183 /* first, let's detach the back-ref from a possible previous session */
1184 if (!LIST_ISEMPTY(&s->data_ctx.sess.bref.users)) {
1185 LIST_DEL(&s->data_ctx.sess.bref.users);
1186 LIST_INIT(&s->data_ctx.sess.bref.users);
1187 }
1188
1189 /* and start from where we stopped */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001190 while (s->data_ctx.sess.bref.ref != &sessions) {
1191 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1192 struct session *curr_sess;
1193
1194 curr_sess = LIST_ELEM(s->data_ctx.sess.bref.ref, struct session *, list);
1195
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001196 chunk_printf(&msg, sizeof(trash),
1197 "%p: proto=%s",
1198 curr_sess,
1199 curr_sess->listener->proto->name);
1200
1201 switch (curr_sess->listener->proto->sock_family) {
1202 case AF_INET:
1203 inet_ntop(AF_INET,
1204 (const void *)&((struct sockaddr_in *)&curr_sess->cli_addr)->sin_addr,
1205 pn, sizeof(pn));
1206
1207 chunk_printf(&msg, sizeof(trash),
1208 " src=%s:%d fe=%s be=%s srv=%s",
1209 pn,
1210 ntohs(((struct sockaddr_in *)&curr_sess->cli_addr)->sin_port),
1211 curr_sess->fe->id,
1212 curr_sess->be->id,
1213 curr_sess->srv ? curr_sess->srv->id : "<none>"
1214 );
1215 break;
1216 case AF_INET6:
1217 inet_ntop(AF_INET6,
1218 (const void *)&((struct sockaddr_in6 *)(&curr_sess->cli_addr))->sin6_addr,
1219 pn, sizeof(pn));
1220
1221 chunk_printf(&msg, sizeof(trash),
1222 " src=%s:%d fe=%s be=%s srv=%s",
1223 pn,
1224 ntohs(((struct sockaddr_in6 *)&curr_sess->cli_addr)->sin6_port),
1225 curr_sess->fe->id,
1226 curr_sess->be->id,
1227 curr_sess->srv ? curr_sess->srv->id : "<none>"
1228 );
1229
1230 break;
1231 case AF_UNIX:
1232 /* no more information to print right now */
1233 break;
1234 }
1235
1236 chunk_printf(&msg, sizeof(trash),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001237 " as=%d ts=%02x age=%s calls=%d",
Willy Tarreau8a5c6262008-12-08 00:16:21 +01001238 curr_sess->ana_state, curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01001239 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
1240 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001241
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001242 chunk_printf(&msg, sizeof(trash),
1243 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
1244 curr_sess->req->flags,
1245 curr_sess->req->l,
1246 curr_sess->req->analysers,
1247 curr_sess->req->rex ?
1248 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
1249 TICKS_TO_MS(1000)) : "");
1250
1251 chunk_printf(&msg, sizeof(trash),
1252 ",wx=%s",
1253 curr_sess->req->wex ?
1254 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
1255 TICKS_TO_MS(1000)) : "");
1256
1257 chunk_printf(&msg, sizeof(trash),
1258 ",ax=%s]",
1259 curr_sess->req->analyse_exp ?
1260 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
1261 TICKS_TO_MS(1000)) : "");
1262
1263 chunk_printf(&msg, sizeof(trash),
1264 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
1265 curr_sess->rep->flags,
1266 curr_sess->rep->l,
1267 curr_sess->rep->analysers,
1268 curr_sess->rep->rex ?
1269 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
1270 TICKS_TO_MS(1000)) : "");
1271
1272 chunk_printf(&msg, sizeof(trash),
1273 ",wx=%s",
1274 curr_sess->rep->wex ?
1275 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
1276 TICKS_TO_MS(1000)) : "");
1277
1278 chunk_printf(&msg, sizeof(trash),
1279 ",ax=%s]",
1280 curr_sess->rep->analyse_exp ?
1281 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
1282 TICKS_TO_MS(1000)) : "");
1283
1284 chunk_printf(&msg, sizeof(trash),
1285 " s0=[%d,%1xh,fd=%d,ex=%s]",
1286 curr_sess->si[0].state,
1287 curr_sess->si[0].flags,
1288 curr_sess->si[0].fd,
1289 curr_sess->si[0].exp ?
1290 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
1291 TICKS_TO_MS(1000)) : "");
1292
1293 chunk_printf(&msg, sizeof(trash),
1294 " s1=[%d,%1xh,fd=%d,ex=%s]",
1295 curr_sess->si[1].state,
1296 curr_sess->si[1].flags,
1297 curr_sess->si[1].fd,
1298 curr_sess->si[1].exp ?
1299 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
1300 TICKS_TO_MS(1000)) : "");
1301
1302 chunk_printf(&msg, sizeof(trash),
1303 " exp=%s",
1304 curr_sess->task->expire ?
1305 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
1306 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01001307 if (task_in_rq(curr_sess->task))
Willy Tarreauc6dcad62009-03-29 00:18:14 +01001308 chunk_printf(&msg, sizeof(trash), " run(nice=%d)", curr_sess->task->nice);
1309
1310 chunk_printf(&msg, sizeof(trash), "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001311
1312 if (buffer_write_chunk(rep, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01001313 /* let's try again later from this session. We add ourselves into
1314 * this session's users so that it can remove us upon termination.
1315 */
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001316 LIST_ADDQ(&curr_sess->back_refs, &s->data_ctx.sess.bref.users);
1317 return;
1318 }
1319
1320 s->data_ctx.sess.bref.ref = curr_sess->list.n;
1321 }
1322 s->data_state = DATA_ST_FIN;
1323 /* fall through */
1324
1325 default:
1326 s->data_state = DATA_ST_FIN;
1327 buffer_stop_hijack(rep);
1328 s->ana_state = STATS_ST_CLOSE;
1329 return;
1330 }
1331}
1332
Willy Tarreau74808cb2009-03-04 15:53:18 +01001333/* print a line of error buffer (limited to 70 bytes) to <out>. The format is :
1334 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
1335 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
1336 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
1337 * lines are respected within the limit of 70 output chars. Lines that are
1338 * continuation of a previous truncated line begin with "+" instead of " "
1339 * after the offset. The new pointer is returned.
1340 */
1341static int dump_error_line(struct chunk *out, int size,
1342 struct error_snapshot *err, int *line, int ptr)
1343{
1344 int end;
1345 unsigned char c;
1346
1347 end = out->len + 80;
1348 if (end > size)
1349 return ptr;
1350
1351 chunk_printf(out, size, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
1352
1353 while (ptr < err->len) {
1354 c = err->buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01001355 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001356 if (out->len > end - 2)
1357 break;
1358 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01001359 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01001360 if (out->len > end - 3)
1361 break;
1362 out->str[out->len++] = '\\';
1363 switch (c) {
1364 case '\t': c = 't'; break;
1365 case '\n': c = 'n'; break;
1366 case '\r': c = 'r'; break;
1367 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01001368 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01001369 }
1370 out->str[out->len++] = c;
1371 } else {
1372 if (out->len > end - 5)
1373 break;
1374 out->str[out->len++] = '\\';
1375 out->str[out->len++] = 'x';
1376 out->str[out->len++] = hextab[(c >> 4) & 0xF];
1377 out->str[out->len++] = hextab[c & 0xF];
1378 }
1379 if (err->buf[ptr++] == '\n') {
1380 /* we had a line break, let's return now */
1381 out->str[out->len++] = '\n';
1382 *line = ptr;
1383 return ptr;
1384 }
1385 }
1386 /* we have an incomplete line, we return it as-is */
1387 out->str[out->len++] = '\n';
1388 return ptr;
1389}
1390
1391/* This function is called to send output to the response buffer.
1392 * It dumps the errors logged in proxies onto the output buffer <rep>.
1393 * Expects to be called with client socket shut down on input.
1394 * s->data_ctx must have been zeroed first, and the flags properly set.
1395 * It automatically clears the HIJACK bit from the response buffer.
1396 */
1397void stats_dump_errors_to_buffer(struct session *s, struct buffer *rep)
1398{
1399 extern const char *monthname[12];
1400 struct chunk msg;
1401
1402 if (unlikely(rep->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
1403 s->data_state = DATA_ST_FIN;
1404 buffer_stop_hijack(rep);
1405 s->ana_state = STATS_ST_CLOSE;
1406 return;
1407 }
1408
1409 if (s->ana_state != STATS_ST_REP)
1410 return;
1411
1412 msg.len = 0;
1413 msg.str = trash;
1414
1415 if (!s->data_ctx.errors.px) {
1416 /* the function had not been called yet, let's prepare the
1417 * buffer for a response.
1418 */
1419 stream_int_retnclose(rep->cons, &msg);
1420 s->data_ctx.errors.px = proxy;
1421 s->data_ctx.errors.buf = 0;
1422 s->data_ctx.errors.bol = 0;
1423 s->data_ctx.errors.ptr = -1;
1424 }
1425
1426 /* we have two inner loops here, one for the proxy, the other one for
1427 * the buffer.
1428 */
1429 while (s->data_ctx.errors.px) {
1430 struct error_snapshot *es;
1431
1432 if (s->data_ctx.errors.buf == 0)
1433 es = &s->data_ctx.errors.px->invalid_req;
1434 else
1435 es = &s->data_ctx.errors.px->invalid_rep;
1436
1437 if (!es->when.tv_sec)
1438 goto next;
1439
1440 if (s->data_ctx.errors.iid >= 0 &&
1441 s->data_ctx.errors.px->uuid != s->data_ctx.errors.iid &&
1442 es->oe->uuid != s->data_ctx.errors.iid)
1443 goto next;
1444
1445 if (s->data_ctx.errors.ptr < 0) {
1446 /* just print headers now */
1447
1448 char pn[INET6_ADDRSTRLEN];
1449 struct tm tm;
1450
1451 get_localtime(es->when.tv_sec, &tm);
1452 chunk_printf(&msg, sizeof(trash), "\n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
1453 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001454 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01001455
1456
1457 if (es->src.ss_family == AF_INET)
1458 inet_ntop(AF_INET,
1459 (const void *)&((struct sockaddr_in *)&es->src)->sin_addr,
1460 pn, sizeof(pn));
1461 else
1462 inet_ntop(AF_INET6,
1463 (const void *)&((struct sockaddr_in6 *)(&es->src))->sin6_addr,
1464 pn, sizeof(pn));
1465
1466 switch (s->data_ctx.errors.buf) {
1467 case 0:
1468 chunk_printf(&msg, sizeof(trash),
1469 " frontend %s (#%d): invalid request\n"
1470 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
1471 " request length %d bytes, error at position %d:\n\n",
1472 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
1473 pn, es->sid, es->oe->id, es->oe->uuid,
1474 es->srv ? es->srv->id : "<NONE>",
1475 es->srv ? es->srv->puid : -1,
1476 es->len, es->pos);
1477 break;
1478 case 1:
1479 chunk_printf(&msg, sizeof(trash),
1480 " backend %s (#%d) : invalid response\n"
1481 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
1482 " response length %d bytes, error at position %d:\n\n",
1483 s->data_ctx.errors.px->id, s->data_ctx.errors.px->uuid,
1484 pn, es->sid, es->oe->id, es->oe->uuid,
1485 es->srv ? es->srv->id : "<NONE>",
1486 es->srv ? es->srv->puid : -1,
1487 es->len, es->pos);
1488 break;
1489 }
1490
1491 if (buffer_write_chunk(rep, &msg) >= 0) {
1492 /* Socket buffer full. Let's try again later from the same point */
1493 return;
1494 }
1495 s->data_ctx.errors.ptr = 0;
1496 s->data_ctx.errors.sid = es->sid;
1497 }
1498
1499 if (s->data_ctx.errors.sid != es->sid) {
1500 /* the snapshot changed while we were dumping it */
1501 chunk_printf(&msg, sizeof(trash),
1502 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
1503 if (buffer_write_chunk(rep, &msg) >= 0)
1504 return;
1505 goto next;
1506 }
1507
1508 /* OK, ptr >= 0, so we have to dump the current line */
1509 while (s->data_ctx.errors.ptr < es->len) {
1510 int newptr;
1511 int newline;
1512
1513 newline = s->data_ctx.errors.bol;
1514 newptr = dump_error_line(&msg, sizeof(trash), es, &newline, s->data_ctx.errors.ptr);
1515 if (newptr == s->data_ctx.errors.ptr)
1516 return;
1517
1518 if (buffer_write_chunk(rep, &msg) >= 0) {
1519 /* Socket buffer full. Let's try again later from the same point */
1520 return;
1521 }
1522 s->data_ctx.errors.ptr = newptr;
1523 s->data_ctx.errors.bol = newline;
1524 };
1525 next:
1526 s->data_ctx.errors.bol = 0;
1527 s->data_ctx.errors.ptr = -1;
1528 s->data_ctx.errors.buf++;
1529 if (s->data_ctx.errors.buf > 1) {
1530 s->data_ctx.errors.buf = 0;
1531 s->data_ctx.errors.px = s->data_ctx.errors.px->next;
1532 }
1533 }
1534
1535 /* dump complete */
1536 buffer_stop_hijack(rep);
1537 s->ana_state = STATS_ST_CLOSE;
1538}
1539
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01001540
Willy Tarreau10522fd2008-07-09 20:12:41 +02001541static struct cfg_kw_list cfg_kws = {{ },{
1542 { CFG_GLOBAL, "stats", stats_parse_global },
1543 { 0, NULL, NULL },
1544}};
1545
1546__attribute__((constructor))
1547static void __dumpstats_module_init(void)
1548{
1549 cfg_register_keywords(&cfg_kws);
1550}
1551
Willy Tarreau91861262007-10-17 17:06:05 +02001552/*
1553 * Local variables:
1554 * c-indent-level: 8
1555 * c-basic-offset: 8
1556 * End:
1557 */