blob: ab4c416373798bf7014515b92201d044c51dfa68 [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 Tarreau07e9e642010-08-17 21:48:17 +0200171 return fe;
172}
173
Willy Tarreaufbee7132007-10-18 13:53:22 +0200174/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200175 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
176 * error message into the <err> buffer which will be preallocated. The trailing
177 * '\n' must not be written. The function must be called with <args> pointing to
178 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200179 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200180static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200181 struct proxy *defpx, const char *file, int line,
182 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200183{
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200184 if (!strcmp(args[1], "socket")) {
Andreas Kohn16171e22011-01-19 20:29:32 +0100185 struct sockaddr_un *su;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200186 int cur_arg;
187
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200188 if (*args[2] == 0) {
189 memprintf(err, "'%s %s' in global section expects a path to a UNIX socket", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200190 return -1;
191 }
192
193 if (global.stats_sock.state != LI_NEW) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200194 memprintf(err, "'%s %s' already specified in global section", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200195 return -1;
196 }
197
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200198 su = str2sun(args[2]);
Andreas Kohn16171e22011-01-19 20:29:32 +0100199 if (!su) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200200 memprintf(err, "'%s %s' : path would require truncation", args[0], args[1]);
Andreas Kohn16171e22011-01-19 20:29:32 +0100201 return -1;
202 }
203 memcpy(&global.stats_sock.addr, su, sizeof(struct sockaddr_un)); // guaranteed to fit
Willy Tarreaufbee7132007-10-18 13:53:22 +0200204
Willy Tarreau89a63132009-08-16 17:41:45 +0200205 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200206 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200207 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200208 return -1;
209 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200210 }
211
Willy Tarreaufbee7132007-10-18 13:53:22 +0200212 global.stats_sock.state = LI_INIT;
Willy Tarreau3c63fd82011-09-07 18:00:47 +0200213 global.stats_sock.options = LI_O_UNLIMITED;
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200214 global.stats_sock.accept = session_accept;
215 global.stats_fe->accept = stats_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200216 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200217 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200218 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreaueb472682010-05-28 18:46:57 +0200219 global.stats_sock.frontend = global.stats_fe;
Willy Tarreau6162db22009-10-10 17:13:00 +0200220 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200221 global.stats_sock.maxconn = global.stats_fe->maxconn;
Willy Tarreau89a63132009-08-16 17:41:45 +0200222 global.stats_sock.timeout = &global.stats_fe->timeout.client;
223
224 global.stats_sock.next = global.stats_fe->listen;
225 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200226
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200227 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200228 while (*args[cur_arg]) {
229 if (!strcmp(args[cur_arg], "uid")) {
230 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
231 cur_arg += 2;
232 }
233 else if (!strcmp(args[cur_arg], "gid")) {
234 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
235 cur_arg += 2;
236 }
237 else if (!strcmp(args[cur_arg], "mode")) {
238 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
239 cur_arg += 2;
240 }
241 else if (!strcmp(args[cur_arg], "user")) {
242 struct passwd *user;
243 user = getpwnam(args[cur_arg + 1]);
244 if (!user) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200245 memprintf(err, "'%s %s' : unknown user '%s'", args[0], args[1], args[cur_arg + 1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200246 return -1;
247 }
248 global.stats_sock.perm.ux.uid = user->pw_uid;
249 cur_arg += 2;
250 }
251 else if (!strcmp(args[cur_arg], "group")) {
252 struct group *group;
253 group = getgrnam(args[cur_arg + 1]);
254 if (!group) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200255 memprintf(err, "'%s %s' : unknown group '%s'", args[0], args[1], args[cur_arg + 1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200256 return -1;
257 }
258 global.stats_sock.perm.ux.gid = group->gr_gid;
259 cur_arg += 2;
260 }
Willy Tarreau6162db22009-10-10 17:13:00 +0200261 else if (!strcmp(args[cur_arg], "level")) {
262 if (!strcmp(args[cur_arg+1], "user"))
263 global.stats_sock.perm.ux.level = ACCESS_LVL_USER;
264 else if (!strcmp(args[cur_arg+1], "operator"))
265 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER;
266 else if (!strcmp(args[cur_arg+1], "admin"))
267 global.stats_sock.perm.ux.level = ACCESS_LVL_ADMIN;
268 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200269 memprintf(err, "'%s %s' : '%s' only supports 'user', 'operator', and 'admin' (got '%s')",
270 args[0], args[1], args[cur_arg], args[cur_arg+1]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200271 return -1;
272 }
273 cur_arg += 2;
274 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200275 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200276 memprintf(err, "'%s %s' only supports 'user', 'uid', 'group', 'gid', 'level', and 'mode' (got '%s')",
277 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200278 return -1;
279 }
280 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100281
Willy Tarreauc5788912012-08-24 18:12:41 +0200282 global.stats_sock.data = &raw_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200283 uxst_add_listener(&global.stats_sock);
284 global.maxsock++;
285 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200286 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100287 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200288 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100289
290 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200291 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100292 return -1;
293 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200294
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100295 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200296 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200297 return -1;
298 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200299 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200300 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200301 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200302 return -1;
303 }
304 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200305 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200306 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200307 else if (!strcmp(args[1], "maxconn")) {
308 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200309
310 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200311 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200312 return -1;
313 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200314
315 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200316 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200317 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200318 return -1;
319 }
320 }
321 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200322 }
323 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200324 memprintf(err, "'%s' only supports 'socket', 'maxconn' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200325 return -1;
326 }
327 return 0;
328}
329
Simon Horman9bd2c732011-06-15 15:18:44 +0900330static int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100331{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200332 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100333 "# pxname,svname,"
334 "qcur,qmax,"
335 "scur,smax,slim,stot,"
336 "bin,bout,"
337 "dreq,dresp,"
338 "ereq,econ,eresp,"
339 "wretr,wredis,"
340 "status,weight,act,bck,"
341 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200342 "pid,iid,sid,throttle,lbtot,tracked,type,"
343 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200344 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100345 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100346 "req_rate,req_rate_max,req_tot,"
347 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100348 "\n");
349}
350
Simon Hormand9366582011-06-15 15:18:45 +0900351/* print a string of text buffer to <out>. The format is :
352 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
353 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
354 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
355 */
356static int dump_text(struct chunk *out, const char *buf, int bsize)
357{
358 unsigned char c;
359 int ptr = 0;
360
361 while (buf[ptr] && ptr < bsize) {
362 c = buf[ptr];
363 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
364 if (out->len > out->size - 1)
365 break;
366 out->str[out->len++] = c;
367 }
368 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
369 if (out->len > out->size - 2)
370 break;
371 out->str[out->len++] = '\\';
372 switch (c) {
373 case ' ': c = ' '; break;
374 case '\t': c = 't'; break;
375 case '\n': c = 'n'; break;
376 case '\r': c = 'r'; break;
377 case '\e': c = 'e'; break;
378 case '\\': c = '\\'; break;
379 }
380 out->str[out->len++] = c;
381 }
382 else {
383 if (out->len > out->size - 4)
384 break;
385 out->str[out->len++] = '\\';
386 out->str[out->len++] = 'x';
387 out->str[out->len++] = hextab[(c >> 4) & 0xF];
388 out->str[out->len++] = hextab[c & 0xF];
389 }
390 ptr++;
391 }
392
393 return ptr;
394}
395
396/* print a buffer in hexa.
397 * Print stopped if <bsize> is reached, or if no more place in the chunk.
398 */
399static int dump_binary(struct chunk *out, const char *buf, int bsize)
400{
401 unsigned char c;
402 int ptr = 0;
403
404 while (ptr < bsize) {
405 c = buf[ptr];
406
407 if (out->len > out->size - 2)
408 break;
409 out->str[out->len++] = hextab[(c >> 4) & 0xF];
410 out->str[out->len++] = hextab[c & 0xF];
411
412 ptr++;
413 }
414 return ptr;
415}
416
417/* Dump the status of a table to a stream interface's
418 * read buffer. It returns 0 if the output buffer is full
419 * and needs to be called again, otherwise non-zero.
420 */
421static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
422 struct proxy *proxy, struct proxy *target)
423{
Willy Tarreau94981132012-05-21 17:09:48 +0200424 struct session *s = si->conn.data_ctx;
Simon Hormand9366582011-06-15 15:18:45 +0900425
426 chunk_printf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
427 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
428
429 /* any other information should be dumped here */
430
431 if (target && s->listener->perm.ux.level < ACCESS_LVL_OPER)
432 chunk_printf(msg, "# contents not dumped due to insufficient privileges\n");
433
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200434 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900435 return 0;
436
437 return 1;
438}
439
440/* Dump the a table entry to a stream interface's
441 * read buffer. It returns 0 if the output buffer is full
442 * and needs to be called again, otherwise non-zero.
443 */
444static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
445 struct proxy *proxy, struct stksess *entry)
446{
447 int dt;
448
449 chunk_printf(msg, "%p:", entry);
450
451 if (proxy->table.type == STKTABLE_TYPE_IP) {
452 char addr[INET_ADDRSTRLEN];
453 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
454 chunk_printf(msg, " key=%s", addr);
455 }
456 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
457 char addr[INET6_ADDRSTRLEN];
458 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
459 chunk_printf(msg, " key=%s", addr);
460 }
461 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
462 chunk_printf(msg, " key=%u", *(unsigned int *)entry->key.key);
463 }
464 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
465 chunk_printf(msg, " key=");
466 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
467 }
468 else {
469 chunk_printf(msg, " key=");
470 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
471 }
472
473 chunk_printf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
474
475 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
476 void *ptr;
477
478 if (proxy->table.data_ofs[dt] == 0)
479 continue;
480 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
481 chunk_printf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
482 else
483 chunk_printf(msg, " %s=", stktable_data_types[dt].name);
484
485 ptr = stktable_data_ptr(&proxy->table, entry, dt);
486 switch (stktable_data_types[dt].std_type) {
487 case STD_T_SINT:
488 chunk_printf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
489 break;
490 case STD_T_UINT:
491 chunk_printf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
492 break;
493 case STD_T_ULL:
494 chunk_printf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
495 break;
496 case STD_T_FRQP:
497 chunk_printf(msg, "%d",
498 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
499 proxy->table.data_arg[dt].u));
500 break;
501 }
502 }
503 chunk_printf(msg, "\n");
504
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200505 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900506 return 0;
507
508 return 1;
509}
510
Willy Tarreaudec98142012-06-06 23:37:08 +0200511static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900512{
Willy Tarreau94981132012-05-21 17:09:48 +0200513 struct session *s = si->conn.data_ctx;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900514 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900515 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900516 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900517 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreaudec98142012-06-06 23:37:08 +0200518 struct chunk msg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200519 long long value;
520 int data_type;
521 void *ptr;
522 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900523
Simon Hormand5b9fd92011-06-15 15:18:48 +0900524 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900525
526 if (!*args[4]) {
527 si->applet.ctx.cli.msg = "Key value expected\n";
528 si->applet.st0 = STAT_CLI_PRINT;
529 return;
530 }
531
Simon Hormanc5b89f62011-06-15 15:18:50 +0900532 switch (px->table.type) {
533 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900534 uint32_key = htonl(inetaddr_host(args[4]));
535 static_table_key.key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900536 break;
537 case STKTABLE_TYPE_IPV6:
538 inet_pton(AF_INET6, args[4], ip6_key);
539 static_table_key.key = &ip6_key;
540 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900541 case STKTABLE_TYPE_INTEGER:
542 {
543 char *endptr;
544 unsigned long val;
545 errno = 0;
546 val = strtoul(args[4], &endptr, 10);
547 if ((errno == ERANGE && val == ULONG_MAX) ||
548 (errno != 0 && val == 0) || endptr == args[4] ||
549 val > 0xffffffff) {
550 si->applet.ctx.cli.msg = "Invalid key\n";
551 si->applet.st0 = STAT_CLI_PRINT;
552 return;
553 }
554 uint32_key = (uint32_t) val;
555 static_table_key.key = &uint32_key;
556 break;
557 }
558 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900559 case STKTABLE_TYPE_STRING:
560 static_table_key.key = args[4];
561 static_table_key.key_len = strlen(args[4]);
562 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900563 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200564 switch (action) {
565 case STAT_CLI_O_TAB:
566 si->applet.ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
567 break;
568 case STAT_CLI_O_CLR:
569 si->applet.ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
570 break;
571 default:
572 si->applet.ctx.cli.msg = "Unknown action\n";
573 break;
574 }
Simon Horman121f3052011-06-15 15:18:46 +0900575 si->applet.st0 = STAT_CLI_PRINT;
576 return;
577 }
578
579 /* check permissions */
580 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
581 si->applet.ctx.cli.msg = stats_permission_denied_msg;
582 si->applet.st0 = STAT_CLI_PRINT;
583 return;
584 }
585
586 ts = stktable_lookup_key(&px->table, &static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900587
Willy Tarreaudec98142012-06-06 23:37:08 +0200588 switch (action) {
589 case STAT_CLI_O_TAB:
590 if (!ts)
591 return;
David du Colombier7af46052012-05-16 14:16:48 +0200592 chunk_init(&msg, trash, trashlen);
Simon Horman17bce342011-06-15 15:18:47 +0900593 if (!stats_dump_table_head_to_buffer(&msg, si, px, px))
594 return;
595 stats_dump_table_entry_to_buffer(&msg, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900596 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200597
598 case STAT_CLI_O_CLR:
599 if (!ts)
600 return;
601 if (ts->ref_cnt) {
602 /* don't delete an entry which is currently referenced */
603 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
604 si->applet.st0 = STAT_CLI_PRINT;
605 return;
606 }
607 stksess_kill(&px->table, ts);
608 break;
Simon Horman17bce342011-06-15 15:18:47 +0900609
Willy Tarreau654694e2012-06-07 01:03:16 +0200610 case STAT_CLI_O_SET:
611 if (strncmp(args[5], "data.", 5) != 0) {
612 si->applet.ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
613 si->applet.st0 = STAT_CLI_PRINT;
614 return;
615 }
616
617 data_type = stktable_get_data_type(args[5] + 5);
618 if (data_type < 0) {
619 si->applet.ctx.cli.msg = "Unknown data type\n";
620 si->applet.st0 = STAT_CLI_PRINT;
621 return;
622 }
623
624 if (!px->table.data_ofs[data_type]) {
625 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
626 si->applet.st0 = STAT_CLI_PRINT;
627 return;
628 }
629
630 if (!*args[6] || strl2llrc(args[6], strlen(args[6]), &value) != 0) {
631 si->applet.ctx.cli.msg = "Require a valid integer value to store\n";
632 si->applet.st0 = STAT_CLI_PRINT;
633 return;
634 }
635
636 if (ts)
637 stktable_touch(&px->table, ts, 1);
638 else {
639 ts = stksess_new(&px->table, &static_table_key);
640 if (!ts) {
641 /* don't delete an entry which is currently referenced */
642 si->applet.ctx.cli.msg = "Unable to allocate a new entry\n";
643 si->applet.st0 = STAT_CLI_PRINT;
644 return;
645 }
646 stktable_store(&px->table, ts, 1);
647 }
648
649 ptr = stktable_data_ptr(&px->table, ts, data_type);
650 switch (stktable_data_types[data_type].std_type) {
651 case STD_T_SINT:
652 stktable_data_cast(ptr, std_t_sint) = value;
653 break;
654 case STD_T_UINT:
655 stktable_data_cast(ptr, std_t_uint) = value;
656 break;
657 case STD_T_ULL:
658 stktable_data_cast(ptr, std_t_ull) = value;
659 break;
660 case STD_T_FRQP:
661 /* We only reset the previous value so that it slowly fades out */
662 frqp = &stktable_data_cast(ptr, std_t_frqp);
663 frqp->curr_tick = now_ms;
664 frqp->prev_ctr = value;
665 frqp->curr_ctr = 0;
666 break;
667 }
668 break;
669
Willy Tarreaudec98142012-06-06 23:37:08 +0200670 default:
671 si->applet.ctx.cli.msg = "Unknown action\n";
Simon Horman121f3052011-06-15 15:18:46 +0900672 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200673 break;
Simon Horman121f3052011-06-15 15:18:46 +0900674 }
Simon Horman121f3052011-06-15 15:18:46 +0900675}
676
Willy Tarreau654694e2012-06-07 01:03:16 +0200677static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900678{
Willy Tarreau654694e2012-06-07 01:03:16 +0200679 if (action != STAT_CLI_O_TAB) {
680 si->applet.ctx.cli.msg = "content-based lookup is only supported with the \"show\" action";
681 si->applet.st0 = STAT_CLI_PRINT;
682 return;
683 }
684
Simon Hormand5b9fd92011-06-15 15:18:48 +0900685 /* condition on stored data value */
686 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
687 if (si->applet.ctx.table.data_type < 0) {
688 si->applet.ctx.cli.msg = "Unknown data type\n";
689 si->applet.st0 = STAT_CLI_PRINT;
690 return;
691 }
692
693 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
694 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
695 si->applet.st0 = STAT_CLI_PRINT;
696 return;
697 }
698
699 si->applet.ctx.table.data_op = get_std_op(args[4]);
700 if (si->applet.ctx.table.data_op < 0) {
701 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
702 si->applet.st0 = STAT_CLI_PRINT;
703 return;
704 }
705
706 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
707 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
708 si->applet.st0 = STAT_CLI_PRINT;
709 return;
710 }
711}
712
Willy Tarreaudec98142012-06-06 23:37:08 +0200713static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900714{
715 si->applet.ctx.table.data_type = -1;
Willy Tarreau94981132012-05-21 17:09:48 +0200716 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200717 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900718 si->applet.ctx.table.proxy = NULL;
719 si->applet.ctx.table.entry = NULL;
Willy Tarreaudec98142012-06-06 23:37:08 +0200720 si->applet.st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900721
722 if (*args[2]) {
723 si->applet.ctx.table.target = find_stktable(args[2]);
724 if (!si->applet.ctx.table.target) {
725 si->applet.ctx.cli.msg = "No such table\n";
726 si->applet.st0 = STAT_CLI_PRINT;
727 return;
728 }
729 }
730 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200731 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900732 goto err_args;
733 return;
734 }
735
736 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200737 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900738 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200739 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900740 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900741 goto err_args;
742
743 return;
744
745err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200746 switch (action) {
747 case STAT_CLI_O_TAB:
Simon Hormand5b9fd92011-06-15 15:18:48 +0900748 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 +0200749 break;
750 case STAT_CLI_O_CLR:
Simon Hormanc88b8872011-06-15 15:18:49 +0900751 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 +0200752 break;
753 default:
754 si->applet.ctx.cli.msg = "Unknown action\n";
755 break;
756 }
Simon Hormand5b9fd92011-06-15 15:18:48 +0900757 si->applet.st0 = STAT_CLI_PRINT;
758}
759
Willy Tarreau532a4502011-09-07 22:37:44 +0200760/* Expects to find a frontend named <arg> and returns it, otherwise displays various
761 * adequate error messages and returns NULL. This function also expects the session
762 * level to be admin.
763 */
764static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
765{
766 struct proxy *px;
767
768 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
769 si->applet.ctx.cli.msg = stats_permission_denied_msg;
770 si->applet.st0 = STAT_CLI_PRINT;
771 return NULL;
772 }
773
774 if (!*arg) {
775 si->applet.ctx.cli.msg = "A frontend name is expected.\n";
776 si->applet.st0 = STAT_CLI_PRINT;
777 return NULL;
778 }
779
780 px = findproxy(arg, PR_CAP_FE);
781 if (!px) {
782 si->applet.ctx.cli.msg = "No such frontend.\n";
783 si->applet.st0 = STAT_CLI_PRINT;
784 return NULL;
785 }
786 return px;
787}
788
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200789/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
790 * and returns the pointer to the server. Otherwise, display adequate error messages
791 * and returns NULL. This function also expects the session level to be admin. Note:
792 * the <arg> is modified to remove the '/'.
793 */
794static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
795{
796 struct proxy *px;
797 struct server *sv;
798 char *line;
799
800 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
801 si->applet.ctx.cli.msg = stats_permission_denied_msg;
802 si->applet.st0 = STAT_CLI_PRINT;
803 return NULL;
804 }
805
806 /* split "backend/server" and make <line> point to server */
807 for (line = arg; *line; line++)
808 if (*line == '/') {
809 *line++ = '\0';
810 break;
811 }
812
813 if (!*line || !*arg) {
814 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
815 si->applet.st0 = STAT_CLI_PRINT;
816 return NULL;
817 }
818
819 if (!get_backend_server(arg, line, &px, &sv)) {
820 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
821 si->applet.st0 = STAT_CLI_PRINT;
822 return NULL;
823 }
824
825 if (px->state == PR_STSTOPPED) {
826 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
827 si->applet.st0 = STAT_CLI_PRINT;
828 return NULL;
829 }
830
831 return sv;
832}
833
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200834/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200835 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100836 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200837 * designating the function which will have to process the request, which can
838 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200839 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900840static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200841{
Willy Tarreau94981132012-05-21 17:09:48 +0200842 struct session *s = si->conn.data_ctx;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200843 char *args[MAX_STATS_ARGS + 1];
844 int arg;
845
846 while (isspace((unsigned char)*line))
847 line++;
848
849 arg = 0;
850 args[arg] = line;
851
852 while (*line && arg < MAX_STATS_ARGS) {
853 if (isspace((unsigned char)*line)) {
854 *line++ = '\0';
855
856 while (isspace((unsigned char)*line))
857 line++;
858
859 args[++arg] = line;
860 continue;
861 }
862
863 line++;
864 }
865
866 while (++arg <= MAX_STATS_ARGS)
867 args[arg] = line;
868
Willy Tarreau295a8372011-03-10 11:25:07 +0100869 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200870 if (strcmp(args[0], "show") == 0) {
871 if (strcmp(args[1], "stat") == 0) {
872 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100873 si->applet.ctx.stats.flags |= STAT_BOUND;
874 si->applet.ctx.stats.iid = atoi(args[2]);
875 si->applet.ctx.stats.type = atoi(args[3]);
876 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200877 }
878
Willy Tarreau295a8372011-03-10 11:25:07 +0100879 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
880 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau94981132012-05-21 17:09:48 +0200881 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100882 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200883 }
884 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100885 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
886 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau94981132012-05-21 17:09:48 +0200887 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100888 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200889 }
890 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau94981132012-05-21 17:09:48 +0200891 si->conn.data_st = STAT_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200892 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100893 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100894 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200895 return 1;
896 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100897 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100898 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100899 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100900 si->applet.ctx.sess.target = NULL;
901 si->applet.ctx.sess.section = 0; /* start with session status */
902 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100903 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200904 }
905 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200906 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100907 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100908 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200909 return 1;
910 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200911 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100912 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200913 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100914 si->applet.ctx.errors.iid = -1;
915 si->applet.ctx.errors.px = NULL;
Willy Tarreau94981132012-05-21 17:09:48 +0200916 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100917 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200918 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200919 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200920 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200921 }
Aman Guptaceafb4a2012-04-02 18:57:54 -0700922 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200923 return 0;
924 }
925 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200926 else if (strcmp(args[0], "clear") == 0) {
927 if (strcmp(args[1], "counters") == 0) {
928 struct proxy *px;
929 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200930 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200931 int clrall = 0;
932
933 if (strcmp(args[2], "all") == 0)
934 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200935
Willy Tarreau6162db22009-10-10 17:13:00 +0200936 /* check permissions */
937 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
938 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100939 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100940 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200941 return 1;
942 }
943
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200944 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100945 if (clrall) {
946 memset(&px->be_counters, 0, sizeof(px->be_counters));
947 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
948 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200949 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100950 px->be_counters.conn_max = 0;
951 px->be_counters.p.http.rps_max = 0;
952 px->be_counters.sps_max = 0;
953 px->be_counters.cps_max = 0;
954 px->be_counters.nbpend_max = 0;
955
956 px->fe_counters.conn_max = 0;
957 px->fe_counters.p.http.rps_max = 0;
958 px->fe_counters.sps_max = 0;
959 px->fe_counters.cps_max = 0;
960 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200961 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200962
963 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200964 if (clrall)
965 memset(&sv->counters, 0, sizeof(sv->counters));
966 else {
967 sv->counters.cur_sess_max = 0;
968 sv->counters.nbpend_max = 0;
969 sv->counters.sps_max = 0;
970 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200971
972 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200973 if (li->counters) {
974 if (clrall)
975 memset(li->counters, 0, sizeof(*li->counters));
976 else
977 li->counters->conn_max = 0;
978 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200979 }
980
Willy Tarreau81c25d02011-09-07 15:17:21 +0200981 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200982 return 1;
983 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200984 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200985 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200986 /* end of processing */
987 return 1;
988 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200989 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200990 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200991 return 0;
992 }
993 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200994 else if (strcmp(args[0], "get") == 0) {
995 if (strcmp(args[1], "weight") == 0) {
996 struct proxy *px;
997 struct server *sv;
998
999 /* split "backend/server" and make <line> point to server */
1000 for (line = args[2]; *line; line++)
1001 if (*line == '/') {
1002 *line++ = '\0';
1003 break;
1004 }
1005
1006 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001007 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001008 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001009 return 1;
1010 }
1011
1012 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001013 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001014 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001015 return 1;
1016 }
1017
1018 /* return server's effective weight at the moment */
David du Colombier7af46052012-05-16 14:16:48 +02001019 snprintf(trash, trashlen, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001020 bi_putstr(si->ib, trash);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001021 return 1;
1022 }
1023 else { /* not "get weight" */
1024 return 0;
1025 }
1026 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001027 else if (strcmp(args[0], "set") == 0) {
1028 if (strcmp(args[1], "weight") == 0) {
1029 struct proxy *px;
1030 struct server *sv;
1031 int w;
1032
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001033 sv = expect_server_admin(s, si, args[2]);
1034 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001035 return 1;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001036 px = sv->proxy;
Willy Tarreau4483d432009-10-10 19:30:08 +02001037
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001038 /* if the weight is terminated with '%', it is set relative to
1039 * the initial weight, otherwise it is absolute.
1040 */
1041 if (!*args[3]) {
1042 si->applet.ctx.cli.msg = "Require <weight> or <weight%>.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001043 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001044 return 1;
1045 }
1046
Willy Tarreau4483d432009-10-10 19:30:08 +02001047 w = atoi(args[3]);
1048 if (strchr(args[3], '%') != NULL) {
1049 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001050 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001051 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001052 return 1;
1053 }
1054 w = sv->iweight * w / 100;
1055 }
1056 else {
1057 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001058 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001059 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001060 return 1;
1061 }
1062 }
1063
1064 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001065 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 +01001066 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001067 return 1;
1068 }
1069
1070 sv->uweight = w;
1071
1072 if (px->lbprm.algo & BE_LB_PROP_DYN) {
1073 /* we must take care of not pushing the server to full throttle during slow starts */
1074 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
1075 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
1076 else
1077 sv->eweight = BE_WEIGHT_SCALE;
1078 sv->eweight *= sv->uweight;
1079 } else {
1080 sv->eweight = sv->uweight;
1081 }
1082
1083 /* static LB algorithms are a bit harder to update */
1084 if (px->lbprm.update_server_eweight)
1085 px->lbprm.update_server_eweight(sv);
Willy Tarreau9580d162012-05-19 19:07:40 +02001086 else if (sv->eweight) {
1087 if (px->lbprm.set_server_status_up)
1088 px->lbprm.set_server_status_up(sv);
1089 }
1090 else {
1091 if (px->lbprm.set_server_status_down)
1092 px->lbprm.set_server_status_down(sv);
1093 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001094
1095 return 1;
1096 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001097 else if (strcmp(args[1], "timeout") == 0) {
1098 if (strcmp(args[2], "cli") == 0) {
1099 unsigned timeout;
1100 const char *res;
1101
1102 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001103 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001104 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001105 return 1;
1106 }
1107
1108 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1109 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001110 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001111 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001112 return 1;
1113 }
1114
1115 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1116 return 1;
1117 }
1118 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001119 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001120 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001121 return 1;
1122 }
1123 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001124 else if (strcmp(args[1], "maxconn") == 0) {
1125 if (strcmp(args[2], "frontend") == 0) {
1126 struct proxy *px;
1127 struct listener *l;
1128 int v;
1129
Willy Tarreau532a4502011-09-07 22:37:44 +02001130 px = expect_frontend_admin(s, si, args[3]);
1131 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001132 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001133
1134 if (!*args[4]) {
1135 si->applet.ctx.cli.msg = "Integer value expected.\n";
1136 si->applet.st0 = STAT_CLI_PRINT;
1137 return 1;
1138 }
1139
1140 v = atoi(args[4]);
1141 /* check for unlimited values, we restore default setting (cfg_maxpconn) */
1142 if (v < 1) {
1143 si->applet.ctx.cli.msg = "Value out of range.\n";
1144 si->applet.st0 = STAT_CLI_PRINT;
1145 return 1;
1146 }
1147
1148 /* OK, the value is fine, so we assign it to the proxy and to all of
1149 * its listeners. The blocked ones will be dequeued.
1150 */
1151 px->maxconn = v;
1152 for (l = px->listen; l != NULL; l = l->next) {
1153 l->maxconn = v;
1154 if (l->state == LI_FULL)
1155 resume_listener(l);
1156 }
1157
1158 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1159 dequeue_all_listeners(&s->fe->listener_queue);
1160
1161 return 1;
1162 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001163 else if (strcmp(args[2], "global") == 0) {
1164 int v;
1165
1166 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1167 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1168 si->applet.st0 = STAT_CLI_PRINT;
1169 return 1;
1170 }
1171
1172 if (!*args[3]) {
1173 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1174 si->applet.st0 = STAT_CLI_PRINT;
1175 return 1;
1176 }
1177
1178 v = atoi(args[3]);
1179 if (v > global.hardmaxconn) {
1180 si->applet.ctx.cli.msg = "Value out of range.\n";
1181 si->applet.st0 = STAT_CLI_PRINT;
1182 return 1;
1183 }
1184
1185 /* check for unlimited values */
1186 if (v <= 0)
1187 v = global.hardmaxconn;
1188
1189 global.maxconn = v;
1190
1191 /* Dequeues all of the listeners waiting for a resource */
1192 if (!LIST_ISEMPTY(&global_listener_queue))
1193 dequeue_all_listeners(&global_listener_queue);
1194
1195 return 1;
1196 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001197 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001198 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001199 si->applet.st0 = STAT_CLI_PRINT;
1200 return 1;
1201 }
1202 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001203 else if (strcmp(args[1], "rate-limit") == 0) {
1204 if (strcmp(args[2], "connections") == 0) {
1205 if (strcmp(args[3], "global") == 0) {
1206 int v;
1207
1208 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1209 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1210 si->applet.st0 = STAT_CLI_PRINT;
1211 return 1;
1212 }
1213
1214 if (!*args[4]) {
1215 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1216 si->applet.st0 = STAT_CLI_PRINT;
1217 return 1;
1218 }
1219
1220 v = atoi(args[4]);
1221 if (v < 0) {
1222 si->applet.ctx.cli.msg = "Value out of range.\n";
1223 si->applet.st0 = STAT_CLI_PRINT;
1224 return 1;
1225 }
1226
1227 global.cps_lim = v;
1228
1229 /* Dequeues all of the listeners waiting for a resource */
1230 if (!LIST_ISEMPTY(&global_listener_queue))
1231 dequeue_all_listeners(&global_listener_queue);
1232
1233 return 1;
1234 }
1235 else {
1236 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1237 si->applet.st0 = STAT_CLI_PRINT;
1238 return 1;
1239 }
1240 }
1241 else {
1242 si->applet.ctx.cli.msg = "'set rate-limit' only supports 'connections'.\n";
1243 si->applet.st0 = STAT_CLI_PRINT;
1244 return 1;
1245 }
1246 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001247 else if (strcmp(args[1], "table") == 0) {
1248 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1249 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001250 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001251 return 0;
1252 }
1253 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001254 else if (strcmp(args[0], "enable") == 0) {
1255 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001256 struct server *sv;
1257
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001258 sv = expect_server_admin(s, si, args[2]);
1259 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001260 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001261
Cyril Bontécd19e512010-01-31 22:34:03 +01001262 if (sv->state & SRV_MAINTAIN) {
1263 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001264 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001265 /* If this server tracks the status of another one,
1266 * we must restore the good status.
1267 */
Willy Tarreau44267702011-10-28 15:35:33 +02001268 if (sv->track->state & SRV_RUNNING) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001269 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001270 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001271 } else {
1272 sv->state &= ~SRV_MAINTAIN;
1273 set_server_down(sv);
1274 }
1275 } else {
1276 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001277 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001278 }
1279 }
1280
Willy Tarreau532a4502011-09-07 22:37:44 +02001281 return 1;
1282 }
1283 else if (strcmp(args[1], "frontend") == 0) {
1284 struct proxy *px;
1285
1286 px = expect_frontend_admin(s, si, args[2]);
1287 if (!px)
1288 return 1;
1289
1290 if (px->state == PR_STSTOPPED) {
1291 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1292 si->applet.st0 = STAT_CLI_PRINT;
1293 return 1;
1294 }
1295
1296 if (px->state != PR_STPAUSED) {
1297 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1298 si->applet.st0 = STAT_CLI_PRINT;
1299 return 1;
1300 }
1301
1302 if (!resume_proxy(px)) {
1303 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1304 si->applet.st0 = STAT_CLI_PRINT;
1305 return 1;
1306 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001307 return 1;
1308 }
1309 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001310 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1311 si->applet.st0 = STAT_CLI_PRINT;
1312 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001313 }
1314 }
1315 else if (strcmp(args[0], "disable") == 0) {
1316 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001317 struct server *sv;
1318
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001319 sv = expect_server_admin(s, si, args[2]);
1320 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001321 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001322
Cyril Bontécd19e512010-01-31 22:34:03 +01001323 if (! (sv->state & SRV_MAINTAIN)) {
1324 /* Not already in maintenance, we can change the server state */
1325 sv->state |= SRV_MAINTAIN;
1326 set_server_down(sv);
1327 }
1328
Willy Tarreau532a4502011-09-07 22:37:44 +02001329 return 1;
1330 }
1331 else if (strcmp(args[1], "frontend") == 0) {
1332 struct proxy *px;
1333
1334 px = expect_frontend_admin(s, si, args[2]);
1335 if (!px)
1336 return 1;
1337
1338 if (px->state == PR_STSTOPPED) {
1339 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1340 si->applet.st0 = STAT_CLI_PRINT;
1341 return 1;
1342 }
1343
1344 if (px->state == PR_STPAUSED) {
1345 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1346 si->applet.st0 = STAT_CLI_PRINT;
1347 return 1;
1348 }
1349
1350 if (!pause_proxy(px)) {
1351 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1352 si->applet.st0 = STAT_CLI_PRINT;
1353 return 1;
1354 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001355 return 1;
1356 }
1357 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001358 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1359 si->applet.st0 = STAT_CLI_PRINT;
1360 return 1;
1361 }
1362 }
1363 else if (strcmp(args[0], "shutdown") == 0) {
1364 if (strcmp(args[1], "frontend") == 0) {
1365 struct proxy *px;
1366
1367 px = expect_frontend_admin(s, si, args[2]);
1368 if (!px)
1369 return 1;
1370
1371 if (px->state == PR_STSTOPPED) {
1372 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1373 si->applet.st0 = STAT_CLI_PRINT;
1374 return 1;
1375 }
1376
1377 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1378 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1379 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1380 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1381 stop_proxy(px);
1382 return 1;
1383 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001384 else if (strcmp(args[1], "session") == 0) {
1385 struct session *sess, *ptr;
1386
1387 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1388 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1389 si->applet.st0 = STAT_CLI_PRINT;
1390 return 1;
1391 }
1392
1393 if (!*args[2]) {
1394 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1395 si->applet.st0 = STAT_CLI_PRINT;
1396 return 1;
1397 }
1398
1399 ptr = (void *)strtoul(args[2], NULL, 0);
1400
1401 /* first, look for the requested session in the session table */
1402 list_for_each_entry(sess, &sessions, list) {
1403 if (sess == ptr)
1404 break;
1405 }
1406
1407 /* do we have the session ? */
1408 if (sess != ptr) {
1409 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1410 si->applet.st0 = STAT_CLI_PRINT;
1411 return 1;
1412 }
1413
1414 session_shutdown(sess, SN_ERR_KILLED);
1415 return 1;
1416 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001417 else if (strcmp(args[1], "sessions") == 0) {
1418 if (strcmp(args[2], "server") == 0) {
1419 struct server *sv;
1420 struct session *sess, *sess_bck;
1421
1422 sv = expect_server_admin(s, si, args[3]);
1423 if (!sv)
1424 return 1;
1425
1426 /* kill all the session that are on this server */
1427 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1428 if (sess->srv_conn == sv)
1429 session_shutdown(sess, SN_ERR_KILLED);
1430
1431 return 1;
1432 }
1433 else {
1434 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1435 si->applet.st0 = STAT_CLI_PRINT;
1436 return 1;
1437 }
1438 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001439 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001440 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001441 si->applet.st0 = STAT_CLI_PRINT;
1442 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001443 }
1444 }
1445 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001446 return 0;
1447 }
1448 return 1;
1449}
1450
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001451/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001452 * used to processes I/O from/to the stats unix socket. The system relies on a
1453 * state machine handling requests and various responses. We read a request,
1454 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001455 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1456 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001457 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001458 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001459static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001460{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001461 struct channel *req = si->ob;
1462 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001463 int reql;
1464 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001465
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001466 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1467 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001468
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001469 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001470 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001471 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001472 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001473 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001474 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001475 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001476 /* Let's close for real now. We just close the request
1477 * side, the conditions below will complete if needed.
1478 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001479 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001480 break;
1481 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001482 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001483 /* ensure we have some output room left in the event we
1484 * would want to return some info right after parsing.
1485 */
Willy Tarreau572bf902012-07-02 17:01:20 +02001486 if (buffer_almost_full(&si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001487 break;
1488
David du Colombier7af46052012-05-16 14:16:48 +02001489 reql = bo_getline(si->ob, trash, trashlen);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001490 if (reql <= 0) { /* closed or EOL not found */
1491 if (reql == 0)
1492 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001493 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001494 continue;
1495 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001496
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001497 /* seek for a possible semi-colon. If we find one, we
1498 * replace it with an LF and skip only this part.
1499 */
1500 for (len = 0; len < reql; len++)
1501 if (trash[len] == ';') {
1502 trash[len] = '\n';
1503 reql = len + 1;
1504 break;
1505 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001506
Willy Tarreau816fc222009-10-04 07:36:58 +02001507 /* now it is time to check that we have a full line,
1508 * remove the trailing \n and possibly \r, then cut the
1509 * line.
1510 */
1511 len = reql - 1;
1512 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001513 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001514 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001515 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001516
Willy Tarreau816fc222009-10-04 07:36:58 +02001517 if (len && trash[len-1] == '\r')
1518 len--;
1519
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001520 trash[len] = '\0';
1521
Willy Tarreaubc4af052011-02-13 13:25:14 +01001522 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001523 if (len) {
1524 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001525 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001526 continue;
1527 }
1528 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001529 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001530 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001531 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001532 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001533 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001534 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001535 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001536 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001537 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001538 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001539 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001540 /* if prompt is disabled, print help on empty lines,
1541 * so that the user at least knows how to enable
1542 * prompt and find help.
1543 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001544 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001545 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001546 }
1547
1548 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001549 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001550 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001551 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001552 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001553 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001554 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001555 continue;
1556 }
1557
Willy Tarreaubc4af052011-02-13 13:25:14 +01001558 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001559 case STAT_CLI_PRINT:
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001560 if (bi_putstr(si->ib, si->applet.ctx.cli.msg) != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001561 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001562 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001563 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001564 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001565 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001566 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001567 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001568 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001569 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001570 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001571 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001572 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001573 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001574 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001575 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001576 case STAT_CLI_O_CLR:
Willy Tarreaudec98142012-06-06 23:37:08 +02001577 if (stats_table_request(si, si->applet.st0))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001578 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001579 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001580 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001581 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001582 break;
1583 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001584
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001585 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001586 if (si->applet.st0 == STAT_CLI_PROMPT) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001587 if (bi_putstr(si->ib, si->applet.st1 ? "\n> " : "\n") != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001588 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001589 }
1590
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001591 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001592 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001593 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001594
1595 /* Now we close the output if one of the writers did so,
1596 * or if we're not in interactive mode and the request
1597 * buffer is empty. This still allows pipelined requests
1598 * to be sent in non-interactive mode.
1599 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001600 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!si->applet.st1 && !req->buf.o)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001601 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001602 continue;
1603 }
1604
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001605 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001606 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001607 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001608 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001609
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001610 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001611 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1612 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001613 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001614 * and nothing more to consume. This is comparable to a broken pipe, so
1615 * we forward the close to the request side so that it flows upstream to
1616 * the client.
1617 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001618 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001619 }
1620
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001621 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001622 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1623 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1624 /* We have no more processing to do, and nothing more to send, and
1625 * the client side has closed. So we'll forward this state downstream
1626 * on the response buffer.
1627 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001628 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001629 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001630 }
1631
1632 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001633 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001634
1635 /* we don't want to expire timeouts while we're processing requests */
1636 si->ib->rex = TICK_ETERNITY;
1637 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001638
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001639 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001640 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 +02001641 __FUNCTION__, __LINE__,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001642 si->state, req->flags, res->flags, req->i, req->o, res->i, res->o);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001643
1644 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1645 /* check that we have released everything then unregister */
1646 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001647 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001648}
1649
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001650/* This function dumps statistics onto the stream interface's read buffer.
1651 * The data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001652 * It returns 0 as long as it does not complete, non-zero upon completion.
1653 * Some states are not used but it makes the code more similar to other
1654 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001655 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001656static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001657{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001658 struct proxy *px;
1659 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001660 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001661
David du Colombier7af46052012-05-16 14:16:48 +02001662 chunk_init(&msg, trash, trashlen);
Willy Tarreau3e76e722007-10-17 18:57:38 +02001663
Willy Tarreau94981132012-05-21 17:09:48 +02001664 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001665 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001666 /* the function had not been called yet */
Willy Tarreau94981132012-05-21 17:09:48 +02001667 si->conn.data_st = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001668 /* fall through */
1669
Willy Tarreau295a8372011-03-10 11:25:07 +01001670 case STAT_ST_HEAD:
1671 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001672 print_csv_header(&msg);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001673 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001674 return 0;
1675 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001676
Willy Tarreau94981132012-05-21 17:09:48 +02001677 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001678 /* fall through */
1679
Willy Tarreau295a8372011-03-10 11:25:07 +01001680 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001681 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001682 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001683 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001684 "Name: " PRODUCT_NAME "\n"
1685 "Version: " HAPROXY_VERSION "\n"
1686 "Release_date: " HAPROXY_DATE "\n"
1687 "Nbproc: %d\n"
1688 "Process_num: %d\n"
1689 "Pid: %d\n"
1690 "Uptime: %dd %dh%02dm%02ds\n"
1691 "Uptime_sec: %d\n"
1692 "Memmax_MB: %d\n"
1693 "Ulimit-n: %d\n"
1694 "Maxsock: %d\n"
1695 "Maxconn: %d\n"
Willy Tarreau91886b62011-09-07 14:38:31 +02001696 "Hard_maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001697 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001698 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001699 "PipesUsed: %d\n"
1700 "PipesFree: %d\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001701 "ConnRate: %d\n"
1702 "ConnRateLimit: %d\n"
1703 "MaxConnRate: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001704 "Tasks: %d\n"
1705 "Run_queue: %d\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02001706 "Idle_pct: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001707 "node: %s\n"
1708 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001709 "",
1710 global.nbproc,
1711 relative_pid,
1712 pid,
1713 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1714 up,
1715 global.rlimit_memmax,
1716 global.rlimit_nofile,
Willy Tarreau91886b62011-09-07 14:38:31 +02001717 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001718 actconn, pipes_used, pipes_free,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001719 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau45a12512011-09-10 16:56:42 +02001720 nb_tasks_cur, run_queue_cur, idle_pct,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001721 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001722 );
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001723 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001724 return 0;
1725 }
1726
Willy Tarreau295a8372011-03-10 11:25:07 +01001727 si->applet.ctx.stats.px = proxy;
1728 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1729 si->applet.ctx.stats.sv = NULL;
Willy Tarreau94981132012-05-21 17:09:48 +02001730 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001731 /* fall through */
1732
Willy Tarreau295a8372011-03-10 11:25:07 +01001733 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001734 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001735 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1736 while (si->applet.ctx.stats.px) {
1737 px = si->applet.ctx.stats.px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001738 /* skip the disabled proxies and non-networked ones */
1739 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001740 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001741 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001742 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001743 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001744
Willy Tarreau295a8372011-03-10 11:25:07 +01001745 si->applet.ctx.stats.px = px->next;
1746 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001747 }
1748 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001749 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001750
Willy Tarreau94981132012-05-21 17:09:48 +02001751 si->conn.data_st = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001752 /* fall through */
1753
Willy Tarreau295a8372011-03-10 11:25:07 +01001754 case STAT_ST_END:
Willy Tarreau94981132012-05-21 17:09:48 +02001755 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001756 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001757
Willy Tarreau295a8372011-03-10 11:25:07 +01001758 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001759 return 1;
1760
1761 default:
1762 /* unknown state ! */
Willy Tarreau94981132012-05-21 17:09:48 +02001763 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001764 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001765 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001766}
1767
1768
Cyril Bonté70be45d2010-10-12 00:14:35 +02001769/* We don't want to land on the posted stats page because a refresh will
1770 * repost the data. We don't want this to happen on accident so we redirect
1771 * the browse to the stats page with a GET.
1772 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001773static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001774{
Willy Tarreau94981132012-05-21 17:09:48 +02001775 struct session *s = si->conn.data_ctx;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001776 struct chunk msg;
1777
David du Colombier7af46052012-05-16 14:16:48 +02001778 chunk_init(&msg, trash, trashlen);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001779
Willy Tarreau94981132012-05-21 17:09:48 +02001780 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001781 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001782 chunk_printf(&msg,
1783 "HTTP/1.0 303 See Other\r\n"
1784 "Cache-Control: no-cache\r\n"
1785 "Content-Type: text/plain\r\n"
1786 "Connection: close\r\n"
1787 "Location: %s;st=%s",
Cyril Bonté19979e12012-04-04 12:57:21 +02001788 uri->uri_prefix,
1789 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
1790 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
1791 stat_status_codes[si->applet.ctx.stats.st_code]) ?
1792 stat_status_codes[si->applet.ctx.stats.st_code] :
1793 stat_status_codes[STAT_STATUS_UNKN]);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001794 chunk_printf(&msg, "\r\n\r\n");
1795
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001796 if (bi_putchk(si->ib, &msg) == -1)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001797 return 0;
1798
1799 s->txn.status = 303;
1800
1801 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1802 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1803 if (!(s->flags & SN_FINST_MASK))
1804 s->flags |= SN_FINST_R;
1805
Willy Tarreau94981132012-05-21 17:09:48 +02001806 si->conn.data_st = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001807 return 1;
1808 }
1809 return 1;
1810}
1811
1812
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001813/* This I/O handler runs as an applet embedded in a stream interface. It is
1814 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001815 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001816 * automatically unregisters itself once transfer is complete.
1817 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001818static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001819{
Willy Tarreau94981132012-05-21 17:09:48 +02001820 struct session *s = si->conn.data_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001821 struct channel *req = si->ob;
1822 struct channel *res = si->ib;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001823
1824 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1825 goto out;
1826
1827 /* check that the output is not closed */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001828 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001829 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001830
Willy Tarreaubc4af052011-02-13 13:25:14 +01001831 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001832 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001833 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001834 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001835 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001836 }
1837 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001838 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001839 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001840 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001841 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001842 }
1843 }
1844
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001845 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
Willy Tarreau73b013b2012-05-21 16:31:45 +02001846 si_shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001847
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001848 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001849 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001850 res->flags |= CF_READ_NULL;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001851 }
1852
1853 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001854 si_update(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001855
1856 /* we don't want to expire timeouts while we're processing requests */
1857 si->ib->rex = TICK_ETERNITY;
1858 si->ob->wex = TICK_ETERNITY;
1859
1860 out:
1861 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1862 /* check that we have released everything then unregister */
1863 stream_int_unregister_handler(si);
1864 }
1865}
1866
1867
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001868/* This function dumps statistics in HTTP format onto the stream interface's
1869 * read buffer. The data_ctx must have been zeroed first, and the flags
1870 * properly set. It returns 0 if it had to stop writing data and an I/O is
1871 * needed, 1 if the dump is finished and the session must be closed, or -1
1872 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001873 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001874static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001875{
Willy Tarreau94981132012-05-21 17:09:48 +02001876 struct session *s = si->conn.data_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001877 struct channel *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001878 struct proxy *px;
1879 struct chunk msg;
1880 unsigned int up;
1881
David du Colombier7af46052012-05-16 14:16:48 +02001882 chunk_init(&msg, trash, trashlen);
Willy Tarreau91861262007-10-17 17:06:05 +02001883
Willy Tarreau94981132012-05-21 17:09:48 +02001884 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001885 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001886 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001887 "HTTP/1.0 200 OK\r\n"
1888 "Cache-Control: no-cache\r\n"
1889 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001890 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001891 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001892
Willy Tarreau295a8372011-03-10 11:25:07 +01001893 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001894 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001895 uri->refresh);
1896
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001897 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001898
1899 s->txn.status = 200;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001900 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001901 return 0;
1902
Willy Tarreau91861262007-10-17 17:06:05 +02001903 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1904 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1905 if (!(s->flags & SN_FINST_MASK))
1906 s->flags |= SN_FINST_R;
1907
1908 if (s->txn.meth == HTTP_METH_HEAD) {
1909 /* that's all we return in case of HEAD request */
Willy Tarreau94981132012-05-21 17:09:48 +02001910 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001911 return 1;
1912 }
1913
Willy Tarreau94981132012-05-21 17:09:48 +02001914 si->conn.data_st = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001915 /* fall through */
1916
Willy Tarreau295a8372011-03-10 11:25:07 +01001917 case STAT_ST_HEAD:
1918 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001919 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001920 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001921 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1922 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001923 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001924 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1925 "<style type=\"text/css\"><!--\n"
1926 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001927 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001928 " font-size: 12px;"
1929 " font-weight: normal;"
1930 " color: black;"
1931 " background: white;"
1932 "}\n"
1933 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001934 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001935 "}\n"
1936 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001937 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001938 " margin-bottom: 0.5em;"
1939 "}\n"
1940 "h2 {"
1941 " font-family: helvetica, arial;"
1942 " font-size: x-large;"
1943 " font-weight: bold;"
1944 " font-style: italic;"
1945 " color: #6020a0;"
1946 " margin-top: 0em;"
1947 " margin-bottom: 0em;"
1948 "}\n"
1949 "h3 {"
1950 " font-family: helvetica, arial;"
1951 " font-size: 16px;"
1952 " font-weight: bold;"
1953 " color: #b00040;"
1954 " background: #e8e8d0;"
1955 " margin-top: 0em;"
1956 " margin-bottom: 0em;"
1957 "}\n"
1958 "li {"
1959 " margin-top: 0.25em;"
1960 " margin-right: 2em;"
1961 "}\n"
1962 ".hr {margin-top: 0.25em;"
1963 " border-color: black;"
1964 " border-bottom-style: solid;"
1965 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001966 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001967 ".total {background: #20D0D0;color: #ffff80;}\n"
1968 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001969 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001970 ".backend {background: #e8e8d0;}\n"
1971 ".active0 {background: #ff9090;}\n"
1972 ".active1 {background: #ffd020;}\n"
1973 ".active2 {background: #ffffa0;}\n"
1974 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001975 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1976 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1977 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001978 ".backup0 {background: #ff9090;}\n"
1979 ".backup1 {background: #ff80ff;}\n"
1980 ".backup2 {background: #c060ff;}\n"
1981 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001982 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1983 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1984 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001985 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001986 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001987 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001988 "a.px:link {color: #ffff40; text-decoration: none;}"
1989 "a.px:visited {color: #ffff40; text-decoration: none;}"
1990 "a.px:hover {color: #ffffff; text-decoration: none;}"
1991 "a.lfsb:link {color: #000000; text-decoration: none;}"
1992 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1993 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1994 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001995 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001996 "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"
1997 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001998 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001999 "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 +02002000 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002001 "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 +02002002 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002003 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2004 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2005 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01002006 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002007 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02002008 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002009 (uri->flags&ST_SHNODE) ? " on " : "",
2010 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02002011 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02002012 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002013 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002014 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002015 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002016 return 0;
2017
Willy Tarreau94981132012-05-21 17:09:48 +02002018 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02002019 /* fall through */
2020
Willy Tarreau295a8372011-03-10 11:25:07 +01002021 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02002022 up = (now.tv_sec - start_date.tv_sec);
2023
2024 /* WARNING! this has to fit the first packet too.
2025 * We are around 3.5 kB, add adding entries will
2026 * become tricky if we want to support 4kB buffers !
2027 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002028 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002029 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002030 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2031 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002032 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002033 "<hr width=\"100%%\" class=\"hr\">\n"
2034 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002035 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01002036 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002037 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002038 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2039 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02002040 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02002041 "Running tasks: %d/%d; idle = %d %%<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002042 "</td><td align=\"center\" nowrap>\n"
2043 "<table class=\"lgd\"><tr>\n"
2044 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2045 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2046 "</tr><tr>\n"
2047 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
2048 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
2049 "</tr><tr>\n"
2050 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
2051 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2052 "</tr><tr>\n"
2053 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01002054 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01002055 "</tr><tr>\n"
2056 "<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 +02002057 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01002058 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02002059 "</td>"
2060 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2061 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2062 "",
2063 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002064 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2065 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
2066 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02002067 up / 86400, (up % 86400) / 3600,
2068 (up % 3600) / 60, (up % 60),
2069 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2070 global.rlimit_memmax ? " MB" : "",
2071 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01002072 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02002073 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau45a12512011-09-10 16:56:42 +02002074 run_queue_cur, nb_tasks_cur, idle_pct
Willy Tarreau91861262007-10-17 17:06:05 +02002075 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002076
Willy Tarreau295a8372011-03-10 11:25:07 +01002077 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002078 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002079 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
2080 uri->uri_prefix,
2081 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01002082 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002083 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002084 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002085 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2086 uri->uri_prefix,
2087 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01002088 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002089
Willy Tarreau55bb8452007-10-17 18:44:57 +02002090 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002091 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002092 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002093 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
2094 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002095 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002096 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002097 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002098 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002099 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
2100 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002101 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002102 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002103 }
Willy Tarreau91861262007-10-17 17:06:05 +02002104
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002105 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002106 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
2107 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002108 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2109 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002110
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002111 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002112 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
2113 uri->uri_prefix,
2114 (uri->refresh > 0) ? ";norefresh" : "");
2115
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002116 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002117 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002118 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
Adrian Bridgettafdb6e52012-03-19 23:36:42 +00002119 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002120 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2121 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2122 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2123 "</ul>"
2124 "</td>"
2125 "</tr></table>\n"
2126 ""
2127 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002128
Willy Tarreau295a8372011-03-10 11:25:07 +01002129 if (si->applet.ctx.stats.st_code) {
Cyril Bonté19979e12012-04-04 12:57:21 +02002130 switch (si->applet.ctx.stats.st_code) {
2131 case STAT_STATUS_DONE:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002132 chunk_printf(&msg,
2133 "<p><div class=active3>"
2134 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2135 "Action processed successfully."
2136 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002137 break;
2138 case STAT_STATUS_NONE:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002139 chunk_printf(&msg,
2140 "<p><div class=active2>"
2141 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2142 "Nothing has changed."
2143 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002144 break;
2145 case STAT_STATUS_PART:
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002146 chunk_printf(&msg,
2147 "<p><div class=active2>"
2148 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2149 "Action partially processed.<br>"
2150 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2151 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002152 break;
2153 case STAT_STATUS_ERRP:
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002154 chunk_printf(&msg,
2155 "<p><div class=active0>"
2156 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2157 "Action not processed because of invalid parameters."
2158 "<ul>"
2159 "<li>The action is maybe unknown.</li>"
2160 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2161 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2162 "</ul>"
2163 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002164 break;
2165 case STAT_STATUS_EXCD:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002166 chunk_printf(&msg,
2167 "<p><div class=active0>"
2168 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2169 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2170 "You should retry with less servers at a time.</b>"
2171 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002172 break;
2173 case STAT_STATUS_DENY:
Cyril Bonté474be412010-10-12 00:14:36 +02002174 chunk_printf(&msg,
2175 "<p><div class=active0>"
2176 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2177 "<b>Action denied.</b>"
2178 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002179 break;
2180 default:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002181 chunk_printf(&msg,
2182 "<p><div class=active6>"
2183 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2184 "Unexpected result."
2185 "</div>\n", uri->uri_prefix);
2186 }
2187 chunk_printf(&msg,"<p>\n");
2188 }
2189
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002190 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002191 return 0;
2192 }
Willy Tarreau91861262007-10-17 17:06:05 +02002193
Willy Tarreau295a8372011-03-10 11:25:07 +01002194 si->applet.ctx.stats.px = proxy;
2195 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau94981132012-05-21 17:09:48 +02002196 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02002197 /* fall through */
2198
Willy Tarreau295a8372011-03-10 11:25:07 +01002199 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02002200 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01002201 while (si->applet.ctx.stats.px) {
Willy Tarreau572bf902012-07-02 17:01:20 +02002202 if (buffer_almost_full(&rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002203 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002204 px = si->applet.ctx.stats.px;
Willy Tarreau91861262007-10-17 17:06:05 +02002205 /* skip the disabled proxies and non-networked ones */
2206 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002207 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002208 return 0;
2209
Willy Tarreau295a8372011-03-10 11:25:07 +01002210 si->applet.ctx.stats.px = px->next;
2211 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02002212 }
2213 /* here, we just have reached the last proxy */
2214
Willy Tarreau94981132012-05-21 17:09:48 +02002215 si->conn.data_st = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002216 /* fall through */
2217
Willy Tarreau295a8372011-03-10 11:25:07 +01002218 case STAT_ST_END:
2219 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002220 chunk_printf(&msg, "</body></html>\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002221 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002222 return 0;
2223 }
Willy Tarreau91861262007-10-17 17:06:05 +02002224
Willy Tarreau94981132012-05-21 17:09:48 +02002225 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002226 /* fall through */
2227
Willy Tarreau295a8372011-03-10 11:25:07 +01002228 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002229 return 1;
2230
2231 default:
2232 /* unknown state ! */
Willy Tarreau94981132012-05-21 17:09:48 +02002233 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002234 return -1;
2235 }
2236}
2237
2238
2239/*
2240 * Dumps statistics for a proxy.
2241 * Returns 0 if it had to stop dumping data because of lack of buffer space,
2242 * ot non-zero if everything completed.
2243 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002244static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002245{
Willy Tarreau94981132012-05-21 17:09:48 +02002246 struct session *s = si->conn.data_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02002247 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002248 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002249 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002250 struct chunk msg;
2251
David du Colombier7af46052012-05-16 14:16:48 +02002252 chunk_init(&msg, trash, trashlen);
Willy Tarreau91861262007-10-17 17:06:05 +02002253
Willy Tarreau295a8372011-03-10 11:25:07 +01002254 switch (si->applet.ctx.stats.px_st) {
2255 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002256 /* we are on a new proxy */
2257
2258 if (uri && uri->scope) {
2259 /* we have a limited scope, we have to check the proxy name */
2260 struct stat_scope *scope;
2261 int len;
2262
2263 len = strlen(px->id);
2264 scope = uri->scope;
2265
2266 while (scope) {
2267 /* match exact proxy name */
2268 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2269 break;
2270
2271 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002272 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002273 break;
2274 scope = scope->next;
2275 }
2276
2277 /* proxy name not found : don't dump anything */
2278 if (scope == NULL)
2279 return 1;
2280 }
2281
Willy Tarreau295a8372011-03-10 11:25:07 +01002282 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
2283 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002284 return 1;
2285
Willy Tarreau295a8372011-03-10 11:25:07 +01002286 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002287 /* fall through */
2288
Willy Tarreau295a8372011-03-10 11:25:07 +01002289 case STAT_PX_ST_TH:
2290 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
2291 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002292 /* A form to enable/disable this proxy servers */
2293 chunk_printf(&msg,
2294 "<form action=\"%s\" method=\"post\">",
2295 uri->uri_prefix);
2296 }
2297
Willy Tarreau55bb8452007-10-17 18:44:57 +02002298 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002299 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002300 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002301 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002302 "<th class=\"pxname\" width=\"10%%\"");
2303
2304 if (uri->flags&ST_SHLGNDS) {
2305 /* cap, mode, id */
2306 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
2307 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2308 px->uuid);
2309
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002310 chunk_printf(&msg, "\"");
2311 }
2312
2313 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002314 ">%s<a name=\"%s\"></a>"
2315 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002316 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002317 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002318 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002319 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002320 "<tr class=\"titre\">",
2321 (uri->flags & ST_SHLGNDS)?"<u>":"",
2322 px->id, px->id, px->id,
2323 (uri->flags & ST_SHLGNDS)?"</u>":"",
2324 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2325
Willy Tarreau295a8372011-03-10 11:25:07 +01002326 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002327 /* Column heading for Enable or Disable server */
2328 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
2329 }
2330
2331 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002332 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002333 "<th colspan=3>Queue</th>"
2334 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002335 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002336 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002337 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002338 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002339 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002340 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002341 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002342 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002343 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002344 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002345 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002346 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2347 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002348 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002349
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002350 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002351 return 0;
2352 }
Willy Tarreau91861262007-10-17 17:06:05 +02002353
Willy Tarreau295a8372011-03-10 11:25:07 +01002354 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002355 /* fall through */
2356
Willy Tarreau295a8372011-03-10 11:25:07 +01002357 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002358 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002359 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002360 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
2361 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002362 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002363 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002364 "<tr class=\"frontend\">");
2365
Willy Tarreau295a8372011-03-10 11:25:07 +01002366 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002367 /* Column sub-heading for Enable or Disable server */
2368 chunk_printf(&msg, "<td></td>");
2369 }
2370
2371 chunk_printf(&msg,
2372 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002373 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002374 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2375 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002376 "",
2377 px->id, px->id);
2378
2379 if (px->mode == PR_MODE_HTTP) {
2380 chunk_printf(&msg,
2381 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01002382 "<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 +01002383 "",
2384 read_freq_ctr(&px->fe_req_per_sec),
2385 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002386 px->fe_counters.p.http.rps_max,
2387 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01002388 LIM2A2(px->fe_sps_lim, "-"));
2389 } else {
2390 chunk_printf(&msg,
2391 /* sessions rate : current, max, limit */
2392 "<td>%s</td><td>%s</td><td>%s</td>"
2393 "",
2394 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002395 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01002396 }
2397
2398 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002399 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002400 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002401 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002402 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002403 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002404
2405 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2406 if (px->mode == PR_MODE_HTTP) {
2407 int i;
2408
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002409 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002410
2411 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002412 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002413
Willy Tarreaueabea072011-09-10 23:29:44 +02002414 chunk_printf(&msg, " other=%lld,", px->fe_counters.p.http.rsp[0]);
2415 chunk_printf(&msg, " intercepted=%lld\"", px->fe_counters.intercepted_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002416 }
2417
2418 chunk_printf(&msg,
2419 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002420 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002421 /* bytes : in, out */
2422 "<td>%s</td><td>%s</td>"
2423 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002424 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002425 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002426 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002427 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002428
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002429 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002430 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002431 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002432 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002433 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002434 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002435 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002436 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002437 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002438 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002439 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002440 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002441 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2442 U2H2(px->fe_counters.failed_req),
Willy Tarreau562515c2011-07-25 08:11:52 +02002443 px->state == PR_STREADY ? "OPEN" :
2444 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002445 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002446 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002447 /* pxid, name, queue cur, queue max, */
2448 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002449 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002450 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002451 /* bytes : in, out */
2452 "%lld,%lld,"
2453 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002454 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002455 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002456 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002457 /* warnings: retries, redispatches */
2458 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002459 /* server status : reflect frontend status */
2460 "%s,"
2461 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002462 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002463 /* pid, iid, sid, throttle, lbtot, tracked, type */
2464 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002465 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002466 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002467 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002468 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002469 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002470 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2471 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2472 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2473 px->fe_counters.failed_req,
Willy Tarreau562515c2011-07-25 08:11:52 +02002474 px->state == PR_STREADY ? "OPEN" :
2475 px->state == PR_STFULL ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002476 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002477 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002478 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002479
2480 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2481 if (px->mode == PR_MODE_HTTP) {
2482 int i;
2483
2484 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002485 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002486
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002487 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002488 } else {
2489 chunk_printf(&msg, ",,,,,,");
2490 }
2491
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002492 /* failed health analyses */
2493 chunk_printf(&msg, ",");
2494
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002495 /* requests : req_rate, req_rate_max, req_tot, */
2496 chunk_printf(&msg, "%u,%u,%lld,",
2497 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002498 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002499
Willy Tarreauae526782010-03-04 20:34:23 +01002500 /* errors: cli_aborts, srv_aborts */
2501 chunk_printf(&msg, ",,");
2502
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002503 /* finish with EOL */
2504 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002505 }
Willy Tarreau91861262007-10-17 17:06:05 +02002506
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002507 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002508 return 0;
2509 }
2510
Willy Tarreau295a8372011-03-10 11:25:07 +01002511 si->applet.ctx.stats.l = px->listen; /* may be NULL */
2512 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002513 /* fall through */
2514
Willy Tarreau295a8372011-03-10 11:25:07 +01002515 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002516 /* stats.l has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002517 for (; si->applet.ctx.stats.l != NULL; si->applet.ctx.stats.l = l->next) {
Willy Tarreau572bf902012-07-02 17:01:20 +02002518 if (buffer_almost_full(&rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002519 return 0;
2520
Willy Tarreau295a8372011-03-10 11:25:07 +01002521 l = si->applet.ctx.stats.l;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002522 if (!l->counters)
2523 continue;
2524
Willy Tarreau295a8372011-03-10 11:25:07 +01002525 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2526 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002527 break;
2528
Willy Tarreau295a8372011-03-10 11:25:07 +01002529 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002530 continue;
2531 }
2532
Willy Tarreau295a8372011-03-10 11:25:07 +01002533 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002534 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002535 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002536 /* Column sub-heading for Enable or Disable server */
2537 chunk_printf(&msg, "<td></td>");
2538 }
2539 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002540
2541 if (uri->flags&ST_SHLGNDS) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02002542 char str[INET6_ADDRSTRLEN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002543 int port;
2544
Willy Tarreau631f01c2011-09-05 00:36:48 +02002545 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002546
Willy Tarreau631f01c2011-09-05 00:36:48 +02002547 port = get_host_port(&l->addr);
2548 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2549 case AF_INET:
2550 chunk_printf(&msg, "IPv4: %s:%d, ", str, port);
2551 break;
2552 case AF_INET6:
2553 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, port);
2554 break;
2555 case AF_UNIX:
2556 chunk_printf(&msg, "unix, ");
2557 break;
2558 case -1:
2559 chunk_printf(&msg, "(%s), ", strerror(errno));
2560 break;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002561 }
2562
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002563 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002564 chunk_printf(&msg, "id: %d\"", l->luid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002565 }
2566
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002567 chunk_printf(&msg,
2568 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002569 ">%s<a name=\"%s/+%s\"></a>"
2570 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002571 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002572 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002573 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002574 "<td>%s</td><td>%s</td><td>%s</td>"
2575 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002576 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002577 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002578 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002579 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002580 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002581 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002582 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2583 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2584
2585 chunk_printf(&msg,
2586 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002587 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002588 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002589 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002590 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002591 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002592 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002593 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002594 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002595 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002596 "",
2597 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2598 U2H2(l->counters->failed_req),
Willy Tarreaua17c2d92011-07-25 08:16:20 +02002599 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002600 } else {
2601 chunk_printf(&msg,
2602 /* pxid, name, queue cur, queue max, */
2603 "%s,%s,,,"
2604 /* sessions: current, max, limit, total */
2605 "%d,%d,%d,%lld,"
2606 /* bytes: in, out */
2607 "%lld,%lld,"
2608 /* denied: req, resp */
2609 "%lld,%lld,"
2610 /* errors: request, connect, response */
2611 "%lld,,,"
2612 /* warnings: retries, redispatches */
2613 ",,"
2614 /* server status: reflect listener status */
2615 "%s,"
2616 /* rest of server: nothing */
2617 ",,,,,,,,"
2618 /* pid, iid, sid, throttle, lbtot, tracked, type */
2619 "%d,%d,%d,,,,%d,"
2620 /* rate, rate_lim, rate_max */
2621 ",,,"
2622 /* check_status, check_code, check_duration */
2623 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002624 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2625 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002626 /* failed health analyses */
2627 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002628 /* requests : req_rate, req_rate_max, req_tot, */
2629 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002630 /* errors: cli_aborts, srv_aborts */
2631 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002632 "\n",
2633 px->id, l->name,
2634 l->nbconn, l->counters->conn_max,
2635 l->maxconn, l->counters->cum_conn,
2636 l->counters->bytes_in, l->counters->bytes_out,
2637 l->counters->denied_req, l->counters->denied_resp,
2638 l->counters->failed_req,
2639 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2640 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2641 }
2642
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002643 if (bi_putchk(rep, &msg) == -1)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002644 return 0;
2645 }
2646
Willy Tarreau295a8372011-03-10 11:25:07 +01002647 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2648 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002649 /* fall through */
2650
Willy Tarreau295a8372011-03-10 11:25:07 +01002651 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002652 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002653 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002654 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 +02002655
Willy Tarreau572bf902012-07-02 17:01:20 +02002656 if (buffer_almost_full(&rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002657 return 0;
2658
Willy Tarreau295a8372011-03-10 11:25:07 +01002659 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002660
Willy Tarreau295a8372011-03-10 11:25:07 +01002661 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2662 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002663 break;
2664
Willy Tarreau295a8372011-03-10 11:25:07 +01002665 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002666 continue;
2667 }
2668
Willy Tarreau44267702011-10-28 15:35:33 +02002669 if (sv->track)
2670 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002671 else
2672 svs = sv;
2673
Willy Tarreau91861262007-10-17 17:06:05 +02002674 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002675 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002676 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002677 else if (svs->state & SRV_RUNNING) {
2678 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002679 sv_state = 3; /* UP */
2680 else
2681 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002682
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002683 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002684 sv_state += 2;
2685 }
Willy Tarreau91861262007-10-17 17:06:05 +02002686 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002687 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002688 sv_state = 1; /* going up */
2689 else
2690 sv_state = 0; /* DOWN */
2691
Willy Tarreau295a8372011-03-10 11:25:07 +01002692 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002693 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002694 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002695 continue;
2696 }
2697
Willy Tarreau295a8372011-03-10 11:25:07 +01002698 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002699 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2700 "UP %d/%d &darr;", "UP",
2701 "NOLB %d/%d &darr;", "NOLB",
2702 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002703 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002704 chunk_printf(&msg,
2705 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002706 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002707 );
2708 }
2709 else {
2710 chunk_printf(&msg,
2711 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002712 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002713 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2714 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002715
Willy Tarreau295a8372011-03-10 11:25:07 +01002716 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002717 chunk_printf(&msg,
2718 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2719 sv->id);
2720 }
2721
2722 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002723
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002724 if (uri->flags&ST_SHLGNDS) {
2725 char str[INET6_ADDRSTRLEN];
2726
Willy Tarreau631f01c2011-09-05 00:36:48 +02002727 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002728
Willy Tarreau631f01c2011-09-05 00:36:48 +02002729 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01002730 case AF_INET:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002731 chunk_printf(&msg, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
David du Colombier6f5ccb12011-03-10 22:26:24 +01002732 break;
2733 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002734 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2735 break;
2736 case AF_UNIX:
2737 chunk_printf(&msg, "unix, ");
2738 break;
2739 case -1:
2740 chunk_printf(&msg, "(%s), ", strerror(errno));
2741 break;
2742 default: /* address family not supported */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002743 break;
2744 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002745
2746 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002747 chunk_printf(&msg, "id: %d", sv->puid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002748
2749 /* cookie */
2750 if (sv->cookie) {
2751 struct chunk src;
2752
2753 chunk_printf(&msg, ", cookie: '");
2754
2755 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2756 chunk_htmlencode(&msg, &src);
2757
2758 chunk_printf(&msg, "'");
2759 }
2760
2761 chunk_printf(&msg, "\"");
2762 }
2763
2764 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002765 ">%s<a name=\"%s/%s\"></a>"
2766 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002767 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002768 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002769 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002770 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002771 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002772 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002773 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002774 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002775 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002776 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002777 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002778 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2779 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002780 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2781
2782 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2783 if (px->mode == PR_MODE_HTTP) {
2784 int i;
2785
2786 chunk_printf(&msg, " title=\"rsp codes:");
2787
2788 for (i = 1; i < 6; i++)
2789 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2790
2791 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2792 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002793
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002794 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002795 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002796 ">%s%s%s</td><td>%s</td>",
2797 (px->mode == PR_MODE_HTTP)?"<u>":"",
2798 U2H0(sv->counters.cum_sess),
2799 (px->mode == PR_MODE_HTTP)?"</u>":"",
2800 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002801
2802 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002803 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002804 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002805 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002806 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002807 /* errors : request, connect */
2808 "<td></td><td>%s</td>"
2809 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002810 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002811 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002812 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002813 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002814 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2815 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002816 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002817 sv->counters.cli_aborts,
2818 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002819 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002820 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002821
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002822 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002823 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002824
Cyril Bontécd19e512010-01-31 22:34:03 +01002825 if (sv->state & SRV_MAINTAIN) {
2826 chunk_printf(&msg, "%s ",
2827 human_time(now.tv_sec - sv->last_change, 1));
2828 chunk_printf(&msg, "MAINT");
2829 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002830 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2831 chunk_printf(&msg, "%s ",
2832 human_time(now.tv_sec - svs->last_change, 1));
2833 chunk_printf(&msg, "MAINT(via)");
2834 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002835 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002836 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002837 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002838
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002839 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002840 srv_hlt_st[sv_state],
2841 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2842 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002843 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002844
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002845 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002846 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002847 get_check_status_description(sv->check_status));
2848
2849 if (*sv->check_desc) {
2850 struct chunk src;
2851
2852 chunk_printf(&msg, ": ");
2853
2854 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2855 chunk_htmlencode(&msg, &src);
2856 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002857
Willy Tarreaue0454092010-02-26 12:29:07 +01002858 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002859 tv_iszero(&sv->check_start)?"":"* ",
2860 get_check_status_info(sv->check_status));
2861
2862 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002863 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002864
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002865 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002866 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002867 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002868 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002869
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002870 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002871 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002872 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002873 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002874 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002875 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002876 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002877 (sv->state & SRV_BACKUP) ? "-" : "Y",
2878 (sv->state & SRV_BACKUP) ? "Y" : "-");
2879
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002880 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002881 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002882 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002883 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2884
2885 if (svs->observe)
2886 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2887
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002888 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002889 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002890 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002891 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002892 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002893 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002894 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002895 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2896 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002897 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002898 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002899 "<td colspan=3></td>");
2900
2901 /* throttle */
2902 if ((sv->state & SRV_WARMINGUP) &&
2903 now.tv_sec < sv->last_change + sv->slowstart &&
2904 now.tv_sec >= sv->last_change) {
2905 unsigned int ratio;
2906 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002907 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002908 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002909 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002910 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002911 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002912 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002913 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002914 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2915 "UP %d/%d,", "UP,",
2916 "NOLB %d/%d,", "NOLB,",
2917 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002918 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002919 /* pxid, name */
2920 "%s,%s,"
2921 /* queue : current, max */
2922 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002923 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002924 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002925 /* bytes : in, out */
2926 "%lld,%lld,"
2927 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002928 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002929 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002930 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002931 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002932 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002933 "",
2934 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002935 sv->nbpend, sv->counters.nbpend_max,
2936 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002937 sv->counters.bytes_in, sv->counters.bytes_out,
2938 sv->counters.failed_secu,
2939 sv->counters.failed_conns, sv->counters.failed_resp,
2940 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002941
Willy Tarreau55bb8452007-10-17 18:44:57 +02002942 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002943 if (sv->state & SRV_MAINTAIN) {
2944 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002945 }
2946 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2947 chunk_printf(&msg, "MAINT(via),");
2948 }
2949 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002950 chunk_printf(&msg,
2951 srv_hlt_st[sv_state],
2952 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2953 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2954 }
Willy Tarreau91861262007-10-17 17:06:05 +02002955
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002956 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002957 /* weight, active, backup */
2958 "%d,%d,%d,"
2959 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002960 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002961 (sv->state & SRV_BACKUP) ? 0 : 1,
2962 (sv->state & SRV_BACKUP) ? 1 : 0);
2963
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002964 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002965 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002966 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002967 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002968 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002969 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002970 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002971 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002972 ",,,,");
2973
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002974 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002975 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002976 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002977 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002978 LIM2A0(sv->maxqueue, ""),
2979 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002980
2981 /* throttle */
2982 if ((sv->state & SRV_WARMINGUP) &&
2983 now.tv_sec < sv->last_change + sv->slowstart &&
2984 now.tv_sec >= sv->last_change) {
2985 unsigned int ratio;
2986 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002987 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002988 }
2989
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002990 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002991 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002992
2993 /* tracked */
Willy Tarreau44267702011-10-28 15:35:33 +02002994 if (sv->track)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002995 chunk_printf(&msg, "%s/%s,",
Willy Tarreau44267702011-10-28 15:35:33 +02002996 sv->track->proxy->id, sv->track->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002997 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002998 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002999
Willy Tarreau7f062c42009-03-05 18:43:00 +01003000 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003001 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01003002
3003 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003004 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003005 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02003006 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01003007
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003008 if (sv->state & SRV_CHECKED) {
3009 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003010 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003011
3012 /* check_code */
3013 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003014 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003015 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003016 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003017
3018 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01003019 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003020 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003021 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003022 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003023
3024 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003025 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003026 }
3027
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003028 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3029 if (px->mode == PR_MODE_HTTP) {
3030 int i;
3031
3032 for (i=1; i<6; i++)
3033 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
3034
3035 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
3036 } else {
3037 chunk_printf(&msg, ",,,,,,");
3038 }
3039
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003040 /* failed health analyses */
3041 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
3042
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003043 /* requests : req_rate, req_rate_max, req_tot, */
3044 chunk_printf(&msg, ",,,");
3045
Willy Tarreauae526782010-03-04 20:34:23 +01003046 /* errors: cli_aborts, srv_aborts */
3047 chunk_printf(&msg, "%lld,%lld,",
3048 sv->counters.cli_aborts, sv->counters.srv_aborts);
3049
Willy Tarreau7f062c42009-03-05 18:43:00 +01003050 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003051 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003052 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003053 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003054 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003055 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02003056
Willy Tarreau295a8372011-03-10 11:25:07 +01003057 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02003058 /* fall through */
3059
Willy Tarreau295a8372011-03-10 11:25:07 +01003060 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02003061 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003062 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01003063 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
3064 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003065 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01003066 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003067 /* Column sub-heading for Enable or Disable server */
3068 chunk_printf(&msg, "<td></td>");
3069 }
3070 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003071
3072 if (uri->flags&ST_SHLGNDS) {
3073 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003074 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003075 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003076
3077 /* cookie */
3078 if (px->cookie_name) {
3079 struct chunk src;
3080
3081 chunk_printf(&msg, ", cookie: '");
3082
3083 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3084 chunk_htmlencode(&msg, &src);
3085
3086 chunk_printf(&msg, "'");
3087 }
3088
3089 chunk_printf(&msg, "\"");
3090
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003091 }
3092
3093 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02003094 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01003095 ">%s<a name=\"%s/Backend\"></a>"
3096 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02003097 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003098 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003099 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003100 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003101 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003102 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02003103 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01003104 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003105 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
3106 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02003107
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003108 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003109 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003110 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003111 "<td"
3112 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003113 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003114
3115 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3116 if (px->mode == PR_MODE_HTTP) {
3117 int i;
3118
3119 chunk_printf(&msg, " title=\"rsp codes:");
3120
3121 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003122 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003123
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003124 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003125 }
3126
3127 chunk_printf(&msg,
3128 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01003129 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003130 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003131 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003132 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003133 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003134 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01003135 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003136 U2H7(px->be_counters.cum_lbconn),
3137 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003138
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003139 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02003140 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003141 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01003142 /* errors : request, connect */
3143 "<td></td><td>%s</td>"
3144 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01003145 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003146 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003147 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003148 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02003149 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003150 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02003151 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003152 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3153 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003154 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003155 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
3156 U2H2(px->be_counters.failed_conns),
3157 px->be_counters.cli_aborts,
3158 px->be_counters.srv_aborts,
3159 U2H5(px->be_counters.failed_resp),
3160 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003161 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01003162 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3163 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01003164 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003165 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003166
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003167 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003168 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003169 "<td class=ac>&nbsp;</td><td>%d</td>"
3170 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003171 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003172 "</tr>",
3173 px->down_trans,
3174 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003175 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003176 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02003177 /* pxid, name */
3178 "%s,BACKEND,"
3179 /* queue : current, max */
3180 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003181 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003182 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003183 /* bytes : in, out */
3184 "%lld,%lld,"
3185 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003186 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003187 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003188 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003189 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003190 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003191 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02003192 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003193 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02003194 * active and backups. */
3195 "%s,"
3196 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003197 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003198 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003199 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003200 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003201 /* rate, rate_lim, rate_max, */
3202 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003203 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003204 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02003205 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003206 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3207 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3208 px->be_counters.bytes_in, px->be_counters.bytes_out,
3209 px->be_counters.denied_req, px->be_counters.denied_resp,
3210 px->be_counters.failed_conns, px->be_counters.failed_resp,
3211 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01003212 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01003213 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003214 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003215 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01003216 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003217 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003218 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003219 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003220 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003221
3222 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3223 if (px->mode == PR_MODE_HTTP) {
3224 int i;
3225
3226 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003227 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003228
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003229 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003230 } else {
3231 chunk_printf(&msg, ",,,,,,");
3232 }
3233
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003234 /* failed health analyses */
3235 chunk_printf(&msg, ",");
3236
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003237 /* requests : req_rate, req_rate_max, req_tot, */
3238 chunk_printf(&msg, ",,,");
3239
Willy Tarreauae526782010-03-04 20:34:23 +01003240 /* errors: cli_aborts, srv_aborts */
3241 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003242 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01003243
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003244 /* finish with EOL */
3245 chunk_printf(&msg, "\n");
3246
Willy Tarreau55bb8452007-10-17 18:44:57 +02003247 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003248 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003249 return 0;
3250 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003251
Willy Tarreau295a8372011-03-10 11:25:07 +01003252 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02003253 /* fall through */
3254
Willy Tarreau295a8372011-03-10 11:25:07 +01003255 case STAT_PX_ST_END:
3256 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003257 chunk_printf(&msg, "</table>");
3258
Willy Tarreau295a8372011-03-10 11:25:07 +01003259 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003260 /* close the form used to enable/disable this proxy servers */
3261 chunk_printf(&msg,
3262 "Choose the action to perform on the checked servers : "
3263 "<select name=action>"
3264 "<option value=\"\"></option>"
3265 "<option value=\"disable\">Disable</option>"
3266 "<option value=\"enable\">Enable</option>"
Willy Tarreaud7282242012-06-04 00:22:44 +02003267 "<option value=\"stop\">Soft Stop</option>"
3268 "<option value=\"start\">Soft Start</option>"
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003269 "<option value=\"shutdown\">Kill Sessions</option>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003270 "</select>"
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003271 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003272 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3273 "</form>",
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003274 px->uuid);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003275 }
3276
3277 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02003278
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003279 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003280 return 0;
3281 }
Willy Tarreau91861262007-10-17 17:06:05 +02003282
Willy Tarreau295a8372011-03-10 11:25:07 +01003283 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02003284 /* fall through */
3285
Willy Tarreau295a8372011-03-10 11:25:07 +01003286 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02003287 return 1;
3288
3289 default:
3290 /* unknown state, we should put an abort() here ! */
3291 return 1;
3292 }
3293}
3294
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003295/* This function dumps a complete session state onto the stream intreface's
3296 * read buffer. The data_ctx must have been zeroed first, and the flags
3297 * properly set. The session has to be set in data_ctx.sess.target. It returns
3298 * 0 if the output buffer is full and it needs to be called again, otherwise
3299 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003300 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003301static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003302{
3303 struct tm tm;
3304 struct chunk msg;
3305 struct session *sess;
3306 extern const char *monthname[12];
3307 char pn[INET6_ADDRSTRLEN];
3308
David du Colombier7af46052012-05-16 14:16:48 +02003309 chunk_init(&msg, trash, trashlen);
Willy Tarreau295a8372011-03-10 11:25:07 +01003310 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003311
Willy Tarreau295a8372011-03-10 11:25:07 +01003312 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003313 /* session changed, no need to go any further */
3314 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003315 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003316 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003317 si->applet.ctx.sess.target = NULL;
3318 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003319 return 1;
3320 }
3321
Willy Tarreau295a8372011-03-10 11:25:07 +01003322 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003323 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003324 si->applet.ctx.sess.uid = sess->uniq_id;
3325 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003326 /* fall through */
3327
3328 case 1:
3329 chunk_printf(&msg,
3330 "%p: id=%u, proto=%s",
3331 sess,
3332 sess->uniq_id,
3333 sess->listener->proto->name);
3334
Willy Tarreau986a9d22012-08-30 21:11:38 +02003335 switch (addr_to_str(&sess->si[0].conn.addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003336 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003337 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003338 chunk_printf(&msg, " source=%s:%d\n",
Willy Tarreau986a9d22012-08-30 21:11:38 +02003339 pn, get_host_port(&sess->si[0].conn.addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003340 break;
3341 case AF_UNIX:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003342 chunk_printf(&msg, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003343 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003344 default:
3345 /* no more information to print right now */
3346 chunk_printf(&msg, "\n");
3347 break;
3348 }
3349
3350 chunk_printf(&msg,
3351 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003352 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003353
3354 chunk_printf(&msg,
Daniel Schultze90690c72012-03-23 10:53:36 -07003355 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003356 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3357 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3358 sess->listener ? sess->listener->luid : 0);
3359
Willy Tarreau986a9d22012-08-30 21:11:38 +02003360 conn_get_to_addr(&sess->si[0].conn);
3361 switch (addr_to_str(&sess->si[0].conn.addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003362 case AF_INET:
3363 case AF_INET6:
3364 chunk_printf(&msg, " addr=%s:%d\n",
Willy Tarreau986a9d22012-08-30 21:11:38 +02003365 pn, get_host_port(&sess->si[0].conn.addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003366 break;
3367 case AF_UNIX:
3368 chunk_printf(&msg, " addr=unix:%d\n", sess->listener->luid);
3369 break;
3370 default:
3371 /* no more information to print right now */
3372 chunk_printf(&msg, "\n");
3373 break;
3374 }
3375
Willy Tarreau50943332011-09-02 17:33:05 +02003376 if (sess->be->cap & PR_CAP_BE)
3377 chunk_printf(&msg,
Daniel Schultze90690c72012-03-23 10:53:36 -07003378 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003379 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003380 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3381 else
3382 chunk_printf(&msg, " backend=<NONE> (id=-1 mode=-)");
3383
Willy Tarreau986a9d22012-08-30 21:11:38 +02003384 conn_get_from_addr(&sess->si[1].conn);
3385 switch (addr_to_str(&sess->si[1].conn.addr.from, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003386 case AF_INET:
3387 case AF_INET6:
3388 chunk_printf(&msg, " addr=%s:%d\n",
Willy Tarreau986a9d22012-08-30 21:11:38 +02003389 pn, get_host_port(&sess->si[1].conn.addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07003390 break;
3391 case AF_UNIX:
3392 chunk_printf(&msg, " addr=unix\n");
3393 break;
3394 default:
3395 /* no more information to print right now */
3396 chunk_printf(&msg, "\n");
3397 break;
3398 }
3399
3400 if (sess->be->cap & PR_CAP_BE)
3401 chunk_printf(&msg,
3402 " server=%s (id=%u)",
Willy Tarreau50943332011-09-02 17:33:05 +02003403 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
3404 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
3405 else
Daniel Schultze90690c72012-03-23 10:53:36 -07003406 chunk_printf(&msg, " server=<NONE> (id=-1)");
3407
Willy Tarreau986a9d22012-08-30 21:11:38 +02003408 conn_get_to_addr(&sess->si[1].conn);
3409 switch (addr_to_str(&sess->si[1].conn.addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003410 case AF_INET:
3411 case AF_INET6:
3412 chunk_printf(&msg, " addr=%s:%d\n",
Willy Tarreau986a9d22012-08-30 21:11:38 +02003413 pn, get_host_port(&sess->si[1].conn.addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003414 break;
3415 case AF_UNIX:
3416 chunk_printf(&msg, " addr=unix\n");
3417 break;
3418 default:
3419 /* no more information to print right now */
3420 chunk_printf(&msg, "\n");
3421 break;
3422 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003423
3424 chunk_printf(&msg,
3425 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
3426 sess->task,
3427 sess->task->state,
3428 sess->task->nice, sess->task->calls,
3429 sess->task->expire ?
3430 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3431 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3432 TICKS_TO_MS(1000)) : "<NEVER>",
3433 task_in_rq(sess->task) ? ", running" : "");
3434
3435 get_localtime(sess->logs.accept_date.tv_sec, &tm);
3436 chunk_printf(&msg,
3437 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
3438 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3439 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3440 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3441
3442 chunk_printf(&msg,
3443 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3444 &sess->si[0],
3445 sess->si[0].state,
3446 sess->si[0].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003447 si_fd(&sess->si[0]),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003448 sess->si[0].exp ?
3449 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3450 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3451 TICKS_TO_MS(1000)) : "<NEVER>",
3452 sess->si[0].err_type);
3453
3454 chunk_printf(&msg,
3455 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3456 &sess->si[1],
3457 sess->si[1].state,
3458 sess->si[1].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003459 si_fd(&sess->si[1]),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003460 sess->si[1].exp ?
3461 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3462 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3463 TICKS_TO_MS(1000)) : "<NEVER>",
3464 sess->si[1].err_type);
3465
3466 chunk_printf(&msg,
3467 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3468 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3469 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3470
3471
3472 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003473 " req=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003474 " an_exp=%s",
3475 sess->req,
3476 sess->req->flags, sess->req->analysers,
Willy Tarreau572bf902012-07-02 17:01:20 +02003477 sess->req->buf.i, sess->req->buf.o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003478 sess->req->pipe ? sess->req->pipe->data : 0,
3479 sess->req->to_forward,
3480 sess->req->analyse_exp ?
3481 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3482 TICKS_TO_MS(1000)) : "<NEVER>");
3483
3484 chunk_printf(&msg,
3485 " rex=%s",
3486 sess->req->rex ?
3487 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3488 TICKS_TO_MS(1000)) : "<NEVER>");
3489
3490 chunk_printf(&msg,
3491 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003492 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003493 sess->req->wex ?
3494 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3495 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau572bf902012-07-02 17:01:20 +02003496 sess->req->buf.data,
3497 (int)(sess->req->buf.p - sess->req->buf.data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003498 sess->txn.req.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003499 sess->req->total);
3500
3501 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003502 " res=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003503 " an_exp=%s",
3504 sess->rep,
3505 sess->rep->flags, sess->rep->analysers,
Willy Tarreau572bf902012-07-02 17:01:20 +02003506 sess->rep->buf.i, sess->rep->buf.o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003507 sess->rep->pipe ? sess->rep->pipe->data : 0,
3508 sess->rep->to_forward,
3509 sess->rep->analyse_exp ?
3510 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3511 TICKS_TO_MS(1000)) : "<NEVER>");
3512
3513 chunk_printf(&msg,
3514 " rex=%s",
3515 sess->rep->rex ?
3516 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3517 TICKS_TO_MS(1000)) : "<NEVER>");
3518
3519 chunk_printf(&msg,
3520 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003521 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003522 sess->rep->wex ?
3523 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3524 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau572bf902012-07-02 17:01:20 +02003525 sess->rep->buf.data,
3526 (int)(sess->rep->buf.p - sess->rep->buf.data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003527 sess->txn.rsp.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003528 sess->rep->total);
3529
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003530 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003531 return 0;
3532
3533 /* use other states to dump the contents */
3534 }
3535 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003536 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003537 return 1;
3538}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003539
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003540/* This function dumps all sessions' states onto the stream intreface's
3541 * read buffer. The data_ctx must have been zeroed first, and the flags
3542 * properly set. It returns 0 if the output buffer is full and it needs
3543 * to be called again, otherwise non-zero. It is designed to be called
3544 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003545 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003546static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003547{
3548 struct chunk msg;
3549
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003550 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003551 /* If we're forced to shut down, we might have to remove our
3552 * reference to the last session being dumped.
3553 */
Willy Tarreau94981132012-05-21 17:09:48 +02003554 if (si->conn.data_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003555 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3556 LIST_DEL(&si->applet.ctx.sess.bref.users);
3557 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003558 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003559 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003560 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003561 }
3562
David du Colombier7af46052012-05-16 14:16:48 +02003563 chunk_init(&msg, trash, trashlen);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003564
Willy Tarreau94981132012-05-21 17:09:48 +02003565 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003566 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003567 /* the function had not been called yet, let's prepare the
3568 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003569 * pointer to the first in the global list. When a target
3570 * session is being destroyed, it is responsible for updating
3571 * this pointer. We know we have reached the end when this
3572 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003573 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003574 LIST_INIT(&si->applet.ctx.sess.bref.users);
3575 si->applet.ctx.sess.bref.ref = sessions.n;
Willy Tarreau94981132012-05-21 17:09:48 +02003576 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003577 /* fall through */
3578
Willy Tarreau295a8372011-03-10 11:25:07 +01003579 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003580 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003581 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3582 LIST_DEL(&si->applet.ctx.sess.bref.users);
3583 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003584 }
3585
3586 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003587 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003588 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003589 struct session *curr_sess;
3590
Willy Tarreau295a8372011-03-10 11:25:07 +01003591 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003592
Willy Tarreau295a8372011-03-10 11:25:07 +01003593 if (si->applet.ctx.sess.target) {
3594 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003595 goto next_sess;
3596
Willy Tarreau295a8372011-03-10 11:25:07 +01003597 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003598 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003599 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003600 return 0;
3601
3602 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003603 LIST_DEL(&si->applet.ctx.sess.bref.users);
3604 LIST_INIT(&si->applet.ctx.sess.bref.users);
3605 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003606 break;
3607 }
3608
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003609 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003610 "%p: proto=%s",
3611 curr_sess,
3612 curr_sess->listener->proto->name);
3613
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003614
Willy Tarreau986a9d22012-08-30 21:11:38 +02003615 switch (addr_to_str(&curr_sess->si[0].conn.addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003616 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003617 case AF_INET6:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003618 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003619 " src=%s:%d fe=%s be=%s srv=%s",
3620 pn,
Willy Tarreau986a9d22012-08-30 21:11:38 +02003621 get_host_port(&curr_sess->si[0].conn.addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003622 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003623 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003624 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003625 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003626 break;
3627 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02003628 chunk_printf(&msg,
3629 " src=unix:%d fe=%s be=%s srv=%s",
3630 curr_sess->listener->luid,
3631 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003632 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003633 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003634 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003635 break;
3636 }
3637
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003638 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003639 " ts=%02x age=%s calls=%d",
3640 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003641 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3642 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003643
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003644 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003645 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003646 curr_sess->req->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02003647 curr_sess->req->buf.i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003648 curr_sess->req->analysers,
3649 curr_sess->req->rex ?
3650 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3651 TICKS_TO_MS(1000)) : "");
3652
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003653 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003654 ",wx=%s",
3655 curr_sess->req->wex ?
3656 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3657 TICKS_TO_MS(1000)) : "");
3658
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003659 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003660 ",ax=%s]",
3661 curr_sess->req->analyse_exp ?
3662 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3663 TICKS_TO_MS(1000)) : "");
3664
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003665 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003666 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003667 curr_sess->rep->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02003668 curr_sess->rep->buf.i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003669 curr_sess->rep->analysers,
3670 curr_sess->rep->rex ?
3671 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3672 TICKS_TO_MS(1000)) : "");
3673
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003674 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003675 ",wx=%s",
3676 curr_sess->rep->wex ?
3677 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3678 TICKS_TO_MS(1000)) : "");
3679
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003680 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003681 ",ax=%s]",
3682 curr_sess->rep->analyse_exp ?
3683 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3684 TICKS_TO_MS(1000)) : "");
3685
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003686 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003687 " s0=[%d,%1xh,fd=%d,ex=%s]",
3688 curr_sess->si[0].state,
3689 curr_sess->si[0].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003690 si_fd(&curr_sess->si[0]),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003691 curr_sess->si[0].exp ?
3692 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3693 TICKS_TO_MS(1000)) : "");
3694
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003695 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003696 " s1=[%d,%1xh,fd=%d,ex=%s]",
3697 curr_sess->si[1].state,
3698 curr_sess->si[1].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003699 si_fd(&curr_sess->si[1]),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003700 curr_sess->si[1].exp ?
3701 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3702 TICKS_TO_MS(1000)) : "");
3703
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003704 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003705 " exp=%s",
3706 curr_sess->task->expire ?
3707 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3708 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003709 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003710 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003711
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003712 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003713
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003714 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003715 /* let's try again later from this session. We add ourselves into
3716 * this session's users so that it can remove us upon termination.
3717 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003718 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003719 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003720 }
3721
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003722 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003723 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003724 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003725
Willy Tarreau295a8372011-03-10 11:25:07 +01003726 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003727 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003728 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003729 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3730 else
3731 chunk_printf(&msg, "Session not found.\n");
3732
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003733 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003734 return 0;
3735
Willy Tarreau295a8372011-03-10 11:25:07 +01003736 si->applet.ctx.sess.target = NULL;
3737 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003738 return 1;
3739 }
3740
Willy Tarreau94981132012-05-21 17:09:48 +02003741 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003742 /* fall through */
3743
3744 default:
Willy Tarreau94981132012-05-21 17:09:48 +02003745 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003746 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003747 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003748}
3749
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003750/* This function dumps all tables' states onto the stream intreface's
3751 * read buffer. The data_ctx must have been zeroed first, and the flags
3752 * properly set. It returns 0 if the output buffer is full and it needs
3753 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003754 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003755static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003756{
Willy Tarreau94981132012-05-21 17:09:48 +02003757 struct session *s = si->conn.data_ctx;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003758 struct chunk msg;
3759 struct ebmb_node *eb;
3760 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003761 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003762
3763 /*
Willy Tarreau94981132012-05-21 17:09:48 +02003764 * We have 3 possible states in si->conn.data_st :
Willy Tarreau295a8372011-03-10 11:25:07 +01003765 * - STAT_ST_INIT : the first call
3766 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003767 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003768 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003769 * and the entry pointer points to the next entry to be dumped,
3770 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003771 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003772 * data though.
3773 */
3774
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003775 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003776 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau94981132012-05-21 17:09:48 +02003777 if (si->conn.data_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003778 si->applet.ctx.table.entry->ref_cnt--;
3779 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003780 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003781 return 1;
3782 }
3783
David du Colombier7af46052012-05-16 14:16:48 +02003784 chunk_init(&msg, trash, trashlen);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003785
Willy Tarreau94981132012-05-21 17:09:48 +02003786 while (si->conn.data_st != STAT_ST_FIN) {
3787 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003788 case STAT_ST_INIT:
3789 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3790 if (!si->applet.ctx.table.proxy)
3791 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003792
Willy Tarreau295a8372011-03-10 11:25:07 +01003793 si->applet.ctx.table.entry = NULL;
Willy Tarreau94981132012-05-21 17:09:48 +02003794 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003795 break;
3796
Willy Tarreau295a8372011-03-10 11:25:07 +01003797 case STAT_ST_INFO:
3798 if (!si->applet.ctx.table.proxy ||
3799 (si->applet.ctx.table.target &&
3800 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
Willy Tarreau94981132012-05-21 17:09:48 +02003801 si->conn.data_st = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003802 break;
3803 }
3804
Willy Tarreau295a8372011-03-10 11:25:07 +01003805 if (si->applet.ctx.table.proxy->table.size) {
Simon Hormanc88b8872011-06-15 15:18:49 +09003806 if (show && !stats_dump_table_head_to_buffer(&msg, si, si->applet.ctx.table.proxy,
3807 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003808 return 0;
3809
Willy Tarreau295a8372011-03-10 11:25:07 +01003810 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003811 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
3812 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003813 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003814 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003815 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3816 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau94981132012-05-21 17:09:48 +02003817 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003818 break;
3819 }
3820 }
3821 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003822 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003823 break;
3824
Willy Tarreau295a8372011-03-10 11:25:07 +01003825 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003826 skip_entry = false;
3827
Willy Tarreau295a8372011-03-10 11:25:07 +01003828 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003829 /* we're filtering on some data contents */
3830 void *ptr;
3831 long long data;
3832
Willy Tarreau295a8372011-03-10 11:25:07 +01003833 dt = si->applet.ctx.table.data_type;
3834 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3835 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003836 dt);
3837
3838 data = 0;
3839 switch (stktable_data_types[dt].std_type) {
3840 case STD_T_SINT:
3841 data = stktable_data_cast(ptr, std_t_sint);
3842 break;
3843 case STD_T_UINT:
3844 data = stktable_data_cast(ptr, std_t_uint);
3845 break;
3846 case STD_T_ULL:
3847 data = stktable_data_cast(ptr, std_t_ull);
3848 break;
3849 case STD_T_FRQP:
3850 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003851 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003852 break;
3853 }
3854
3855 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003856 if ((data < si->applet.ctx.table.value &&
3857 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3858 si->applet.ctx.table.data_op == STD_OP_GT ||
3859 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3860 (data == si->applet.ctx.table.value &&
3861 (si->applet.ctx.table.data_op == STD_OP_NE ||
3862 si->applet.ctx.table.data_op == STD_OP_GT ||
3863 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3864 (data > si->applet.ctx.table.value &&
3865 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3866 si->applet.ctx.table.data_op == STD_OP_LT ||
3867 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003868 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003869 }
3870
Simon Hormanc88b8872011-06-15 15:18:49 +09003871 if (show && !skip_entry &&
3872 !stats_dump_table_entry_to_buffer(&msg, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003873 si->applet.ctx.table.entry))
3874 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003875
Willy Tarreau295a8372011-03-10 11:25:07 +01003876 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003877
Willy Tarreau295a8372011-03-10 11:25:07 +01003878 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003879 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003880 struct stksess *old = si->applet.ctx.table.entry;
3881 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01003882 if (show)
3883 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3884 else
3885 stksess_kill(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau295a8372011-03-10 11:25:07 +01003886 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003887 break;
3888 }
3889
Simon Hormanc88b8872011-06-15 15:18:49 +09003890
3891 if (show)
3892 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3893 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3894 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3895
Willy Tarreau295a8372011-03-10 11:25:07 +01003896 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau94981132012-05-21 17:09:48 +02003897 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003898 break;
3899
Willy Tarreau295a8372011-03-10 11:25:07 +01003900 case STAT_ST_END:
Willy Tarreau94981132012-05-21 17:09:48 +02003901 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003902 break;
3903 }
3904 }
3905 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003906}
3907
Willy Tarreaud426a182010-03-05 14:58:26 +01003908/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003909 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3910 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3911 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3912 * lines are respected within the limit of 70 output chars. Lines that are
3913 * continuation of a previous truncated line begin with "+" instead of " "
3914 * after the offset. The new pointer is returned.
3915 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003916static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3917 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003918{
3919 int end;
3920 unsigned char c;
3921
3922 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003923 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003924 return ptr;
3925
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003926 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003927
Willy Tarreaud426a182010-03-05 14:58:26 +01003928 while (ptr < len && ptr < bsize) {
3929 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003930 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003931 if (out->len > end - 2)
3932 break;
3933 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003934 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003935 if (out->len > end - 3)
3936 break;
3937 out->str[out->len++] = '\\';
3938 switch (c) {
3939 case '\t': c = 't'; break;
3940 case '\n': c = 'n'; break;
3941 case '\r': c = 'r'; break;
3942 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003943 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003944 }
3945 out->str[out->len++] = c;
3946 } else {
3947 if (out->len > end - 5)
3948 break;
3949 out->str[out->len++] = '\\';
3950 out->str[out->len++] = 'x';
3951 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3952 out->str[out->len++] = hextab[c & 0xF];
3953 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003954 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003955 /* we had a line break, let's return now */
3956 out->str[out->len++] = '\n';
3957 *line = ptr;
3958 return ptr;
3959 }
3960 }
3961 /* we have an incomplete line, we return it as-is */
3962 out->str[out->len++] = '\n';
3963 return ptr;
3964}
3965
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003966/* This function dumps all captured errors onto the stream intreface's
3967 * read buffer. The data_ctx must have been zeroed first, and the flags
3968 * properly set. It returns 0 if the output buffer is full and it needs
3969 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003970 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003971static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003972{
3973 extern const char *monthname[12];
3974 struct chunk msg;
3975
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003976 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003977 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003978
David du Colombier7af46052012-05-16 14:16:48 +02003979 chunk_init(&msg, trash, trashlen);
Willy Tarreau74808cb2009-03-04 15:53:18 +01003980
Willy Tarreau295a8372011-03-10 11:25:07 +01003981 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003982 /* the function had not been called yet, let's prepare the
3983 * buffer for a response.
3984 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003985 struct tm tm;
3986
3987 get_localtime(date.tv_sec, &tm);
3988 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3989 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3990 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3991 error_snapshot_id);
3992
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003993 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003994 /* Socket buffer full. Let's try again later from the same point */
3995 return 0;
3996 }
3997
Willy Tarreau295a8372011-03-10 11:25:07 +01003998 si->applet.ctx.errors.px = proxy;
3999 si->applet.ctx.errors.buf = 0;
4000 si->applet.ctx.errors.bol = 0;
4001 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004002 }
4003
4004 /* we have two inner loops here, one for the proxy, the other one for
4005 * the buffer.
4006 */
Willy Tarreau295a8372011-03-10 11:25:07 +01004007 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004008 struct error_snapshot *es;
4009
Willy Tarreau295a8372011-03-10 11:25:07 +01004010 if (si->applet.ctx.errors.buf == 0)
4011 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004012 else
Willy Tarreau295a8372011-03-10 11:25:07 +01004013 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004014
4015 if (!es->when.tv_sec)
4016 goto next;
4017
Willy Tarreau295a8372011-03-10 11:25:07 +01004018 if (si->applet.ctx.errors.iid >= 0 &&
4019 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
4020 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004021 goto next;
4022
Willy Tarreau295a8372011-03-10 11:25:07 +01004023 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004024 /* just print headers now */
4025
4026 char pn[INET6_ADDRSTRLEN];
4027 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004028 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004029
4030 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01004031 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01004032 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02004033 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01004034
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004035 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
4036 case AF_INET:
4037 case AF_INET6:
4038 port = get_host_port(&es->src);
4039 break;
4040 default:
4041 port = 0;
4042 }
4043
Willy Tarreau295a8372011-03-10 11:25:07 +01004044 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004045 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004046 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004047 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004048 " backend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004049 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004050 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
4051 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004052 break;
4053 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004054 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004055 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004056 " frontend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004057 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004058 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004059 break;
4060 }
4061
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004062 chunk_printf(&msg,
4063 ", server %s (#%d), event #%u\n"
4064 " src %s:%d, session #%d, session flags 0x%08x\n"
4065 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
4066 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
4067 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
4068 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
4069 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
4070 es->ev_id,
4071 pn, port, es->sid, es->s_flags,
4072 es->state, es->m_flags, es->t_flags,
4073 es->m_clen, es->m_blen,
4074 es->b_flags, es->b_out, es->b_tot,
4075 es->len, es->b_wrap, es->pos);
4076
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004077 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004078 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004079 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004080 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004081 si->applet.ctx.errors.ptr = 0;
4082 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004083 }
4084
Willy Tarreau295a8372011-03-10 11:25:07 +01004085 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004086 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004087 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004088 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004089 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004090 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004091 goto next;
4092 }
4093
4094 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01004095 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004096 int newptr;
4097 int newline;
4098
Willy Tarreau295a8372011-03-10 11:25:07 +01004099 newline = si->applet.ctx.errors.bol;
4100 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
4101 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004102 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004103
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004104 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004105 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004106 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004107 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004108 si->applet.ctx.errors.ptr = newptr;
4109 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004110 };
4111 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01004112 si->applet.ctx.errors.bol = 0;
4113 si->applet.ctx.errors.ptr = -1;
4114 si->applet.ctx.errors.buf++;
4115 if (si->applet.ctx.errors.buf > 1) {
4116 si->applet.ctx.errors.buf = 0;
4117 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004118 }
4119 }
4120
4121 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004122 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004123}
4124
Willy Tarreaub24281b2011-02-13 13:16:36 +01004125struct si_applet http_stats_applet = {
4126 .name = "<STATS>", /* used for logging */
4127 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004128 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004129};
4130
Simon Horman9bd2c732011-06-15 15:18:44 +09004131static struct si_applet cli_applet = {
Willy Tarreaub24281b2011-02-13 13:16:36 +01004132 .name = "<CLI>", /* used for logging */
4133 .fct = cli_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004134 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004135};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004136
Willy Tarreau10522fd2008-07-09 20:12:41 +02004137static struct cfg_kw_list cfg_kws = {{ },{
4138 { CFG_GLOBAL, "stats", stats_parse_global },
4139 { 0, NULL, NULL },
4140}};
4141
4142__attribute__((constructor))
4143static void __dumpstats_module_init(void)
4144{
4145 cfg_register_keywords(&cfg_kws);
4146}
4147
Willy Tarreau91861262007-10-17 17:06:05 +02004148/*
4149 * Local variables:
4150 * c-indent-level: 8
4151 * c-basic-offset: 8
4152 * End:
4153 */