blob: 8564e2d943a2b927fb1c9e30d2e2ca03d01dcbe0 [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 */
154static struct proxy *alloc_stats_fe(const char *name)
155{
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 */
169
Willy Tarreau07e9e642010-08-17 21:48:17 +0200170 return fe;
171}
172
Willy Tarreaufbee7132007-10-18 13:53:22 +0200173/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200174 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
175 * error message into the <err> buffer which will be preallocated. The trailing
176 * '\n' must not be written. The function must be called with <args> pointing to
177 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200178 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200179static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200180 struct proxy *defpx, const char *file, int line,
181 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200182{
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200183 if (!strcmp(args[1], "socket")) {
Andreas Kohn16171e22011-01-19 20:29:32 +0100184 struct sockaddr_un *su;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200185 int cur_arg;
186
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200187 if (*args[2] == 0) {
188 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 +0200189 return -1;
190 }
191
192 if (global.stats_sock.state != LI_NEW) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200193 memprintf(err, "'%s %s' already specified in global section", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200194 return -1;
195 }
196
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200197 su = str2sun(args[2]);
Andreas Kohn16171e22011-01-19 20:29:32 +0100198 if (!su) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200199 memprintf(err, "'%s %s' : path would require truncation", args[0], args[1]);
Andreas Kohn16171e22011-01-19 20:29:32 +0100200 return -1;
201 }
202 memcpy(&global.stats_sock.addr, su, sizeof(struct sockaddr_un)); // guaranteed to fit
Willy Tarreaufbee7132007-10-18 13:53:22 +0200203
Willy Tarreau89a63132009-08-16 17:41:45 +0200204 if (!global.stats_fe) {
Willy Tarreau07e9e642010-08-17 21:48:17 +0200205 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200206 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200207 return -1;
208 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200209 }
210
Willy Tarreaufbee7132007-10-18 13:53:22 +0200211 global.stats_sock.state = LI_INIT;
Willy Tarreau3c63fd82011-09-07 18:00:47 +0200212 global.stats_sock.options = LI_O_UNLIMITED;
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200213 global.stats_sock.accept = session_accept;
214 global.stats_fe->accept = stats_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200215 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200216 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200217 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreaueb472682010-05-28 18:46:57 +0200218 global.stats_sock.frontend = global.stats_fe;
Willy Tarreau6162db22009-10-10 17:13:00 +0200219 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200220 global.stats_sock.maxconn = global.stats_fe->maxconn;
Willy Tarreau89a63132009-08-16 17:41:45 +0200221 global.stats_sock.timeout = &global.stats_fe->timeout.client;
222
223 global.stats_sock.next = global.stats_fe->listen;
224 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200225
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200226 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200227 while (*args[cur_arg]) {
228 if (!strcmp(args[cur_arg], "uid")) {
229 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
230 cur_arg += 2;
231 }
232 else if (!strcmp(args[cur_arg], "gid")) {
233 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
234 cur_arg += 2;
235 }
236 else if (!strcmp(args[cur_arg], "mode")) {
237 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
238 cur_arg += 2;
239 }
240 else if (!strcmp(args[cur_arg], "user")) {
241 struct passwd *user;
242 user = getpwnam(args[cur_arg + 1]);
243 if (!user) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200244 memprintf(err, "'%s %s' : unknown user '%s'", args[0], args[1], args[cur_arg + 1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200245 return -1;
246 }
247 global.stats_sock.perm.ux.uid = user->pw_uid;
248 cur_arg += 2;
249 }
250 else if (!strcmp(args[cur_arg], "group")) {
251 struct group *group;
252 group = getgrnam(args[cur_arg + 1]);
253 if (!group) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200254 memprintf(err, "'%s %s' : unknown group '%s'", args[0], args[1], args[cur_arg + 1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200255 return -1;
256 }
257 global.stats_sock.perm.ux.gid = group->gr_gid;
258 cur_arg += 2;
259 }
Willy Tarreau6162db22009-10-10 17:13:00 +0200260 else if (!strcmp(args[cur_arg], "level")) {
261 if (!strcmp(args[cur_arg+1], "user"))
262 global.stats_sock.perm.ux.level = ACCESS_LVL_USER;
263 else if (!strcmp(args[cur_arg+1], "operator"))
264 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER;
265 else if (!strcmp(args[cur_arg+1], "admin"))
266 global.stats_sock.perm.ux.level = ACCESS_LVL_ADMIN;
267 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200268 memprintf(err, "'%s %s' : '%s' only supports 'user', 'operator', and 'admin' (got '%s')",
269 args[0], args[1], args[cur_arg], args[cur_arg+1]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200270 return -1;
271 }
272 cur_arg += 2;
273 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200274 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200275 memprintf(err, "'%s %s' only supports 'user', 'uid', 'group', 'gid', 'level', and 'mode' (got '%s')",
276 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200277 return -1;
278 }
279 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100280
Willy Tarreauc5788912012-08-24 18:12:41 +0200281 global.stats_sock.data = &raw_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200282 uxst_add_listener(&global.stats_sock);
283 global.maxsock++;
284 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200285 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100286 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200287 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100288
289 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200290 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100291 return -1;
292 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200293
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100294 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200295 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200296 return -1;
297 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200298 if (!global.stats_fe) {
299 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200300 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200301 return -1;
302 }
303 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200304 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200305 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200306 else if (!strcmp(args[1], "maxconn")) {
307 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200308
309 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200310 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200311 return -1;
312 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200313
314 if (!global.stats_fe) {
315 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200316 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200317 return -1;
318 }
319 }
320 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200321 }
322 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200323 memprintf(err, "'%s' only supports 'socket', 'maxconn' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200324 return -1;
325 }
326 return 0;
327}
328
Simon Horman9bd2c732011-06-15 15:18:44 +0900329static int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100330{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200331 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100332 "# pxname,svname,"
333 "qcur,qmax,"
334 "scur,smax,slim,stot,"
335 "bin,bout,"
336 "dreq,dresp,"
337 "ereq,econ,eresp,"
338 "wretr,wredis,"
339 "status,weight,act,bck,"
340 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200341 "pid,iid,sid,throttle,lbtot,tracked,type,"
342 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200343 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100344 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100345 "req_rate,req_rate_max,req_tot,"
346 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100347 "\n");
348}
349
Simon Hormand9366582011-06-15 15:18:45 +0900350/* print a string of text buffer to <out>. The format is :
351 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
352 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
353 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
354 */
355static int dump_text(struct chunk *out, const char *buf, int bsize)
356{
357 unsigned char c;
358 int ptr = 0;
359
360 while (buf[ptr] && ptr < bsize) {
361 c = buf[ptr];
362 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
363 if (out->len > out->size - 1)
364 break;
365 out->str[out->len++] = c;
366 }
367 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
368 if (out->len > out->size - 2)
369 break;
370 out->str[out->len++] = '\\';
371 switch (c) {
372 case ' ': c = ' '; break;
373 case '\t': c = 't'; break;
374 case '\n': c = 'n'; break;
375 case '\r': c = 'r'; break;
376 case '\e': c = 'e'; break;
377 case '\\': c = '\\'; break;
378 }
379 out->str[out->len++] = c;
380 }
381 else {
382 if (out->len > out->size - 4)
383 break;
384 out->str[out->len++] = '\\';
385 out->str[out->len++] = 'x';
386 out->str[out->len++] = hextab[(c >> 4) & 0xF];
387 out->str[out->len++] = hextab[c & 0xF];
388 }
389 ptr++;
390 }
391
392 return ptr;
393}
394
395/* print a buffer in hexa.
396 * Print stopped if <bsize> is reached, or if no more place in the chunk.
397 */
398static int dump_binary(struct chunk *out, const char *buf, int bsize)
399{
400 unsigned char c;
401 int ptr = 0;
402
403 while (ptr < bsize) {
404 c = buf[ptr];
405
406 if (out->len > out->size - 2)
407 break;
408 out->str[out->len++] = hextab[(c >> 4) & 0xF];
409 out->str[out->len++] = hextab[c & 0xF];
410
411 ptr++;
412 }
413 return ptr;
414}
415
416/* Dump the status of a table to a stream interface's
417 * read buffer. It returns 0 if the output buffer is full
418 * and needs to be called again, otherwise non-zero.
419 */
420static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
421 struct proxy *proxy, struct proxy *target)
422{
Willy Tarreau94981132012-05-21 17:09:48 +0200423 struct session *s = si->conn.data_ctx;
Simon Hormand9366582011-06-15 15:18:45 +0900424
425 chunk_printf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
426 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
427
428 /* any other information should be dumped here */
429
430 if (target && s->listener->perm.ux.level < ACCESS_LVL_OPER)
431 chunk_printf(msg, "# contents not dumped due to insufficient privileges\n");
432
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200433 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900434 return 0;
435
436 return 1;
437}
438
439/* Dump the a table entry to a stream interface's
440 * read buffer. It returns 0 if the output buffer is full
441 * and needs to be called again, otherwise non-zero.
442 */
443static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
444 struct proxy *proxy, struct stksess *entry)
445{
446 int dt;
447
448 chunk_printf(msg, "%p:", entry);
449
450 if (proxy->table.type == STKTABLE_TYPE_IP) {
451 char addr[INET_ADDRSTRLEN];
452 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
453 chunk_printf(msg, " key=%s", addr);
454 }
455 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
456 char addr[INET6_ADDRSTRLEN];
457 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
458 chunk_printf(msg, " key=%s", addr);
459 }
460 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
461 chunk_printf(msg, " key=%u", *(unsigned int *)entry->key.key);
462 }
463 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
464 chunk_printf(msg, " key=");
465 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
466 }
467 else {
468 chunk_printf(msg, " key=");
469 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
470 }
471
472 chunk_printf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
473
474 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
475 void *ptr;
476
477 if (proxy->table.data_ofs[dt] == 0)
478 continue;
479 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
480 chunk_printf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
481 else
482 chunk_printf(msg, " %s=", stktable_data_types[dt].name);
483
484 ptr = stktable_data_ptr(&proxy->table, entry, dt);
485 switch (stktable_data_types[dt].std_type) {
486 case STD_T_SINT:
487 chunk_printf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
488 break;
489 case STD_T_UINT:
490 chunk_printf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
491 break;
492 case STD_T_ULL:
493 chunk_printf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
494 break;
495 case STD_T_FRQP:
496 chunk_printf(msg, "%d",
497 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
498 proxy->table.data_arg[dt].u));
499 break;
500 }
501 }
502 chunk_printf(msg, "\n");
503
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200504 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900505 return 0;
506
507 return 1;
508}
509
Willy Tarreaudec98142012-06-06 23:37:08 +0200510static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900511{
Willy Tarreau94981132012-05-21 17:09:48 +0200512 struct session *s = si->conn.data_ctx;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900513 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900514 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900515 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900516 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreaudec98142012-06-06 23:37:08 +0200517 struct chunk msg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200518 long long value;
519 int data_type;
520 void *ptr;
521 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900522
Simon Hormand5b9fd92011-06-15 15:18:48 +0900523 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900524
525 if (!*args[4]) {
526 si->applet.ctx.cli.msg = "Key value expected\n";
527 si->applet.st0 = STAT_CLI_PRINT;
528 return;
529 }
530
Simon Hormanc5b89f62011-06-15 15:18:50 +0900531 switch (px->table.type) {
532 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900533 uint32_key = htonl(inetaddr_host(args[4]));
534 static_table_key.key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900535 break;
536 case STKTABLE_TYPE_IPV6:
537 inet_pton(AF_INET6, args[4], ip6_key);
538 static_table_key.key = &ip6_key;
539 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900540 case STKTABLE_TYPE_INTEGER:
541 {
542 char *endptr;
543 unsigned long val;
544 errno = 0;
545 val = strtoul(args[4], &endptr, 10);
546 if ((errno == ERANGE && val == ULONG_MAX) ||
547 (errno != 0 && val == 0) || endptr == args[4] ||
548 val > 0xffffffff) {
549 si->applet.ctx.cli.msg = "Invalid key\n";
550 si->applet.st0 = STAT_CLI_PRINT;
551 return;
552 }
553 uint32_key = (uint32_t) val;
554 static_table_key.key = &uint32_key;
555 break;
556 }
557 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900558 case STKTABLE_TYPE_STRING:
559 static_table_key.key = args[4];
560 static_table_key.key_len = strlen(args[4]);
561 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900562 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200563 switch (action) {
564 case STAT_CLI_O_TAB:
565 si->applet.ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
566 break;
567 case STAT_CLI_O_CLR:
568 si->applet.ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
569 break;
570 default:
571 si->applet.ctx.cli.msg = "Unknown action\n";
572 break;
573 }
Simon Horman121f3052011-06-15 15:18:46 +0900574 si->applet.st0 = STAT_CLI_PRINT;
575 return;
576 }
577
578 /* check permissions */
579 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
580 si->applet.ctx.cli.msg = stats_permission_denied_msg;
581 si->applet.st0 = STAT_CLI_PRINT;
582 return;
583 }
584
585 ts = stktable_lookup_key(&px->table, &static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900586
Willy Tarreaudec98142012-06-06 23:37:08 +0200587 switch (action) {
588 case STAT_CLI_O_TAB:
589 if (!ts)
590 return;
David du Colombier7af46052012-05-16 14:16:48 +0200591 chunk_init(&msg, trash, trashlen);
Simon Horman17bce342011-06-15 15:18:47 +0900592 if (!stats_dump_table_head_to_buffer(&msg, si, px, px))
593 return;
594 stats_dump_table_entry_to_buffer(&msg, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900595 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200596
597 case STAT_CLI_O_CLR:
598 if (!ts)
599 return;
600 if (ts->ref_cnt) {
601 /* don't delete an entry which is currently referenced */
602 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
603 si->applet.st0 = STAT_CLI_PRINT;
604 return;
605 }
606 stksess_kill(&px->table, ts);
607 break;
Simon Horman17bce342011-06-15 15:18:47 +0900608
Willy Tarreau654694e2012-06-07 01:03:16 +0200609 case STAT_CLI_O_SET:
610 if (strncmp(args[5], "data.", 5) != 0) {
611 si->applet.ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
612 si->applet.st0 = STAT_CLI_PRINT;
613 return;
614 }
615
616 data_type = stktable_get_data_type(args[5] + 5);
617 if (data_type < 0) {
618 si->applet.ctx.cli.msg = "Unknown data type\n";
619 si->applet.st0 = STAT_CLI_PRINT;
620 return;
621 }
622
623 if (!px->table.data_ofs[data_type]) {
624 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
625 si->applet.st0 = STAT_CLI_PRINT;
626 return;
627 }
628
629 if (!*args[6] || strl2llrc(args[6], strlen(args[6]), &value) != 0) {
630 si->applet.ctx.cli.msg = "Require a valid integer value to store\n";
631 si->applet.st0 = STAT_CLI_PRINT;
632 return;
633 }
634
635 if (ts)
636 stktable_touch(&px->table, ts, 1);
637 else {
638 ts = stksess_new(&px->table, &static_table_key);
639 if (!ts) {
640 /* don't delete an entry which is currently referenced */
641 si->applet.ctx.cli.msg = "Unable to allocate a new entry\n";
642 si->applet.st0 = STAT_CLI_PRINT;
643 return;
644 }
645 stktable_store(&px->table, ts, 1);
646 }
647
648 ptr = stktable_data_ptr(&px->table, ts, data_type);
649 switch (stktable_data_types[data_type].std_type) {
650 case STD_T_SINT:
651 stktable_data_cast(ptr, std_t_sint) = value;
652 break;
653 case STD_T_UINT:
654 stktable_data_cast(ptr, std_t_uint) = value;
655 break;
656 case STD_T_ULL:
657 stktable_data_cast(ptr, std_t_ull) = value;
658 break;
659 case STD_T_FRQP:
660 /* We only reset the previous value so that it slowly fades out */
661 frqp = &stktable_data_cast(ptr, std_t_frqp);
662 frqp->curr_tick = now_ms;
663 frqp->prev_ctr = value;
664 frqp->curr_ctr = 0;
665 break;
666 }
667 break;
668
Willy Tarreaudec98142012-06-06 23:37:08 +0200669 default:
670 si->applet.ctx.cli.msg = "Unknown action\n";
Simon Horman121f3052011-06-15 15:18:46 +0900671 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200672 break;
Simon Horman121f3052011-06-15 15:18:46 +0900673 }
Simon Horman121f3052011-06-15 15:18:46 +0900674}
675
Willy Tarreau654694e2012-06-07 01:03:16 +0200676static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900677{
Willy Tarreau654694e2012-06-07 01:03:16 +0200678 if (action != STAT_CLI_O_TAB) {
679 si->applet.ctx.cli.msg = "content-based lookup is only supported with the \"show\" action";
680 si->applet.st0 = STAT_CLI_PRINT;
681 return;
682 }
683
Simon Hormand5b9fd92011-06-15 15:18:48 +0900684 /* condition on stored data value */
685 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
686 if (si->applet.ctx.table.data_type < 0) {
687 si->applet.ctx.cli.msg = "Unknown data type\n";
688 si->applet.st0 = STAT_CLI_PRINT;
689 return;
690 }
691
692 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
693 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
694 si->applet.st0 = STAT_CLI_PRINT;
695 return;
696 }
697
698 si->applet.ctx.table.data_op = get_std_op(args[4]);
699 if (si->applet.ctx.table.data_op < 0) {
700 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
701 si->applet.st0 = STAT_CLI_PRINT;
702 return;
703 }
704
705 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
706 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
707 si->applet.st0 = STAT_CLI_PRINT;
708 return;
709 }
710}
711
Willy Tarreaudec98142012-06-06 23:37:08 +0200712static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900713{
714 si->applet.ctx.table.data_type = -1;
Willy Tarreau94981132012-05-21 17:09:48 +0200715 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200716 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900717 si->applet.ctx.table.proxy = NULL;
718 si->applet.ctx.table.entry = NULL;
Willy Tarreaudec98142012-06-06 23:37:08 +0200719 si->applet.st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900720
721 if (*args[2]) {
722 si->applet.ctx.table.target = find_stktable(args[2]);
723 if (!si->applet.ctx.table.target) {
724 si->applet.ctx.cli.msg = "No such table\n";
725 si->applet.st0 = STAT_CLI_PRINT;
726 return;
727 }
728 }
729 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200730 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900731 goto err_args;
732 return;
733 }
734
735 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200736 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900737 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200738 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900739 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900740 goto err_args;
741
742 return;
743
744err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200745 switch (action) {
746 case STAT_CLI_O_TAB:
Simon Hormand5b9fd92011-06-15 15:18:48 +0900747 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 +0200748 break;
749 case STAT_CLI_O_CLR:
Simon Hormanc88b8872011-06-15 15:18:49 +0900750 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 +0200751 break;
752 default:
753 si->applet.ctx.cli.msg = "Unknown action\n";
754 break;
755 }
Simon Hormand5b9fd92011-06-15 15:18:48 +0900756 si->applet.st0 = STAT_CLI_PRINT;
757}
758
Willy Tarreau532a4502011-09-07 22:37:44 +0200759/* Expects to find a frontend named <arg> and returns it, otherwise displays various
760 * adequate error messages and returns NULL. This function also expects the session
761 * level to be admin.
762 */
763static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
764{
765 struct proxy *px;
766
767 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
768 si->applet.ctx.cli.msg = stats_permission_denied_msg;
769 si->applet.st0 = STAT_CLI_PRINT;
770 return NULL;
771 }
772
773 if (!*arg) {
774 si->applet.ctx.cli.msg = "A frontend name is expected.\n";
775 si->applet.st0 = STAT_CLI_PRINT;
776 return NULL;
777 }
778
779 px = findproxy(arg, PR_CAP_FE);
780 if (!px) {
781 si->applet.ctx.cli.msg = "No such frontend.\n";
782 si->applet.st0 = STAT_CLI_PRINT;
783 return NULL;
784 }
785 return px;
786}
787
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200788/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
789 * and returns the pointer to the server. Otherwise, display adequate error messages
790 * and returns NULL. This function also expects the session level to be admin. Note:
791 * the <arg> is modified to remove the '/'.
792 */
793static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
794{
795 struct proxy *px;
796 struct server *sv;
797 char *line;
798
799 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
800 si->applet.ctx.cli.msg = stats_permission_denied_msg;
801 si->applet.st0 = STAT_CLI_PRINT;
802 return NULL;
803 }
804
805 /* split "backend/server" and make <line> point to server */
806 for (line = arg; *line; line++)
807 if (*line == '/') {
808 *line++ = '\0';
809 break;
810 }
811
812 if (!*line || !*arg) {
813 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
814 si->applet.st0 = STAT_CLI_PRINT;
815 return NULL;
816 }
817
818 if (!get_backend_server(arg, line, &px, &sv)) {
819 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
820 si->applet.st0 = STAT_CLI_PRINT;
821 return NULL;
822 }
823
824 if (px->state == PR_STSTOPPED) {
825 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
826 si->applet.st0 = STAT_CLI_PRINT;
827 return NULL;
828 }
829
830 return sv;
831}
832
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200833/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200834 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100835 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200836 * designating the function which will have to process the request, which can
837 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200838 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900839static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200840{
Willy Tarreau94981132012-05-21 17:09:48 +0200841 struct session *s = si->conn.data_ctx;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200842 char *args[MAX_STATS_ARGS + 1];
843 int arg;
844
845 while (isspace((unsigned char)*line))
846 line++;
847
848 arg = 0;
849 args[arg] = line;
850
851 while (*line && arg < MAX_STATS_ARGS) {
852 if (isspace((unsigned char)*line)) {
853 *line++ = '\0';
854
855 while (isspace((unsigned char)*line))
856 line++;
857
858 args[++arg] = line;
859 continue;
860 }
861
862 line++;
863 }
864
865 while (++arg <= MAX_STATS_ARGS)
866 args[arg] = line;
867
Willy Tarreau295a8372011-03-10 11:25:07 +0100868 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200869 if (strcmp(args[0], "show") == 0) {
870 if (strcmp(args[1], "stat") == 0) {
871 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100872 si->applet.ctx.stats.flags |= STAT_BOUND;
873 si->applet.ctx.stats.iid = atoi(args[2]);
874 si->applet.ctx.stats.type = atoi(args[3]);
875 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200876 }
877
Willy Tarreau295a8372011-03-10 11:25:07 +0100878 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
879 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau94981132012-05-21 17:09:48 +0200880 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100881 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200882 }
883 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100884 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
885 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau94981132012-05-21 17:09:48 +0200886 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100887 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200888 }
889 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau94981132012-05-21 17:09:48 +0200890 si->conn.data_st = STAT_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200891 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100892 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100893 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200894 return 1;
895 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100896 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100897 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100898 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100899 si->applet.ctx.sess.target = NULL;
900 si->applet.ctx.sess.section = 0; /* start with session status */
901 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100902 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200903 }
904 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200905 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100906 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100907 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200908 return 1;
909 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200910 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100911 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200912 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100913 si->applet.ctx.errors.iid = -1;
914 si->applet.ctx.errors.px = NULL;
Willy Tarreau94981132012-05-21 17:09:48 +0200915 si->conn.data_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100916 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200917 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200918 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200919 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200920 }
Aman Guptaceafb4a2012-04-02 18:57:54 -0700921 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200922 return 0;
923 }
924 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200925 else if (strcmp(args[0], "clear") == 0) {
926 if (strcmp(args[1], "counters") == 0) {
927 struct proxy *px;
928 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200929 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200930 int clrall = 0;
931
932 if (strcmp(args[2], "all") == 0)
933 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200934
Willy Tarreau6162db22009-10-10 17:13:00 +0200935 /* check permissions */
936 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
937 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100938 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100939 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200940 return 1;
941 }
942
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200943 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100944 if (clrall) {
945 memset(&px->be_counters, 0, sizeof(px->be_counters));
946 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
947 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200948 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100949 px->be_counters.conn_max = 0;
950 px->be_counters.p.http.rps_max = 0;
951 px->be_counters.sps_max = 0;
952 px->be_counters.cps_max = 0;
953 px->be_counters.nbpend_max = 0;
954
955 px->fe_counters.conn_max = 0;
956 px->fe_counters.p.http.rps_max = 0;
957 px->fe_counters.sps_max = 0;
958 px->fe_counters.cps_max = 0;
959 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200960 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200961
962 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200963 if (clrall)
964 memset(&sv->counters, 0, sizeof(sv->counters));
965 else {
966 sv->counters.cur_sess_max = 0;
967 sv->counters.nbpend_max = 0;
968 sv->counters.sps_max = 0;
969 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200970
971 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200972 if (li->counters) {
973 if (clrall)
974 memset(li->counters, 0, sizeof(*li->counters));
975 else
976 li->counters->conn_max = 0;
977 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200978 }
979
Willy Tarreau81c25d02011-09-07 15:17:21 +0200980 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200981 return 1;
982 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200983 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200984 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200985 /* end of processing */
986 return 1;
987 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200988 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200989 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200990 return 0;
991 }
992 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200993 else if (strcmp(args[0], "get") == 0) {
994 if (strcmp(args[1], "weight") == 0) {
995 struct proxy *px;
996 struct server *sv;
997
998 /* split "backend/server" and make <line> point to server */
999 for (line = args[2]; *line; line++)
1000 if (*line == '/') {
1001 *line++ = '\0';
1002 break;
1003 }
1004
1005 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001006 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001007 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001008 return 1;
1009 }
1010
1011 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001012 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001013 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001014 return 1;
1015 }
1016
1017 /* return server's effective weight at the moment */
David du Colombier7af46052012-05-16 14:16:48 +02001018 snprintf(trash, trashlen, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001019 bi_putstr(si->ib, trash);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001020 return 1;
1021 }
1022 else { /* not "get weight" */
1023 return 0;
1024 }
1025 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001026 else if (strcmp(args[0], "set") == 0) {
1027 if (strcmp(args[1], "weight") == 0) {
1028 struct proxy *px;
1029 struct server *sv;
1030 int w;
1031
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001032 sv = expect_server_admin(s, si, args[2]);
1033 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001034 return 1;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001035 px = sv->proxy;
Willy Tarreau4483d432009-10-10 19:30:08 +02001036
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001037 /* if the weight is terminated with '%', it is set relative to
1038 * the initial weight, otherwise it is absolute.
1039 */
1040 if (!*args[3]) {
1041 si->applet.ctx.cli.msg = "Require <weight> or <weight%>.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001042 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001043 return 1;
1044 }
1045
Willy Tarreau4483d432009-10-10 19:30:08 +02001046 w = atoi(args[3]);
1047 if (strchr(args[3], '%') != NULL) {
1048 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001049 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001050 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001051 return 1;
1052 }
1053 w = sv->iweight * w / 100;
1054 }
1055 else {
1056 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001057 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001058 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001059 return 1;
1060 }
1061 }
1062
1063 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001064 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 +01001065 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001066 return 1;
1067 }
1068
1069 sv->uweight = w;
1070
1071 if (px->lbprm.algo & BE_LB_PROP_DYN) {
1072 /* we must take care of not pushing the server to full throttle during slow starts */
1073 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
1074 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
1075 else
1076 sv->eweight = BE_WEIGHT_SCALE;
1077 sv->eweight *= sv->uweight;
1078 } else {
1079 sv->eweight = sv->uweight;
1080 }
1081
1082 /* static LB algorithms are a bit harder to update */
1083 if (px->lbprm.update_server_eweight)
1084 px->lbprm.update_server_eweight(sv);
Willy Tarreau9580d162012-05-19 19:07:40 +02001085 else if (sv->eweight) {
1086 if (px->lbprm.set_server_status_up)
1087 px->lbprm.set_server_status_up(sv);
1088 }
1089 else {
1090 if (px->lbprm.set_server_status_down)
1091 px->lbprm.set_server_status_down(sv);
1092 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001093
1094 return 1;
1095 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001096 else if (strcmp(args[1], "timeout") == 0) {
1097 if (strcmp(args[2], "cli") == 0) {
1098 unsigned timeout;
1099 const char *res;
1100
1101 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001102 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001103 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001104 return 1;
1105 }
1106
1107 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1108 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001109 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001110 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001111 return 1;
1112 }
1113
1114 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1115 return 1;
1116 }
1117 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001118 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001119 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001120 return 1;
1121 }
1122 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001123 else if (strcmp(args[1], "maxconn") == 0) {
1124 if (strcmp(args[2], "frontend") == 0) {
1125 struct proxy *px;
1126 struct listener *l;
1127 int v;
1128
Willy Tarreau532a4502011-09-07 22:37:44 +02001129 px = expect_frontend_admin(s, si, args[3]);
1130 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001131 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001132
1133 if (!*args[4]) {
1134 si->applet.ctx.cli.msg = "Integer value expected.\n";
1135 si->applet.st0 = STAT_CLI_PRINT;
1136 return 1;
1137 }
1138
1139 v = atoi(args[4]);
1140 /* check for unlimited values, we restore default setting (cfg_maxpconn) */
1141 if (v < 1) {
1142 si->applet.ctx.cli.msg = "Value out of range.\n";
1143 si->applet.st0 = STAT_CLI_PRINT;
1144 return 1;
1145 }
1146
1147 /* OK, the value is fine, so we assign it to the proxy and to all of
1148 * its listeners. The blocked ones will be dequeued.
1149 */
1150 px->maxconn = v;
1151 for (l = px->listen; l != NULL; l = l->next) {
1152 l->maxconn = v;
1153 if (l->state == LI_FULL)
1154 resume_listener(l);
1155 }
1156
1157 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1158 dequeue_all_listeners(&s->fe->listener_queue);
1159
1160 return 1;
1161 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001162 else if (strcmp(args[2], "global") == 0) {
1163 int v;
1164
1165 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1166 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1167 si->applet.st0 = STAT_CLI_PRINT;
1168 return 1;
1169 }
1170
1171 if (!*args[3]) {
1172 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1173 si->applet.st0 = STAT_CLI_PRINT;
1174 return 1;
1175 }
1176
1177 v = atoi(args[3]);
1178 if (v > global.hardmaxconn) {
1179 si->applet.ctx.cli.msg = "Value out of range.\n";
1180 si->applet.st0 = STAT_CLI_PRINT;
1181 return 1;
1182 }
1183
1184 /* check for unlimited values */
1185 if (v <= 0)
1186 v = global.hardmaxconn;
1187
1188 global.maxconn = v;
1189
1190 /* Dequeues all of the listeners waiting for a resource */
1191 if (!LIST_ISEMPTY(&global_listener_queue))
1192 dequeue_all_listeners(&global_listener_queue);
1193
1194 return 1;
1195 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001196 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001197 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001198 si->applet.st0 = STAT_CLI_PRINT;
1199 return 1;
1200 }
1201 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001202 else if (strcmp(args[1], "rate-limit") == 0) {
1203 if (strcmp(args[2], "connections") == 0) {
1204 if (strcmp(args[3], "global") == 0) {
1205 int v;
1206
1207 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1208 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1209 si->applet.st0 = STAT_CLI_PRINT;
1210 return 1;
1211 }
1212
1213 if (!*args[4]) {
1214 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1215 si->applet.st0 = STAT_CLI_PRINT;
1216 return 1;
1217 }
1218
1219 v = atoi(args[4]);
1220 if (v < 0) {
1221 si->applet.ctx.cli.msg = "Value out of range.\n";
1222 si->applet.st0 = STAT_CLI_PRINT;
1223 return 1;
1224 }
1225
1226 global.cps_lim = v;
1227
1228 /* Dequeues all of the listeners waiting for a resource */
1229 if (!LIST_ISEMPTY(&global_listener_queue))
1230 dequeue_all_listeners(&global_listener_queue);
1231
1232 return 1;
1233 }
1234 else {
1235 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1236 si->applet.st0 = STAT_CLI_PRINT;
1237 return 1;
1238 }
1239 }
1240 else {
1241 si->applet.ctx.cli.msg = "'set rate-limit' only supports 'connections'.\n";
1242 si->applet.st0 = STAT_CLI_PRINT;
1243 return 1;
1244 }
1245 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001246 else if (strcmp(args[1], "table") == 0) {
1247 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1248 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001249 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001250 return 0;
1251 }
1252 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001253 else if (strcmp(args[0], "enable") == 0) {
1254 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001255 struct server *sv;
1256
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001257 sv = expect_server_admin(s, si, args[2]);
1258 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001259 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001260
Cyril Bontécd19e512010-01-31 22:34:03 +01001261 if (sv->state & SRV_MAINTAIN) {
1262 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001263 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001264 /* If this server tracks the status of another one,
1265 * we must restore the good status.
1266 */
Willy Tarreau44267702011-10-28 15:35:33 +02001267 if (sv->track->state & SRV_RUNNING) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001268 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001269 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001270 } else {
1271 sv->state &= ~SRV_MAINTAIN;
1272 set_server_down(sv);
1273 }
1274 } else {
1275 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001276 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001277 }
1278 }
1279
Willy Tarreau532a4502011-09-07 22:37:44 +02001280 return 1;
1281 }
1282 else if (strcmp(args[1], "frontend") == 0) {
1283 struct proxy *px;
1284
1285 px = expect_frontend_admin(s, si, args[2]);
1286 if (!px)
1287 return 1;
1288
1289 if (px->state == PR_STSTOPPED) {
1290 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1291 si->applet.st0 = STAT_CLI_PRINT;
1292 return 1;
1293 }
1294
1295 if (px->state != PR_STPAUSED) {
1296 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1297 si->applet.st0 = STAT_CLI_PRINT;
1298 return 1;
1299 }
1300
1301 if (!resume_proxy(px)) {
1302 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1303 si->applet.st0 = STAT_CLI_PRINT;
1304 return 1;
1305 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001306 return 1;
1307 }
1308 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001309 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1310 si->applet.st0 = STAT_CLI_PRINT;
1311 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001312 }
1313 }
1314 else if (strcmp(args[0], "disable") == 0) {
1315 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001316 struct server *sv;
1317
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001318 sv = expect_server_admin(s, si, args[2]);
1319 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001320 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001321
Cyril Bontécd19e512010-01-31 22:34:03 +01001322 if (! (sv->state & SRV_MAINTAIN)) {
1323 /* Not already in maintenance, we can change the server state */
1324 sv->state |= SRV_MAINTAIN;
1325 set_server_down(sv);
1326 }
1327
Willy Tarreau532a4502011-09-07 22:37:44 +02001328 return 1;
1329 }
1330 else if (strcmp(args[1], "frontend") == 0) {
1331 struct proxy *px;
1332
1333 px = expect_frontend_admin(s, si, args[2]);
1334 if (!px)
1335 return 1;
1336
1337 if (px->state == PR_STSTOPPED) {
1338 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1339 si->applet.st0 = STAT_CLI_PRINT;
1340 return 1;
1341 }
1342
1343 if (px->state == PR_STPAUSED) {
1344 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1345 si->applet.st0 = STAT_CLI_PRINT;
1346 return 1;
1347 }
1348
1349 if (!pause_proxy(px)) {
1350 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1351 si->applet.st0 = STAT_CLI_PRINT;
1352 return 1;
1353 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001354 return 1;
1355 }
1356 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001357 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1358 si->applet.st0 = STAT_CLI_PRINT;
1359 return 1;
1360 }
1361 }
1362 else if (strcmp(args[0], "shutdown") == 0) {
1363 if (strcmp(args[1], "frontend") == 0) {
1364 struct proxy *px;
1365
1366 px = expect_frontend_admin(s, si, args[2]);
1367 if (!px)
1368 return 1;
1369
1370 if (px->state == PR_STSTOPPED) {
1371 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1372 si->applet.st0 = STAT_CLI_PRINT;
1373 return 1;
1374 }
1375
1376 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1377 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1378 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1379 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1380 stop_proxy(px);
1381 return 1;
1382 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001383 else if (strcmp(args[1], "session") == 0) {
1384 struct session *sess, *ptr;
1385
1386 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1387 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1388 si->applet.st0 = STAT_CLI_PRINT;
1389 return 1;
1390 }
1391
1392 if (!*args[2]) {
1393 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1394 si->applet.st0 = STAT_CLI_PRINT;
1395 return 1;
1396 }
1397
1398 ptr = (void *)strtoul(args[2], NULL, 0);
1399
1400 /* first, look for the requested session in the session table */
1401 list_for_each_entry(sess, &sessions, list) {
1402 if (sess == ptr)
1403 break;
1404 }
1405
1406 /* do we have the session ? */
1407 if (sess != ptr) {
1408 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1409 si->applet.st0 = STAT_CLI_PRINT;
1410 return 1;
1411 }
1412
1413 session_shutdown(sess, SN_ERR_KILLED);
1414 return 1;
1415 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001416 else if (strcmp(args[1], "sessions") == 0) {
1417 if (strcmp(args[2], "server") == 0) {
1418 struct server *sv;
1419 struct session *sess, *sess_bck;
1420
1421 sv = expect_server_admin(s, si, args[3]);
1422 if (!sv)
1423 return 1;
1424
1425 /* kill all the session that are on this server */
1426 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1427 if (sess->srv_conn == sv)
1428 session_shutdown(sess, SN_ERR_KILLED);
1429
1430 return 1;
1431 }
1432 else {
1433 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1434 si->applet.st0 = STAT_CLI_PRINT;
1435 return 1;
1436 }
1437 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001438 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001439 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001440 si->applet.st0 = STAT_CLI_PRINT;
1441 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001442 }
1443 }
1444 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001445 return 0;
1446 }
1447 return 1;
1448}
1449
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001450/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001451 * used to processes I/O from/to the stats unix socket. The system relies on a
1452 * state machine handling requests and various responses. We read a request,
1453 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001454 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1455 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001456 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001457 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001458static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001459{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001460 struct channel *req = si->ob;
1461 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001462 int reql;
1463 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001464
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001465 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1466 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001467
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001468 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001469 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001470 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001471 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001472 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001473 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001474 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001475 /* Let's close for real now. We just close the request
1476 * side, the conditions below will complete if needed.
1477 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001478 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001479 break;
1480 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001481 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001482 /* ensure we have some output room left in the event we
1483 * would want to return some info right after parsing.
1484 */
Willy Tarreau572bf902012-07-02 17:01:20 +02001485 if (buffer_almost_full(&si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001486 break;
1487
David du Colombier7af46052012-05-16 14:16:48 +02001488 reql = bo_getline(si->ob, trash, trashlen);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001489 if (reql <= 0) { /* closed or EOL not found */
1490 if (reql == 0)
1491 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001492 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001493 continue;
1494 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001495
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001496 /* seek for a possible semi-colon. If we find one, we
1497 * replace it with an LF and skip only this part.
1498 */
1499 for (len = 0; len < reql; len++)
1500 if (trash[len] == ';') {
1501 trash[len] = '\n';
1502 reql = len + 1;
1503 break;
1504 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001505
Willy Tarreau816fc222009-10-04 07:36:58 +02001506 /* now it is time to check that we have a full line,
1507 * remove the trailing \n and possibly \r, then cut the
1508 * line.
1509 */
1510 len = reql - 1;
1511 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001512 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001513 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001514 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001515
Willy Tarreau816fc222009-10-04 07:36:58 +02001516 if (len && trash[len-1] == '\r')
1517 len--;
1518
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001519 trash[len] = '\0';
1520
Willy Tarreaubc4af052011-02-13 13:25:14 +01001521 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001522 if (len) {
1523 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001524 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001525 continue;
1526 }
1527 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001528 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001529 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001530 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001531 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001532 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001533 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001534 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001535 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001536 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001537 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001538 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001539 /* if prompt is disabled, print help on empty lines,
1540 * so that the user at least knows how to enable
1541 * prompt and find help.
1542 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001543 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001544 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001545 }
1546
1547 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001548 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001549 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001550 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001551 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001552 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001553 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001554 continue;
1555 }
1556
Willy Tarreaubc4af052011-02-13 13:25:14 +01001557 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001558 case STAT_CLI_PRINT:
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001559 if (bi_putstr(si->ib, si->applet.ctx.cli.msg) != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001560 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001561 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001562 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001563 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001564 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001565 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001566 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001567 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001568 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001569 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001570 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001571 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001572 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001573 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001574 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001575 case STAT_CLI_O_CLR:
Willy Tarreaudec98142012-06-06 23:37:08 +02001576 if (stats_table_request(si, si->applet.st0))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001577 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001578 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001579 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001580 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001581 break;
1582 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001583
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001584 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001585 if (si->applet.st0 == STAT_CLI_PROMPT) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001586 if (bi_putstr(si->ib, si->applet.st1 ? "\n> " : "\n") != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001587 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001588 }
1589
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001590 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001591 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001592 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001593
1594 /* Now we close the output if one of the writers did so,
1595 * or if we're not in interactive mode and the request
1596 * buffer is empty. This still allows pipelined requests
1597 * to be sent in non-interactive mode.
1598 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001599 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!si->applet.st1 && !req->buf.o)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001600 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001601 continue;
1602 }
1603
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001604 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001605 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001606 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001607 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001608
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001609 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001610 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1611 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001612 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001613 * and nothing more to consume. This is comparable to a broken pipe, so
1614 * we forward the close to the request side so that it flows upstream to
1615 * the client.
1616 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001617 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001618 }
1619
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001620 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001621 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1622 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1623 /* We have no more processing to do, and nothing more to send, and
1624 * the client side has closed. So we'll forward this state downstream
1625 * on the response buffer.
1626 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001627 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001628 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001629 }
1630
1631 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001632 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001633
1634 /* we don't want to expire timeouts while we're processing requests */
1635 si->ib->rex = TICK_ETERNITY;
1636 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001637
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001638 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001639 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 +02001640 __FUNCTION__, __LINE__,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001641 si->state, req->flags, res->flags, req->i, req->o, res->i, res->o);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001642
1643 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1644 /* check that we have released everything then unregister */
1645 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001646 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001647}
1648
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001649/* This function dumps statistics onto the stream interface's read buffer.
1650 * The data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001651 * It returns 0 as long as it does not complete, non-zero upon completion.
1652 * Some states are not used but it makes the code more similar to other
1653 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001654 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001655static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001656{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001657 struct proxy *px;
1658 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001659 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001660
David du Colombier7af46052012-05-16 14:16:48 +02001661 chunk_init(&msg, trash, trashlen);
Willy Tarreau3e76e722007-10-17 18:57:38 +02001662
Willy Tarreau94981132012-05-21 17:09:48 +02001663 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001664 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001665 /* the function had not been called yet */
Willy Tarreau94981132012-05-21 17:09:48 +02001666 si->conn.data_st = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001667 /* fall through */
1668
Willy Tarreau295a8372011-03-10 11:25:07 +01001669 case STAT_ST_HEAD:
1670 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001671 print_csv_header(&msg);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001672 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001673 return 0;
1674 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001675
Willy Tarreau94981132012-05-21 17:09:48 +02001676 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001677 /* fall through */
1678
Willy Tarreau295a8372011-03-10 11:25:07 +01001679 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001680 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001681 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001682 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001683 "Name: " PRODUCT_NAME "\n"
1684 "Version: " HAPROXY_VERSION "\n"
1685 "Release_date: " HAPROXY_DATE "\n"
1686 "Nbproc: %d\n"
1687 "Process_num: %d\n"
1688 "Pid: %d\n"
1689 "Uptime: %dd %dh%02dm%02ds\n"
1690 "Uptime_sec: %d\n"
1691 "Memmax_MB: %d\n"
1692 "Ulimit-n: %d\n"
1693 "Maxsock: %d\n"
1694 "Maxconn: %d\n"
Willy Tarreau91886b62011-09-07 14:38:31 +02001695 "Hard_maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001696 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001697 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001698 "PipesUsed: %d\n"
1699 "PipesFree: %d\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001700 "ConnRate: %d\n"
1701 "ConnRateLimit: %d\n"
1702 "MaxConnRate: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001703 "Tasks: %d\n"
1704 "Run_queue: %d\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02001705 "Idle_pct: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001706 "node: %s\n"
1707 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001708 "",
1709 global.nbproc,
1710 relative_pid,
1711 pid,
1712 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1713 up,
1714 global.rlimit_memmax,
1715 global.rlimit_nofile,
Willy Tarreau91886b62011-09-07 14:38:31 +02001716 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001717 actconn, pipes_used, pipes_free,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001718 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau45a12512011-09-10 16:56:42 +02001719 nb_tasks_cur, run_queue_cur, idle_pct,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001720 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001721 );
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001722 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001723 return 0;
1724 }
1725
Willy Tarreau295a8372011-03-10 11:25:07 +01001726 si->applet.ctx.stats.px = proxy;
1727 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1728 si->applet.ctx.stats.sv = NULL;
Willy Tarreau94981132012-05-21 17:09:48 +02001729 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001730 /* fall through */
1731
Willy Tarreau295a8372011-03-10 11:25:07 +01001732 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001733 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001734 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1735 while (si->applet.ctx.stats.px) {
1736 px = si->applet.ctx.stats.px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001737 /* skip the disabled proxies and non-networked ones */
1738 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001739 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001740 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001741 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001742 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001743
Willy Tarreau295a8372011-03-10 11:25:07 +01001744 si->applet.ctx.stats.px = px->next;
1745 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001746 }
1747 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001748 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001749
Willy Tarreau94981132012-05-21 17:09:48 +02001750 si->conn.data_st = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001751 /* fall through */
1752
Willy Tarreau295a8372011-03-10 11:25:07 +01001753 case STAT_ST_END:
Willy Tarreau94981132012-05-21 17:09:48 +02001754 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001755 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001756
Willy Tarreau295a8372011-03-10 11:25:07 +01001757 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001758 return 1;
1759
1760 default:
1761 /* unknown state ! */
Willy Tarreau94981132012-05-21 17:09:48 +02001762 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001763 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001764 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001765}
1766
1767
Cyril Bonté70be45d2010-10-12 00:14:35 +02001768/* We don't want to land on the posted stats page because a refresh will
1769 * repost the data. We don't want this to happen on accident so we redirect
1770 * the browse to the stats page with a GET.
1771 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001772static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001773{
Willy Tarreau94981132012-05-21 17:09:48 +02001774 struct session *s = si->conn.data_ctx;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001775 struct chunk msg;
1776
David du Colombier7af46052012-05-16 14:16:48 +02001777 chunk_init(&msg, trash, trashlen);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001778
Willy Tarreau94981132012-05-21 17:09:48 +02001779 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001780 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001781 chunk_printf(&msg,
1782 "HTTP/1.0 303 See Other\r\n"
1783 "Cache-Control: no-cache\r\n"
1784 "Content-Type: text/plain\r\n"
1785 "Connection: close\r\n"
1786 "Location: %s;st=%s",
Cyril Bonté19979e12012-04-04 12:57:21 +02001787 uri->uri_prefix,
1788 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
1789 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
1790 stat_status_codes[si->applet.ctx.stats.st_code]) ?
1791 stat_status_codes[si->applet.ctx.stats.st_code] :
1792 stat_status_codes[STAT_STATUS_UNKN]);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001793 chunk_printf(&msg, "\r\n\r\n");
1794
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001795 if (bi_putchk(si->ib, &msg) == -1)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001796 return 0;
1797
1798 s->txn.status = 303;
1799
1800 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1801 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1802 if (!(s->flags & SN_FINST_MASK))
1803 s->flags |= SN_FINST_R;
1804
Willy Tarreau94981132012-05-21 17:09:48 +02001805 si->conn.data_st = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001806 return 1;
1807 }
1808 return 1;
1809}
1810
1811
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001812/* This I/O handler runs as an applet embedded in a stream interface. It is
1813 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001814 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001815 * automatically unregisters itself once transfer is complete.
1816 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001817static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001818{
Willy Tarreau94981132012-05-21 17:09:48 +02001819 struct session *s = si->conn.data_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001820 struct channel *req = si->ob;
1821 struct channel *res = si->ib;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001822
1823 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1824 goto out;
1825
1826 /* check that the output is not closed */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001827 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001828 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001829
Willy Tarreaubc4af052011-02-13 13:25:14 +01001830 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001831 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001832 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001833 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001834 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001835 }
1836 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001837 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001838 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001839 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001840 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001841 }
1842 }
1843
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001844 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
Willy Tarreau73b013b2012-05-21 16:31:45 +02001845 si_shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001846
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001847 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001848 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001849 res->flags |= CF_READ_NULL;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001850 }
1851
1852 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001853 si_update(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001854
1855 /* we don't want to expire timeouts while we're processing requests */
1856 si->ib->rex = TICK_ETERNITY;
1857 si->ob->wex = TICK_ETERNITY;
1858
1859 out:
1860 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1861 /* check that we have released everything then unregister */
1862 stream_int_unregister_handler(si);
1863 }
1864}
1865
1866
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001867/* This function dumps statistics in HTTP format onto the stream interface's
1868 * read buffer. The data_ctx must have been zeroed first, and the flags
1869 * properly set. It returns 0 if it had to stop writing data and an I/O is
1870 * needed, 1 if the dump is finished and the session must be closed, or -1
1871 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001872 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001873static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001874{
Willy Tarreau94981132012-05-21 17:09:48 +02001875 struct session *s = si->conn.data_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001876 struct channel *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001877 struct proxy *px;
1878 struct chunk msg;
1879 unsigned int up;
1880
David du Colombier7af46052012-05-16 14:16:48 +02001881 chunk_init(&msg, trash, trashlen);
Willy Tarreau91861262007-10-17 17:06:05 +02001882
Willy Tarreau94981132012-05-21 17:09:48 +02001883 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001884 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001885 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001886 "HTTP/1.0 200 OK\r\n"
1887 "Cache-Control: no-cache\r\n"
1888 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001889 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001890 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001891
Willy Tarreau295a8372011-03-10 11:25:07 +01001892 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001893 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001894 uri->refresh);
1895
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001896 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001897
1898 s->txn.status = 200;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001899 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001900 return 0;
1901
Willy Tarreau91861262007-10-17 17:06:05 +02001902 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1903 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1904 if (!(s->flags & SN_FINST_MASK))
1905 s->flags |= SN_FINST_R;
1906
1907 if (s->txn.meth == HTTP_METH_HEAD) {
1908 /* that's all we return in case of HEAD request */
Willy Tarreau94981132012-05-21 17:09:48 +02001909 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001910 return 1;
1911 }
1912
Willy Tarreau94981132012-05-21 17:09:48 +02001913 si->conn.data_st = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001914 /* fall through */
1915
Willy Tarreau295a8372011-03-10 11:25:07 +01001916 case STAT_ST_HEAD:
1917 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001918 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001919 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001920 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1921 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001922 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001923 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1924 "<style type=\"text/css\"><!--\n"
1925 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001926 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001927 " font-size: 12px;"
1928 " font-weight: normal;"
1929 " color: black;"
1930 " background: white;"
1931 "}\n"
1932 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001933 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001934 "}\n"
1935 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001936 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001937 " margin-bottom: 0.5em;"
1938 "}\n"
1939 "h2 {"
1940 " font-family: helvetica, arial;"
1941 " font-size: x-large;"
1942 " font-weight: bold;"
1943 " font-style: italic;"
1944 " color: #6020a0;"
1945 " margin-top: 0em;"
1946 " margin-bottom: 0em;"
1947 "}\n"
1948 "h3 {"
1949 " font-family: helvetica, arial;"
1950 " font-size: 16px;"
1951 " font-weight: bold;"
1952 " color: #b00040;"
1953 " background: #e8e8d0;"
1954 " margin-top: 0em;"
1955 " margin-bottom: 0em;"
1956 "}\n"
1957 "li {"
1958 " margin-top: 0.25em;"
1959 " margin-right: 2em;"
1960 "}\n"
1961 ".hr {margin-top: 0.25em;"
1962 " border-color: black;"
1963 " border-bottom-style: solid;"
1964 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001965 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001966 ".total {background: #20D0D0;color: #ffff80;}\n"
1967 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001968 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001969 ".backend {background: #e8e8d0;}\n"
1970 ".active0 {background: #ff9090;}\n"
1971 ".active1 {background: #ffd020;}\n"
1972 ".active2 {background: #ffffa0;}\n"
1973 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001974 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1975 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1976 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001977 ".backup0 {background: #ff9090;}\n"
1978 ".backup1 {background: #ff80ff;}\n"
1979 ".backup2 {background: #c060ff;}\n"
1980 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001981 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1982 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1983 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001984 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001985 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001986 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001987 "a.px:link {color: #ffff40; text-decoration: none;}"
1988 "a.px:visited {color: #ffff40; text-decoration: none;}"
1989 "a.px:hover {color: #ffffff; text-decoration: none;}"
1990 "a.lfsb:link {color: #000000; text-decoration: none;}"
1991 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1992 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1993 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001994 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001995 "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"
1996 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001997 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001998 "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 +02001999 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002000 "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 +02002001 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002002 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2003 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2004 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01002005 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002006 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02002007 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002008 (uri->flags&ST_SHNODE) ? " on " : "",
2009 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02002010 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02002011 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002012 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002013 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002014 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002015 return 0;
2016
Willy Tarreau94981132012-05-21 17:09:48 +02002017 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02002018 /* fall through */
2019
Willy Tarreau295a8372011-03-10 11:25:07 +01002020 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02002021 up = (now.tv_sec - start_date.tv_sec);
2022
2023 /* WARNING! this has to fit the first packet too.
2024 * We are around 3.5 kB, add adding entries will
2025 * become tricky if we want to support 4kB buffers !
2026 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002027 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002028 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002029 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2030 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002031 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002032 "<hr width=\"100%%\" class=\"hr\">\n"
2033 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002034 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01002035 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002036 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002037 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2038 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02002039 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02002040 "Running tasks: %d/%d; idle = %d %%<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002041 "</td><td align=\"center\" nowrap>\n"
2042 "<table class=\"lgd\"><tr>\n"
2043 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2044 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2045 "</tr><tr>\n"
2046 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
2047 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
2048 "</tr><tr>\n"
2049 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
2050 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2051 "</tr><tr>\n"
2052 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01002053 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01002054 "</tr><tr>\n"
2055 "<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 +02002056 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01002057 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02002058 "</td>"
2059 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2060 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2061 "",
2062 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002063 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2064 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
2065 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02002066 up / 86400, (up % 86400) / 3600,
2067 (up % 3600) / 60, (up % 60),
2068 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2069 global.rlimit_memmax ? " MB" : "",
2070 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01002071 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02002072 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau45a12512011-09-10 16:56:42 +02002073 run_queue_cur, nb_tasks_cur, idle_pct
Willy Tarreau91861262007-10-17 17:06:05 +02002074 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002075
Willy Tarreau295a8372011-03-10 11:25:07 +01002076 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002077 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002078 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
2079 uri->uri_prefix,
2080 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01002081 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002082 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002083 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002084 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2085 uri->uri_prefix,
2086 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01002087 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002088
Willy Tarreau55bb8452007-10-17 18:44:57 +02002089 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002090 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002091 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002092 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
2093 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002094 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002095 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002096 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002097 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002098 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
2099 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002100 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002101 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002102 }
Willy Tarreau91861262007-10-17 17:06:05 +02002103
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002104 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002105 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
2106 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002107 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2108 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002109
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002110 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002111 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
2112 uri->uri_prefix,
2113 (uri->refresh > 0) ? ";norefresh" : "");
2114
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002115 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002116 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002117 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
Adrian Bridgettafdb6e52012-03-19 23:36:42 +00002118 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002119 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2120 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2121 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2122 "</ul>"
2123 "</td>"
2124 "</tr></table>\n"
2125 ""
2126 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002127
Willy Tarreau295a8372011-03-10 11:25:07 +01002128 if (si->applet.ctx.stats.st_code) {
Cyril Bonté19979e12012-04-04 12:57:21 +02002129 switch (si->applet.ctx.stats.st_code) {
2130 case STAT_STATUS_DONE:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002131 chunk_printf(&msg,
2132 "<p><div class=active3>"
2133 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2134 "Action processed successfully."
2135 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002136 break;
2137 case STAT_STATUS_NONE:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002138 chunk_printf(&msg,
2139 "<p><div class=active2>"
2140 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2141 "Nothing has changed."
2142 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002143 break;
2144 case STAT_STATUS_PART:
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002145 chunk_printf(&msg,
2146 "<p><div class=active2>"
2147 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2148 "Action partially processed.<br>"
2149 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2150 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002151 break;
2152 case STAT_STATUS_ERRP:
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002153 chunk_printf(&msg,
2154 "<p><div class=active0>"
2155 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2156 "Action not processed because of invalid parameters."
2157 "<ul>"
2158 "<li>The action is maybe unknown.</li>"
2159 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2160 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2161 "</ul>"
2162 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002163 break;
2164 case STAT_STATUS_EXCD:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002165 chunk_printf(&msg,
2166 "<p><div class=active0>"
2167 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2168 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2169 "You should retry with less servers at a time.</b>"
2170 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002171 break;
2172 case STAT_STATUS_DENY:
Cyril Bonté474be412010-10-12 00:14:36 +02002173 chunk_printf(&msg,
2174 "<p><div class=active0>"
2175 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2176 "<b>Action denied.</b>"
2177 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002178 break;
2179 default:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002180 chunk_printf(&msg,
2181 "<p><div class=active6>"
2182 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2183 "Unexpected result."
2184 "</div>\n", uri->uri_prefix);
2185 }
2186 chunk_printf(&msg,"<p>\n");
2187 }
2188
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002189 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002190 return 0;
2191 }
Willy Tarreau91861262007-10-17 17:06:05 +02002192
Willy Tarreau295a8372011-03-10 11:25:07 +01002193 si->applet.ctx.stats.px = proxy;
2194 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau94981132012-05-21 17:09:48 +02002195 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02002196 /* fall through */
2197
Willy Tarreau295a8372011-03-10 11:25:07 +01002198 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02002199 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01002200 while (si->applet.ctx.stats.px) {
Willy Tarreau572bf902012-07-02 17:01:20 +02002201 if (buffer_almost_full(&rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002202 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002203 px = si->applet.ctx.stats.px;
Willy Tarreau91861262007-10-17 17:06:05 +02002204 /* skip the disabled proxies and non-networked ones */
2205 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002206 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002207 return 0;
2208
Willy Tarreau295a8372011-03-10 11:25:07 +01002209 si->applet.ctx.stats.px = px->next;
2210 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02002211 }
2212 /* here, we just have reached the last proxy */
2213
Willy Tarreau94981132012-05-21 17:09:48 +02002214 si->conn.data_st = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002215 /* fall through */
2216
Willy Tarreau295a8372011-03-10 11:25:07 +01002217 case STAT_ST_END:
2218 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002219 chunk_printf(&msg, "</body></html>\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002220 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002221 return 0;
2222 }
Willy Tarreau91861262007-10-17 17:06:05 +02002223
Willy Tarreau94981132012-05-21 17:09:48 +02002224 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002225 /* fall through */
2226
Willy Tarreau295a8372011-03-10 11:25:07 +01002227 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002228 return 1;
2229
2230 default:
2231 /* unknown state ! */
Willy Tarreau94981132012-05-21 17:09:48 +02002232 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002233 return -1;
2234 }
2235}
2236
2237
2238/*
2239 * Dumps statistics for a proxy.
2240 * Returns 0 if it had to stop dumping data because of lack of buffer space,
2241 * ot non-zero if everything completed.
2242 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002243static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002244{
Willy Tarreau94981132012-05-21 17:09:48 +02002245 struct session *s = si->conn.data_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02002246 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002247 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002248 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002249 struct chunk msg;
2250
David du Colombier7af46052012-05-16 14:16:48 +02002251 chunk_init(&msg, trash, trashlen);
Willy Tarreau91861262007-10-17 17:06:05 +02002252
Willy Tarreau295a8372011-03-10 11:25:07 +01002253 switch (si->applet.ctx.stats.px_st) {
2254 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002255 /* we are on a new proxy */
2256
2257 if (uri && uri->scope) {
2258 /* we have a limited scope, we have to check the proxy name */
2259 struct stat_scope *scope;
2260 int len;
2261
2262 len = strlen(px->id);
2263 scope = uri->scope;
2264
2265 while (scope) {
2266 /* match exact proxy name */
2267 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2268 break;
2269
2270 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002271 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002272 break;
2273 scope = scope->next;
2274 }
2275
2276 /* proxy name not found : don't dump anything */
2277 if (scope == NULL)
2278 return 1;
2279 }
2280
Willy Tarreau295a8372011-03-10 11:25:07 +01002281 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
2282 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002283 return 1;
2284
Willy Tarreau295a8372011-03-10 11:25:07 +01002285 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002286 /* fall through */
2287
Willy Tarreau295a8372011-03-10 11:25:07 +01002288 case STAT_PX_ST_TH:
2289 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
2290 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002291 /* A form to enable/disable this proxy servers */
2292 chunk_printf(&msg,
2293 "<form action=\"%s\" method=\"post\">",
2294 uri->uri_prefix);
2295 }
2296
Willy Tarreau55bb8452007-10-17 18:44:57 +02002297 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002298 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002299 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002300 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002301 "<th class=\"pxname\" width=\"10%%\"");
2302
2303 if (uri->flags&ST_SHLGNDS) {
2304 /* cap, mode, id */
2305 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
2306 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2307 px->uuid);
2308
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002309 chunk_printf(&msg, "\"");
2310 }
2311
2312 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002313 ">%s<a name=\"%s\"></a>"
2314 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002315 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002316 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002317 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002318 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002319 "<tr class=\"titre\">",
2320 (uri->flags & ST_SHLGNDS)?"<u>":"",
2321 px->id, px->id, px->id,
2322 (uri->flags & ST_SHLGNDS)?"</u>":"",
2323 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2324
Willy Tarreau295a8372011-03-10 11:25:07 +01002325 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002326 /* Column heading for Enable or Disable server */
2327 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
2328 }
2329
2330 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002331 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002332 "<th colspan=3>Queue</th>"
2333 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002334 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002335 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002336 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002337 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002338 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002339 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002340 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002341 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002342 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002343 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002344 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002345 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2346 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002347 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002348
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002349 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002350 return 0;
2351 }
Willy Tarreau91861262007-10-17 17:06:05 +02002352
Willy Tarreau295a8372011-03-10 11:25:07 +01002353 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002354 /* fall through */
2355
Willy Tarreau295a8372011-03-10 11:25:07 +01002356 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002357 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002358 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002359 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
2360 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002361 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002362 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002363 "<tr class=\"frontend\">");
2364
Willy Tarreau295a8372011-03-10 11:25:07 +01002365 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002366 /* Column sub-heading for Enable or Disable server */
2367 chunk_printf(&msg, "<td></td>");
2368 }
2369
2370 chunk_printf(&msg,
2371 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002372 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002373 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2374 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002375 "",
2376 px->id, px->id);
2377
2378 if (px->mode == PR_MODE_HTTP) {
2379 chunk_printf(&msg,
2380 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01002381 "<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 +01002382 "",
2383 read_freq_ctr(&px->fe_req_per_sec),
2384 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002385 px->fe_counters.p.http.rps_max,
2386 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01002387 LIM2A2(px->fe_sps_lim, "-"));
2388 } else {
2389 chunk_printf(&msg,
2390 /* sessions rate : current, max, limit */
2391 "<td>%s</td><td>%s</td><td>%s</td>"
2392 "",
2393 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002394 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01002395 }
2396
2397 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002398 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002399 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002400 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002401 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002402 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002403
2404 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2405 if (px->mode == PR_MODE_HTTP) {
2406 int i;
2407
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002408 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002409
2410 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002411 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002412
Willy Tarreaueabea072011-09-10 23:29:44 +02002413 chunk_printf(&msg, " other=%lld,", px->fe_counters.p.http.rsp[0]);
2414 chunk_printf(&msg, " intercepted=%lld\"", px->fe_counters.intercepted_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002415 }
2416
2417 chunk_printf(&msg,
2418 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002419 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002420 /* bytes : in, out */
2421 "<td>%s</td><td>%s</td>"
2422 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002423 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002424 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002425 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002426 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002427
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002428 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002429 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002430 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002431 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002432 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002433 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002434 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002435 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002436 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002437 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002438 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002439 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002440 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2441 U2H2(px->fe_counters.failed_req),
Willy Tarreau562515c2011-07-25 08:11:52 +02002442 px->state == PR_STREADY ? "OPEN" :
2443 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002444 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002445 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002446 /* pxid, name, queue cur, queue max, */
2447 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002448 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002449 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002450 /* bytes : in, out */
2451 "%lld,%lld,"
2452 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002453 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002454 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002455 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002456 /* warnings: retries, redispatches */
2457 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002458 /* server status : reflect frontend status */
2459 "%s,"
2460 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002461 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002462 /* pid, iid, sid, throttle, lbtot, tracked, type */
2463 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002464 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002465 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002466 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002467 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002468 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002469 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2470 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2471 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2472 px->fe_counters.failed_req,
Willy Tarreau562515c2011-07-25 08:11:52 +02002473 px->state == PR_STREADY ? "OPEN" :
2474 px->state == PR_STFULL ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002475 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002476 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002477 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002478
2479 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2480 if (px->mode == PR_MODE_HTTP) {
2481 int i;
2482
2483 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002484 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002485
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002486 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002487 } else {
2488 chunk_printf(&msg, ",,,,,,");
2489 }
2490
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002491 /* failed health analyses */
2492 chunk_printf(&msg, ",");
2493
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002494 /* requests : req_rate, req_rate_max, req_tot, */
2495 chunk_printf(&msg, "%u,%u,%lld,",
2496 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002497 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002498
Willy Tarreauae526782010-03-04 20:34:23 +01002499 /* errors: cli_aborts, srv_aborts */
2500 chunk_printf(&msg, ",,");
2501
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002502 /* finish with EOL */
2503 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002504 }
Willy Tarreau91861262007-10-17 17:06:05 +02002505
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002506 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002507 return 0;
2508 }
2509
Willy Tarreau295a8372011-03-10 11:25:07 +01002510 si->applet.ctx.stats.l = px->listen; /* may be NULL */
2511 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002512 /* fall through */
2513
Willy Tarreau295a8372011-03-10 11:25:07 +01002514 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002515 /* stats.l has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002516 for (; si->applet.ctx.stats.l != NULL; si->applet.ctx.stats.l = l->next) {
Willy Tarreau572bf902012-07-02 17:01:20 +02002517 if (buffer_almost_full(&rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002518 return 0;
2519
Willy Tarreau295a8372011-03-10 11:25:07 +01002520 l = si->applet.ctx.stats.l;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002521 if (!l->counters)
2522 continue;
2523
Willy Tarreau295a8372011-03-10 11:25:07 +01002524 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2525 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002526 break;
2527
Willy Tarreau295a8372011-03-10 11:25:07 +01002528 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002529 continue;
2530 }
2531
Willy Tarreau295a8372011-03-10 11:25:07 +01002532 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002533 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002534 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002535 /* Column sub-heading for Enable or Disable server */
2536 chunk_printf(&msg, "<td></td>");
2537 }
2538 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002539
2540 if (uri->flags&ST_SHLGNDS) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02002541 char str[INET6_ADDRSTRLEN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002542 int port;
2543
Willy Tarreau631f01c2011-09-05 00:36:48 +02002544 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002545
Willy Tarreau631f01c2011-09-05 00:36:48 +02002546 port = get_host_port(&l->addr);
2547 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2548 case AF_INET:
2549 chunk_printf(&msg, "IPv4: %s:%d, ", str, port);
2550 break;
2551 case AF_INET6:
2552 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, port);
2553 break;
2554 case AF_UNIX:
2555 chunk_printf(&msg, "unix, ");
2556 break;
2557 case -1:
2558 chunk_printf(&msg, "(%s), ", strerror(errno));
2559 break;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002560 }
2561
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002562 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002563 chunk_printf(&msg, "id: %d\"", l->luid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002564 }
2565
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002566 chunk_printf(&msg,
2567 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002568 ">%s<a name=\"%s/+%s\"></a>"
2569 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002570 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002571 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002572 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002573 "<td>%s</td><td>%s</td><td>%s</td>"
2574 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002575 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002576 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002577 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002578 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002579 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002580 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002581 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2582 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2583
2584 chunk_printf(&msg,
2585 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002586 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002587 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002588 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002589 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002590 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002591 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002592 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002593 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002594 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002595 "",
2596 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2597 U2H2(l->counters->failed_req),
Willy Tarreaua17c2d92011-07-25 08:16:20 +02002598 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002599 } else {
2600 chunk_printf(&msg,
2601 /* pxid, name, queue cur, queue max, */
2602 "%s,%s,,,"
2603 /* sessions: current, max, limit, total */
2604 "%d,%d,%d,%lld,"
2605 /* bytes: in, out */
2606 "%lld,%lld,"
2607 /* denied: req, resp */
2608 "%lld,%lld,"
2609 /* errors: request, connect, response */
2610 "%lld,,,"
2611 /* warnings: retries, redispatches */
2612 ",,"
2613 /* server status: reflect listener status */
2614 "%s,"
2615 /* rest of server: nothing */
2616 ",,,,,,,,"
2617 /* pid, iid, sid, throttle, lbtot, tracked, type */
2618 "%d,%d,%d,,,,%d,"
2619 /* rate, rate_lim, rate_max */
2620 ",,,"
2621 /* check_status, check_code, check_duration */
2622 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002623 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2624 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002625 /* failed health analyses */
2626 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002627 /* requests : req_rate, req_rate_max, req_tot, */
2628 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002629 /* errors: cli_aborts, srv_aborts */
2630 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002631 "\n",
2632 px->id, l->name,
2633 l->nbconn, l->counters->conn_max,
2634 l->maxconn, l->counters->cum_conn,
2635 l->counters->bytes_in, l->counters->bytes_out,
2636 l->counters->denied_req, l->counters->denied_resp,
2637 l->counters->failed_req,
2638 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2639 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2640 }
2641
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002642 if (bi_putchk(rep, &msg) == -1)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002643 return 0;
2644 }
2645
Willy Tarreau295a8372011-03-10 11:25:07 +01002646 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2647 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002648 /* fall through */
2649
Willy Tarreau295a8372011-03-10 11:25:07 +01002650 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002651 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002652 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002653 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 +02002654
Willy Tarreau572bf902012-07-02 17:01:20 +02002655 if (buffer_almost_full(&rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002656 return 0;
2657
Willy Tarreau295a8372011-03-10 11:25:07 +01002658 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002659
Willy Tarreau295a8372011-03-10 11:25:07 +01002660 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2661 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002662 break;
2663
Willy Tarreau295a8372011-03-10 11:25:07 +01002664 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002665 continue;
2666 }
2667
Willy Tarreau44267702011-10-28 15:35:33 +02002668 if (sv->track)
2669 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002670 else
2671 svs = sv;
2672
Willy Tarreau91861262007-10-17 17:06:05 +02002673 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002674 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002675 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002676 else if (svs->state & SRV_RUNNING) {
2677 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002678 sv_state = 3; /* UP */
2679 else
2680 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002681
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002682 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002683 sv_state += 2;
2684 }
Willy Tarreau91861262007-10-17 17:06:05 +02002685 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002686 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002687 sv_state = 1; /* going up */
2688 else
2689 sv_state = 0; /* DOWN */
2690
Willy Tarreau295a8372011-03-10 11:25:07 +01002691 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002692 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002693 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002694 continue;
2695 }
2696
Willy Tarreau295a8372011-03-10 11:25:07 +01002697 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002698 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2699 "UP %d/%d &darr;", "UP",
2700 "NOLB %d/%d &darr;", "NOLB",
2701 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002702 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002703 chunk_printf(&msg,
2704 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002705 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002706 );
2707 }
2708 else {
2709 chunk_printf(&msg,
2710 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002711 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002712 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2713 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002714
Willy Tarreau295a8372011-03-10 11:25:07 +01002715 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002716 chunk_printf(&msg,
2717 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2718 sv->id);
2719 }
2720
2721 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002722
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002723 if (uri->flags&ST_SHLGNDS) {
2724 char str[INET6_ADDRSTRLEN];
2725
Willy Tarreau631f01c2011-09-05 00:36:48 +02002726 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002727
Willy Tarreau631f01c2011-09-05 00:36:48 +02002728 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01002729 case AF_INET:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002730 chunk_printf(&msg, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
David du Colombier6f5ccb12011-03-10 22:26:24 +01002731 break;
2732 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002733 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2734 break;
2735 case AF_UNIX:
2736 chunk_printf(&msg, "unix, ");
2737 break;
2738 case -1:
2739 chunk_printf(&msg, "(%s), ", strerror(errno));
2740 break;
2741 default: /* address family not supported */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002742 break;
2743 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002744
2745 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002746 chunk_printf(&msg, "id: %d", sv->puid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002747
2748 /* cookie */
2749 if (sv->cookie) {
2750 struct chunk src;
2751
2752 chunk_printf(&msg, ", cookie: '");
2753
2754 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2755 chunk_htmlencode(&msg, &src);
2756
2757 chunk_printf(&msg, "'");
2758 }
2759
2760 chunk_printf(&msg, "\"");
2761 }
2762
2763 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002764 ">%s<a name=\"%s/%s\"></a>"
2765 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002766 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002767 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002768 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002769 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002770 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002771 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002772 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002773 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002774 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002775 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002776 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002777 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2778 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002779 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2780
2781 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2782 if (px->mode == PR_MODE_HTTP) {
2783 int i;
2784
2785 chunk_printf(&msg, " title=\"rsp codes:");
2786
2787 for (i = 1; i < 6; i++)
2788 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2789
2790 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2791 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002792
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002793 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002794 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002795 ">%s%s%s</td><td>%s</td>",
2796 (px->mode == PR_MODE_HTTP)?"<u>":"",
2797 U2H0(sv->counters.cum_sess),
2798 (px->mode == PR_MODE_HTTP)?"</u>":"",
2799 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002800
2801 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002802 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002803 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002804 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002805 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002806 /* errors : request, connect */
2807 "<td></td><td>%s</td>"
2808 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002809 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002810 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002811 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002812 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002813 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2814 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002815 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002816 sv->counters.cli_aborts,
2817 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002818 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002819 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002820
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002821 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002822 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002823
Cyril Bontécd19e512010-01-31 22:34:03 +01002824 if (sv->state & SRV_MAINTAIN) {
2825 chunk_printf(&msg, "%s ",
2826 human_time(now.tv_sec - sv->last_change, 1));
2827 chunk_printf(&msg, "MAINT");
2828 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002829 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2830 chunk_printf(&msg, "%s ",
2831 human_time(now.tv_sec - svs->last_change, 1));
2832 chunk_printf(&msg, "MAINT(via)");
2833 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002834 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002835 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002836 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002837
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002838 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002839 srv_hlt_st[sv_state],
2840 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2841 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002842 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002843
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002844 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002845 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002846 get_check_status_description(sv->check_status));
2847
2848 if (*sv->check_desc) {
2849 struct chunk src;
2850
2851 chunk_printf(&msg, ": ");
2852
2853 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2854 chunk_htmlencode(&msg, &src);
2855 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002856
Willy Tarreaue0454092010-02-26 12:29:07 +01002857 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002858 tv_iszero(&sv->check_start)?"":"* ",
2859 get_check_status_info(sv->check_status));
2860
2861 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002862 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002863
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002864 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002865 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002866 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002867 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002868
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002869 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002870 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002871 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002872 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002873 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002874 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002875 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002876 (sv->state & SRV_BACKUP) ? "-" : "Y",
2877 (sv->state & SRV_BACKUP) ? "Y" : "-");
2878
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002879 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002880 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002881 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002882 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2883
2884 if (svs->observe)
2885 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2886
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002887 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002888 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002889 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002890 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002891 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002892 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002893 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002894 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2895 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002896 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002897 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002898 "<td colspan=3></td>");
2899
2900 /* throttle */
2901 if ((sv->state & SRV_WARMINGUP) &&
2902 now.tv_sec < sv->last_change + sv->slowstart &&
2903 now.tv_sec >= sv->last_change) {
2904 unsigned int ratio;
2905 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002906 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002907 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002908 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002909 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002910 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002911 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002912 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002913 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2914 "UP %d/%d,", "UP,",
2915 "NOLB %d/%d,", "NOLB,",
2916 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002917 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002918 /* pxid, name */
2919 "%s,%s,"
2920 /* queue : current, max */
2921 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002922 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002923 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002924 /* bytes : in, out */
2925 "%lld,%lld,"
2926 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002927 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002928 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002929 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002930 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002931 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002932 "",
2933 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002934 sv->nbpend, sv->counters.nbpend_max,
2935 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002936 sv->counters.bytes_in, sv->counters.bytes_out,
2937 sv->counters.failed_secu,
2938 sv->counters.failed_conns, sv->counters.failed_resp,
2939 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002940
Willy Tarreau55bb8452007-10-17 18:44:57 +02002941 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002942 if (sv->state & SRV_MAINTAIN) {
2943 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002944 }
2945 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2946 chunk_printf(&msg, "MAINT(via),");
2947 }
2948 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002949 chunk_printf(&msg,
2950 srv_hlt_st[sv_state],
2951 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2952 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2953 }
Willy Tarreau91861262007-10-17 17:06:05 +02002954
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002955 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002956 /* weight, active, backup */
2957 "%d,%d,%d,"
2958 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002959 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002960 (sv->state & SRV_BACKUP) ? 0 : 1,
2961 (sv->state & SRV_BACKUP) ? 1 : 0);
2962
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002963 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002964 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002965 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002966 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002967 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002968 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002969 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002970 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002971 ",,,,");
2972
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002973 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002974 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002975 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002976 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002977 LIM2A0(sv->maxqueue, ""),
2978 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002979
2980 /* throttle */
2981 if ((sv->state & SRV_WARMINGUP) &&
2982 now.tv_sec < sv->last_change + sv->slowstart &&
2983 now.tv_sec >= sv->last_change) {
2984 unsigned int ratio;
2985 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002986 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002987 }
2988
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002989 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002990 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002991
2992 /* tracked */
Willy Tarreau44267702011-10-28 15:35:33 +02002993 if (sv->track)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002994 chunk_printf(&msg, "%s/%s,",
Willy Tarreau44267702011-10-28 15:35:33 +02002995 sv->track->proxy->id, sv->track->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002996 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002997 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002998
Willy Tarreau7f062c42009-03-05 18:43:00 +01002999 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003000 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01003001
3002 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003003 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003004 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02003005 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01003006
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003007 if (sv->state & SRV_CHECKED) {
3008 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003009 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003010
3011 /* check_code */
3012 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003013 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003014 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003015 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003016
3017 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01003018 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003019 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003020 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003021 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003022
3023 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003024 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003025 }
3026
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003027 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3028 if (px->mode == PR_MODE_HTTP) {
3029 int i;
3030
3031 for (i=1; i<6; i++)
3032 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
3033
3034 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
3035 } else {
3036 chunk_printf(&msg, ",,,,,,");
3037 }
3038
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003039 /* failed health analyses */
3040 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
3041
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003042 /* requests : req_rate, req_rate_max, req_tot, */
3043 chunk_printf(&msg, ",,,");
3044
Willy Tarreauae526782010-03-04 20:34:23 +01003045 /* errors: cli_aborts, srv_aborts */
3046 chunk_printf(&msg, "%lld,%lld,",
3047 sv->counters.cli_aborts, sv->counters.srv_aborts);
3048
Willy Tarreau7f062c42009-03-05 18:43:00 +01003049 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003050 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003051 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003052 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003053 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003054 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02003055
Willy Tarreau295a8372011-03-10 11:25:07 +01003056 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02003057 /* fall through */
3058
Willy Tarreau295a8372011-03-10 11:25:07 +01003059 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02003060 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003061 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01003062 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
3063 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003064 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01003065 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003066 /* Column sub-heading for Enable or Disable server */
3067 chunk_printf(&msg, "<td></td>");
3068 }
3069 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003070
3071 if (uri->flags&ST_SHLGNDS) {
3072 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003073 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003074 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003075
3076 /* cookie */
3077 if (px->cookie_name) {
3078 struct chunk src;
3079
3080 chunk_printf(&msg, ", cookie: '");
3081
3082 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3083 chunk_htmlencode(&msg, &src);
3084
3085 chunk_printf(&msg, "'");
3086 }
3087
3088 chunk_printf(&msg, "\"");
3089
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003090 }
3091
3092 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02003093 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01003094 ">%s<a name=\"%s/Backend\"></a>"
3095 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02003096 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003097 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003098 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003099 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003100 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003101 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02003102 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01003103 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003104 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
3105 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02003106
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003107 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003108 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003109 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003110 "<td"
3111 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003112 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003113
3114 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3115 if (px->mode == PR_MODE_HTTP) {
3116 int i;
3117
3118 chunk_printf(&msg, " title=\"rsp codes:");
3119
3120 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003121 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003122
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003123 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003124 }
3125
3126 chunk_printf(&msg,
3127 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01003128 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003129 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003130 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003131 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003132 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003133 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01003134 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003135 U2H7(px->be_counters.cum_lbconn),
3136 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003137
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003138 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02003139 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003140 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01003141 /* errors : request, connect */
3142 "<td></td><td>%s</td>"
3143 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01003144 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003145 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003146 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003147 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02003148 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003149 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02003150 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003151 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3152 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003153 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003154 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
3155 U2H2(px->be_counters.failed_conns),
3156 px->be_counters.cli_aborts,
3157 px->be_counters.srv_aborts,
3158 U2H5(px->be_counters.failed_resp),
3159 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003160 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01003161 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3162 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01003163 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003164 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003165
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003166 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003167 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003168 "<td class=ac>&nbsp;</td><td>%d</td>"
3169 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003170 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003171 "</tr>",
3172 px->down_trans,
3173 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003174 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003175 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02003176 /* pxid, name */
3177 "%s,BACKEND,"
3178 /* queue : current, max */
3179 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003180 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003181 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003182 /* bytes : in, out */
3183 "%lld,%lld,"
3184 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003185 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003186 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003187 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003188 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003189 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003190 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02003191 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003192 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02003193 * active and backups. */
3194 "%s,"
3195 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003196 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003197 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003198 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003199 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003200 /* rate, rate_lim, rate_max, */
3201 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003202 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003203 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02003204 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003205 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3206 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3207 px->be_counters.bytes_in, px->be_counters.bytes_out,
3208 px->be_counters.denied_req, px->be_counters.denied_resp,
3209 px->be_counters.failed_conns, px->be_counters.failed_resp,
3210 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01003211 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01003212 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003213 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003214 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01003215 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003216 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003217 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003218 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003219 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003220
3221 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3222 if (px->mode == PR_MODE_HTTP) {
3223 int i;
3224
3225 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003226 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003227
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003228 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003229 } else {
3230 chunk_printf(&msg, ",,,,,,");
3231 }
3232
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003233 /* failed health analyses */
3234 chunk_printf(&msg, ",");
3235
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003236 /* requests : req_rate, req_rate_max, req_tot, */
3237 chunk_printf(&msg, ",,,");
3238
Willy Tarreauae526782010-03-04 20:34:23 +01003239 /* errors: cli_aborts, srv_aborts */
3240 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003241 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01003242
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003243 /* finish with EOL */
3244 chunk_printf(&msg, "\n");
3245
Willy Tarreau55bb8452007-10-17 18:44:57 +02003246 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003247 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003248 return 0;
3249 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003250
Willy Tarreau295a8372011-03-10 11:25:07 +01003251 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02003252 /* fall through */
3253
Willy Tarreau295a8372011-03-10 11:25:07 +01003254 case STAT_PX_ST_END:
3255 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003256 chunk_printf(&msg, "</table>");
3257
Willy Tarreau295a8372011-03-10 11:25:07 +01003258 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003259 /* close the form used to enable/disable this proxy servers */
3260 chunk_printf(&msg,
3261 "Choose the action to perform on the checked servers : "
3262 "<select name=action>"
3263 "<option value=\"\"></option>"
3264 "<option value=\"disable\">Disable</option>"
3265 "<option value=\"enable\">Enable</option>"
Willy Tarreaud7282242012-06-04 00:22:44 +02003266 "<option value=\"stop\">Soft Stop</option>"
3267 "<option value=\"start\">Soft Start</option>"
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003268 "<option value=\"shutdown\">Kill Sessions</option>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003269 "</select>"
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003270 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003271 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3272 "</form>",
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003273 px->uuid);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003274 }
3275
3276 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02003277
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003278 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003279 return 0;
3280 }
Willy Tarreau91861262007-10-17 17:06:05 +02003281
Willy Tarreau295a8372011-03-10 11:25:07 +01003282 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02003283 /* fall through */
3284
Willy Tarreau295a8372011-03-10 11:25:07 +01003285 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02003286 return 1;
3287
3288 default:
3289 /* unknown state, we should put an abort() here ! */
3290 return 1;
3291 }
3292}
3293
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003294/* This function dumps a complete session state onto the stream intreface's
3295 * read buffer. The data_ctx must have been zeroed first, and the flags
3296 * properly set. The session has to be set in data_ctx.sess.target. It returns
3297 * 0 if the output buffer is full and it needs to be called again, otherwise
3298 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003299 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003300static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003301{
3302 struct tm tm;
3303 struct chunk msg;
3304 struct session *sess;
3305 extern const char *monthname[12];
3306 char pn[INET6_ADDRSTRLEN];
3307
David du Colombier7af46052012-05-16 14:16:48 +02003308 chunk_init(&msg, trash, trashlen);
Willy Tarreau295a8372011-03-10 11:25:07 +01003309 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003310
Willy Tarreau295a8372011-03-10 11:25:07 +01003311 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003312 /* session changed, no need to go any further */
3313 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003314 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003315 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003316 si->applet.ctx.sess.target = NULL;
3317 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003318 return 1;
3319 }
3320
Willy Tarreau295a8372011-03-10 11:25:07 +01003321 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003322 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003323 si->applet.ctx.sess.uid = sess->uniq_id;
3324 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003325 /* fall through */
3326
3327 case 1:
3328 chunk_printf(&msg,
3329 "%p: id=%u, proto=%s",
3330 sess,
3331 sess->uniq_id,
3332 sess->listener->proto->name);
3333
Willy Tarreau986a9d22012-08-30 21:11:38 +02003334 switch (addr_to_str(&sess->si[0].conn.addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003335 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003336 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003337 chunk_printf(&msg, " source=%s:%d\n",
Willy Tarreau986a9d22012-08-30 21:11:38 +02003338 pn, get_host_port(&sess->si[0].conn.addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003339 break;
3340 case AF_UNIX:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003341 chunk_printf(&msg, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003342 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003343 default:
3344 /* no more information to print right now */
3345 chunk_printf(&msg, "\n");
3346 break;
3347 }
3348
3349 chunk_printf(&msg,
3350 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003351 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003352
3353 chunk_printf(&msg,
Daniel Schultze90690c72012-03-23 10:53:36 -07003354 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003355 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3356 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3357 sess->listener ? sess->listener->luid : 0);
3358
Willy Tarreau986a9d22012-08-30 21:11:38 +02003359 conn_get_to_addr(&sess->si[0].conn);
3360 switch (addr_to_str(&sess->si[0].conn.addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003361 case AF_INET:
3362 case AF_INET6:
3363 chunk_printf(&msg, " addr=%s:%d\n",
Willy Tarreau986a9d22012-08-30 21:11:38 +02003364 pn, get_host_port(&sess->si[0].conn.addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003365 break;
3366 case AF_UNIX:
3367 chunk_printf(&msg, " addr=unix:%d\n", sess->listener->luid);
3368 break;
3369 default:
3370 /* no more information to print right now */
3371 chunk_printf(&msg, "\n");
3372 break;
3373 }
3374
Willy Tarreau50943332011-09-02 17:33:05 +02003375 if (sess->be->cap & PR_CAP_BE)
3376 chunk_printf(&msg,
Daniel Schultze90690c72012-03-23 10:53:36 -07003377 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003378 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003379 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3380 else
3381 chunk_printf(&msg, " backend=<NONE> (id=-1 mode=-)");
3382
Willy Tarreau986a9d22012-08-30 21:11:38 +02003383 conn_get_from_addr(&sess->si[1].conn);
3384 switch (addr_to_str(&sess->si[1].conn.addr.from, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003385 case AF_INET:
3386 case AF_INET6:
3387 chunk_printf(&msg, " addr=%s:%d\n",
Willy Tarreau986a9d22012-08-30 21:11:38 +02003388 pn, get_host_port(&sess->si[1].conn.addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07003389 break;
3390 case AF_UNIX:
3391 chunk_printf(&msg, " addr=unix\n");
3392 break;
3393 default:
3394 /* no more information to print right now */
3395 chunk_printf(&msg, "\n");
3396 break;
3397 }
3398
3399 if (sess->be->cap & PR_CAP_BE)
3400 chunk_printf(&msg,
3401 " server=%s (id=%u)",
Willy Tarreau50943332011-09-02 17:33:05 +02003402 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
3403 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
3404 else
Daniel Schultze90690c72012-03-23 10:53:36 -07003405 chunk_printf(&msg, " server=<NONE> (id=-1)");
3406
Willy Tarreau986a9d22012-08-30 21:11:38 +02003407 conn_get_to_addr(&sess->si[1].conn);
3408 switch (addr_to_str(&sess->si[1].conn.addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003409 case AF_INET:
3410 case AF_INET6:
3411 chunk_printf(&msg, " addr=%s:%d\n",
Willy Tarreau986a9d22012-08-30 21:11:38 +02003412 pn, get_host_port(&sess->si[1].conn.addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003413 break;
3414 case AF_UNIX:
3415 chunk_printf(&msg, " addr=unix\n");
3416 break;
3417 default:
3418 /* no more information to print right now */
3419 chunk_printf(&msg, "\n");
3420 break;
3421 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003422
3423 chunk_printf(&msg,
3424 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
3425 sess->task,
3426 sess->task->state,
3427 sess->task->nice, sess->task->calls,
3428 sess->task->expire ?
3429 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3430 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3431 TICKS_TO_MS(1000)) : "<NEVER>",
3432 task_in_rq(sess->task) ? ", running" : "");
3433
3434 get_localtime(sess->logs.accept_date.tv_sec, &tm);
3435 chunk_printf(&msg,
3436 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
3437 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3438 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3439 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3440
3441 chunk_printf(&msg,
3442 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3443 &sess->si[0],
3444 sess->si[0].state,
3445 sess->si[0].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003446 si_fd(&sess->si[0]),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003447 sess->si[0].exp ?
3448 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3449 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3450 TICKS_TO_MS(1000)) : "<NEVER>",
3451 sess->si[0].err_type);
3452
3453 chunk_printf(&msg,
3454 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3455 &sess->si[1],
3456 sess->si[1].state,
3457 sess->si[1].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003458 si_fd(&sess->si[1]),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003459 sess->si[1].exp ?
3460 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3461 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3462 TICKS_TO_MS(1000)) : "<NEVER>",
3463 sess->si[1].err_type);
3464
3465 chunk_printf(&msg,
3466 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3467 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3468 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3469
3470
3471 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003472 " req=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003473 " an_exp=%s",
3474 sess->req,
3475 sess->req->flags, sess->req->analysers,
Willy Tarreau572bf902012-07-02 17:01:20 +02003476 sess->req->buf.i, sess->req->buf.o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003477 sess->req->pipe ? sess->req->pipe->data : 0,
3478 sess->req->to_forward,
3479 sess->req->analyse_exp ?
3480 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3481 TICKS_TO_MS(1000)) : "<NEVER>");
3482
3483 chunk_printf(&msg,
3484 " rex=%s",
3485 sess->req->rex ?
3486 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3487 TICKS_TO_MS(1000)) : "<NEVER>");
3488
3489 chunk_printf(&msg,
3490 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003491 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003492 sess->req->wex ?
3493 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3494 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau572bf902012-07-02 17:01:20 +02003495 sess->req->buf.data,
3496 (int)(sess->req->buf.p - sess->req->buf.data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003497 sess->txn.req.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003498 sess->req->total);
3499
3500 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003501 " res=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003502 " an_exp=%s",
3503 sess->rep,
3504 sess->rep->flags, sess->rep->analysers,
Willy Tarreau572bf902012-07-02 17:01:20 +02003505 sess->rep->buf.i, sess->rep->buf.o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003506 sess->rep->pipe ? sess->rep->pipe->data : 0,
3507 sess->rep->to_forward,
3508 sess->rep->analyse_exp ?
3509 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3510 TICKS_TO_MS(1000)) : "<NEVER>");
3511
3512 chunk_printf(&msg,
3513 " rex=%s",
3514 sess->rep->rex ?
3515 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3516 TICKS_TO_MS(1000)) : "<NEVER>");
3517
3518 chunk_printf(&msg,
3519 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003520 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003521 sess->rep->wex ?
3522 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3523 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau572bf902012-07-02 17:01:20 +02003524 sess->rep->buf.data,
3525 (int)(sess->rep->buf.p - sess->rep->buf.data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003526 sess->txn.rsp.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003527 sess->rep->total);
3528
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003529 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003530 return 0;
3531
3532 /* use other states to dump the contents */
3533 }
3534 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003535 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003536 return 1;
3537}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003538
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003539/* This function dumps all sessions' states onto the stream intreface's
3540 * read buffer. The data_ctx must have been zeroed first, and the flags
3541 * properly set. It returns 0 if the output buffer is full and it needs
3542 * to be called again, otherwise non-zero. It is designed to be called
3543 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003544 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003545static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003546{
3547 struct chunk msg;
3548
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003549 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003550 /* If we're forced to shut down, we might have to remove our
3551 * reference to the last session being dumped.
3552 */
Willy Tarreau94981132012-05-21 17:09:48 +02003553 if (si->conn.data_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003554 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3555 LIST_DEL(&si->applet.ctx.sess.bref.users);
3556 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003557 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003558 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003559 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003560 }
3561
David du Colombier7af46052012-05-16 14:16:48 +02003562 chunk_init(&msg, trash, trashlen);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003563
Willy Tarreau94981132012-05-21 17:09:48 +02003564 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003565 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003566 /* the function had not been called yet, let's prepare the
3567 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003568 * pointer to the first in the global list. When a target
3569 * session is being destroyed, it is responsible for updating
3570 * this pointer. We know we have reached the end when this
3571 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003572 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003573 LIST_INIT(&si->applet.ctx.sess.bref.users);
3574 si->applet.ctx.sess.bref.ref = sessions.n;
Willy Tarreau94981132012-05-21 17:09:48 +02003575 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003576 /* fall through */
3577
Willy Tarreau295a8372011-03-10 11:25:07 +01003578 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003579 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003580 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3581 LIST_DEL(&si->applet.ctx.sess.bref.users);
3582 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003583 }
3584
3585 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003586 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003587 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003588 struct session *curr_sess;
3589
Willy Tarreau295a8372011-03-10 11:25:07 +01003590 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003591
Willy Tarreau295a8372011-03-10 11:25:07 +01003592 if (si->applet.ctx.sess.target) {
3593 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003594 goto next_sess;
3595
Willy Tarreau295a8372011-03-10 11:25:07 +01003596 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003597 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003598 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003599 return 0;
3600
3601 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003602 LIST_DEL(&si->applet.ctx.sess.bref.users);
3603 LIST_INIT(&si->applet.ctx.sess.bref.users);
3604 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003605 break;
3606 }
3607
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003608 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003609 "%p: proto=%s",
3610 curr_sess,
3611 curr_sess->listener->proto->name);
3612
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003613
Willy Tarreau986a9d22012-08-30 21:11:38 +02003614 switch (addr_to_str(&curr_sess->si[0].conn.addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003615 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003616 case AF_INET6:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003617 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003618 " src=%s:%d fe=%s be=%s srv=%s",
3619 pn,
Willy Tarreau986a9d22012-08-30 21:11:38 +02003620 get_host_port(&curr_sess->si[0].conn.addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003621 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003622 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003623 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003624 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003625 break;
3626 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02003627 chunk_printf(&msg,
3628 " src=unix:%d fe=%s be=%s srv=%s",
3629 curr_sess->listener->luid,
3630 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003631 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003632 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003633 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003634 break;
3635 }
3636
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003637 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003638 " ts=%02x age=%s calls=%d",
3639 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003640 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3641 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003642
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003643 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003644 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003645 curr_sess->req->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02003646 curr_sess->req->buf.i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003647 curr_sess->req->analysers,
3648 curr_sess->req->rex ?
3649 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3650 TICKS_TO_MS(1000)) : "");
3651
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003652 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003653 ",wx=%s",
3654 curr_sess->req->wex ?
3655 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3656 TICKS_TO_MS(1000)) : "");
3657
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003658 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003659 ",ax=%s]",
3660 curr_sess->req->analyse_exp ?
3661 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3662 TICKS_TO_MS(1000)) : "");
3663
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003664 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003665 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003666 curr_sess->rep->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02003667 curr_sess->rep->buf.i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003668 curr_sess->rep->analysers,
3669 curr_sess->rep->rex ?
3670 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3671 TICKS_TO_MS(1000)) : "");
3672
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003673 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003674 ",wx=%s",
3675 curr_sess->rep->wex ?
3676 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3677 TICKS_TO_MS(1000)) : "");
3678
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003679 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003680 ",ax=%s]",
3681 curr_sess->rep->analyse_exp ?
3682 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3683 TICKS_TO_MS(1000)) : "");
3684
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003685 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003686 " s0=[%d,%1xh,fd=%d,ex=%s]",
3687 curr_sess->si[0].state,
3688 curr_sess->si[0].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003689 si_fd(&curr_sess->si[0]),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003690 curr_sess->si[0].exp ?
3691 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3692 TICKS_TO_MS(1000)) : "");
3693
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003694 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003695 " s1=[%d,%1xh,fd=%d,ex=%s]",
3696 curr_sess->si[1].state,
3697 curr_sess->si[1].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003698 si_fd(&curr_sess->si[1]),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003699 curr_sess->si[1].exp ?
3700 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3701 TICKS_TO_MS(1000)) : "");
3702
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003703 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003704 " exp=%s",
3705 curr_sess->task->expire ?
3706 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3707 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003708 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003709 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003710
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003711 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003712
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003713 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003714 /* let's try again later from this session. We add ourselves into
3715 * this session's users so that it can remove us upon termination.
3716 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003717 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003718 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003719 }
3720
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003721 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003722 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003723 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003724
Willy Tarreau295a8372011-03-10 11:25:07 +01003725 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003726 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003727 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003728 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3729 else
3730 chunk_printf(&msg, "Session not found.\n");
3731
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003732 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003733 return 0;
3734
Willy Tarreau295a8372011-03-10 11:25:07 +01003735 si->applet.ctx.sess.target = NULL;
3736 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003737 return 1;
3738 }
3739
Willy Tarreau94981132012-05-21 17:09:48 +02003740 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003741 /* fall through */
3742
3743 default:
Willy Tarreau94981132012-05-21 17:09:48 +02003744 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003745 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003746 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003747}
3748
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003749/* This function dumps all tables' states onto the stream intreface's
3750 * read buffer. The data_ctx must have been zeroed first, and the flags
3751 * properly set. It returns 0 if the output buffer is full and it needs
3752 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003753 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003754static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003755{
Willy Tarreau94981132012-05-21 17:09:48 +02003756 struct session *s = si->conn.data_ctx;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003757 struct chunk msg;
3758 struct ebmb_node *eb;
3759 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003760 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003761
3762 /*
Willy Tarreau94981132012-05-21 17:09:48 +02003763 * We have 3 possible states in si->conn.data_st :
Willy Tarreau295a8372011-03-10 11:25:07 +01003764 * - STAT_ST_INIT : the first call
3765 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003766 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003767 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003768 * and the entry pointer points to the next entry to be dumped,
3769 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003770 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003771 * data though.
3772 */
3773
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003774 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003775 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau94981132012-05-21 17:09:48 +02003776 if (si->conn.data_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003777 si->applet.ctx.table.entry->ref_cnt--;
3778 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003779 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003780 return 1;
3781 }
3782
David du Colombier7af46052012-05-16 14:16:48 +02003783 chunk_init(&msg, trash, trashlen);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003784
Willy Tarreau94981132012-05-21 17:09:48 +02003785 while (si->conn.data_st != STAT_ST_FIN) {
3786 switch (si->conn.data_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003787 case STAT_ST_INIT:
3788 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3789 if (!si->applet.ctx.table.proxy)
3790 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003791
Willy Tarreau295a8372011-03-10 11:25:07 +01003792 si->applet.ctx.table.entry = NULL;
Willy Tarreau94981132012-05-21 17:09:48 +02003793 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003794 break;
3795
Willy Tarreau295a8372011-03-10 11:25:07 +01003796 case STAT_ST_INFO:
3797 if (!si->applet.ctx.table.proxy ||
3798 (si->applet.ctx.table.target &&
3799 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
Willy Tarreau94981132012-05-21 17:09:48 +02003800 si->conn.data_st = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003801 break;
3802 }
3803
Willy Tarreau295a8372011-03-10 11:25:07 +01003804 if (si->applet.ctx.table.proxy->table.size) {
Simon Hormanc88b8872011-06-15 15:18:49 +09003805 if (show && !stats_dump_table_head_to_buffer(&msg, si, si->applet.ctx.table.proxy,
3806 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003807 return 0;
3808
Willy Tarreau295a8372011-03-10 11:25:07 +01003809 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003810 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
3811 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003812 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003813 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003814 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3815 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau94981132012-05-21 17:09:48 +02003816 si->conn.data_st = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003817 break;
3818 }
3819 }
3820 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003821 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003822 break;
3823
Willy Tarreau295a8372011-03-10 11:25:07 +01003824 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003825 skip_entry = false;
3826
Willy Tarreau295a8372011-03-10 11:25:07 +01003827 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003828 /* we're filtering on some data contents */
3829 void *ptr;
3830 long long data;
3831
Willy Tarreau295a8372011-03-10 11:25:07 +01003832 dt = si->applet.ctx.table.data_type;
3833 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3834 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003835 dt);
3836
3837 data = 0;
3838 switch (stktable_data_types[dt].std_type) {
3839 case STD_T_SINT:
3840 data = stktable_data_cast(ptr, std_t_sint);
3841 break;
3842 case STD_T_UINT:
3843 data = stktable_data_cast(ptr, std_t_uint);
3844 break;
3845 case STD_T_ULL:
3846 data = stktable_data_cast(ptr, std_t_ull);
3847 break;
3848 case STD_T_FRQP:
3849 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003850 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003851 break;
3852 }
3853
3854 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003855 if ((data < si->applet.ctx.table.value &&
3856 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3857 si->applet.ctx.table.data_op == STD_OP_GT ||
3858 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3859 (data == si->applet.ctx.table.value &&
3860 (si->applet.ctx.table.data_op == STD_OP_NE ||
3861 si->applet.ctx.table.data_op == STD_OP_GT ||
3862 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3863 (data > si->applet.ctx.table.value &&
3864 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3865 si->applet.ctx.table.data_op == STD_OP_LT ||
3866 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003867 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003868 }
3869
Simon Hormanc88b8872011-06-15 15:18:49 +09003870 if (show && !skip_entry &&
3871 !stats_dump_table_entry_to_buffer(&msg, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003872 si->applet.ctx.table.entry))
3873 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003874
Willy Tarreau295a8372011-03-10 11:25:07 +01003875 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003876
Willy Tarreau295a8372011-03-10 11:25:07 +01003877 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003878 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003879 struct stksess *old = si->applet.ctx.table.entry;
3880 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01003881 if (show)
3882 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3883 else
3884 stksess_kill(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau295a8372011-03-10 11:25:07 +01003885 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003886 break;
3887 }
3888
Simon Hormanc88b8872011-06-15 15:18:49 +09003889
3890 if (show)
3891 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3892 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3893 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3894
Willy Tarreau295a8372011-03-10 11:25:07 +01003895 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau94981132012-05-21 17:09:48 +02003896 si->conn.data_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003897 break;
3898
Willy Tarreau295a8372011-03-10 11:25:07 +01003899 case STAT_ST_END:
Willy Tarreau94981132012-05-21 17:09:48 +02003900 si->conn.data_st = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003901 break;
3902 }
3903 }
3904 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003905}
3906
Willy Tarreaud426a182010-03-05 14:58:26 +01003907/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003908 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3909 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3910 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3911 * lines are respected within the limit of 70 output chars. Lines that are
3912 * continuation of a previous truncated line begin with "+" instead of " "
3913 * after the offset. The new pointer is returned.
3914 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003915static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3916 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003917{
3918 int end;
3919 unsigned char c;
3920
3921 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003922 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003923 return ptr;
3924
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003925 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003926
Willy Tarreaud426a182010-03-05 14:58:26 +01003927 while (ptr < len && ptr < bsize) {
3928 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003929 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003930 if (out->len > end - 2)
3931 break;
3932 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003933 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003934 if (out->len > end - 3)
3935 break;
3936 out->str[out->len++] = '\\';
3937 switch (c) {
3938 case '\t': c = 't'; break;
3939 case '\n': c = 'n'; break;
3940 case '\r': c = 'r'; break;
3941 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003942 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003943 }
3944 out->str[out->len++] = c;
3945 } else {
3946 if (out->len > end - 5)
3947 break;
3948 out->str[out->len++] = '\\';
3949 out->str[out->len++] = 'x';
3950 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3951 out->str[out->len++] = hextab[c & 0xF];
3952 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003953 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003954 /* we had a line break, let's return now */
3955 out->str[out->len++] = '\n';
3956 *line = ptr;
3957 return ptr;
3958 }
3959 }
3960 /* we have an incomplete line, we return it as-is */
3961 out->str[out->len++] = '\n';
3962 return ptr;
3963}
3964
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003965/* This function dumps all captured errors onto the stream intreface's
3966 * read buffer. The data_ctx must have been zeroed first, and the flags
3967 * properly set. It returns 0 if the output buffer is full and it needs
3968 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003969 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003970static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003971{
3972 extern const char *monthname[12];
3973 struct chunk msg;
3974
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003975 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003976 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003977
David du Colombier7af46052012-05-16 14:16:48 +02003978 chunk_init(&msg, trash, trashlen);
Willy Tarreau74808cb2009-03-04 15:53:18 +01003979
Willy Tarreau295a8372011-03-10 11:25:07 +01003980 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003981 /* the function had not been called yet, let's prepare the
3982 * buffer for a response.
3983 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003984 struct tm tm;
3985
3986 get_localtime(date.tv_sec, &tm);
3987 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3988 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3989 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3990 error_snapshot_id);
3991
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003992 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003993 /* Socket buffer full. Let's try again later from the same point */
3994 return 0;
3995 }
3996
Willy Tarreau295a8372011-03-10 11:25:07 +01003997 si->applet.ctx.errors.px = proxy;
3998 si->applet.ctx.errors.buf = 0;
3999 si->applet.ctx.errors.bol = 0;
4000 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004001 }
4002
4003 /* we have two inner loops here, one for the proxy, the other one for
4004 * the buffer.
4005 */
Willy Tarreau295a8372011-03-10 11:25:07 +01004006 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004007 struct error_snapshot *es;
4008
Willy Tarreau295a8372011-03-10 11:25:07 +01004009 if (si->applet.ctx.errors.buf == 0)
4010 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004011 else
Willy Tarreau295a8372011-03-10 11:25:07 +01004012 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004013
4014 if (!es->when.tv_sec)
4015 goto next;
4016
Willy Tarreau295a8372011-03-10 11:25:07 +01004017 if (si->applet.ctx.errors.iid >= 0 &&
4018 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
4019 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004020 goto next;
4021
Willy Tarreau295a8372011-03-10 11:25:07 +01004022 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004023 /* just print headers now */
4024
4025 char pn[INET6_ADDRSTRLEN];
4026 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004027 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004028
4029 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01004030 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01004031 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02004032 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01004033
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004034 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
4035 case AF_INET:
4036 case AF_INET6:
4037 port = get_host_port(&es->src);
4038 break;
4039 default:
4040 port = 0;
4041 }
4042
Willy Tarreau295a8372011-03-10 11:25:07 +01004043 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004044 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004045 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004046 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004047 " backend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004048 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004049 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
4050 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004051 break;
4052 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004053 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004054 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004055 " frontend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004056 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004057 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004058 break;
4059 }
4060
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004061 chunk_printf(&msg,
4062 ", server %s (#%d), event #%u\n"
4063 " src %s:%d, session #%d, session flags 0x%08x\n"
4064 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
4065 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
4066 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
4067 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
4068 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
4069 es->ev_id,
4070 pn, port, es->sid, es->s_flags,
4071 es->state, es->m_flags, es->t_flags,
4072 es->m_clen, es->m_blen,
4073 es->b_flags, es->b_out, es->b_tot,
4074 es->len, es->b_wrap, es->pos);
4075
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004076 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004077 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004078 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004079 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004080 si->applet.ctx.errors.ptr = 0;
4081 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004082 }
4083
Willy Tarreau295a8372011-03-10 11:25:07 +01004084 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004085 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004086 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004087 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004088 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004089 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004090 goto next;
4091 }
4092
4093 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01004094 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004095 int newptr;
4096 int newline;
4097
Willy Tarreau295a8372011-03-10 11:25:07 +01004098 newline = si->applet.ctx.errors.bol;
4099 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
4100 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004101 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004102
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004103 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004104 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004105 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004106 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004107 si->applet.ctx.errors.ptr = newptr;
4108 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004109 };
4110 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01004111 si->applet.ctx.errors.bol = 0;
4112 si->applet.ctx.errors.ptr = -1;
4113 si->applet.ctx.errors.buf++;
4114 if (si->applet.ctx.errors.buf > 1) {
4115 si->applet.ctx.errors.buf = 0;
4116 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004117 }
4118 }
4119
4120 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004121 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004122}
4123
Willy Tarreaub24281b2011-02-13 13:16:36 +01004124struct si_applet http_stats_applet = {
4125 .name = "<STATS>", /* used for logging */
4126 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004127 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004128};
4129
Simon Horman9bd2c732011-06-15 15:18:44 +09004130static struct si_applet cli_applet = {
Willy Tarreaub24281b2011-02-13 13:16:36 +01004131 .name = "<CLI>", /* used for logging */
4132 .fct = cli_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004133 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004134};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004135
Willy Tarreau10522fd2008-07-09 20:12:41 +02004136static struct cfg_kw_list cfg_kws = {{ },{
4137 { CFG_GLOBAL, "stats", stats_parse_global },
4138 { 0, NULL, NULL },
4139}};
4140
4141__attribute__((constructor))
4142static void __dumpstats_module_init(void)
4143{
4144 cfg_register_keywords(&cfg_kws);
4145}
4146
Willy Tarreau91861262007-10-17 17:06:05 +02004147/*
4148 * Local variables:
4149 * c-indent-level: 8
4150 * c-basic-offset: 8
4151 * End:
4152 */