blob: 7c01e79bc08627c694a6b7d4df0d878c27d70aa6 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaueb472682010-05-28 18:46:57 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
Simon Horman17bce342011-06-15 15:18:47 +090017#include <stdbool.h>
Willy Tarreau91861262007-10-17 17:06:05 +020018#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020021#include <pwd.h>
22#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020023
24#include <sys/socket.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27
Willy Tarreau10522fd2008-07-09 20:12:41 +020028#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020029#include <common/compat.h>
30#include <common/config.h>
31#include <common/debug.h>
32#include <common/memory.h>
33#include <common/mini-clist.h>
34#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020035#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020036#include <common/time.h>
37#include <common/uri_auth.h>
38#include <common/version.h>
39
Willy Tarreau91861262007-10-17 17:06:05 +020040#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020041
42#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020043#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020044#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020045#include <proto/dumpstats.h>
46#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010047#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020048#include <proto/log.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010049#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020050#include <proto/listener.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020051#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020052#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020053#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020054#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020055#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010056#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010057#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020058
Simon Horman9bd2c732011-06-15 15:18:44 +090059static int stats_dump_raw_to_buffer(struct stream_interface *si);
60static int stats_dump_full_sess_to_buffer(struct stream_interface *si);
61static int stats_dump_sess_to_buffer(struct stream_interface *si);
62static int stats_dump_errors_to_buffer(struct stream_interface *si);
Simon Hormanc88b8872011-06-15 15:18:49 +090063static int stats_table_request(struct stream_interface *si, bool show);
Simon Horman9bd2c732011-06-15 15:18:44 +090064static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
65static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri);
66
67static struct si_applet cli_applet;
68
69static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020070 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +020071 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +020072 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020073 " help : this message\n"
74 " prompt : toggle interactive mode with prompt\n"
75 " quit : disconnect\n"
76 " show info : report information about the running process\n"
77 " show stat : report counters for each proxy and server\n"
78 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +010079 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +020080 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +020081 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +020082 " set weight : change a server's weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +020083 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +010084 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +020085 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +020086 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +020087 " disable : put a server or frontend in maintenance mode\n"
88 " enable : re-enable a server or frontend which is in maintenance mode\n"
89 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020090 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020091
Simon Horman9bd2c732011-06-15 15:18:44 +090092static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +020093 "Permission denied\n"
94 "";
95
Willy Tarreau295a8372011-03-10 11:25:07 +010096/* data transmission states for the stats responses */
97enum {
98 STAT_ST_INIT = 0,
99 STAT_ST_HEAD,
100 STAT_ST_INFO,
101 STAT_ST_LIST,
102 STAT_ST_END,
103 STAT_ST_FIN,
104};
105
106/* data transmission states for the stats responses inside a proxy */
107enum {
108 STAT_PX_ST_INIT = 0,
109 STAT_PX_ST_TH,
110 STAT_PX_ST_FE,
111 STAT_PX_ST_LI,
112 STAT_PX_ST_SV,
113 STAT_PX_ST_BE,
114 STAT_PX_ST_END,
115 STAT_PX_ST_FIN,
116};
117
Cyril Bonté19979e12012-04-04 12:57:21 +0200118extern const char *stat_status_codes[];
119
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200120/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +0200121 * a new stats socket. It returns a positive value upon success, 0 if the connection
122 * needs to be closed and ignored, or a negative value upon critical failure.
123 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900124static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200125{
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200126 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100127 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreau3cefd522012-08-30 15:49:18 +0200128 copy_target(&s->target, &s->si[1].conn.target); // for logging only
Willy Tarreau94981132012-05-21 17:09:48 +0200129 s->si[1].conn.data_ctx = s;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100130 s->si[1].applet.st1 = 0;
131 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200132
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200133 tv_zero(&s->logs.tv_request);
134 s->logs.t_queue = 0;
135 s->logs.t_connect = 0;
136 s->logs.t_data = 0;
137 s->logs.t_close = 0;
138 s->logs.bytes_in = s->logs.bytes_out = 0;
139 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
140 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200141
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200142 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200143
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200144 if (s->listener->timeout) {
145 s->req->rto = *s->listener->timeout;
146 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200147 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200148 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200149}
150
Willy Tarreau07e9e642010-08-17 21:48:17 +0200151/* allocate a new stats frontend named <name>, and return it
152 * (or NULL in case of lack of memory).
153 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200154static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200155{
156 struct proxy *fe;
157
158 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
159 if (!fe)
160 return NULL;
161
Willy Tarreau237250c2011-07-29 01:49:03 +0200162 init_new_proxy(fe);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200163
164 fe->last_change = now.tv_sec;
165 fe->id = strdup("GLOBAL");
166 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200167 fe->maxconn = 10; /* default to 10 concurrent connections */
168 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200169 fe->conf.file = strdup(file);
170 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200171 fe->accept = stats_accept;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200172 return fe;
173}
174
Willy Tarreaufbee7132007-10-18 13:53:22 +0200175/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200176 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
177 * error message into the <err> buffer which will be preallocated. The trailing
178 * '\n' must not be written. The function must be called with <args> pointing to
179 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200180 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200181static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200182 struct proxy *defpx, const char *file, int line,
183 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200184{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200185 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200186 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200187
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200188 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200189 int cur_arg;
190
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200191 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200192 memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200193 return -1;
194 }
195
Willy Tarreau89a63132009-08-16 17:41:45 +0200196 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200197 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200198 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200199 return -1;
200 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200201 }
202
Willy Tarreau4348fad2012-09-20 16:48:07 +0200203 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200204 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200205
Willy Tarreauc53d4222012-09-20 20:19:28 +0200206 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
207 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
208 file, line, args[0], args[1], err && *err ? *err : "error");
209 return -1;
210 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200211
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200212 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200213 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200214 static int bind_dumped;
215 struct bind_kw *kw;
216
217 kw = bind_find_kw(args[cur_arg]);
218 if (kw) {
219 if (!kw->parse) {
220 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
221 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200222 return -1;
223 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200224
225 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
226 if (err && *err)
227 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
228 else
229 memprintf(err, "'%s %s' : error encountered while processing '%s'",
230 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200231 return -1;
232 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200233
234 cur_arg += 1 + kw->skip;
235 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200236 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200237
238 if (!bind_dumped) {
239 bind_dump_kws(err);
240 indent_msg(err, 4);
241 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200242 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200243
244 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
245 args[0], args[1], args[cur_arg],
246 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
247 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200248 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100249
Willy Tarreauc53d4222012-09-20 20:19:28 +0200250 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
251 l->maxconn = global.stats_fe->maxconn;
252 l->backlog = global.stats_fe->backlog;
253 l->timeout = &global.stats_fe->timeout.client;
254 l->accept = session_accept;
255 l->handler = process_session;
256 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
257 l->nice = -64; /* we want to boost priority for local stats */
258 global.maxsock += l->maxconn;
259 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200260 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200261 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100262 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200263 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100264
265 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200266 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100267 return -1;
268 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200269
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100270 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200271 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200272 return -1;
273 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200274 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200275 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200276 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200277 return -1;
278 }
279 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200280 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200281 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200282 else if (!strcmp(args[1], "maxconn")) {
283 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200284
285 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200286 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200287 return -1;
288 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200289
290 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200291 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200292 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200293 return -1;
294 }
295 }
296 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200297 }
298 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200299 memprintf(err, "'%s' only supports 'socket', 'maxconn' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200300 return -1;
301 }
302 return 0;
303}
304
Simon Horman9bd2c732011-06-15 15:18:44 +0900305static int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100306{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200307 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100308 "# pxname,svname,"
309 "qcur,qmax,"
310 "scur,smax,slim,stot,"
311 "bin,bout,"
312 "dreq,dresp,"
313 "ereq,econ,eresp,"
314 "wretr,wredis,"
315 "status,weight,act,bck,"
316 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200317 "pid,iid,sid,throttle,lbtot,tracked,type,"
318 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200319 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100320 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100321 "req_rate,req_rate_max,req_tot,"
322 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100323 "\n");
324}
325
Simon Hormand9366582011-06-15 15:18:45 +0900326/* print a string of text buffer to <out>. The format is :
327 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
328 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
329 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
330 */
331static int dump_text(struct chunk *out, const char *buf, int bsize)
332{
333 unsigned char c;
334 int ptr = 0;
335
336 while (buf[ptr] && ptr < bsize) {
337 c = buf[ptr];
338 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
339 if (out->len > out->size - 1)
340 break;
341 out->str[out->len++] = c;
342 }
343 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
344 if (out->len > out->size - 2)
345 break;
346 out->str[out->len++] = '\\';
347 switch (c) {
348 case ' ': c = ' '; break;
349 case '\t': c = 't'; break;
350 case '\n': c = 'n'; break;
351 case '\r': c = 'r'; break;
352 case '\e': c = 'e'; break;
353 case '\\': c = '\\'; break;
354 }
355 out->str[out->len++] = c;
356 }
357 else {
358 if (out->len > out->size - 4)
359 break;
360 out->str[out->len++] = '\\';
361 out->str[out->len++] = 'x';
362 out->str[out->len++] = hextab[(c >> 4) & 0xF];
363 out->str[out->len++] = hextab[c & 0xF];
364 }
365 ptr++;
366 }
367
368 return ptr;
369}
370
371/* print a buffer in hexa.
372 * Print stopped if <bsize> is reached, or if no more place in the chunk.
373 */
374static int dump_binary(struct chunk *out, const char *buf, int bsize)
375{
376 unsigned char c;
377 int ptr = 0;
378
379 while (ptr < bsize) {
380 c = buf[ptr];
381
382 if (out->len > out->size - 2)
383 break;
384 out->str[out->len++] = hextab[(c >> 4) & 0xF];
385 out->str[out->len++] = hextab[c & 0xF];
386
387 ptr++;
388 }
389 return ptr;
390}
391
392/* Dump the status of a table to a stream interface's
393 * read buffer. It returns 0 if the output buffer is full
394 * and needs to be called again, otherwise non-zero.
395 */
396static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
397 struct proxy *proxy, struct proxy *target)
398{
Willy Tarreau94981132012-05-21 17:09:48 +0200399 struct session *s = si->conn.data_ctx;
Simon Hormand9366582011-06-15 15:18:45 +0900400
401 chunk_printf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
402 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
403
404 /* any other information should be dumped here */
405
Willy Tarreau290e63a2012-09-20 18:07:14 +0200406 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Simon Hormand9366582011-06-15 15:18:45 +0900407 chunk_printf(msg, "# contents not dumped due to insufficient privileges\n");
408
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200409 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900410 return 0;
411
412 return 1;
413}
414
415/* Dump the a table entry to a stream interface's
416 * read buffer. It returns 0 if the output buffer is full
417 * and needs to be called again, otherwise non-zero.
418 */
419static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
420 struct proxy *proxy, struct stksess *entry)
421{
422 int dt;
423
424 chunk_printf(msg, "%p:", entry);
425
426 if (proxy->table.type == STKTABLE_TYPE_IP) {
427 char addr[INET_ADDRSTRLEN];
428 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
429 chunk_printf(msg, " key=%s", addr);
430 }
431 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
432 char addr[INET6_ADDRSTRLEN];
433 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
434 chunk_printf(msg, " key=%s", addr);
435 }
436 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
437 chunk_printf(msg, " key=%u", *(unsigned int *)entry->key.key);
438 }
439 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
440 chunk_printf(msg, " key=");
441 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
442 }
443 else {
444 chunk_printf(msg, " key=");
445 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
446 }
447
448 chunk_printf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
449
450 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
451 void *ptr;
452
453 if (proxy->table.data_ofs[dt] == 0)
454 continue;
455 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
456 chunk_printf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
457 else
458 chunk_printf(msg, " %s=", stktable_data_types[dt].name);
459
460 ptr = stktable_data_ptr(&proxy->table, entry, dt);
461 switch (stktable_data_types[dt].std_type) {
462 case STD_T_SINT:
463 chunk_printf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
464 break;
465 case STD_T_UINT:
466 chunk_printf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
467 break;
468 case STD_T_ULL:
469 chunk_printf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
470 break;
471 case STD_T_FRQP:
472 chunk_printf(msg, "%d",
473 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
474 proxy->table.data_arg[dt].u));
475 break;
476 }
477 }
478 chunk_printf(msg, "\n");
479
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200480 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900481 return 0;
482
483 return 1;
484}
485
Willy Tarreaudec98142012-06-06 23:37:08 +0200486static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900487{
Willy Tarreau94981132012-05-21 17:09:48 +0200488 struct session *s = si->conn.data_ctx;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900489 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900490 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900491 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900492 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreaudec98142012-06-06 23:37:08 +0200493 struct chunk msg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200494 long long value;
495 int data_type;
496 void *ptr;
497 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900498
Simon Hormand5b9fd92011-06-15 15:18:48 +0900499 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900500
501 if (!*args[4]) {
502 si->applet.ctx.cli.msg = "Key value expected\n";
503 si->applet.st0 = STAT_CLI_PRINT;
504 return;
505 }
506
Simon Hormanc5b89f62011-06-15 15:18:50 +0900507 switch (px->table.type) {
508 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900509 uint32_key = htonl(inetaddr_host(args[4]));
510 static_table_key.key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900511 break;
512 case STKTABLE_TYPE_IPV6:
513 inet_pton(AF_INET6, args[4], ip6_key);
514 static_table_key.key = &ip6_key;
515 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900516 case STKTABLE_TYPE_INTEGER:
517 {
518 char *endptr;
519 unsigned long val;
520 errno = 0;
521 val = strtoul(args[4], &endptr, 10);
522 if ((errno == ERANGE && val == ULONG_MAX) ||
523 (errno != 0 && val == 0) || endptr == args[4] ||
524 val > 0xffffffff) {
525 si->applet.ctx.cli.msg = "Invalid key\n";
526 si->applet.st0 = STAT_CLI_PRINT;
527 return;
528 }
529 uint32_key = (uint32_t) val;
530 static_table_key.key = &uint32_key;
531 break;
532 }
533 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900534 case STKTABLE_TYPE_STRING:
535 static_table_key.key = args[4];
536 static_table_key.key_len = strlen(args[4]);
537 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900538 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200539 switch (action) {
540 case STAT_CLI_O_TAB:
541 si->applet.ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
542 break;
543 case STAT_CLI_O_CLR:
544 si->applet.ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
545 break;
546 default:
547 si->applet.ctx.cli.msg = "Unknown action\n";
548 break;
549 }
Simon Horman121f3052011-06-15 15:18:46 +0900550 si->applet.st0 = STAT_CLI_PRINT;
551 return;
552 }
553
554 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200555 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Simon Horman121f3052011-06-15 15:18:46 +0900556 si->applet.ctx.cli.msg = stats_permission_denied_msg;
557 si->applet.st0 = STAT_CLI_PRINT;
558 return;
559 }
560
561 ts = stktable_lookup_key(&px->table, &static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900562
Willy Tarreaudec98142012-06-06 23:37:08 +0200563 switch (action) {
564 case STAT_CLI_O_TAB:
565 if (!ts)
566 return;
David du Colombier7af46052012-05-16 14:16:48 +0200567 chunk_init(&msg, trash, trashlen);
Simon Horman17bce342011-06-15 15:18:47 +0900568 if (!stats_dump_table_head_to_buffer(&msg, si, px, px))
569 return;
570 stats_dump_table_entry_to_buffer(&msg, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900571 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200572
573 case STAT_CLI_O_CLR:
574 if (!ts)
575 return;
576 if (ts->ref_cnt) {
577 /* don't delete an entry which is currently referenced */
578 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
579 si->applet.st0 = STAT_CLI_PRINT;
580 return;
581 }
582 stksess_kill(&px->table, ts);
583 break;
Simon Horman17bce342011-06-15 15:18:47 +0900584
Willy Tarreau654694e2012-06-07 01:03:16 +0200585 case STAT_CLI_O_SET:
586 if (strncmp(args[5], "data.", 5) != 0) {
587 si->applet.ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
588 si->applet.st0 = STAT_CLI_PRINT;
589 return;
590 }
591
592 data_type = stktable_get_data_type(args[5] + 5);
593 if (data_type < 0) {
594 si->applet.ctx.cli.msg = "Unknown data type\n";
595 si->applet.st0 = STAT_CLI_PRINT;
596 return;
597 }
598
599 if (!px->table.data_ofs[data_type]) {
600 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
601 si->applet.st0 = STAT_CLI_PRINT;
602 return;
603 }
604
605 if (!*args[6] || strl2llrc(args[6], strlen(args[6]), &value) != 0) {
606 si->applet.ctx.cli.msg = "Require a valid integer value to store\n";
607 si->applet.st0 = STAT_CLI_PRINT;
608 return;
609 }
610
611 if (ts)
612 stktable_touch(&px->table, ts, 1);
613 else {
614 ts = stksess_new(&px->table, &static_table_key);
615 if (!ts) {
616 /* don't delete an entry which is currently referenced */
617 si->applet.ctx.cli.msg = "Unable to allocate a new entry\n";
618 si->applet.st0 = STAT_CLI_PRINT;
619 return;
620 }
621 stktable_store(&px->table, ts, 1);
622 }
623
624 ptr = stktable_data_ptr(&px->table, ts, data_type);
625 switch (stktable_data_types[data_type].std_type) {
626 case STD_T_SINT:
627 stktable_data_cast(ptr, std_t_sint) = value;
628 break;
629 case STD_T_UINT:
630 stktable_data_cast(ptr, std_t_uint) = value;
631 break;
632 case STD_T_ULL:
633 stktable_data_cast(ptr, std_t_ull) = value;
634 break;
635 case STD_T_FRQP:
636 /* We only reset the previous value so that it slowly fades out */
637 frqp = &stktable_data_cast(ptr, std_t_frqp);
638 frqp->curr_tick = now_ms;
639 frqp->prev_ctr = value;
640 frqp->curr_ctr = 0;
641 break;
642 }
643 break;
644
Willy Tarreaudec98142012-06-06 23:37:08 +0200645 default:
646 si->applet.ctx.cli.msg = "Unknown action\n";
Simon Horman121f3052011-06-15 15:18:46 +0900647 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200648 break;
Simon Horman121f3052011-06-15 15:18:46 +0900649 }
Simon Horman121f3052011-06-15 15:18:46 +0900650}
651
Willy Tarreau654694e2012-06-07 01:03:16 +0200652static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900653{
Willy Tarreau654694e2012-06-07 01:03:16 +0200654 if (action != STAT_CLI_O_TAB) {
655 si->applet.ctx.cli.msg = "content-based lookup is only supported with the \"show\" action";
656 si->applet.st0 = STAT_CLI_PRINT;
657 return;
658 }
659
Simon Hormand5b9fd92011-06-15 15:18:48 +0900660 /* condition on stored data value */
661 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
662 if (si->applet.ctx.table.data_type < 0) {
663 si->applet.ctx.cli.msg = "Unknown data type\n";
664 si->applet.st0 = STAT_CLI_PRINT;
665 return;
666 }
667
668 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
669 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
670 si->applet.st0 = STAT_CLI_PRINT;
671 return;
672 }
673
674 si->applet.ctx.table.data_op = get_std_op(args[4]);
675 if (si->applet.ctx.table.data_op < 0) {
676 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
677 si->applet.st0 = STAT_CLI_PRINT;
678 return;
679 }
680
681 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
682 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
683 si->applet.st0 = STAT_CLI_PRINT;
684 return;
685 }
686}
687
Willy Tarreaudec98142012-06-06 23:37:08 +0200688static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900689{
690 si->applet.ctx.table.data_type = -1;
Willy Tarreau94981132012-05-21 17:09:48 +0200691 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200692 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900693 si->applet.ctx.table.proxy = NULL;
694 si->applet.ctx.table.entry = NULL;
Willy Tarreaudec98142012-06-06 23:37:08 +0200695 si->applet.st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900696
697 if (*args[2]) {
698 si->applet.ctx.table.target = find_stktable(args[2]);
699 if (!si->applet.ctx.table.target) {
700 si->applet.ctx.cli.msg = "No such table\n";
701 si->applet.st0 = STAT_CLI_PRINT;
702 return;
703 }
704 }
705 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200706 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900707 goto err_args;
708 return;
709 }
710
711 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200712 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900713 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200714 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900715 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900716 goto err_args;
717
718 return;
719
720err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200721 switch (action) {
722 case STAT_CLI_O_TAB:
Simon Hormand5b9fd92011-06-15 15:18:48 +0900723 si->applet.ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200724 break;
725 case STAT_CLI_O_CLR:
Simon Hormanc88b8872011-06-15 15:18:49 +0900726 si->applet.ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200727 break;
728 default:
729 si->applet.ctx.cli.msg = "Unknown action\n";
730 break;
731 }
Simon Hormand5b9fd92011-06-15 15:18:48 +0900732 si->applet.st0 = STAT_CLI_PRINT;
733}
734
Willy Tarreau532a4502011-09-07 22:37:44 +0200735/* Expects to find a frontend named <arg> and returns it, otherwise displays various
736 * adequate error messages and returns NULL. This function also expects the session
737 * level to be admin.
738 */
739static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
740{
741 struct proxy *px;
742
Willy Tarreau290e63a2012-09-20 18:07:14 +0200743 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau532a4502011-09-07 22:37:44 +0200744 si->applet.ctx.cli.msg = stats_permission_denied_msg;
745 si->applet.st0 = STAT_CLI_PRINT;
746 return NULL;
747 }
748
749 if (!*arg) {
750 si->applet.ctx.cli.msg = "A frontend name is expected.\n";
751 si->applet.st0 = STAT_CLI_PRINT;
752 return NULL;
753 }
754
755 px = findproxy(arg, PR_CAP_FE);
756 if (!px) {
757 si->applet.ctx.cli.msg = "No such frontend.\n";
758 si->applet.st0 = STAT_CLI_PRINT;
759 return NULL;
760 }
761 return px;
762}
763
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200764/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
765 * and returns the pointer to the server. Otherwise, display adequate error messages
766 * and returns NULL. This function also expects the session level to be admin. Note:
767 * the <arg> is modified to remove the '/'.
768 */
769static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
770{
771 struct proxy *px;
772 struct server *sv;
773 char *line;
774
Willy Tarreau290e63a2012-09-20 18:07:14 +0200775 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200776 si->applet.ctx.cli.msg = stats_permission_denied_msg;
777 si->applet.st0 = STAT_CLI_PRINT;
778 return NULL;
779 }
780
781 /* split "backend/server" and make <line> point to server */
782 for (line = arg; *line; line++)
783 if (*line == '/') {
784 *line++ = '\0';
785 break;
786 }
787
788 if (!*line || !*arg) {
789 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
790 si->applet.st0 = STAT_CLI_PRINT;
791 return NULL;
792 }
793
794 if (!get_backend_server(arg, line, &px, &sv)) {
795 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
796 si->applet.st0 = STAT_CLI_PRINT;
797 return NULL;
798 }
799
800 if (px->state == PR_STSTOPPED) {
801 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
802 si->applet.st0 = STAT_CLI_PRINT;
803 return NULL;
804 }
805
806 return sv;
807}
808
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200809/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200810 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100811 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200812 * designating the function which will have to process the request, which can
813 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200814 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900815static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200816{
Willy Tarreau94981132012-05-21 17:09:48 +0200817 struct session *s = si->conn.data_ctx;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200818 char *args[MAX_STATS_ARGS + 1];
819 int arg;
820
821 while (isspace((unsigned char)*line))
822 line++;
823
824 arg = 0;
825 args[arg] = line;
826
827 while (*line && arg < MAX_STATS_ARGS) {
828 if (isspace((unsigned char)*line)) {
829 *line++ = '\0';
830
831 while (isspace((unsigned char)*line))
832 line++;
833
834 args[++arg] = line;
835 continue;
836 }
837
838 line++;
839 }
840
841 while (++arg <= MAX_STATS_ARGS)
842 args[arg] = line;
843
Willy Tarreau295a8372011-03-10 11:25:07 +0100844 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200845 if (strcmp(args[0], "show") == 0) {
846 if (strcmp(args[1], "stat") == 0) {
847 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100848 si->applet.ctx.stats.flags |= STAT_BOUND;
849 si->applet.ctx.stats.iid = atoi(args[2]);
850 si->applet.ctx.stats.type = atoi(args[3]);
851 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200852 }
853
Willy Tarreau295a8372011-03-10 11:25:07 +0100854 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
855 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau94981132012-05-21 17:09:48 +0200856 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100857 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200858 }
859 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100860 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
861 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau94981132012-05-21 17:09:48 +0200862 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100863 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200864 }
865 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau94981132012-05-21 17:09:48 +0200866 si->conn.data_st = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +0200867 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100868 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100869 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200870 return 1;
871 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100872 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100873 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100874 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100875 si->applet.ctx.sess.target = NULL;
876 si->applet.ctx.sess.section = 0; /* start with session status */
877 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100878 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200879 }
880 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +0200881 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100882 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100883 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200884 return 1;
885 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200886 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100887 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200888 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100889 si->applet.ctx.errors.iid = -1;
890 si->applet.ctx.errors.px = NULL;
Willy Tarreau94981132012-05-21 17:09:48 +0200891 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100892 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200893 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200894 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200895 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200896 }
Aman Guptaceafb4a2012-04-02 18:57:54 -0700897 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200898 return 0;
899 }
900 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200901 else if (strcmp(args[0], "clear") == 0) {
902 if (strcmp(args[1], "counters") == 0) {
903 struct proxy *px;
904 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200905 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200906 int clrall = 0;
907
908 if (strcmp(args[2], "all") == 0)
909 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200910
Willy Tarreau6162db22009-10-10 17:13:00 +0200911 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200912 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
913 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100914 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100915 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200916 return 1;
917 }
918
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200919 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100920 if (clrall) {
921 memset(&px->be_counters, 0, sizeof(px->be_counters));
922 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
923 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200924 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100925 px->be_counters.conn_max = 0;
926 px->be_counters.p.http.rps_max = 0;
927 px->be_counters.sps_max = 0;
928 px->be_counters.cps_max = 0;
929 px->be_counters.nbpend_max = 0;
930
931 px->fe_counters.conn_max = 0;
932 px->fe_counters.p.http.rps_max = 0;
933 px->fe_counters.sps_max = 0;
934 px->fe_counters.cps_max = 0;
935 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200936 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200937
938 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200939 if (clrall)
940 memset(&sv->counters, 0, sizeof(sv->counters));
941 else {
942 sv->counters.cur_sess_max = 0;
943 sv->counters.nbpend_max = 0;
944 sv->counters.sps_max = 0;
945 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200946
Willy Tarreaua7944ad2012-09-26 21:03:11 +0200947 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200948 if (li->counters) {
949 if (clrall)
950 memset(li->counters, 0, sizeof(*li->counters));
951 else
952 li->counters->conn_max = 0;
953 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200954 }
955
Willy Tarreau81c25d02011-09-07 15:17:21 +0200956 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200957 return 1;
958 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200959 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200960 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200961 /* end of processing */
962 return 1;
963 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200964 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200965 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200966 return 0;
967 }
968 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200969 else if (strcmp(args[0], "get") == 0) {
970 if (strcmp(args[1], "weight") == 0) {
971 struct proxy *px;
972 struct server *sv;
973
974 /* split "backend/server" and make <line> point to server */
975 for (line = args[2]; *line; line++)
976 if (*line == '/') {
977 *line++ = '\0';
978 break;
979 }
980
981 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100982 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100983 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200984 return 1;
985 }
986
987 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100988 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100989 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200990 return 1;
991 }
992
993 /* return server's effective weight at the moment */
David du Colombier7af46052012-05-16 14:16:48 +0200994 snprintf(trash, trashlen, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200995 bi_putstr(si->ib, trash);
Willy Tarreau38338fa2009-10-10 18:37:29 +0200996 return 1;
997 }
998 else { /* not "get weight" */
999 return 0;
1000 }
1001 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001002 else if (strcmp(args[0], "set") == 0) {
1003 if (strcmp(args[1], "weight") == 0) {
1004 struct proxy *px;
1005 struct server *sv;
1006 int w;
1007
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001008 sv = expect_server_admin(s, si, args[2]);
1009 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001010 return 1;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001011 px = sv->proxy;
Willy Tarreau4483d432009-10-10 19:30:08 +02001012
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001013 /* if the weight is terminated with '%', it is set relative to
1014 * the initial weight, otherwise it is absolute.
1015 */
1016 if (!*args[3]) {
1017 si->applet.ctx.cli.msg = "Require <weight> or <weight%>.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001018 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001019 return 1;
1020 }
1021
Willy Tarreau4483d432009-10-10 19:30:08 +02001022 w = atoi(args[3]);
1023 if (strchr(args[3], '%') != NULL) {
1024 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001025 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001026 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001027 return 1;
1028 }
1029 w = sv->iweight * w / 100;
1030 }
1031 else {
1032 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001033 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001034 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001035 return 1;
1036 }
1037 }
1038
1039 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001040 si->applet.ctx.cli.msg = "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001041 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001042 return 1;
1043 }
1044
1045 sv->uweight = w;
1046
1047 if (px->lbprm.algo & BE_LB_PROP_DYN) {
1048 /* we must take care of not pushing the server to full throttle during slow starts */
1049 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
1050 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
1051 else
1052 sv->eweight = BE_WEIGHT_SCALE;
1053 sv->eweight *= sv->uweight;
1054 } else {
1055 sv->eweight = sv->uweight;
1056 }
1057
1058 /* static LB algorithms are a bit harder to update */
1059 if (px->lbprm.update_server_eweight)
1060 px->lbprm.update_server_eweight(sv);
Willy Tarreau9580d162012-05-19 19:07:40 +02001061 else if (sv->eweight) {
1062 if (px->lbprm.set_server_status_up)
1063 px->lbprm.set_server_status_up(sv);
1064 }
1065 else {
1066 if (px->lbprm.set_server_status_down)
1067 px->lbprm.set_server_status_down(sv);
1068 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001069
1070 return 1;
1071 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001072 else if (strcmp(args[1], "timeout") == 0) {
1073 if (strcmp(args[2], "cli") == 0) {
1074 unsigned timeout;
1075 const char *res;
1076
1077 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001078 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001079 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001080 return 1;
1081 }
1082
1083 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1084 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001085 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001086 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001087 return 1;
1088 }
1089
1090 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1091 return 1;
1092 }
1093 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001094 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001095 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001096 return 1;
1097 }
1098 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001099 else if (strcmp(args[1], "maxconn") == 0) {
1100 if (strcmp(args[2], "frontend") == 0) {
1101 struct proxy *px;
1102 struct listener *l;
1103 int v;
1104
Willy Tarreau532a4502011-09-07 22:37:44 +02001105 px = expect_frontend_admin(s, si, args[3]);
1106 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001107 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001108
1109 if (!*args[4]) {
1110 si->applet.ctx.cli.msg = "Integer value expected.\n";
1111 si->applet.st0 = STAT_CLI_PRINT;
1112 return 1;
1113 }
1114
1115 v = atoi(args[4]);
1116 /* check for unlimited values, we restore default setting (cfg_maxpconn) */
1117 if (v < 1) {
1118 si->applet.ctx.cli.msg = "Value out of range.\n";
1119 si->applet.st0 = STAT_CLI_PRINT;
1120 return 1;
1121 }
1122
1123 /* OK, the value is fine, so we assign it to the proxy and to all of
1124 * its listeners. The blocked ones will be dequeued.
1125 */
1126 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001127 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001128 l->maxconn = v;
1129 if (l->state == LI_FULL)
1130 resume_listener(l);
1131 }
1132
1133 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1134 dequeue_all_listeners(&s->fe->listener_queue);
1135
1136 return 1;
1137 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001138 else if (strcmp(args[2], "global") == 0) {
1139 int v;
1140
Willy Tarreau290e63a2012-09-20 18:07:14 +02001141 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau91886b62011-09-07 14:38:31 +02001142 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1143 si->applet.st0 = STAT_CLI_PRINT;
1144 return 1;
1145 }
1146
1147 if (!*args[3]) {
1148 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1149 si->applet.st0 = STAT_CLI_PRINT;
1150 return 1;
1151 }
1152
1153 v = atoi(args[3]);
1154 if (v > global.hardmaxconn) {
1155 si->applet.ctx.cli.msg = "Value out of range.\n";
1156 si->applet.st0 = STAT_CLI_PRINT;
1157 return 1;
1158 }
1159
1160 /* check for unlimited values */
1161 if (v <= 0)
1162 v = global.hardmaxconn;
1163
1164 global.maxconn = v;
1165
1166 /* Dequeues all of the listeners waiting for a resource */
1167 if (!LIST_ISEMPTY(&global_listener_queue))
1168 dequeue_all_listeners(&global_listener_queue);
1169
1170 return 1;
1171 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001172 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001173 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001174 si->applet.st0 = STAT_CLI_PRINT;
1175 return 1;
1176 }
1177 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001178 else if (strcmp(args[1], "rate-limit") == 0) {
1179 if (strcmp(args[2], "connections") == 0) {
1180 if (strcmp(args[3], "global") == 0) {
1181 int v;
1182
Willy Tarreau290e63a2012-09-20 18:07:14 +02001183 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreauf5b22872011-09-07 16:13:44 +02001184 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1185 si->applet.st0 = STAT_CLI_PRINT;
1186 return 1;
1187 }
1188
1189 if (!*args[4]) {
1190 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1191 si->applet.st0 = STAT_CLI_PRINT;
1192 return 1;
1193 }
1194
1195 v = atoi(args[4]);
1196 if (v < 0) {
1197 si->applet.ctx.cli.msg = "Value out of range.\n";
1198 si->applet.st0 = STAT_CLI_PRINT;
1199 return 1;
1200 }
1201
1202 global.cps_lim = v;
1203
1204 /* Dequeues all of the listeners waiting for a resource */
1205 if (!LIST_ISEMPTY(&global_listener_queue))
1206 dequeue_all_listeners(&global_listener_queue);
1207
1208 return 1;
1209 }
1210 else {
1211 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1212 si->applet.st0 = STAT_CLI_PRINT;
1213 return 1;
1214 }
1215 }
1216 else {
1217 si->applet.ctx.cli.msg = "'set rate-limit' only supports 'connections'.\n";
1218 si->applet.st0 = STAT_CLI_PRINT;
1219 return 1;
1220 }
1221 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001222 else if (strcmp(args[1], "table") == 0) {
1223 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1224 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001225 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001226 return 0;
1227 }
1228 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001229 else if (strcmp(args[0], "enable") == 0) {
1230 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001231 struct server *sv;
1232
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001233 sv = expect_server_admin(s, si, args[2]);
1234 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001235 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001236
Cyril Bontécd19e512010-01-31 22:34:03 +01001237 if (sv->state & SRV_MAINTAIN) {
1238 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001239 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001240 /* If this server tracks the status of another one,
1241 * we must restore the good status.
1242 */
Willy Tarreau44267702011-10-28 15:35:33 +02001243 if (sv->track->state & SRV_RUNNING) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001244 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001245 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001246 } else {
1247 sv->state &= ~SRV_MAINTAIN;
1248 set_server_down(sv);
1249 }
1250 } else {
1251 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001252 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001253 }
1254 }
1255
Willy Tarreau532a4502011-09-07 22:37:44 +02001256 return 1;
1257 }
1258 else if (strcmp(args[1], "frontend") == 0) {
1259 struct proxy *px;
1260
1261 px = expect_frontend_admin(s, si, args[2]);
1262 if (!px)
1263 return 1;
1264
1265 if (px->state == PR_STSTOPPED) {
1266 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1267 si->applet.st0 = STAT_CLI_PRINT;
1268 return 1;
1269 }
1270
1271 if (px->state != PR_STPAUSED) {
1272 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1273 si->applet.st0 = STAT_CLI_PRINT;
1274 return 1;
1275 }
1276
1277 if (!resume_proxy(px)) {
1278 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1279 si->applet.st0 = STAT_CLI_PRINT;
1280 return 1;
1281 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001282 return 1;
1283 }
1284 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001285 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1286 si->applet.st0 = STAT_CLI_PRINT;
1287 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001288 }
1289 }
1290 else if (strcmp(args[0], "disable") == 0) {
1291 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001292 struct server *sv;
1293
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001294 sv = expect_server_admin(s, si, args[2]);
1295 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001296 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001297
Cyril Bontécd19e512010-01-31 22:34:03 +01001298 if (! (sv->state & SRV_MAINTAIN)) {
1299 /* Not already in maintenance, we can change the server state */
1300 sv->state |= SRV_MAINTAIN;
1301 set_server_down(sv);
1302 }
1303
Willy Tarreau532a4502011-09-07 22:37:44 +02001304 return 1;
1305 }
1306 else if (strcmp(args[1], "frontend") == 0) {
1307 struct proxy *px;
1308
1309 px = expect_frontend_admin(s, si, args[2]);
1310 if (!px)
1311 return 1;
1312
1313 if (px->state == PR_STSTOPPED) {
1314 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1315 si->applet.st0 = STAT_CLI_PRINT;
1316 return 1;
1317 }
1318
1319 if (px->state == PR_STPAUSED) {
1320 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1321 si->applet.st0 = STAT_CLI_PRINT;
1322 return 1;
1323 }
1324
1325 if (!pause_proxy(px)) {
1326 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1327 si->applet.st0 = STAT_CLI_PRINT;
1328 return 1;
1329 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001330 return 1;
1331 }
1332 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001333 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1334 si->applet.st0 = STAT_CLI_PRINT;
1335 return 1;
1336 }
1337 }
1338 else if (strcmp(args[0], "shutdown") == 0) {
1339 if (strcmp(args[1], "frontend") == 0) {
1340 struct proxy *px;
1341
1342 px = expect_frontend_admin(s, si, args[2]);
1343 if (!px)
1344 return 1;
1345
1346 if (px->state == PR_STSTOPPED) {
1347 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1348 si->applet.st0 = STAT_CLI_PRINT;
1349 return 1;
1350 }
1351
1352 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1353 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1354 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1355 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1356 stop_proxy(px);
1357 return 1;
1358 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001359 else if (strcmp(args[1], "session") == 0) {
1360 struct session *sess, *ptr;
1361
Willy Tarreau290e63a2012-09-20 18:07:14 +02001362 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02001363 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1364 si->applet.st0 = STAT_CLI_PRINT;
1365 return 1;
1366 }
1367
1368 if (!*args[2]) {
1369 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1370 si->applet.st0 = STAT_CLI_PRINT;
1371 return 1;
1372 }
1373
1374 ptr = (void *)strtoul(args[2], NULL, 0);
1375
1376 /* first, look for the requested session in the session table */
1377 list_for_each_entry(sess, &sessions, list) {
1378 if (sess == ptr)
1379 break;
1380 }
1381
1382 /* do we have the session ? */
1383 if (sess != ptr) {
1384 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1385 si->applet.st0 = STAT_CLI_PRINT;
1386 return 1;
1387 }
1388
1389 session_shutdown(sess, SN_ERR_KILLED);
1390 return 1;
1391 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001392 else if (strcmp(args[1], "sessions") == 0) {
1393 if (strcmp(args[2], "server") == 0) {
1394 struct server *sv;
1395 struct session *sess, *sess_bck;
1396
1397 sv = expect_server_admin(s, si, args[3]);
1398 if (!sv)
1399 return 1;
1400
1401 /* kill all the session that are on this server */
1402 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1403 if (sess->srv_conn == sv)
1404 session_shutdown(sess, SN_ERR_KILLED);
1405
1406 return 1;
1407 }
1408 else {
1409 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1410 si->applet.st0 = STAT_CLI_PRINT;
1411 return 1;
1412 }
1413 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001414 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001415 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001416 si->applet.st0 = STAT_CLI_PRINT;
1417 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001418 }
1419 }
1420 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001421 return 0;
1422 }
1423 return 1;
1424}
1425
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001426/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001427 * used to processes I/O from/to the stats unix socket. The system relies on a
1428 * state machine handling requests and various responses. We read a request,
1429 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001430 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1431 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001432 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001433 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001434static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001435{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001436 struct channel *req = si->ob;
1437 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001438 int reql;
1439 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001440
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001441 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1442 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001443
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001444 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001445 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001446 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001447 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001448 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001449 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001450 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001451 /* Let's close for real now. We just close the request
1452 * side, the conditions below will complete if needed.
1453 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001454 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001455 break;
1456 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001457 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001458 /* ensure we have some output room left in the event we
1459 * would want to return some info right after parsing.
1460 */
Willy Tarreau572bf902012-07-02 17:01:20 +02001461 if (buffer_almost_full(&si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001462 break;
1463
David du Colombier7af46052012-05-16 14:16:48 +02001464 reql = bo_getline(si->ob, trash, trashlen);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001465 if (reql <= 0) { /* closed or EOL not found */
1466 if (reql == 0)
1467 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001468 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001469 continue;
1470 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001471
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001472 /* seek for a possible semi-colon. If we find one, we
1473 * replace it with an LF and skip only this part.
1474 */
1475 for (len = 0; len < reql; len++)
1476 if (trash[len] == ';') {
1477 trash[len] = '\n';
1478 reql = len + 1;
1479 break;
1480 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001481
Willy Tarreau816fc222009-10-04 07:36:58 +02001482 /* now it is time to check that we have a full line,
1483 * remove the trailing \n and possibly \r, then cut the
1484 * line.
1485 */
1486 len = reql - 1;
1487 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001488 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001489 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001490 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001491
Willy Tarreau816fc222009-10-04 07:36:58 +02001492 if (len && trash[len-1] == '\r')
1493 len--;
1494
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001495 trash[len] = '\0';
1496
Willy Tarreaubc4af052011-02-13 13:25:14 +01001497 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001498 if (len) {
1499 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001500 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001501 continue;
1502 }
1503 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001504 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001505 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001506 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001507 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001508 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001509 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001510 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001511 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001512 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001513 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001514 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001515 /* if prompt is disabled, print help on empty lines,
1516 * so that the user at least knows how to enable
1517 * prompt and find help.
1518 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001519 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001520 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001521 }
1522
1523 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001524 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001525 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001526 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001527 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001528 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001529 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001530 continue;
1531 }
1532
Willy Tarreaubc4af052011-02-13 13:25:14 +01001533 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001534 case STAT_CLI_PRINT:
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001535 if (bi_putstr(si->ib, si->applet.ctx.cli.msg) != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001536 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001537 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001538 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001539 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001540 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001541 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001542 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001543 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001544 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001545 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001546 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001547 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001548 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001549 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001550 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001551 case STAT_CLI_O_CLR:
Willy Tarreaudec98142012-06-06 23:37:08 +02001552 if (stats_table_request(si, si->applet.st0))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001553 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001554 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001555 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001556 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001557 break;
1558 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001559
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001560 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001561 if (si->applet.st0 == STAT_CLI_PROMPT) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001562 if (bi_putstr(si->ib, si->applet.st1 ? "\n> " : "\n") != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001563 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001564 }
1565
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001566 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001567 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001568 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001569
1570 /* Now we close the output if one of the writers did so,
1571 * or if we're not in interactive mode and the request
1572 * buffer is empty. This still allows pipelined requests
1573 * to be sent in non-interactive mode.
1574 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001575 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!si->applet.st1 && !req->buf.o)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001576 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001577 continue;
1578 }
1579
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001580 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001581 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001582 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001583 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001584
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001585 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001586 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1587 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001588 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001589 * and nothing more to consume. This is comparable to a broken pipe, so
1590 * we forward the close to the request side so that it flows upstream to
1591 * the client.
1592 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001593 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001594 }
1595
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001596 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001597 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1598 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1599 /* We have no more processing to do, and nothing more to send, and
1600 * the client side has closed. So we'll forward this state downstream
1601 * on the response buffer.
1602 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001603 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001604 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001605 }
1606
1607 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001608 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001609
1610 /* we don't want to expire timeouts while we're processing requests */
1611 si->ib->rex = TICK_ETERNITY;
1612 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001613
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001614 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001615 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rqh=%d, rqs=%d, rh=%d, rs=%d\n",
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001616 __FUNCTION__, __LINE__,
Cyril Bonté3aaba442012-09-23 14:19:12 +02001617 si->state, req->flags, res->flags, req->buf.i, req->buf.o, res->buf.i, res->buf.o);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001618
1619 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1620 /* check that we have released everything then unregister */
1621 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001622 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001623}
1624
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001625/* This function dumps statistics onto the stream interface's read buffer.
1626 * The data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001627 * It returns 0 as long as it does not complete, non-zero upon completion.
1628 * Some states are not used but it makes the code more similar to other
1629 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001630 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001631static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001632{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001633 struct proxy *px;
1634 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001635 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001636
David du Colombier7af46052012-05-16 14:16:48 +02001637 chunk_init(&msg, trash, trashlen);
Willy Tarreau3e76e722007-10-17 18:57:38 +02001638
Willy Tarreau94981132012-05-21 17:09:48 +02001639 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001640 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001641 /* the function had not been called yet */
Willy Tarreau94981132012-05-21 17:09:48 +02001642 si->conn.data_st = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001643 /* fall through */
1644
Willy Tarreau295a8372011-03-10 11:25:07 +01001645 case STAT_ST_HEAD:
1646 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001647 print_csv_header(&msg);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001648 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001649 return 0;
1650 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001651
Willy Tarreau94981132012-05-21 17:09:48 +02001652 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001653 /* fall through */
1654
Willy Tarreau295a8372011-03-10 11:25:07 +01001655 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001656 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001657 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001658 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001659 "Name: " PRODUCT_NAME "\n"
1660 "Version: " HAPROXY_VERSION "\n"
1661 "Release_date: " HAPROXY_DATE "\n"
1662 "Nbproc: %d\n"
1663 "Process_num: %d\n"
1664 "Pid: %d\n"
1665 "Uptime: %dd %dh%02dm%02ds\n"
1666 "Uptime_sec: %d\n"
1667 "Memmax_MB: %d\n"
1668 "Ulimit-n: %d\n"
1669 "Maxsock: %d\n"
1670 "Maxconn: %d\n"
Willy Tarreau91886b62011-09-07 14:38:31 +02001671 "Hard_maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001672 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001673 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001674 "PipesUsed: %d\n"
1675 "PipesFree: %d\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001676 "ConnRate: %d\n"
1677 "ConnRateLimit: %d\n"
1678 "MaxConnRate: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001679 "Tasks: %d\n"
1680 "Run_queue: %d\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02001681 "Idle_pct: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001682 "node: %s\n"
1683 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001684 "",
1685 global.nbproc,
1686 relative_pid,
1687 pid,
1688 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1689 up,
1690 global.rlimit_memmax,
1691 global.rlimit_nofile,
Willy Tarreau91886b62011-09-07 14:38:31 +02001692 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001693 actconn, pipes_used, pipes_free,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001694 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau45a12512011-09-10 16:56:42 +02001695 nb_tasks_cur, run_queue_cur, idle_pct,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001696 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001697 );
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001698 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001699 return 0;
1700 }
1701
Willy Tarreau295a8372011-03-10 11:25:07 +01001702 si->applet.ctx.stats.px = proxy;
1703 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1704 si->applet.ctx.stats.sv = NULL;
Willy Tarreau94981132012-05-21 17:09:48 +02001705 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001706 /* fall through */
1707
Willy Tarreau295a8372011-03-10 11:25:07 +01001708 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001709 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001710 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1711 while (si->applet.ctx.stats.px) {
1712 px = si->applet.ctx.stats.px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001713 /* skip the disabled proxies and non-networked ones */
1714 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001715 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001716 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001717 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001718 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001719
Willy Tarreau295a8372011-03-10 11:25:07 +01001720 si->applet.ctx.stats.px = px->next;
1721 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001722 }
1723 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001724 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001725
Willy Tarreau94981132012-05-21 17:09:48 +02001726 si->conn.data_st = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001727 /* fall through */
1728
Willy Tarreau295a8372011-03-10 11:25:07 +01001729 case STAT_ST_END:
Willy Tarreau94981132012-05-21 17:09:48 +02001730 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001731 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001732
Willy Tarreau295a8372011-03-10 11:25:07 +01001733 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001734 return 1;
1735
1736 default:
1737 /* unknown state ! */
Willy Tarreau94981132012-05-21 17:09:48 +02001738 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001739 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001740 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001741}
1742
1743
Cyril Bonté70be45d2010-10-12 00:14:35 +02001744/* We don't want to land on the posted stats page because a refresh will
1745 * repost the data. We don't want this to happen on accident so we redirect
1746 * the browse to the stats page with a GET.
1747 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001748static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001749{
Willy Tarreau94981132012-05-21 17:09:48 +02001750 struct session *s = si->conn.data_ctx;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001751 struct chunk msg;
1752
David du Colombier7af46052012-05-16 14:16:48 +02001753 chunk_init(&msg, trash, trashlen);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001754
Willy Tarreau94981132012-05-21 17:09:48 +02001755 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001756 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001757 chunk_printf(&msg,
1758 "HTTP/1.0 303 See Other\r\n"
1759 "Cache-Control: no-cache\r\n"
1760 "Content-Type: text/plain\r\n"
1761 "Connection: close\r\n"
1762 "Location: %s;st=%s",
Cyril Bonté19979e12012-04-04 12:57:21 +02001763 uri->uri_prefix,
1764 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
1765 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
1766 stat_status_codes[si->applet.ctx.stats.st_code]) ?
1767 stat_status_codes[si->applet.ctx.stats.st_code] :
1768 stat_status_codes[STAT_STATUS_UNKN]);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001769 chunk_printf(&msg, "\r\n\r\n");
1770
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001771 if (bi_putchk(si->ib, &msg) == -1)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001772 return 0;
1773
1774 s->txn.status = 303;
1775
1776 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1777 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1778 if (!(s->flags & SN_FINST_MASK))
1779 s->flags |= SN_FINST_R;
1780
Willy Tarreau94981132012-05-21 17:09:48 +02001781 si->conn.data_st = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001782 return 1;
1783 }
1784 return 1;
1785}
1786
1787
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001788/* This I/O handler runs as an applet embedded in a stream interface. It is
1789 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001790 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001791 * automatically unregisters itself once transfer is complete.
1792 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001793static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001794{
Willy Tarreau94981132012-05-21 17:09:48 +02001795 struct session *s = si->conn.data_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001796 struct channel *req = si->ob;
1797 struct channel *res = si->ib;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001798
1799 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1800 goto out;
1801
1802 /* check that the output is not closed */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001803 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001804 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001805
Willy Tarreaubc4af052011-02-13 13:25:14 +01001806 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001807 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001808 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001809 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001810 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001811 }
1812 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001813 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001814 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001815 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001816 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001817 }
1818 }
1819
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001820 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
Willy Tarreau73b013b2012-05-21 16:31:45 +02001821 si_shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001822
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001823 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001824 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001825 res->flags |= CF_READ_NULL;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001826 }
1827
1828 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001829 si_update(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001830
1831 /* we don't want to expire timeouts while we're processing requests */
1832 si->ib->rex = TICK_ETERNITY;
1833 si->ob->wex = TICK_ETERNITY;
1834
1835 out:
1836 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1837 /* check that we have released everything then unregister */
1838 stream_int_unregister_handler(si);
1839 }
1840}
1841
1842
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001843/* This function dumps statistics in HTTP format onto the stream interface's
1844 * read buffer. The data_ctx must have been zeroed first, and the flags
1845 * properly set. It returns 0 if it had to stop writing data and an I/O is
1846 * needed, 1 if the dump is finished and the session must be closed, or -1
1847 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001848 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001849static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001850{
Willy Tarreau94981132012-05-21 17:09:48 +02001851 struct session *s = si->conn.data_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001852 struct channel *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001853 struct proxy *px;
1854 struct chunk msg;
1855 unsigned int up;
1856
David du Colombier7af46052012-05-16 14:16:48 +02001857 chunk_init(&msg, trash, trashlen);
Willy Tarreau91861262007-10-17 17:06:05 +02001858
Willy Tarreau94981132012-05-21 17:09:48 +02001859 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001860 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001861 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001862 "HTTP/1.0 200 OK\r\n"
1863 "Cache-Control: no-cache\r\n"
1864 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001865 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001866 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001867
Willy Tarreau295a8372011-03-10 11:25:07 +01001868 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001869 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001870 uri->refresh);
1871
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001872 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001873
1874 s->txn.status = 200;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001875 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001876 return 0;
1877
Willy Tarreau91861262007-10-17 17:06:05 +02001878 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1879 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1880 if (!(s->flags & SN_FINST_MASK))
1881 s->flags |= SN_FINST_R;
1882
1883 if (s->txn.meth == HTTP_METH_HEAD) {
1884 /* that's all we return in case of HEAD request */
Willy Tarreau94981132012-05-21 17:09:48 +02001885 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001886 return 1;
1887 }
1888
Willy Tarreau94981132012-05-21 17:09:48 +02001889 si->conn.data_st = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001890 /* fall through */
1891
Willy Tarreau295a8372011-03-10 11:25:07 +01001892 case STAT_ST_HEAD:
1893 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001894 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001895 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001896 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1897 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001898 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001899 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1900 "<style type=\"text/css\"><!--\n"
1901 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001902 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001903 " font-size: 12px;"
1904 " font-weight: normal;"
1905 " color: black;"
1906 " background: white;"
1907 "}\n"
1908 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001909 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001910 "}\n"
1911 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001912 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001913 " margin-bottom: 0.5em;"
1914 "}\n"
1915 "h2 {"
1916 " font-family: helvetica, arial;"
1917 " font-size: x-large;"
1918 " font-weight: bold;"
1919 " font-style: italic;"
1920 " color: #6020a0;"
1921 " margin-top: 0em;"
1922 " margin-bottom: 0em;"
1923 "}\n"
1924 "h3 {"
1925 " font-family: helvetica, arial;"
1926 " font-size: 16px;"
1927 " font-weight: bold;"
1928 " color: #b00040;"
1929 " background: #e8e8d0;"
1930 " margin-top: 0em;"
1931 " margin-bottom: 0em;"
1932 "}\n"
1933 "li {"
1934 " margin-top: 0.25em;"
1935 " margin-right: 2em;"
1936 "}\n"
1937 ".hr {margin-top: 0.25em;"
1938 " border-color: black;"
1939 " border-bottom-style: solid;"
1940 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001941 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001942 ".total {background: #20D0D0;color: #ffff80;}\n"
1943 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001944 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001945 ".backend {background: #e8e8d0;}\n"
1946 ".active0 {background: #ff9090;}\n"
1947 ".active1 {background: #ffd020;}\n"
1948 ".active2 {background: #ffffa0;}\n"
1949 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001950 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1951 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1952 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001953 ".backup0 {background: #ff9090;}\n"
1954 ".backup1 {background: #ff80ff;}\n"
1955 ".backup2 {background: #c060ff;}\n"
1956 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001957 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1958 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1959 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001960 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001961 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001962 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001963 "a.px:link {color: #ffff40; text-decoration: none;}"
1964 "a.px:visited {color: #ffff40; text-decoration: none;}"
1965 "a.px:hover {color: #ffffff; text-decoration: none;}"
1966 "a.lfsb:link {color: #000000; text-decoration: none;}"
1967 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1968 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1969 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001970 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001971 "table.tbl td { text-align: right; border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray; white-space: nowrap;}\n"
1972 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001973 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001974 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001975 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001976 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001977 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001978 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1979 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1980 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001981 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001982 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001983 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001984 (uri->flags&ST_SHNODE) ? " on " : "",
1985 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001986 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001987 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001988 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001989 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001990 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02001991 return 0;
1992
Willy Tarreau94981132012-05-21 17:09:48 +02001993 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02001994 /* fall through */
1995
Willy Tarreau295a8372011-03-10 11:25:07 +01001996 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02001997 up = (now.tv_sec - start_date.tv_sec);
1998
1999 /* WARNING! this has to fit the first packet too.
2000 * We are around 3.5 kB, add adding entries will
2001 * become tricky if we want to support 4kB buffers !
2002 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002003 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002004 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002005 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2006 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002007 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002008 "<hr width=\"100%%\" class=\"hr\">\n"
2009 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002010 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01002011 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002012 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002013 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2014 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02002015 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02002016 "Running tasks: %d/%d; idle = %d %%<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002017 "</td><td align=\"center\" nowrap>\n"
2018 "<table class=\"lgd\"><tr>\n"
2019 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2020 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2021 "</tr><tr>\n"
2022 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
2023 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
2024 "</tr><tr>\n"
2025 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
2026 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2027 "</tr><tr>\n"
2028 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01002029 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01002030 "</tr><tr>\n"
2031 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002032 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01002033 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02002034 "</td>"
2035 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2036 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2037 "",
2038 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002039 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2040 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
2041 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02002042 up / 86400, (up % 86400) / 3600,
2043 (up % 3600) / 60, (up % 60),
2044 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2045 global.rlimit_memmax ? " MB" : "",
2046 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01002047 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02002048 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau45a12512011-09-10 16:56:42 +02002049 run_queue_cur, nb_tasks_cur, idle_pct
Willy Tarreau91861262007-10-17 17:06:05 +02002050 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002051
Willy Tarreau295a8372011-03-10 11:25:07 +01002052 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002053 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002054 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
2055 uri->uri_prefix,
2056 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01002057 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002058 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002059 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002060 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2061 uri->uri_prefix,
2062 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01002063 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002064
Willy Tarreau55bb8452007-10-17 18:44:57 +02002065 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002066 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002067 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002068 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
2069 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002070 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002071 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002072 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002073 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002074 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
2075 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002076 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002077 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002078 }
Willy Tarreau91861262007-10-17 17:06:05 +02002079
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002080 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002081 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
2082 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002083 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2084 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002085
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002086 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002087 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
2088 uri->uri_prefix,
2089 (uri->refresh > 0) ? ";norefresh" : "");
2090
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002091 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002092 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002093 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
Adrian Bridgettafdb6e52012-03-19 23:36:42 +00002094 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002095 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2096 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2097 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2098 "</ul>"
2099 "</td>"
2100 "</tr></table>\n"
2101 ""
2102 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002103
Willy Tarreau295a8372011-03-10 11:25:07 +01002104 if (si->applet.ctx.stats.st_code) {
Cyril Bonté19979e12012-04-04 12:57:21 +02002105 switch (si->applet.ctx.stats.st_code) {
2106 case STAT_STATUS_DONE:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002107 chunk_printf(&msg,
2108 "<p><div class=active3>"
2109 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2110 "Action processed successfully."
2111 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002112 break;
2113 case STAT_STATUS_NONE:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002114 chunk_printf(&msg,
2115 "<p><div class=active2>"
2116 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2117 "Nothing has changed."
2118 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002119 break;
2120 case STAT_STATUS_PART:
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002121 chunk_printf(&msg,
2122 "<p><div class=active2>"
2123 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2124 "Action partially processed.<br>"
2125 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2126 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002127 break;
2128 case STAT_STATUS_ERRP:
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002129 chunk_printf(&msg,
2130 "<p><div class=active0>"
2131 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2132 "Action not processed because of invalid parameters."
2133 "<ul>"
2134 "<li>The action is maybe unknown.</li>"
2135 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2136 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2137 "</ul>"
2138 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002139 break;
2140 case STAT_STATUS_EXCD:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002141 chunk_printf(&msg,
2142 "<p><div class=active0>"
2143 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2144 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2145 "You should retry with less servers at a time.</b>"
2146 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002147 break;
2148 case STAT_STATUS_DENY:
Cyril Bonté474be412010-10-12 00:14:36 +02002149 chunk_printf(&msg,
2150 "<p><div class=active0>"
2151 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2152 "<b>Action denied.</b>"
2153 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002154 break;
2155 default:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002156 chunk_printf(&msg,
2157 "<p><div class=active6>"
2158 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2159 "Unexpected result."
2160 "</div>\n", uri->uri_prefix);
2161 }
2162 chunk_printf(&msg,"<p>\n");
2163 }
2164
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002165 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002166 return 0;
2167 }
Willy Tarreau91861262007-10-17 17:06:05 +02002168
Willy Tarreau295a8372011-03-10 11:25:07 +01002169 si->applet.ctx.stats.px = proxy;
2170 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau94981132012-05-21 17:09:48 +02002171 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02002172 /* fall through */
2173
Willy Tarreau295a8372011-03-10 11:25:07 +01002174 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02002175 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01002176 while (si->applet.ctx.stats.px) {
Willy Tarreau572bf902012-07-02 17:01:20 +02002177 if (buffer_almost_full(&rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002178 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002179 px = si->applet.ctx.stats.px;
Willy Tarreau91861262007-10-17 17:06:05 +02002180 /* skip the disabled proxies and non-networked ones */
2181 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002182 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002183 return 0;
2184
Willy Tarreau295a8372011-03-10 11:25:07 +01002185 si->applet.ctx.stats.px = px->next;
2186 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02002187 }
2188 /* here, we just have reached the last proxy */
2189
Willy Tarreau94981132012-05-21 17:09:48 +02002190 si->conn.data_st = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002191 /* fall through */
2192
Willy Tarreau295a8372011-03-10 11:25:07 +01002193 case STAT_ST_END:
2194 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002195 chunk_printf(&msg, "</body></html>\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002196 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002197 return 0;
2198 }
Willy Tarreau91861262007-10-17 17:06:05 +02002199
Willy Tarreau94981132012-05-21 17:09:48 +02002200 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002201 /* fall through */
2202
Willy Tarreau295a8372011-03-10 11:25:07 +01002203 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002204 return 1;
2205
2206 default:
2207 /* unknown state ! */
Willy Tarreau94981132012-05-21 17:09:48 +02002208 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002209 return -1;
2210 }
2211}
2212
2213
2214/*
2215 * Dumps statistics for a proxy.
2216 * Returns 0 if it had to stop dumping data because of lack of buffer space,
2217 * ot non-zero if everything completed.
2218 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002219static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002220{
Willy Tarreau94981132012-05-21 17:09:48 +02002221 struct session *s = si->conn.data_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02002222 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002223 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002224 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002225 struct chunk msg;
2226
David du Colombier7af46052012-05-16 14:16:48 +02002227 chunk_init(&msg, trash, trashlen);
Willy Tarreau91861262007-10-17 17:06:05 +02002228
Willy Tarreau295a8372011-03-10 11:25:07 +01002229 switch (si->applet.ctx.stats.px_st) {
2230 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002231 /* we are on a new proxy */
2232
2233 if (uri && uri->scope) {
2234 /* we have a limited scope, we have to check the proxy name */
2235 struct stat_scope *scope;
2236 int len;
2237
2238 len = strlen(px->id);
2239 scope = uri->scope;
2240
2241 while (scope) {
2242 /* match exact proxy name */
2243 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2244 break;
2245
2246 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002247 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002248 break;
2249 scope = scope->next;
2250 }
2251
2252 /* proxy name not found : don't dump anything */
2253 if (scope == NULL)
2254 return 1;
2255 }
2256
Willy Tarreau295a8372011-03-10 11:25:07 +01002257 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
2258 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002259 return 1;
2260
Willy Tarreau295a8372011-03-10 11:25:07 +01002261 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002262 /* fall through */
2263
Willy Tarreau295a8372011-03-10 11:25:07 +01002264 case STAT_PX_ST_TH:
2265 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
2266 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002267 /* A form to enable/disable this proxy servers */
2268 chunk_printf(&msg,
2269 "<form action=\"%s\" method=\"post\">",
2270 uri->uri_prefix);
2271 }
2272
Willy Tarreau55bb8452007-10-17 18:44:57 +02002273 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002274 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002275 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002276 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002277 "<th class=\"pxname\" width=\"10%%\"");
2278
2279 if (uri->flags&ST_SHLGNDS) {
2280 /* cap, mode, id */
2281 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
2282 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2283 px->uuid);
2284
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002285 chunk_printf(&msg, "\"");
2286 }
2287
2288 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002289 ">%s<a name=\"%s\"></a>"
2290 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002291 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002292 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002293 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002294 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002295 "<tr class=\"titre\">",
2296 (uri->flags & ST_SHLGNDS)?"<u>":"",
2297 px->id, px->id, px->id,
2298 (uri->flags & ST_SHLGNDS)?"</u>":"",
2299 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2300
Willy Tarreau295a8372011-03-10 11:25:07 +01002301 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002302 /* Column heading for Enable or Disable server */
2303 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
2304 }
2305
2306 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002307 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002308 "<th colspan=3>Queue</th>"
2309 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002310 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002311 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002312 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002313 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002314 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002315 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002316 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002317 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002318 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002319 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002320 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002321 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2322 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002323 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002324
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002325 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002326 return 0;
2327 }
Willy Tarreau91861262007-10-17 17:06:05 +02002328
Willy Tarreau295a8372011-03-10 11:25:07 +01002329 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002330 /* fall through */
2331
Willy Tarreau295a8372011-03-10 11:25:07 +01002332 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002333 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002334 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002335 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
2336 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002337 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002338 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002339 "<tr class=\"frontend\">");
2340
Willy Tarreau295a8372011-03-10 11:25:07 +01002341 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002342 /* Column sub-heading for Enable or Disable server */
2343 chunk_printf(&msg, "<td></td>");
2344 }
2345
2346 chunk_printf(&msg,
2347 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002348 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002349 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2350 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002351 "",
2352 px->id, px->id);
2353
2354 if (px->mode == PR_MODE_HTTP) {
2355 chunk_printf(&msg,
2356 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01002357 "<td title=\"Cur: %u req/s\"><u>%s</u></td><td title=\"Max: %u req/s\"><u>%s</u></td><td>%s</td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002358 "",
2359 read_freq_ctr(&px->fe_req_per_sec),
2360 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002361 px->fe_counters.p.http.rps_max,
2362 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01002363 LIM2A2(px->fe_sps_lim, "-"));
2364 } else {
2365 chunk_printf(&msg,
2366 /* sessions rate : current, max, limit */
2367 "<td>%s</td><td>%s</td><td>%s</td>"
2368 "",
2369 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002370 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01002371 }
2372
2373 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002374 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002375 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002376 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002377 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002378 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002379
2380 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2381 if (px->mode == PR_MODE_HTTP) {
2382 int i;
2383
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002384 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002385
2386 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002387 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002388
Willy Tarreaueabea072011-09-10 23:29:44 +02002389 chunk_printf(&msg, " other=%lld,", px->fe_counters.p.http.rsp[0]);
2390 chunk_printf(&msg, " intercepted=%lld\"", px->fe_counters.intercepted_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002391 }
2392
2393 chunk_printf(&msg,
2394 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002395 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002396 /* bytes : in, out */
2397 "<td>%s</td><td>%s</td>"
2398 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002399 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002400 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002401 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002402 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002403
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002404 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002405 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002406 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002407 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002408 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002409 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002410 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002411 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002412 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002413 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002414 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002415 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002416 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2417 U2H2(px->fe_counters.failed_req),
Willy Tarreau562515c2011-07-25 08:11:52 +02002418 px->state == PR_STREADY ? "OPEN" :
2419 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002420 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002421 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002422 /* pxid, name, queue cur, queue max, */
2423 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002424 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002425 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002426 /* bytes : in, out */
2427 "%lld,%lld,"
2428 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002429 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002430 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002431 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002432 /* warnings: retries, redispatches */
2433 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002434 /* server status : reflect frontend status */
2435 "%s,"
2436 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002437 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002438 /* pid, iid, sid, throttle, lbtot, tracked, type */
2439 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002440 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002441 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002442 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002443 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002444 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002445 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2446 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2447 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2448 px->fe_counters.failed_req,
Willy Tarreau562515c2011-07-25 08:11:52 +02002449 px->state == PR_STREADY ? "OPEN" :
2450 px->state == PR_STFULL ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002451 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002452 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002453 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002454
2455 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2456 if (px->mode == PR_MODE_HTTP) {
2457 int i;
2458
2459 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002460 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002461
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002462 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002463 } else {
2464 chunk_printf(&msg, ",,,,,,");
2465 }
2466
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002467 /* failed health analyses */
2468 chunk_printf(&msg, ",");
2469
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002470 /* requests : req_rate, req_rate_max, req_tot, */
2471 chunk_printf(&msg, "%u,%u,%lld,",
2472 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002473 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002474
Willy Tarreauae526782010-03-04 20:34:23 +01002475 /* errors: cli_aborts, srv_aborts */
2476 chunk_printf(&msg, ",,");
2477
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002478 /* finish with EOL */
2479 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002480 }
Willy Tarreau91861262007-10-17 17:06:05 +02002481
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002482 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002483 return 0;
2484 }
2485
Willy Tarreau4348fad2012-09-20 16:48:07 +02002486 si->applet.ctx.stats.l = px->conf.listeners.n;
Willy Tarreau295a8372011-03-10 11:25:07 +01002487 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002488 /* fall through */
2489
Willy Tarreau295a8372011-03-10 11:25:07 +01002490 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002491 /* stats.l has been initialized above */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002492 for (; si->applet.ctx.stats.l != &px->conf.listeners; si->applet.ctx.stats.l = l->by_fe.n) {
Willy Tarreau572bf902012-07-02 17:01:20 +02002493 if (buffer_almost_full(&rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002494 return 0;
2495
Willy Tarreau4348fad2012-09-20 16:48:07 +02002496 l = LIST_ELEM(si->applet.ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002497 if (!l->counters)
2498 continue;
2499
Willy Tarreau295a8372011-03-10 11:25:07 +01002500 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2501 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002502 break;
2503
Willy Tarreau295a8372011-03-10 11:25:07 +01002504 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002505 continue;
2506 }
2507
Willy Tarreau295a8372011-03-10 11:25:07 +01002508 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002509 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002510 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002511 /* Column sub-heading for Enable or Disable server */
2512 chunk_printf(&msg, "<td></td>");
2513 }
2514 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002515
2516 if (uri->flags&ST_SHLGNDS) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02002517 char str[INET6_ADDRSTRLEN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002518 int port;
2519
Willy Tarreau631f01c2011-09-05 00:36:48 +02002520 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002521
Willy Tarreau631f01c2011-09-05 00:36:48 +02002522 port = get_host_port(&l->addr);
2523 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2524 case AF_INET:
2525 chunk_printf(&msg, "IPv4: %s:%d, ", str, port);
2526 break;
2527 case AF_INET6:
2528 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, port);
2529 break;
2530 case AF_UNIX:
2531 chunk_printf(&msg, "unix, ");
2532 break;
2533 case -1:
2534 chunk_printf(&msg, "(%s), ", strerror(errno));
2535 break;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002536 }
2537
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002538 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002539 chunk_printf(&msg, "id: %d\"", l->luid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002540 }
2541
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002542 chunk_printf(&msg,
2543 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002544 ">%s<a name=\"%s/+%s\"></a>"
2545 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002546 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002547 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002548 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002549 "<td>%s</td><td>%s</td><td>%s</td>"
2550 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002551 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002552 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002553 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002554 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002555 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002556 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002557 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2558 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2559
2560 chunk_printf(&msg,
2561 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002562 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002563 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002564 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002565 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002566 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002567 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002568 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002569 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002570 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002571 "",
2572 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2573 U2H2(l->counters->failed_req),
Willy Tarreaua17c2d92011-07-25 08:16:20 +02002574 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002575 } else {
2576 chunk_printf(&msg,
2577 /* pxid, name, queue cur, queue max, */
2578 "%s,%s,,,"
2579 /* sessions: current, max, limit, total */
2580 "%d,%d,%d,%lld,"
2581 /* bytes: in, out */
2582 "%lld,%lld,"
2583 /* denied: req, resp */
2584 "%lld,%lld,"
2585 /* errors: request, connect, response */
2586 "%lld,,,"
2587 /* warnings: retries, redispatches */
2588 ",,"
2589 /* server status: reflect listener status */
2590 "%s,"
2591 /* rest of server: nothing */
2592 ",,,,,,,,"
2593 /* pid, iid, sid, throttle, lbtot, tracked, type */
2594 "%d,%d,%d,,,,%d,"
2595 /* rate, rate_lim, rate_max */
2596 ",,,"
2597 /* check_status, check_code, check_duration */
2598 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002599 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2600 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002601 /* failed health analyses */
2602 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002603 /* requests : req_rate, req_rate_max, req_tot, */
2604 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002605 /* errors: cli_aborts, srv_aborts */
2606 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002607 "\n",
2608 px->id, l->name,
2609 l->nbconn, l->counters->conn_max,
2610 l->maxconn, l->counters->cum_conn,
2611 l->counters->bytes_in, l->counters->bytes_out,
2612 l->counters->denied_req, l->counters->denied_resp,
2613 l->counters->failed_req,
2614 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2615 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2616 }
2617
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002618 if (bi_putchk(rep, &msg) == -1)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002619 return 0;
2620 }
2621
Willy Tarreau295a8372011-03-10 11:25:07 +01002622 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2623 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002624 /* fall through */
2625
Willy Tarreau295a8372011-03-10 11:25:07 +01002626 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002627 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002628 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002629 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 +02002630
Willy Tarreau572bf902012-07-02 17:01:20 +02002631 if (buffer_almost_full(&rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002632 return 0;
2633
Willy Tarreau295a8372011-03-10 11:25:07 +01002634 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002635
Willy Tarreau295a8372011-03-10 11:25:07 +01002636 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2637 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002638 break;
2639
Willy Tarreau295a8372011-03-10 11:25:07 +01002640 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002641 continue;
2642 }
2643
Willy Tarreau44267702011-10-28 15:35:33 +02002644 if (sv->track)
2645 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002646 else
2647 svs = sv;
2648
Willy Tarreau91861262007-10-17 17:06:05 +02002649 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002650 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002651 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002652 else if (svs->state & SRV_RUNNING) {
2653 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002654 sv_state = 3; /* UP */
2655 else
2656 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002657
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002658 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002659 sv_state += 2;
2660 }
Willy Tarreau91861262007-10-17 17:06:05 +02002661 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002662 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002663 sv_state = 1; /* going up */
2664 else
2665 sv_state = 0; /* DOWN */
2666
Willy Tarreau295a8372011-03-10 11:25:07 +01002667 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002668 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002669 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002670 continue;
2671 }
2672
Willy Tarreau295a8372011-03-10 11:25:07 +01002673 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002674 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2675 "UP %d/%d &darr;", "UP",
2676 "NOLB %d/%d &darr;", "NOLB",
2677 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002678 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002679 chunk_printf(&msg,
2680 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002681 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002682 );
2683 }
2684 else {
2685 chunk_printf(&msg,
2686 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002687 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002688 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2689 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002690
Willy Tarreau295a8372011-03-10 11:25:07 +01002691 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002692 chunk_printf(&msg,
2693 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2694 sv->id);
2695 }
2696
2697 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002698
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002699 if (uri->flags&ST_SHLGNDS) {
2700 char str[INET6_ADDRSTRLEN];
2701
Willy Tarreau631f01c2011-09-05 00:36:48 +02002702 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002703
Willy Tarreau631f01c2011-09-05 00:36:48 +02002704 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01002705 case AF_INET:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002706 chunk_printf(&msg, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
David du Colombier6f5ccb12011-03-10 22:26:24 +01002707 break;
2708 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002709 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2710 break;
2711 case AF_UNIX:
2712 chunk_printf(&msg, "unix, ");
2713 break;
2714 case -1:
2715 chunk_printf(&msg, "(%s), ", strerror(errno));
2716 break;
2717 default: /* address family not supported */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002718 break;
2719 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002720
2721 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002722 chunk_printf(&msg, "id: %d", sv->puid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002723
2724 /* cookie */
2725 if (sv->cookie) {
2726 struct chunk src;
2727
2728 chunk_printf(&msg, ", cookie: '");
2729
2730 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2731 chunk_htmlencode(&msg, &src);
2732
2733 chunk_printf(&msg, "'");
2734 }
2735
2736 chunk_printf(&msg, "\"");
2737 }
2738
2739 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002740 ">%s<a name=\"%s/%s\"></a>"
2741 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002742 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002743 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002744 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002745 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002746 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002747 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002748 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002749 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002750 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002751 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002752 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002753 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2754 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002755 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2756
2757 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2758 if (px->mode == PR_MODE_HTTP) {
2759 int i;
2760
2761 chunk_printf(&msg, " title=\"rsp codes:");
2762
2763 for (i = 1; i < 6; i++)
2764 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2765
2766 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2767 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002768
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002769 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002770 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002771 ">%s%s%s</td><td>%s</td>",
2772 (px->mode == PR_MODE_HTTP)?"<u>":"",
2773 U2H0(sv->counters.cum_sess),
2774 (px->mode == PR_MODE_HTTP)?"</u>":"",
2775 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002776
2777 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002778 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002779 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002780 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002781 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002782 /* errors : request, connect */
2783 "<td></td><td>%s</td>"
2784 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002785 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002786 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002787 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002788 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002789 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2790 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002791 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002792 sv->counters.cli_aborts,
2793 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002794 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002795 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002796
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002797 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002798 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002799
Cyril Bontécd19e512010-01-31 22:34:03 +01002800 if (sv->state & SRV_MAINTAIN) {
2801 chunk_printf(&msg, "%s ",
2802 human_time(now.tv_sec - sv->last_change, 1));
2803 chunk_printf(&msg, "MAINT");
2804 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002805 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2806 chunk_printf(&msg, "%s ",
2807 human_time(now.tv_sec - svs->last_change, 1));
2808 chunk_printf(&msg, "MAINT(via)");
2809 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002810 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002811 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002812 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002813
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002814 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002815 srv_hlt_st[sv_state],
2816 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2817 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002818 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002819
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002820 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002821 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002822 get_check_status_description(sv->check_status));
2823
2824 if (*sv->check_desc) {
2825 struct chunk src;
2826
2827 chunk_printf(&msg, ": ");
2828
2829 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2830 chunk_htmlencode(&msg, &src);
2831 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002832
Willy Tarreaue0454092010-02-26 12:29:07 +01002833 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002834 tv_iszero(&sv->check_start)?"":"* ",
2835 get_check_status_info(sv->check_status));
2836
2837 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002838 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002839
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002840 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002841 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002842 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002843 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002844
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002845 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002846 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002847 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002848 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002849 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002850 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002851 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002852 (sv->state & SRV_BACKUP) ? "-" : "Y",
2853 (sv->state & SRV_BACKUP) ? "Y" : "-");
2854
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002855 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002856 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002857 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002858 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2859
2860 if (svs->observe)
2861 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2862
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002863 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002864 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002865 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002866 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002867 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002868 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002869 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002870 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2871 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002872 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002873 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002874 "<td colspan=3></td>");
2875
2876 /* throttle */
2877 if ((sv->state & SRV_WARMINGUP) &&
2878 now.tv_sec < sv->last_change + sv->slowstart &&
2879 now.tv_sec >= sv->last_change) {
2880 unsigned int ratio;
2881 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002882 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002883 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002884 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002885 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002886 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002887 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002888 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002889 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2890 "UP %d/%d,", "UP,",
2891 "NOLB %d/%d,", "NOLB,",
2892 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002893 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002894 /* pxid, name */
2895 "%s,%s,"
2896 /* queue : current, max */
2897 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002898 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002899 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002900 /* bytes : in, out */
2901 "%lld,%lld,"
2902 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002903 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002904 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002905 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002906 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002907 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002908 "",
2909 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002910 sv->nbpend, sv->counters.nbpend_max,
2911 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002912 sv->counters.bytes_in, sv->counters.bytes_out,
2913 sv->counters.failed_secu,
2914 sv->counters.failed_conns, sv->counters.failed_resp,
2915 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002916
Willy Tarreau55bb8452007-10-17 18:44:57 +02002917 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002918 if (sv->state & SRV_MAINTAIN) {
2919 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002920 }
2921 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2922 chunk_printf(&msg, "MAINT(via),");
2923 }
2924 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002925 chunk_printf(&msg,
2926 srv_hlt_st[sv_state],
2927 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2928 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2929 }
Willy Tarreau91861262007-10-17 17:06:05 +02002930
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002931 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002932 /* weight, active, backup */
2933 "%d,%d,%d,"
2934 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002935 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002936 (sv->state & SRV_BACKUP) ? 0 : 1,
2937 (sv->state & SRV_BACKUP) ? 1 : 0);
2938
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002939 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002940 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002941 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002942 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002943 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002944 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002945 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002946 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002947 ",,,,");
2948
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002949 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002950 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002951 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002952 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002953 LIM2A0(sv->maxqueue, ""),
2954 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002955
2956 /* throttle */
2957 if ((sv->state & SRV_WARMINGUP) &&
2958 now.tv_sec < sv->last_change + sv->slowstart &&
2959 now.tv_sec >= sv->last_change) {
2960 unsigned int ratio;
2961 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002962 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002963 }
2964
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002965 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002966 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002967
2968 /* tracked */
Willy Tarreau44267702011-10-28 15:35:33 +02002969 if (sv->track)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002970 chunk_printf(&msg, "%s/%s,",
Willy Tarreau44267702011-10-28 15:35:33 +02002971 sv->track->proxy->id, sv->track->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002972 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002973 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002974
Willy Tarreau7f062c42009-03-05 18:43:00 +01002975 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002976 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002977
2978 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002979 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002980 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002981 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002982
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002983 if (sv->state & SRV_CHECKED) {
2984 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002985 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002986
2987 /* check_code */
2988 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002989 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002990 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002991 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002992
2993 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002994 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002995 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002996 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002997 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002998
2999 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003000 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003001 }
3002
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003003 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3004 if (px->mode == PR_MODE_HTTP) {
3005 int i;
3006
3007 for (i=1; i<6; i++)
3008 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
3009
3010 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
3011 } else {
3012 chunk_printf(&msg, ",,,,,,");
3013 }
3014
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003015 /* failed health analyses */
3016 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
3017
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003018 /* requests : req_rate, req_rate_max, req_tot, */
3019 chunk_printf(&msg, ",,,");
3020
Willy Tarreauae526782010-03-04 20:34:23 +01003021 /* errors: cli_aborts, srv_aborts */
3022 chunk_printf(&msg, "%lld,%lld,",
3023 sv->counters.cli_aborts, sv->counters.srv_aborts);
3024
Willy Tarreau7f062c42009-03-05 18:43:00 +01003025 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003026 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003027 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003028 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003029 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003030 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02003031
Willy Tarreau295a8372011-03-10 11:25:07 +01003032 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02003033 /* fall through */
3034
Willy Tarreau295a8372011-03-10 11:25:07 +01003035 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02003036 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003037 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01003038 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
3039 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003040 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01003041 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003042 /* Column sub-heading for Enable or Disable server */
3043 chunk_printf(&msg, "<td></td>");
3044 }
3045 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003046
3047 if (uri->flags&ST_SHLGNDS) {
3048 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003049 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003050 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003051
3052 /* cookie */
3053 if (px->cookie_name) {
3054 struct chunk src;
3055
3056 chunk_printf(&msg, ", cookie: '");
3057
3058 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3059 chunk_htmlencode(&msg, &src);
3060
3061 chunk_printf(&msg, "'");
3062 }
3063
3064 chunk_printf(&msg, "\"");
3065
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003066 }
3067
3068 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02003069 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01003070 ">%s<a name=\"%s/Backend\"></a>"
3071 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02003072 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003073 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003074 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003075 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003076 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003077 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02003078 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01003079 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003080 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
3081 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02003082
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003083 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003084 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003085 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003086 "<td"
3087 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003088 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003089
3090 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3091 if (px->mode == PR_MODE_HTTP) {
3092 int i;
3093
3094 chunk_printf(&msg, " title=\"rsp codes:");
3095
3096 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003097 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003098
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003099 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003100 }
3101
3102 chunk_printf(&msg,
3103 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01003104 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003105 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003106 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003107 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003108 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003109 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01003110 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003111 U2H7(px->be_counters.cum_lbconn),
3112 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003113
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003114 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02003115 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003116 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01003117 /* errors : request, connect */
3118 "<td></td><td>%s</td>"
3119 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01003120 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003121 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003122 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003123 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02003124 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003125 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02003126 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003127 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3128 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003129 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003130 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
3131 U2H2(px->be_counters.failed_conns),
3132 px->be_counters.cli_aborts,
3133 px->be_counters.srv_aborts,
3134 U2H5(px->be_counters.failed_resp),
3135 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003136 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01003137 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3138 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01003139 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003140 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003141
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003142 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003143 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003144 "<td class=ac>&nbsp;</td><td>%d</td>"
3145 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003146 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003147 "</tr>",
3148 px->down_trans,
3149 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003150 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003151 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02003152 /* pxid, name */
3153 "%s,BACKEND,"
3154 /* queue : current, max */
3155 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003156 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003157 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003158 /* bytes : in, out */
3159 "%lld,%lld,"
3160 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003161 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003162 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003163 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003164 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003165 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003166 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02003167 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003168 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02003169 * active and backups. */
3170 "%s,"
3171 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003172 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003173 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003174 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003175 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003176 /* rate, rate_lim, rate_max, */
3177 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003178 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003179 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02003180 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003181 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3182 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3183 px->be_counters.bytes_in, px->be_counters.bytes_out,
3184 px->be_counters.denied_req, px->be_counters.denied_resp,
3185 px->be_counters.failed_conns, px->be_counters.failed_resp,
3186 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01003187 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01003188 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003189 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003190 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01003191 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003192 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003193 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003194 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003195 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003196
3197 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3198 if (px->mode == PR_MODE_HTTP) {
3199 int i;
3200
3201 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003202 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003203
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003204 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003205 } else {
3206 chunk_printf(&msg, ",,,,,,");
3207 }
3208
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003209 /* failed health analyses */
3210 chunk_printf(&msg, ",");
3211
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003212 /* requests : req_rate, req_rate_max, req_tot, */
3213 chunk_printf(&msg, ",,,");
3214
Willy Tarreauae526782010-03-04 20:34:23 +01003215 /* errors: cli_aborts, srv_aborts */
3216 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003217 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01003218
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003219 /* finish with EOL */
3220 chunk_printf(&msg, "\n");
3221
Willy Tarreau55bb8452007-10-17 18:44:57 +02003222 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003223 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003224 return 0;
3225 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003226
Willy Tarreau295a8372011-03-10 11:25:07 +01003227 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02003228 /* fall through */
3229
Willy Tarreau295a8372011-03-10 11:25:07 +01003230 case STAT_PX_ST_END:
3231 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003232 chunk_printf(&msg, "</table>");
3233
Willy Tarreau295a8372011-03-10 11:25:07 +01003234 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003235 /* close the form used to enable/disable this proxy servers */
3236 chunk_printf(&msg,
3237 "Choose the action to perform on the checked servers : "
3238 "<select name=action>"
3239 "<option value=\"\"></option>"
3240 "<option value=\"disable\">Disable</option>"
3241 "<option value=\"enable\">Enable</option>"
Willy Tarreaud7282242012-06-04 00:22:44 +02003242 "<option value=\"stop\">Soft Stop</option>"
3243 "<option value=\"start\">Soft Start</option>"
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003244 "<option value=\"shutdown\">Kill Sessions</option>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003245 "</select>"
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003246 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003247 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3248 "</form>",
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003249 px->uuid);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003250 }
3251
3252 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02003253
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003254 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003255 return 0;
3256 }
Willy Tarreau91861262007-10-17 17:06:05 +02003257
Willy Tarreau295a8372011-03-10 11:25:07 +01003258 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02003259 /* fall through */
3260
Willy Tarreau295a8372011-03-10 11:25:07 +01003261 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02003262 return 1;
3263
3264 default:
3265 /* unknown state, we should put an abort() here ! */
3266 return 1;
3267 }
3268}
3269
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003270/* This function dumps a complete session state onto the stream intreface's
3271 * read buffer. The data_ctx must have been zeroed first, and the flags
3272 * properly set. The session has to be set in data_ctx.sess.target. It returns
3273 * 0 if the output buffer is full and it needs to be called again, otherwise
3274 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003275 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003276static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003277{
3278 struct tm tm;
3279 struct chunk msg;
3280 struct session *sess;
3281 extern const char *monthname[12];
3282 char pn[INET6_ADDRSTRLEN];
3283
David du Colombier7af46052012-05-16 14:16:48 +02003284 chunk_init(&msg, trash, trashlen);
Willy Tarreau295a8372011-03-10 11:25:07 +01003285 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003286
Willy Tarreau295a8372011-03-10 11:25:07 +01003287 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003288 /* session changed, no need to go any further */
3289 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003290 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003291 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003292 si->applet.ctx.sess.target = NULL;
3293 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003294 return 1;
3295 }
3296
Willy Tarreau295a8372011-03-10 11:25:07 +01003297 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003298 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003299 si->applet.ctx.sess.uid = sess->uniq_id;
3300 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003301 /* fall through */
3302
3303 case 1:
3304 chunk_printf(&msg,
3305 "%p: id=%u, proto=%s",
3306 sess,
3307 sess->uniq_id,
3308 sess->listener->proto->name);
3309
Willy Tarreau986a9d22012-08-30 21:11:38 +02003310 switch (addr_to_str(&sess->si[0].conn.addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003311 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003312 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003313 chunk_printf(&msg, " source=%s:%d\n",
Willy Tarreau986a9d22012-08-30 21:11:38 +02003314 pn, get_host_port(&sess->si[0].conn.addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003315 break;
3316 case AF_UNIX:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003317 chunk_printf(&msg, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003318 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003319 default:
3320 /* no more information to print right now */
3321 chunk_printf(&msg, "\n");
3322 break;
3323 }
3324
3325 chunk_printf(&msg,
3326 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003327 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003328
3329 chunk_printf(&msg,
Daniel Schultze90690c72012-03-23 10:53:36 -07003330 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003331 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3332 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3333 sess->listener ? sess->listener->luid : 0);
3334
Willy Tarreau986a9d22012-08-30 21:11:38 +02003335 conn_get_to_addr(&sess->si[0].conn);
3336 switch (addr_to_str(&sess->si[0].conn.addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003337 case AF_INET:
3338 case AF_INET6:
3339 chunk_printf(&msg, " addr=%s:%d\n",
Willy Tarreau986a9d22012-08-30 21:11:38 +02003340 pn, get_host_port(&sess->si[0].conn.addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003341 break;
3342 case AF_UNIX:
3343 chunk_printf(&msg, " addr=unix:%d\n", sess->listener->luid);
3344 break;
3345 default:
3346 /* no more information to print right now */
3347 chunk_printf(&msg, "\n");
3348 break;
3349 }
3350
Willy Tarreau50943332011-09-02 17:33:05 +02003351 if (sess->be->cap & PR_CAP_BE)
3352 chunk_printf(&msg,
Daniel Schultze90690c72012-03-23 10:53:36 -07003353 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003354 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003355 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3356 else
3357 chunk_printf(&msg, " backend=<NONE> (id=-1 mode=-)");
3358
Willy Tarreau986a9d22012-08-30 21:11:38 +02003359 conn_get_from_addr(&sess->si[1].conn);
3360 switch (addr_to_str(&sess->si[1].conn.addr.from, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003361 case AF_INET:
3362 case AF_INET6:
3363 chunk_printf(&msg, " addr=%s:%d\n",
Willy Tarreau986a9d22012-08-30 21:11:38 +02003364 pn, get_host_port(&sess->si[1].conn.addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07003365 break;
3366 case AF_UNIX:
3367 chunk_printf(&msg, " addr=unix\n");
3368 break;
3369 default:
3370 /* no more information to print right now */
3371 chunk_printf(&msg, "\n");
3372 break;
3373 }
3374
3375 if (sess->be->cap & PR_CAP_BE)
3376 chunk_printf(&msg,
3377 " server=%s (id=%u)",
Willy Tarreau50943332011-09-02 17:33:05 +02003378 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
3379 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
3380 else
Daniel Schultze90690c72012-03-23 10:53:36 -07003381 chunk_printf(&msg, " server=<NONE> (id=-1)");
3382
Willy Tarreau986a9d22012-08-30 21:11:38 +02003383 conn_get_to_addr(&sess->si[1].conn);
3384 switch (addr_to_str(&sess->si[1].conn.addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003385 case AF_INET:
3386 case AF_INET6:
3387 chunk_printf(&msg, " addr=%s:%d\n",
Willy Tarreau986a9d22012-08-30 21:11:38 +02003388 pn, get_host_port(&sess->si[1].conn.addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003389 break;
3390 case AF_UNIX:
3391 chunk_printf(&msg, " addr=unix\n");
3392 break;
3393 default:
3394 /* no more information to print right now */
3395 chunk_printf(&msg, "\n");
3396 break;
3397 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003398
3399 chunk_printf(&msg,
3400 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
3401 sess->task,
3402 sess->task->state,
3403 sess->task->nice, sess->task->calls,
3404 sess->task->expire ?
3405 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3406 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3407 TICKS_TO_MS(1000)) : "<NEVER>",
3408 task_in_rq(sess->task) ? ", running" : "");
3409
3410 get_localtime(sess->logs.accept_date.tv_sec, &tm);
3411 chunk_printf(&msg,
3412 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
3413 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3414 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3415 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3416
3417 chunk_printf(&msg,
3418 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3419 &sess->si[0],
3420 sess->si[0].state,
3421 sess->si[0].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003422 si_fd(&sess->si[0]),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003423 sess->si[0].exp ?
3424 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3425 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3426 TICKS_TO_MS(1000)) : "<NEVER>",
3427 sess->si[0].err_type);
3428
3429 chunk_printf(&msg,
3430 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3431 &sess->si[1],
3432 sess->si[1].state,
3433 sess->si[1].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003434 si_fd(&sess->si[1]),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003435 sess->si[1].exp ?
3436 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3437 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3438 TICKS_TO_MS(1000)) : "<NEVER>",
3439 sess->si[1].err_type);
3440
3441 chunk_printf(&msg,
3442 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3443 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3444 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3445
3446
3447 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003448 " req=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003449 " an_exp=%s",
3450 sess->req,
3451 sess->req->flags, sess->req->analysers,
Willy Tarreau572bf902012-07-02 17:01:20 +02003452 sess->req->buf.i, sess->req->buf.o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003453 sess->req->pipe ? sess->req->pipe->data : 0,
3454 sess->req->to_forward,
3455 sess->req->analyse_exp ?
3456 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3457 TICKS_TO_MS(1000)) : "<NEVER>");
3458
3459 chunk_printf(&msg,
3460 " rex=%s",
3461 sess->req->rex ?
3462 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3463 TICKS_TO_MS(1000)) : "<NEVER>");
3464
3465 chunk_printf(&msg,
3466 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003467 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003468 sess->req->wex ?
3469 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3470 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau572bf902012-07-02 17:01:20 +02003471 sess->req->buf.data,
3472 (int)(sess->req->buf.p - sess->req->buf.data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003473 sess->txn.req.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003474 sess->req->total);
3475
3476 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003477 " res=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003478 " an_exp=%s",
3479 sess->rep,
3480 sess->rep->flags, sess->rep->analysers,
Willy Tarreau572bf902012-07-02 17:01:20 +02003481 sess->rep->buf.i, sess->rep->buf.o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003482 sess->rep->pipe ? sess->rep->pipe->data : 0,
3483 sess->rep->to_forward,
3484 sess->rep->analyse_exp ?
3485 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3486 TICKS_TO_MS(1000)) : "<NEVER>");
3487
3488 chunk_printf(&msg,
3489 " rex=%s",
3490 sess->rep->rex ?
3491 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3492 TICKS_TO_MS(1000)) : "<NEVER>");
3493
3494 chunk_printf(&msg,
3495 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003496 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003497 sess->rep->wex ?
3498 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3499 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau572bf902012-07-02 17:01:20 +02003500 sess->rep->buf.data,
3501 (int)(sess->rep->buf.p - sess->rep->buf.data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003502 sess->txn.rsp.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003503 sess->rep->total);
3504
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003505 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003506 return 0;
3507
3508 /* use other states to dump the contents */
3509 }
3510 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003511 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003512 return 1;
3513}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003514
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003515/* This function dumps all sessions' states onto the stream intreface's
3516 * read buffer. The data_ctx must have been zeroed first, and the flags
3517 * properly set. It returns 0 if the output buffer is full and it needs
3518 * to be called again, otherwise non-zero. It is designed to be called
3519 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003520 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003521static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003522{
3523 struct chunk msg;
3524
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003525 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003526 /* If we're forced to shut down, we might have to remove our
3527 * reference to the last session being dumped.
3528 */
Willy Tarreau94981132012-05-21 17:09:48 +02003529 if (si->conn.data_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003530 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3531 LIST_DEL(&si->applet.ctx.sess.bref.users);
3532 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003533 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003534 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003535 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003536 }
3537
David du Colombier7af46052012-05-16 14:16:48 +02003538 chunk_init(&msg, trash, trashlen);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003539
Willy Tarreau94981132012-05-21 17:09:48 +02003540 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003541 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003542 /* the function had not been called yet, let's prepare the
3543 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003544 * pointer to the first in the global list. When a target
3545 * session is being destroyed, it is responsible for updating
3546 * this pointer. We know we have reached the end when this
3547 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003548 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003549 LIST_INIT(&si->applet.ctx.sess.bref.users);
3550 si->applet.ctx.sess.bref.ref = sessions.n;
Willy Tarreau94981132012-05-21 17:09:48 +02003551 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003552 /* fall through */
3553
Willy Tarreau295a8372011-03-10 11:25:07 +01003554 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003555 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003556 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3557 LIST_DEL(&si->applet.ctx.sess.bref.users);
3558 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003559 }
3560
3561 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003562 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003563 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003564 struct session *curr_sess;
3565
Willy Tarreau295a8372011-03-10 11:25:07 +01003566 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003567
Willy Tarreau295a8372011-03-10 11:25:07 +01003568 if (si->applet.ctx.sess.target) {
3569 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003570 goto next_sess;
3571
Willy Tarreau295a8372011-03-10 11:25:07 +01003572 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003573 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003574 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003575 return 0;
3576
3577 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003578 LIST_DEL(&si->applet.ctx.sess.bref.users);
3579 LIST_INIT(&si->applet.ctx.sess.bref.users);
3580 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003581 break;
3582 }
3583
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003584 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003585 "%p: proto=%s",
3586 curr_sess,
3587 curr_sess->listener->proto->name);
3588
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003589
Willy Tarreau986a9d22012-08-30 21:11:38 +02003590 switch (addr_to_str(&curr_sess->si[0].conn.addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003591 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003592 case AF_INET6:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003593 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003594 " src=%s:%d fe=%s be=%s srv=%s",
3595 pn,
Willy Tarreau986a9d22012-08-30 21:11:38 +02003596 get_host_port(&curr_sess->si[0].conn.addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003597 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003598 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003599 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003600 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003601 break;
3602 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02003603 chunk_printf(&msg,
3604 " src=unix:%d fe=%s be=%s srv=%s",
3605 curr_sess->listener->luid,
3606 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003607 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003608 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003609 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003610 break;
3611 }
3612
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003613 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003614 " ts=%02x age=%s calls=%d",
3615 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003616 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3617 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003618
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003619 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003620 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003621 curr_sess->req->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02003622 curr_sess->req->buf.i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003623 curr_sess->req->analysers,
3624 curr_sess->req->rex ?
3625 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3626 TICKS_TO_MS(1000)) : "");
3627
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003628 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003629 ",wx=%s",
3630 curr_sess->req->wex ?
3631 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3632 TICKS_TO_MS(1000)) : "");
3633
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003634 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003635 ",ax=%s]",
3636 curr_sess->req->analyse_exp ?
3637 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3638 TICKS_TO_MS(1000)) : "");
3639
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003640 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003641 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003642 curr_sess->rep->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02003643 curr_sess->rep->buf.i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003644 curr_sess->rep->analysers,
3645 curr_sess->rep->rex ?
3646 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3647 TICKS_TO_MS(1000)) : "");
3648
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003649 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003650 ",wx=%s",
3651 curr_sess->rep->wex ?
3652 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3653 TICKS_TO_MS(1000)) : "");
3654
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003655 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003656 ",ax=%s]",
3657 curr_sess->rep->analyse_exp ?
3658 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3659 TICKS_TO_MS(1000)) : "");
3660
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003661 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003662 " s0=[%d,%1xh,fd=%d,ex=%s]",
3663 curr_sess->si[0].state,
3664 curr_sess->si[0].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003665 si_fd(&curr_sess->si[0]),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003666 curr_sess->si[0].exp ?
3667 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3668 TICKS_TO_MS(1000)) : "");
3669
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003670 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003671 " s1=[%d,%1xh,fd=%d,ex=%s]",
3672 curr_sess->si[1].state,
3673 curr_sess->si[1].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003674 si_fd(&curr_sess->si[1]),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003675 curr_sess->si[1].exp ?
3676 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3677 TICKS_TO_MS(1000)) : "");
3678
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003679 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003680 " exp=%s",
3681 curr_sess->task->expire ?
3682 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3683 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003684 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003685 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003686
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003687 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003688
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003689 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003690 /* let's try again later from this session. We add ourselves into
3691 * this session's users so that it can remove us upon termination.
3692 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003693 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003694 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003695 }
3696
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003697 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003698 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003699 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003700
Willy Tarreau295a8372011-03-10 11:25:07 +01003701 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003702 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003703 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003704 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3705 else
3706 chunk_printf(&msg, "Session not found.\n");
3707
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003708 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003709 return 0;
3710
Willy Tarreau295a8372011-03-10 11:25:07 +01003711 si->applet.ctx.sess.target = NULL;
3712 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003713 return 1;
3714 }
3715
Willy Tarreau94981132012-05-21 17:09:48 +02003716 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003717 /* fall through */
3718
3719 default:
Willy Tarreau94981132012-05-21 17:09:48 +02003720 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003721 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003722 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003723}
3724
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003725/* This function dumps all tables' states onto the stream intreface's
3726 * read buffer. The data_ctx must have been zeroed first, and the flags
3727 * properly set. It returns 0 if the output buffer is full and it needs
3728 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003729 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003730static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003731{
Willy Tarreau94981132012-05-21 17:09:48 +02003732 struct session *s = si->conn.data_ctx;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003733 struct chunk msg;
3734 struct ebmb_node *eb;
3735 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003736 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003737
3738 /*
Willy Tarreau94981132012-05-21 17:09:48 +02003739 * We have 3 possible states in si->conn.data_st :
Willy Tarreau295a8372011-03-10 11:25:07 +01003740 * - STAT_ST_INIT : the first call
3741 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003742 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003743 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003744 * and the entry pointer points to the next entry to be dumped,
3745 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003746 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003747 * data though.
3748 */
3749
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003750 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003751 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau94981132012-05-21 17:09:48 +02003752 if (si->conn.data_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003753 si->applet.ctx.table.entry->ref_cnt--;
3754 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003755 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003756 return 1;
3757 }
3758
David du Colombier7af46052012-05-16 14:16:48 +02003759 chunk_init(&msg, trash, trashlen);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003760
Willy Tarreau94981132012-05-21 17:09:48 +02003761 while (si->conn.data_st != STAT_ST_FIN) {
3762 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003763 case STAT_ST_INIT:
3764 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3765 if (!si->applet.ctx.table.proxy)
3766 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003767
Willy Tarreau295a8372011-03-10 11:25:07 +01003768 si->applet.ctx.table.entry = NULL;
Willy Tarreau94981132012-05-21 17:09:48 +02003769 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003770 break;
3771
Willy Tarreau295a8372011-03-10 11:25:07 +01003772 case STAT_ST_INFO:
3773 if (!si->applet.ctx.table.proxy ||
3774 (si->applet.ctx.table.target &&
3775 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
Willy Tarreau94981132012-05-21 17:09:48 +02003776 si->conn.data_st = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003777 break;
3778 }
3779
Willy Tarreau295a8372011-03-10 11:25:07 +01003780 if (si->applet.ctx.table.proxy->table.size) {
Simon Hormanc88b8872011-06-15 15:18:49 +09003781 if (show && !stats_dump_table_head_to_buffer(&msg, si, si->applet.ctx.table.proxy,
3782 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003783 return 0;
3784
Willy Tarreau295a8372011-03-10 11:25:07 +01003785 if (si->applet.ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02003786 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003787 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003788 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003789 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003790 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3791 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau94981132012-05-21 17:09:48 +02003792 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003793 break;
3794 }
3795 }
3796 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003797 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003798 break;
3799
Willy Tarreau295a8372011-03-10 11:25:07 +01003800 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003801 skip_entry = false;
3802
Willy Tarreau295a8372011-03-10 11:25:07 +01003803 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003804 /* we're filtering on some data contents */
3805 void *ptr;
3806 long long data;
3807
Willy Tarreau295a8372011-03-10 11:25:07 +01003808 dt = si->applet.ctx.table.data_type;
3809 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3810 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003811 dt);
3812
3813 data = 0;
3814 switch (stktable_data_types[dt].std_type) {
3815 case STD_T_SINT:
3816 data = stktable_data_cast(ptr, std_t_sint);
3817 break;
3818 case STD_T_UINT:
3819 data = stktable_data_cast(ptr, std_t_uint);
3820 break;
3821 case STD_T_ULL:
3822 data = stktable_data_cast(ptr, std_t_ull);
3823 break;
3824 case STD_T_FRQP:
3825 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003826 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003827 break;
3828 }
3829
3830 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003831 if ((data < si->applet.ctx.table.value &&
3832 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3833 si->applet.ctx.table.data_op == STD_OP_GT ||
3834 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3835 (data == si->applet.ctx.table.value &&
3836 (si->applet.ctx.table.data_op == STD_OP_NE ||
3837 si->applet.ctx.table.data_op == STD_OP_GT ||
3838 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3839 (data > si->applet.ctx.table.value &&
3840 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3841 si->applet.ctx.table.data_op == STD_OP_LT ||
3842 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003843 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003844 }
3845
Simon Hormanc88b8872011-06-15 15:18:49 +09003846 if (show && !skip_entry &&
3847 !stats_dump_table_entry_to_buffer(&msg, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003848 si->applet.ctx.table.entry))
3849 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003850
Willy Tarreau295a8372011-03-10 11:25:07 +01003851 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003852
Willy Tarreau295a8372011-03-10 11:25:07 +01003853 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003854 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003855 struct stksess *old = si->applet.ctx.table.entry;
3856 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01003857 if (show)
3858 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3859 else
3860 stksess_kill(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau295a8372011-03-10 11:25:07 +01003861 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003862 break;
3863 }
3864
Simon Hormanc88b8872011-06-15 15:18:49 +09003865
3866 if (show)
3867 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3868 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3869 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3870
Willy Tarreau295a8372011-03-10 11:25:07 +01003871 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau94981132012-05-21 17:09:48 +02003872 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003873 break;
3874
Willy Tarreau295a8372011-03-10 11:25:07 +01003875 case STAT_ST_END:
Willy Tarreau94981132012-05-21 17:09:48 +02003876 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003877 break;
3878 }
3879 }
3880 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003881}
3882
Willy Tarreaud426a182010-03-05 14:58:26 +01003883/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003884 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3885 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3886 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3887 * lines are respected within the limit of 70 output chars. Lines that are
3888 * continuation of a previous truncated line begin with "+" instead of " "
3889 * after the offset. The new pointer is returned.
3890 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003891static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3892 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003893{
3894 int end;
3895 unsigned char c;
3896
3897 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003898 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003899 return ptr;
3900
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003901 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003902
Willy Tarreaud426a182010-03-05 14:58:26 +01003903 while (ptr < len && ptr < bsize) {
3904 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003905 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003906 if (out->len > end - 2)
3907 break;
3908 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003909 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003910 if (out->len > end - 3)
3911 break;
3912 out->str[out->len++] = '\\';
3913 switch (c) {
3914 case '\t': c = 't'; break;
3915 case '\n': c = 'n'; break;
3916 case '\r': c = 'r'; break;
3917 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003918 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003919 }
3920 out->str[out->len++] = c;
3921 } else {
3922 if (out->len > end - 5)
3923 break;
3924 out->str[out->len++] = '\\';
3925 out->str[out->len++] = 'x';
3926 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3927 out->str[out->len++] = hextab[c & 0xF];
3928 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003929 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003930 /* we had a line break, let's return now */
3931 out->str[out->len++] = '\n';
3932 *line = ptr;
3933 return ptr;
3934 }
3935 }
3936 /* we have an incomplete line, we return it as-is */
3937 out->str[out->len++] = '\n';
3938 return ptr;
3939}
3940
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003941/* This function dumps all captured errors onto the stream intreface's
3942 * read buffer. The data_ctx must have been zeroed first, and the flags
3943 * properly set. It returns 0 if the output buffer is full and it needs
3944 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003945 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003946static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003947{
3948 extern const char *monthname[12];
3949 struct chunk msg;
3950
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003951 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003952 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003953
David du Colombier7af46052012-05-16 14:16:48 +02003954 chunk_init(&msg, trash, trashlen);
Willy Tarreau74808cb2009-03-04 15:53:18 +01003955
Willy Tarreau295a8372011-03-10 11:25:07 +01003956 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003957 /* the function had not been called yet, let's prepare the
3958 * buffer for a response.
3959 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003960 struct tm tm;
3961
3962 get_localtime(date.tv_sec, &tm);
3963 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3964 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3965 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3966 error_snapshot_id);
3967
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003968 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003969 /* Socket buffer full. Let's try again later from the same point */
3970 return 0;
3971 }
3972
Willy Tarreau295a8372011-03-10 11:25:07 +01003973 si->applet.ctx.errors.px = proxy;
3974 si->applet.ctx.errors.buf = 0;
3975 si->applet.ctx.errors.bol = 0;
3976 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003977 }
3978
3979 /* we have two inner loops here, one for the proxy, the other one for
3980 * the buffer.
3981 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003982 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003983 struct error_snapshot *es;
3984
Willy Tarreau295a8372011-03-10 11:25:07 +01003985 if (si->applet.ctx.errors.buf == 0)
3986 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003987 else
Willy Tarreau295a8372011-03-10 11:25:07 +01003988 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003989
3990 if (!es->when.tv_sec)
3991 goto next;
3992
Willy Tarreau295a8372011-03-10 11:25:07 +01003993 if (si->applet.ctx.errors.iid >= 0 &&
3994 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
3995 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003996 goto next;
3997
Willy Tarreau295a8372011-03-10 11:25:07 +01003998 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003999 /* just print headers now */
4000
4001 char pn[INET6_ADDRSTRLEN];
4002 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004003 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004004
4005 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01004006 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01004007 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02004008 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01004009
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004010 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
4011 case AF_INET:
4012 case AF_INET6:
4013 port = get_host_port(&es->src);
4014 break;
4015 default:
4016 port = 0;
4017 }
4018
Willy Tarreau295a8372011-03-10 11:25:07 +01004019 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004020 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004021 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004022 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004023 " backend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004024 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004025 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
4026 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004027 break;
4028 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004029 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004030 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004031 " frontend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004032 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004033 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004034 break;
4035 }
4036
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004037 chunk_printf(&msg,
4038 ", server %s (#%d), event #%u\n"
4039 " src %s:%d, session #%d, session flags 0x%08x\n"
4040 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
4041 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
4042 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
4043 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
4044 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
4045 es->ev_id,
4046 pn, port, es->sid, es->s_flags,
4047 es->state, es->m_flags, es->t_flags,
4048 es->m_clen, es->m_blen,
4049 es->b_flags, es->b_out, es->b_tot,
4050 es->len, es->b_wrap, es->pos);
4051
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004052 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004053 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004054 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004055 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004056 si->applet.ctx.errors.ptr = 0;
4057 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004058 }
4059
Willy Tarreau295a8372011-03-10 11:25:07 +01004060 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004061 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004062 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004063 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004064 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004065 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004066 goto next;
4067 }
4068
4069 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01004070 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004071 int newptr;
4072 int newline;
4073
Willy Tarreau295a8372011-03-10 11:25:07 +01004074 newline = si->applet.ctx.errors.bol;
4075 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
4076 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004077 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004078
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004079 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004080 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004081 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004082 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004083 si->applet.ctx.errors.ptr = newptr;
4084 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004085 };
4086 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01004087 si->applet.ctx.errors.bol = 0;
4088 si->applet.ctx.errors.ptr = -1;
4089 si->applet.ctx.errors.buf++;
4090 if (si->applet.ctx.errors.buf > 1) {
4091 si->applet.ctx.errors.buf = 0;
4092 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004093 }
4094 }
4095
4096 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004097 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004098}
4099
Willy Tarreaud5781202012-09-22 19:32:35 +02004100/* parse the "level" argument on the bind lines */
4101static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
4102{
4103 if (!*args[cur_arg + 1]) {
4104 memprintf(err, "'%s' : missing level", args[cur_arg]);
4105 return ERR_ALERT | ERR_FATAL;
4106 }
4107
4108 if (!strcmp(args[cur_arg+1], "user"))
4109 conf->level = ACCESS_LVL_USER;
4110 else if (!strcmp(args[cur_arg+1], "operator"))
4111 conf->level = ACCESS_LVL_OPER;
4112 else if (!strcmp(args[cur_arg+1], "admin"))
4113 conf->level = ACCESS_LVL_ADMIN;
4114 else {
4115 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
4116 args[cur_arg], args[cur_arg+1]);
4117 return ERR_ALERT | ERR_FATAL;
4118 }
4119
4120 return 0;
4121}
4122
Willy Tarreaub24281b2011-02-13 13:16:36 +01004123struct si_applet http_stats_applet = {
4124 .name = "<STATS>", /* used for logging */
4125 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004126 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004127};
4128
Simon Horman9bd2c732011-06-15 15:18:44 +09004129static struct si_applet cli_applet = {
Willy Tarreaub24281b2011-02-13 13:16:36 +01004130 .name = "<CLI>", /* used for logging */
4131 .fct = cli_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004132 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004133};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004134
Willy Tarreau10522fd2008-07-09 20:12:41 +02004135static struct cfg_kw_list cfg_kws = {{ },{
4136 { CFG_GLOBAL, "stats", stats_parse_global },
4137 { 0, NULL, NULL },
4138}};
4139
Willy Tarreaud5781202012-09-22 19:32:35 +02004140static struct bind_kw_list bind_kws = { "STAT", { }, {
4141 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
4142 { NULL, NULL, 0 },
4143}};
4144
Willy Tarreau10522fd2008-07-09 20:12:41 +02004145__attribute__((constructor))
4146static void __dumpstats_module_init(void)
4147{
4148 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02004149 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02004150}
4151
Willy Tarreau91861262007-10-17 17:06:05 +02004152/*
4153 * Local variables:
4154 * c-indent-level: 8
4155 * c-basic-offset: 8
4156 * End:
4157 */