blob: bfa6964105bcd5f69f9e4a2926c48b4016cf08e8 [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 Tarreaud9bdcd52012-12-22 20:31:10 +01004 * Copyright 2000-2012 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>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020020#include <pwd.h>
21#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020022
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreau10522fd2008-07-09 20:12:41 +020027#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020028#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
Emeric Brun4147b2e2014-06-16 18:36:30 +020038#include <common/base64.h>
Willy Tarreau91861262007-10-17 17:06:05 +020039
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020040#include <types/applet.h>
Willy Tarreau91861262007-10-17 17:06:05 +020041#include <types/global.h>
Baptiste Assmann3863f972015-05-17 00:33:24 +020042#include <types/dns.h>
Willy Tarreau91861262007-10-17 17:06:05 +020043
44#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020045#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020046#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010047#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020048#include <proto/dumpstats.h>
49#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010050#include <proto/freq_ctr.h>
Willy Tarreau32b60d42015-03-13 16:14:57 +010051#include <proto/frontend.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020052#include <proto/log.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010053#include <proto/pattern.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010054#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020055#include <proto/listener.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010056#include <proto/map.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010057#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020058#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020059#include <proto/proxy.h>
Willy Tarreau1cf8f082014-02-07 12:14:54 +010060#include <proto/sample.h>
Willy Tarreau9903f0e2015-04-04 18:50:31 +020061#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020062#include <proto/stream.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020063#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020064#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010065#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010066#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020067
Willy Tarreau7a0169a2012-11-19 17:13:16 +010068#ifdef USE_OPENSSL
69#include <proto/ssl_sock.h>
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +020070#include <types/ssl_sock.h>
Willy Tarreau7a0169a2012-11-19 17:13:16 +010071#endif
72
Willy Tarreau91b843d2014-01-28 16:27:17 +010073/* stats socket states */
74enum {
75 STAT_CLI_INIT = 0, /* initial state, must leave to zero ! */
76 STAT_CLI_END, /* final state, let's close */
77 STAT_CLI_GETREQ, /* wait for a request */
78 STAT_CLI_OUTPUT, /* all states after this one are responses */
79 STAT_CLI_PROMPT, /* display the prompt (first output, same code) */
80 STAT_CLI_PRINT, /* display message in cli->msg */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +010081 STAT_CLI_PRINT_FREE, /* display message in cli->msg. After the display, free the pointer */
Willy Tarreau91b843d2014-01-28 16:27:17 +010082 STAT_CLI_O_INFO, /* dump info */
Willy Tarreau87b09662015-04-03 00:22:06 +020083 STAT_CLI_O_SESS, /* dump streams */
Willy Tarreau91b843d2014-01-28 16:27:17 +010084 STAT_CLI_O_ERR, /* dump errors */
85 STAT_CLI_O_TAB, /* dump tables */
86 STAT_CLI_O_CLR, /* clear tables */
87 STAT_CLI_O_SET, /* set entries in tables */
88 STAT_CLI_O_STAT, /* dump stats */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010089 STAT_CLI_O_PATS, /* list all pattern reference avalaible */
90 STAT_CLI_O_PAT, /* list all entries of a pattern */
Willy Tarreau91b843d2014-01-28 16:27:17 +010091 STAT_CLI_O_MLOOK, /* lookup a map entry */
Willy Tarreau12833bb2014-01-28 16:49:56 +010092 STAT_CLI_O_POOLS, /* dump memory pools */
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +020093 STAT_CLI_O_TLSK, /* list all TLS ticket keys references */
Baptiste Assmann3863f972015-05-17 00:33:24 +020094 STAT_CLI_O_RESOLVERS,/* dump a resolver's section nameservers counters */
Baptiste Assmann28289462015-07-03 08:01:20 +020095 STAT_CLI_O_SERVERS_STATE, /* dump server state and changing information */
Baptiste Assmann9b6857e2015-09-18 14:49:12 +020096 STAT_CLI_O_BACKEND, /* dump backend list */
Willy Tarreauae795722016-02-16 11:27:28 +010097 STAT_CLI_O_ENV, /* dump environment */
Willy Tarreau91b843d2014-01-28 16:27:17 +010098};
99
Willy Tarreaued7df902014-05-22 18:04:49 +0200100/* Actions available for the stats admin forms */
101enum {
102 ST_ADM_ACTION_NONE = 0,
Willy Tarreau248a60e2014-05-23 14:59:48 +0200103
104 /* enable/disable health checks */
105 ST_ADM_ACTION_DHLTH,
106 ST_ADM_ACTION_EHLTH,
107
108 /* force health check status */
109 ST_ADM_ACTION_HRUNN,
110 ST_ADM_ACTION_HNOLB,
111 ST_ADM_ACTION_HDOWN,
112
113 /* enable/disable agent checks */
114 ST_ADM_ACTION_DAGENT,
115 ST_ADM_ACTION_EAGENT,
116
117 /* force agent check status */
118 ST_ADM_ACTION_ARUNN,
119 ST_ADM_ACTION_ADOWN,
120
121 /* set admin state */
Willy Tarreaued7df902014-05-22 18:04:49 +0200122 ST_ADM_ACTION_READY,
123 ST_ADM_ACTION_DRAIN,
124 ST_ADM_ACTION_MAINT,
125 ST_ADM_ACTION_SHUTDOWN,
126 /* these are the ancient actions, still available for compatibility */
127 ST_ADM_ACTION_DISABLE,
128 ST_ADM_ACTION_ENABLE,
129 ST_ADM_ACTION_STOP,
130 ST_ADM_ACTION_START,
131};
132
Willy Tarreauc4832de2016-01-11 18:12:26 +0100133
134/* Show Info fields for CLI output. For any field added here, please add the text
135 * representation in the info_field_names array below. Please only append at the end,
136 * before the INF_TOTAL_FIELDS entry, and never insert anything in the middle
137 * nor at the beginning.
138 */
139enum info_field {
140 INF_NAME,
141 INF_VERSION,
142 INF_RELEASE_DATE,
143 INF_NBPROC,
144 INF_PROCESS_NUM,
145 INF_PID,
146 INF_UPTIME,
147 INF_UPTIME_SEC,
148 INF_MEMMAX_MB,
149 INF_POOL_ALLOC_MB,
150 INF_POOL_USED_MB,
151 INF_POOL_FAILED,
152 INF_ULIMIT_N,
153 INF_MAXSOCK,
154 INF_MAXCONN,
155 INF_HARD_MAXCONN,
156 INF_CURR_CONN,
157 INF_CUM_CONN,
158 INF_CUM_REQ,
159 INF_MAX_SSL_CONNS,
160 INF_CURR_SSL_CONNS,
161 INF_CUM_SSL_CONNS,
162 INF_MAXPIPES,
163 INF_PIPES_USED,
164 INF_PIPES_FREE,
165 INF_CONN_RATE,
166 INF_CONN_RATE_LIMIT,
167 INF_MAX_CONN_RATE,
168 INF_SESS_RATE,
169 INF_SESS_RATE_LIMIT,
170 INF_MAX_SESS_RATE,
171 INF_SSL_RATE,
172 INF_SSL_RATE_LIMIT,
173 INF_MAX_SSL_RATE,
174 INF_SSL_FRONTEND_KEY_RATE,
175 INF_SSL_FRONTEND_MAX_KEY_RATE,
176 INF_SSL_FRONTEND_SESSION_REUSE_PCT,
177 INF_SSL_BACKEND_KEY_RATE,
178 INF_SSL_BACKEND_MAX_KEY_RATE,
179 INF_SSL_CACHE_LOOKUPS,
180 INF_SSL_CACHE_MISSES,
181 INF_COMPRESS_BPS_IN,
182 INF_COMPRESS_BPS_OUT,
183 INF_COMPRESS_BPS_RATE_LIM,
184 INF_ZLIB_MEM_USAGE,
185 INF_MAX_ZLIB_MEM_USAGE,
186 INF_TASKS,
187 INF_RUN_QUEUE,
188 INF_IDLE_PCT,
189 INF_NODE,
190 INF_DESCRIPTION,
191
192 /* must always be the last one */
193 INF_TOTAL_FIELDS
194};
195
196/* These are the field names for each INF_* field position. Please pay attention
197 * to always use the exact same name except that the strings for new names must
198 * be lower case or CamelCase while the enum entries must be upper case.
199 */
200const char *info_field_names[INF_TOTAL_FIELDS] = {
201 [INF_NAME] = "Name",
202 [INF_VERSION] = "Version",
203 [INF_RELEASE_DATE] = "Release_date",
204 [INF_NBPROC] = "Nbproc",
205 [INF_PROCESS_NUM] = "Process_num",
206 [INF_PID] = "Pid",
207 [INF_UPTIME] = "Uptime",
208 [INF_UPTIME_SEC] = "Uptime_sec",
209 [INF_MEMMAX_MB] = "Memmax_MB",
210 [INF_POOL_ALLOC_MB] = "PoolAlloc_MB",
211 [INF_POOL_USED_MB] = "PoolUsed_MB",
212 [INF_POOL_FAILED] = "PoolFailed",
213 [INF_ULIMIT_N] = "Ulimit-n",
214 [INF_MAXSOCK] = "Maxsock",
215 [INF_MAXCONN] = "Maxconn",
216 [INF_HARD_MAXCONN] = "Hard_maxconn",
217 [INF_CURR_CONN] = "CurrConns",
218 [INF_CUM_CONN] = "CumConns",
219 [INF_CUM_REQ] = "CumReq",
220 [INF_MAX_SSL_CONNS] = "MaxSslConns",
221 [INF_CURR_SSL_CONNS] = "CurrSslConns",
222 [INF_CUM_SSL_CONNS] = "CumSslConns",
223 [INF_MAXPIPES] = "Maxpipes",
224 [INF_PIPES_USED] = "PipesUsed",
225 [INF_PIPES_FREE] = "PipesFree",
226 [INF_CONN_RATE] = "ConnRate",
227 [INF_CONN_RATE_LIMIT] = "ConnRateLimit",
228 [INF_MAX_CONN_RATE] = "MaxConnRate",
229 [INF_SESS_RATE] = "SessRate",
230 [INF_SESS_RATE_LIMIT] = "SessRateLimit",
231 [INF_MAX_SESS_RATE] = "MaxSessRate",
232 [INF_SSL_RATE] = "SslRate",
233 [INF_SSL_RATE_LIMIT] = "SslRateLimit",
234 [INF_MAX_SSL_RATE] = "MaxSslRate",
235 [INF_SSL_FRONTEND_KEY_RATE] = "SslFrontendKeyRate",
236 [INF_SSL_FRONTEND_MAX_KEY_RATE] = "SslFrontendMaxKeyRate",
237 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = "SslFrontendSessionReuse_pct",
238 [INF_SSL_BACKEND_KEY_RATE] = "SslBackendKeyRate",
239 [INF_SSL_BACKEND_MAX_KEY_RATE] = "SslBackendMaxKeyRate",
240 [INF_SSL_CACHE_LOOKUPS] = "SslCacheLookups",
241 [INF_SSL_CACHE_MISSES] = "SslCacheMisses",
242 [INF_COMPRESS_BPS_IN] = "CompressBpsIn",
243 [INF_COMPRESS_BPS_OUT] = "CompressBpsOut",
244 [INF_COMPRESS_BPS_RATE_LIM] = "CompressBpsRateLim",
245 [INF_ZLIB_MEM_USAGE] = "ZlibMemUsage",
246 [INF_MAX_ZLIB_MEM_USAGE] = "MaxZlibMemUsage",
247 [INF_TASKS] = "Tasks",
248 [INF_RUN_QUEUE] = "Run_queue",
249 [INF_IDLE_PCT] = "Idle_pct",
250 [INF_NODE] = "node",
251 [INF_DESCRIPTION] = "description",
252};
253
254/* one line of stats */
255static struct field info[INF_TOTAL_FIELDS];
256
Willy Tarreau8e205142016-01-04 17:23:25 +0100257/* Stats fields for CSV output. For any field added here, please add the text
258 * representation in the stat_field_names array below. Please only append at the end,
259 * before the ST_F_TOTAL_FIELDS entry, and never insert anything in the middle
260 * nor at the beginning.
261 */
262enum stat_field {
263 ST_F_PXNAME,
264 ST_F_SVNAME,
265 ST_F_QCUR,
266 ST_F_QMAX,
267 ST_F_SCUR,
268 ST_F_SMAX,
269 ST_F_SLIM,
270 ST_F_STOT,
271 ST_F_BIN ,
272 ST_F_BOUT,
273 ST_F_DREQ,
274 ST_F_DRESP,
275 ST_F_EREQ,
276 ST_F_ECON,
277 ST_F_ERESP,
278 ST_F_WRETR,
279 ST_F_WREDIS,
280 ST_F_STATUS,
281 ST_F_WEIGHT,
282 ST_F_ACT,
283 ST_F_BCK,
284 ST_F_CHKFAIL,
285 ST_F_CHKDOWN,
286 ST_F_LASTCHG,
287 ST_F_DOWNTIME,
288 ST_F_QLIMIT,
289 ST_F_PID,
290 ST_F_IID,
291 ST_F_SID,
292 ST_F_THROTTLE,
293 ST_F_LBTOT,
294 ST_F_TRACKED,
295 ST_F_TYPE,
296 ST_F_RATE,
297 ST_F_RATE_LIM,
298 ST_F_RATE_MAX,
299 ST_F_CHECK_STATUS,
300 ST_F_CHECK_CODE,
301 ST_F_CHECK_DURATION,
302 ST_F_HRSP_1XX,
303 ST_F_HRSP_2XX,
304 ST_F_HRSP_3XX,
305 ST_F_HRSP_4XX,
306 ST_F_HRSP_5XX,
307 ST_F_HRSP_OTHER,
308 ST_F_HANAFAIL,
309 ST_F_REQ_RATE,
310 ST_F_REQ_RATE_MAX,
311 ST_F_REQ_TOT,
312 ST_F_CLI_ABRT,
313 ST_F_SRV_ABRT,
314 ST_F_COMP_IN,
315 ST_F_COMP_OUT,
316 ST_F_COMP_BYP,
317 ST_F_COMP_RSP,
318 ST_F_LASTSESS,
319 ST_F_LAST_CHK,
320 ST_F_LAST_AGT,
321 ST_F_QTIME,
322 ST_F_CTIME,
323 ST_F_RTIME,
324 ST_F_TTIME,
325
326 /* must always be the last one */
327 ST_F_TOTAL_FIELDS
328};
329
330/* These are the field names for each ST_F_* field position. Please pay attention
331 * to always use the exact same name except that the strings must be lower case
332 * while the enum entries must be upper case.
333 */
334const char *stat_field_names[ST_F_TOTAL_FIELDS] = {
335 [ST_F_PXNAME] = "pxname",
336 [ST_F_SVNAME] = "svname",
337 [ST_F_QCUR] = "qcur",
338 [ST_F_QMAX] = "qmax",
339 [ST_F_SCUR] = "scur",
340 [ST_F_SMAX] = "smax",
341 [ST_F_SLIM] = "slim",
342 [ST_F_STOT] = "stot",
343 [ST_F_BIN] = "bin",
344 [ST_F_BOUT] = "bout",
345 [ST_F_DREQ] = "dreq",
346 [ST_F_DRESP] = "dresp",
347 [ST_F_EREQ] = "ereq",
348 [ST_F_ECON] = "econ",
349 [ST_F_ERESP] = "eresp",
350 [ST_F_WRETR] = "wretr",
351 [ST_F_WREDIS] = "wredis",
352 [ST_F_STATUS] = "status",
353 [ST_F_WEIGHT] = "weight",
354 [ST_F_ACT] = "act",
355 [ST_F_BCK] = "bck",
356 [ST_F_CHKFAIL] = "chkfail",
357 [ST_F_CHKDOWN] = "chkdown",
358 [ST_F_LASTCHG] = "lastchg",
359 [ST_F_DOWNTIME] = "downtime",
360 [ST_F_QLIMIT] = "qlimit",
361 [ST_F_PID] = "pid",
362 [ST_F_IID] = "iid",
363 [ST_F_SID] = "sid",
364 [ST_F_THROTTLE] = "throttle",
365 [ST_F_LBTOT] = "lbtot",
366 [ST_F_TRACKED] = "tracked",
367 [ST_F_TYPE] = "type",
368 [ST_F_RATE] = "rate",
369 [ST_F_RATE_LIM] = "rate_lim",
370 [ST_F_RATE_MAX] = "rate_max",
371 [ST_F_CHECK_STATUS] = "check_status",
372 [ST_F_CHECK_CODE] = "check_code",
373 [ST_F_CHECK_DURATION] = "check_duration",
374 [ST_F_HRSP_1XX] = "hrsp_1xx",
375 [ST_F_HRSP_2XX] = "hrsp_2xx",
376 [ST_F_HRSP_3XX] = "hrsp_3xx",
377 [ST_F_HRSP_4XX] = "hrsp_4xx",
378 [ST_F_HRSP_5XX] = "hrsp_5xx",
379 [ST_F_HRSP_OTHER] = "hrsp_other",
380 [ST_F_HANAFAIL] = "hanafail",
381 [ST_F_REQ_RATE] = "req_rate",
382 [ST_F_REQ_RATE_MAX] = "req_rate_max",
383 [ST_F_REQ_TOT] = "req_tot",
384 [ST_F_CLI_ABRT] = "cli_abrt",
385 [ST_F_SRV_ABRT] = "srv_abrt",
386 [ST_F_COMP_IN] = "comp_in",
387 [ST_F_COMP_OUT] = "comp_out",
388 [ST_F_COMP_BYP] = "comp_byp",
389 [ST_F_COMP_RSP] = "comp_rsp",
390 [ST_F_LASTSESS] = "lastsess",
391 [ST_F_LAST_CHK] = "last_chk",
392 [ST_F_LAST_AGT] = "last_agt",
393 [ST_F_QTIME] = "qtime",
394 [ST_F_CTIME] = "ctime",
395 [ST_F_RTIME] = "rtime",
396 [ST_F_TTIME] = "ttime",
397};
398
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200399static int stats_dump_backend_to_buffer(struct stream_interface *si);
Willy Tarreauae795722016-02-16 11:27:28 +0100400static int stats_dump_env_to_buffer(struct stream_interface *si);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100401static int stats_dump_info_to_buffer(struct stream_interface *si);
Baptiste Assmann28289462015-07-03 08:01:20 +0200402static int stats_dump_servers_state_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100403static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau87b09662015-04-03 00:22:06 +0200404static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900405static int stats_dump_sess_to_buffer(struct stream_interface *si);
406static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100407static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100408static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
409static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Baptiste Assmann3863f972015-05-17 00:33:24 +0200410static int stats_dump_resolvers_to_buffer(struct stream_interface *si);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100411static int stats_pats_list(struct stream_interface *si);
412static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100413static int stats_map_lookup(struct stream_interface *si);
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200414#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
415static int stats_tlskeys_list(struct stream_interface *si);
416#endif
Willy Tarreau00a37f02015-04-13 12:05:19 +0200417static void cli_release_handler(struct appctx *appctx);
Simon Horman9bd2c732011-06-15 15:18:44 +0900418
Baptiste Assmann28289462015-07-03 08:01:20 +0200419static void dump_servers_state(struct proxy *backend, struct chunk *buf);
420
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100421/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100422 * cli_io_handler()
423 * -> stats_dump_sess_to_buffer() // "show sess"
424 * -> stats_dump_errors_to_buffer() // "show errors"
425 * -> stats_dump_info_to_buffer() // "show info"
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200426 * -> stats_dump_backend_to_buffer() // "show backend"
Baptiste Assmann28289462015-07-03 08:01:20 +0200427 * -> stats_dump_servers_state_to_buffer() // "show servers state [<backend name>]"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100428 * -> stats_dump_stat_to_buffer() // "show stat"
Baptiste Assmann3863f972015-05-17 00:33:24 +0200429 * -> stats_dump_resolvers_to_buffer() // "show stat resolver <id>"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100430 * -> stats_dump_csv_header()
431 * -> stats_dump_proxy_to_buffer()
432 * -> stats_dump_fe_stats()
433 * -> stats_dump_li_stats()
434 * -> stats_dump_sv_stats()
435 * -> stats_dump_be_stats()
436 *
437 * http_stats_io_handler()
438 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
439 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
440 * -> stats_dump_html_head() // emits the HTML headers
441 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
442 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
443 * -> stats_dump_html_px_hdr()
444 * -> stats_dump_fe_stats()
445 * -> stats_dump_li_stats()
446 * -> stats_dump_sv_stats()
447 * -> stats_dump_be_stats()
448 * -> stats_dump_html_px_end()
449 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100450 */
451
Willy Tarreau30576452015-04-13 13:50:30 +0200452static struct applet cli_applet;
Simon Horman9bd2c732011-06-15 15:18:44 +0900453
454static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200455 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200456 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200457 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200458 " help : this message\n"
459 " prompt : toggle interactive mode with prompt\n"
460 " quit : disconnect\n"
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200461 " show backend : list backends in the current running config\n"
Willy Tarreauae795722016-02-16 11:27:28 +0100462 " show env [var] : dump environment variables known to the process\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200463 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100464 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200465 " show stat : report counters for each proxy and server\n"
466 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100467 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200468 " show table [id]: report table usage stats or dump this table's contents\n"
Baptiste Assmann28289462015-07-03 08:01:20 +0200469 " show servers state [id]: dump volatile server information (for backend <id>)\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200470 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200471 " set weight : change a server's weight\n"
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200472 " set server : change a server's state, weight or address\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200473 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100474 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200475 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200476 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200477 " disable : put a server or frontend in maintenance mode\n"
478 " enable : re-enable a server or frontend which is in maintenance mode\n"
479 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100480 " show acl [id] : report avalaible acls or dump an acl's contents\n"
481 " get acl : reports the patterns matching a sample for an ACL\n"
482 " add acl : add acl entry\n"
483 " del acl : delete acl entry\n"
484 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100485 " show map [id] : report avalaible maps or dump a map's contents\n"
486 " get map : reports the keys and values matching a sample for a map\n"
487 " set map : modify map entry\n"
488 " add map : add map entry\n"
489 " del map : delete map entry\n"
490 " clear map <id> : clear the content of this map\n"
Emeric Brun4147b2e2014-06-16 18:36:30 +0200491 " set ssl <stmt> : set statement for ssl\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200492 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200493
Simon Horman9bd2c732011-06-15 15:18:44 +0900494static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200495 "Permission denied\n"
496 "";
497
Willy Tarreau295a8372011-03-10 11:25:07 +0100498/* data transmission states for the stats responses */
499enum {
500 STAT_ST_INIT = 0,
501 STAT_ST_HEAD,
502 STAT_ST_INFO,
503 STAT_ST_LIST,
504 STAT_ST_END,
505 STAT_ST_FIN,
506};
507
508/* data transmission states for the stats responses inside a proxy */
509enum {
510 STAT_PX_ST_INIT = 0,
511 STAT_PX_ST_TH,
512 STAT_PX_ST_FE,
513 STAT_PX_ST_LI,
514 STAT_PX_ST_SV,
515 STAT_PX_ST_BE,
516 STAT_PX_ST_END,
517 STAT_PX_ST_FIN,
518};
519
Cyril Bonté19979e12012-04-04 12:57:21 +0200520extern const char *stat_status_codes[];
521
Willy Tarreau07e9e642010-08-17 21:48:17 +0200522/* allocate a new stats frontend named <name>, and return it
523 * (or NULL in case of lack of memory).
524 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200525static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200526{
527 struct proxy *fe;
528
529 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
530 if (!fe)
531 return NULL;
532
Willy Tarreau237250c2011-07-29 01:49:03 +0200533 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200534 fe->next = proxy;
535 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200536 fe->last_change = now.tv_sec;
537 fe->id = strdup("GLOBAL");
538 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200539 fe->maxconn = 10; /* default to 10 concurrent connections */
540 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200541 fe->conf.file = strdup(file);
542 fe->conf.line = line;
Willy Tarreau32b60d42015-03-13 16:14:57 +0100543 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +0100544 fe->default_target = &cli_applet.obj_type;
Willy Tarreau050536d2012-10-04 08:47:34 +0200545
546 /* the stats frontend is the only one able to assign ID #0 */
547 fe->conf.id.key = fe->uuid = 0;
548 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200549 return fe;
550}
551
Willy Tarreaufbee7132007-10-18 13:53:22 +0200552/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200553 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
554 * error message into the <err> buffer which will be preallocated. The trailing
555 * '\n' must not be written. The function must be called with <args> pointing to
556 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200557 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200558static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200559 struct proxy *defpx, const char *file, int line,
560 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200561{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200562 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200563 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200564
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200565 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200566 int cur_arg;
567
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200568 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200569 memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200570 return -1;
571 }
572
Willy Tarreau89a63132009-08-16 17:41:45 +0200573 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200574 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200575 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200576 return -1;
577 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200578 }
579
Willy Tarreau4348fad2012-09-20 16:48:07 +0200580 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200581 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200582
Willy Tarreauc53d4222012-09-20 20:19:28 +0200583 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
584 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
585 file, line, args[0], args[1], err && *err ? *err : "error");
586 return -1;
587 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200588
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200589 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200590 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200591 static int bind_dumped;
592 struct bind_kw *kw;
593
594 kw = bind_find_kw(args[cur_arg]);
595 if (kw) {
596 if (!kw->parse) {
597 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
598 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200599 return -1;
600 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200601
Willy Tarreau061b5de2015-10-13 15:06:57 +0200602 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200603 if (err && *err)
604 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
605 else
606 memprintf(err, "'%s %s' : error encountered while processing '%s'",
607 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200608 return -1;
609 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200610
611 cur_arg += 1 + kw->skip;
612 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200613 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200614
615 if (!bind_dumped) {
616 bind_dump_kws(err);
617 indent_msg(err, 4);
618 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200619 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200620
621 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
622 args[0], args[1], args[cur_arg],
623 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
624 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200625 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100626
Willy Tarreauc53d4222012-09-20 20:19:28 +0200627 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
628 l->maxconn = global.stats_fe->maxconn;
629 l->backlog = global.stats_fe->backlog;
Willy Tarreau9903f0e2015-04-04 18:50:31 +0200630 l->accept = session_accept_fd;
Willy Tarreau87b09662015-04-03 00:22:06 +0200631 l->handler = process_stream;
Willy Tarreau10b688f2015-03-13 16:43:12 +0100632 l->default_target = global.stats_fe->default_target;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200633 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
634 l->nice = -64; /* we want to boost priority for local stats */
635 global.maxsock += l->maxconn;
636 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200637 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200638 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100639 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200640 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100641
642 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200643 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100644 return -1;
645 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200646
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100647 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200648 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200649 return -1;
650 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200651 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200652 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200653 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200654 return -1;
655 }
656 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200657 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200658 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200659 else if (!strcmp(args[1], "maxconn")) {
660 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200661
662 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200663 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200664 return -1;
665 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200666
667 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200668 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200669 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200670 return -1;
671 }
672 }
673 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200674 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200675 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
676 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100677 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200678
Willy Tarreau91319572013-04-20 09:48:50 +0200679 if (!global.stats_fe) {
680 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
681 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
682 return -1;
683 }
684 }
685
Willy Tarreau35b7b162012-10-22 23:17:18 +0200686 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100687 unsigned int low, high;
688
Willy Tarreau35b7b162012-10-22 23:17:18 +0200689 if (strcmp(args[cur_arg], "all") == 0) {
690 set = 0;
691 break;
692 }
693 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100694 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200695 }
696 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100697 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200698 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100699 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100700 char *dash = strchr(args[cur_arg], '-');
701
702 low = high = str2uic(args[cur_arg]);
703 if (dash)
704 high = str2uic(dash + 1);
705
706 if (high < low) {
707 unsigned int swap = low;
708 low = high;
709 high = swap;
710 }
711
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100712 if (low < 1 || high > LONGBITS) {
713 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
714 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200715 return -1;
716 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100717 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100718 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100719 }
720 else {
721 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100722 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
723 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100724 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200725 }
726 cur_arg++;
727 }
728 global.stats_fe->bind_proc = set;
729 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200730 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200731 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200732 return -1;
733 }
734 return 0;
735}
736
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100737/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
738 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100739 * NOTE: Some tools happen to rely on the field position instead of its name,
740 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100741 */
742static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100743{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100744 chunk_appendf(&trash,
745 "# pxname,svname,"
746 "qcur,qmax,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100747 "scur,smax,slim,stot,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100748 "bin,bout,"
749 "dreq,dresp,"
750 "ereq,econ,eresp,"
751 "wretr,wredis,"
752 "status,weight,act,bck,"
753 "chkfail,chkdown,lastchg,downtime,qlimit,"
754 "pid,iid,sid,throttle,lbtot,tracked,type,"
755 "rate,rate_lim,rate_max,"
756 "check_status,check_code,check_duration,"
757 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
758 "req_rate,req_rate_max,req_tot,"
759 "cli_abrt,srv_abrt,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +0200760 "comp_in,comp_out,comp_byp,comp_rsp,lastsess,last_chk,last_agt,qtime,ctime,rtime,ttime,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100761 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100762}
763
Simon Hormand9366582011-06-15 15:18:45 +0900764/* print a string of text buffer to <out>. The format is :
765 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
766 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
767 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
768 */
769static int dump_text(struct chunk *out, const char *buf, int bsize)
770{
771 unsigned char c;
772 int ptr = 0;
773
774 while (buf[ptr] && ptr < bsize) {
775 c = buf[ptr];
776 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
777 if (out->len > out->size - 1)
778 break;
779 out->str[out->len++] = c;
780 }
781 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
782 if (out->len > out->size - 2)
783 break;
784 out->str[out->len++] = '\\';
785 switch (c) {
786 case ' ': c = ' '; break;
787 case '\t': c = 't'; break;
788 case '\n': c = 'n'; break;
789 case '\r': c = 'r'; break;
790 case '\e': c = 'e'; break;
791 case '\\': c = '\\'; break;
792 }
793 out->str[out->len++] = c;
794 }
795 else {
796 if (out->len > out->size - 4)
797 break;
798 out->str[out->len++] = '\\';
799 out->str[out->len++] = 'x';
800 out->str[out->len++] = hextab[(c >> 4) & 0xF];
801 out->str[out->len++] = hextab[c & 0xF];
802 }
803 ptr++;
804 }
805
806 return ptr;
807}
808
809/* print a buffer in hexa.
810 * Print stopped if <bsize> is reached, or if no more place in the chunk.
811 */
812static int dump_binary(struct chunk *out, const char *buf, int bsize)
813{
814 unsigned char c;
815 int ptr = 0;
816
817 while (ptr < bsize) {
818 c = buf[ptr];
819
820 if (out->len > out->size - 2)
821 break;
822 out->str[out->len++] = hextab[(c >> 4) & 0xF];
823 out->str[out->len++] = hextab[c & 0xF];
824
825 ptr++;
826 }
827 return ptr;
828}
829
830/* Dump the status of a table to a stream interface's
831 * read buffer. It returns 0 if the output buffer is full
832 * and needs to be called again, otherwise non-zero.
833 */
834static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
835 struct proxy *proxy, struct proxy *target)
836{
Willy Tarreau87b09662015-04-03 00:22:06 +0200837 struct stream *s = si_strm(si);
Simon Hormand9366582011-06-15 15:18:45 +0900838
Willy Tarreau77804732012-10-29 16:14:26 +0100839 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900840 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
841
842 /* any other information should be dumped here */
843
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200844 if (target && strm_li(s)->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100845 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900846
Willy Tarreaubc18da12015-03-13 14:00:47 +0100847 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200848 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900849 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100850 }
Simon Hormand9366582011-06-15 15:18:45 +0900851
852 return 1;
853}
854
855/* Dump the a table entry to a stream interface's
856 * read buffer. It returns 0 if the output buffer is full
857 * and needs to be called again, otherwise non-zero.
858 */
859static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
860 struct proxy *proxy, struct stksess *entry)
861{
862 int dt;
863
Willy Tarreau77804732012-10-29 16:14:26 +0100864 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900865
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200866 if (proxy->table.type == SMP_T_IPV4) {
Simon Hormand9366582011-06-15 15:18:45 +0900867 char addr[INET_ADDRSTRLEN];
868 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100869 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900870 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200871 else if (proxy->table.type == SMP_T_IPV6) {
Simon Hormand9366582011-06-15 15:18:45 +0900872 char addr[INET6_ADDRSTRLEN];
873 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100874 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900875 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200876 else if (proxy->table.type == SMP_T_SINT) {
Willy Tarreau77804732012-10-29 16:14:26 +0100877 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900878 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200879 else if (proxy->table.type == SMP_T_STR) {
Willy Tarreau77804732012-10-29 16:14:26 +0100880 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900881 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
882 }
883 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100884 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900885 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
886 }
887
Willy Tarreau77804732012-10-29 16:14:26 +0100888 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900889
890 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
891 void *ptr;
892
893 if (proxy->table.data_ofs[dt] == 0)
894 continue;
895 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100896 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900897 else
Willy Tarreau77804732012-10-29 16:14:26 +0100898 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900899
900 ptr = stktable_data_ptr(&proxy->table, entry, dt);
901 switch (stktable_data_types[dt].std_type) {
902 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100903 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900904 break;
905 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100906 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900907 break;
908 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100909 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900910 break;
911 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100912 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900913 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
914 proxy->table.data_arg[dt].u));
915 break;
916 }
917 }
Willy Tarreau77804732012-10-29 16:14:26 +0100918 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900919
Willy Tarreaubc18da12015-03-13 14:00:47 +0100920 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200921 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900922 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100923 }
Simon Hormand9366582011-06-15 15:18:45 +0900924
925 return 1;
926}
927
Willy Tarreaudec98142012-06-06 23:37:08 +0200928static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900929{
Willy Tarreau87b09662015-04-03 00:22:06 +0200930 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100931 struct appctx *appctx = __objt_appctx(si->end);
932 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900933 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900934 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900935 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200936 long long value;
937 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200938 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200939 void *ptr;
940 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900941
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100942 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900943
944 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100945 appctx->ctx.cli.msg = "Key value expected\n";
946 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900947 return;
948 }
949
Simon Hormanc5b89f62011-06-15 15:18:50 +0900950 switch (px->table.type) {
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200951 case SMP_T_IPV4:
Simon Hormancec9a222011-06-15 15:18:51 +0900952 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100953 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900954 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200955 case SMP_T_IPV6:
Simon Hormanc5b89f62011-06-15 15:18:50 +0900956 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100957 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900958 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200959 case SMP_T_SINT:
Simon Hormancec9a222011-06-15 15:18:51 +0900960 {
961 char *endptr;
962 unsigned long val;
963 errno = 0;
964 val = strtoul(args[4], &endptr, 10);
965 if ((errno == ERANGE && val == ULONG_MAX) ||
966 (errno != 0 && val == 0) || endptr == args[4] ||
967 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100968 appctx->ctx.cli.msg = "Invalid key\n";
969 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900970 return;
971 }
972 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100973 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900974 break;
975 }
976 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200977 case SMP_T_STR:
Willy Tarreau07115412012-10-29 21:56:59 +0100978 static_table_key->key = args[4];
979 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900980 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900981 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200982 switch (action) {
983 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100984 appctx->ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200985 break;
986 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100987 appctx->ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200988 break;
989 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100990 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200991 break;
992 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100993 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900994 return;
995 }
996
997 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200998 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100999 appctx->ctx.cli.msg = stats_permission_denied_msg;
1000 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +09001001 return;
1002 }
1003
Willy Tarreau07115412012-10-29 21:56:59 +01001004 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +09001005
Willy Tarreaudec98142012-06-06 23:37:08 +02001006 switch (action) {
1007 case STAT_CLI_O_TAB:
1008 if (!ts)
1009 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001010 chunk_reset(&trash);
1011 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +09001012 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001013 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +09001014 return;
Willy Tarreaudec98142012-06-06 23:37:08 +02001015
1016 case STAT_CLI_O_CLR:
1017 if (!ts)
1018 return;
1019 if (ts->ref_cnt) {
1020 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001021 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
1022 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +02001023 return;
1024 }
1025 stksess_kill(&px->table, ts);
1026 break;
Simon Horman17bce342011-06-15 15:18:47 +09001027
Willy Tarreau654694e2012-06-07 01:03:16 +02001028 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +02001029 if (ts)
1030 stktable_touch(&px->table, ts, 1);
1031 else {
Willy Tarreau07115412012-10-29 21:56:59 +01001032 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +02001033 if (!ts) {
1034 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001035 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
1036 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +02001037 return;
1038 }
1039 stktable_store(&px->table, ts, 1);
1040 }
1041
Willy Tarreau47060b62013-08-01 21:11:42 +02001042 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
1043 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001044 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
1045 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +02001046 return;
1047 }
1048
1049 data_type = stktable_get_data_type(args[cur_arg] + 5);
1050 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001051 appctx->ctx.cli.msg = "Unknown data type\n";
1052 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +02001053 return;
1054 }
1055
1056 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001057 appctx->ctx.cli.msg = "Data type not stored in this table\n";
1058 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +02001059 return;
1060 }
1061
1062 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001063 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
1064 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +02001065 return;
1066 }
1067
1068 ptr = stktable_data_ptr(&px->table, ts, data_type);
1069
1070 switch (stktable_data_types[data_type].std_type) {
1071 case STD_T_SINT:
1072 stktable_data_cast(ptr, std_t_sint) = value;
1073 break;
1074 case STD_T_UINT:
1075 stktable_data_cast(ptr, std_t_uint) = value;
1076 break;
1077 case STD_T_ULL:
1078 stktable_data_cast(ptr, std_t_ull) = value;
1079 break;
1080 case STD_T_FRQP:
1081 /* We set both the current and previous values. That way
1082 * the reported frequency is stable during all the period
1083 * then slowly fades out. This allows external tools to
1084 * push measures without having to update them too often.
1085 */
1086 frqp = &stktable_data_cast(ptr, std_t_frqp);
1087 frqp->curr_tick = now_ms;
1088 frqp->prev_ctr = 0;
1089 frqp->curr_ctr = value;
1090 break;
1091 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001092 }
1093 break;
1094
Willy Tarreaudec98142012-06-06 23:37:08 +02001095 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001096 appctx->ctx.cli.msg = "Unknown action\n";
1097 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +02001098 break;
Simon Horman121f3052011-06-15 15:18:46 +09001099 }
Simon Horman121f3052011-06-15 15:18:46 +09001100}
1101
Willy Tarreau654694e2012-06-07 01:03:16 +02001102static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001103{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001104 struct appctx *appctx = __objt_appctx(si->end);
1105
Willy Tarreau04b3a192013-04-13 09:41:37 +02001106 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001107 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
1108 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +02001109 return;
1110 }
1111
Simon Hormand5b9fd92011-06-15 15:18:48 +09001112 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001113 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
1114 if (appctx->ctx.table.data_type < 0) {
1115 appctx->ctx.cli.msg = "Unknown data type\n";
1116 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001117 return;
1118 }
1119
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001120 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
1121 appctx->ctx.cli.msg = "Data type not stored in this table\n";
1122 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001123 return;
1124 }
1125
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001126 appctx->ctx.table.data_op = get_std_op(args[4]);
1127 if (appctx->ctx.table.data_op < 0) {
1128 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
1129 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001130 return;
1131 }
1132
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001133 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
1134 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
1135 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001136 return;
1137 }
1138}
1139
Willy Tarreaudec98142012-06-06 23:37:08 +02001140static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001141{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001142 struct appctx *appctx = __objt_appctx(si->end);
1143
1144 appctx->ctx.table.data_type = -1;
1145 appctx->st2 = STAT_ST_INIT;
1146 appctx->ctx.table.target = NULL;
1147 appctx->ctx.table.proxy = NULL;
1148 appctx->ctx.table.entry = NULL;
1149 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001150
1151 if (*args[2]) {
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001152 appctx->ctx.table.target = proxy_tbl_by_name(args[2]);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001153 if (!appctx->ctx.table.target) {
1154 appctx->ctx.cli.msg = "No such table\n";
1155 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001156 return;
1157 }
1158 }
1159 else {
Willy Tarreaudec98142012-06-06 23:37:08 +02001160 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001161 goto err_args;
1162 return;
1163 }
1164
1165 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +02001166 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +09001167 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +02001168 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +09001169 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +09001170 goto err_args;
1171
1172 return;
1173
1174err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +02001175 switch (action) {
1176 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001177 appctx->ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +02001178 break;
1179 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001180 appctx->ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +02001181 break;
1182 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001183 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +02001184 break;
1185 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001186 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001187}
1188
Willy Tarreau532a4502011-09-07 22:37:44 +02001189/* Expects to find a frontend named <arg> and returns it, otherwise displays various
Willy Tarreau87b09662015-04-03 00:22:06 +02001190 * adequate error messages and returns NULL. This function also expects the stream
Willy Tarreau532a4502011-09-07 22:37:44 +02001191 * level to be admin.
1192 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001193static struct proxy *expect_frontend_admin(struct stream *s, struct stream_interface *si, const char *arg)
Willy Tarreau532a4502011-09-07 22:37:44 +02001194{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001195 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +02001196 struct proxy *px;
1197
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001198 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001199 appctx->ctx.cli.msg = stats_permission_denied_msg;
1200 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001201 return NULL;
1202 }
1203
1204 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001205 appctx->ctx.cli.msg = "A frontend name is expected.\n";
1206 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001207 return NULL;
1208 }
1209
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001210 px = proxy_fe_by_name(arg);
Willy Tarreau532a4502011-09-07 22:37:44 +02001211 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001212 appctx->ctx.cli.msg = "No such frontend.\n";
1213 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001214 return NULL;
1215 }
1216 return px;
1217}
1218
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001219/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
1220 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau87b09662015-04-03 00:22:06 +02001221 * and returns NULL. This function also expects the stream level to be admin. Note:
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001222 * the <arg> is modified to remove the '/'.
1223 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001224static struct server *expect_server_admin(struct stream *s, struct stream_interface *si, char *arg)
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001225{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001226 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001227 struct proxy *px;
1228 struct server *sv;
1229 char *line;
1230
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001231 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001232 appctx->ctx.cli.msg = stats_permission_denied_msg;
1233 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001234 return NULL;
1235 }
1236
1237 /* split "backend/server" and make <line> point to server */
1238 for (line = arg; *line; line++)
1239 if (*line == '/') {
1240 *line++ = '\0';
1241 break;
1242 }
1243
1244 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001245 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1246 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001247 return NULL;
1248 }
1249
1250 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001251 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1252 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001253 return NULL;
1254 }
1255
1256 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001257 appctx->ctx.cli.msg = "Proxy is disabled.\n";
1258 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001259 return NULL;
1260 }
1261
1262 return sv;
1263}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001264
1265/* This function is used with TLS ticket keys management. It permits to browse
1266 * each reference. The variable <getnext> must contain the current node,
1267 * <end> point to the root node.
1268 */
1269#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1270static inline
1271struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
1272{
1273 struct tls_keys_ref *ref = getnext;
1274
1275 while (1) {
1276
1277 /* Get next list entry. */
1278 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
1279
1280 /* If the entry is the last of the list, return NULL. */
1281 if (&ref->list == end)
1282 return NULL;
1283
1284 return ref;
1285 }
1286}
1287
1288static inline
1289struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
1290{
1291 int id;
1292 char *error;
1293
1294 /* If the reference starts by a '#', this is numeric id. */
1295 if (reference[0] == '#') {
1296 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1297 id = strtol(reference + 1, &error, 10);
1298 if (*error != '\0')
1299 return NULL;
1300
1301 /* Perform the unique id lookup. */
1302 return tlskeys_ref_lookupid(id);
1303 }
1304
1305 /* Perform the string lookup. */
1306 return tlskeys_ref_lookup(reference);
1307}
1308#endif
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001309
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001310/* This function is used with map and acl management. It permits to browse
1311 * each reference. The variable <getnext> must contain the current node,
1312 * <end> point to the root node and the <flags> permit to filter required
1313 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001314 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001315static inline
1316struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
1317 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001318{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001319 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001320
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001321 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001322
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001323 /* Get next list entry. */
1324 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001325
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001326 /* If the entry is the last of the list, return NULL. */
1327 if (&ref->list == end)
1328 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001329
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001330 /* If the entry match the flag, return it. */
1331 if (ref->flags & flags)
1332 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001333 }
1334}
1335
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001336static inline
1337struct pat_ref *pat_ref_lookup_ref(const char *reference)
1338{
1339 int id;
1340 char *error;
1341
1342 /* If the reference starts by a '#', this is numeric id. */
1343 if (reference[0] == '#') {
1344 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1345 id = strtol(reference + 1, &error, 10);
1346 if (*error != '\0')
1347 return NULL;
1348
1349 /* Perform the unique id lookup. */
1350 return pat_ref_lookupid(id);
1351 }
1352
1353 /* Perform the string lookup. */
1354 return pat_ref_lookup(reference);
1355}
1356
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001357/* This function is used with map and acl management. It permits to browse
1358 * each reference.
1359 */
1360static inline
1361struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1362{
1363 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001364 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1365 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001366 return NULL;
1367 return expr;
1368}
1369
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001370/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001371 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001372 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001373 * designating the function which will have to process the request, which can
1374 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001375 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001376static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001377{
Willy Tarreau87b09662015-04-03 00:22:06 +02001378 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001379 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001380 char *args[MAX_STATS_ARGS + 1];
1381 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001382 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001383
1384 while (isspace((unsigned char)*line))
1385 line++;
1386
1387 arg = 0;
1388 args[arg] = line;
1389
1390 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001391 if (*line == '\\') {
1392 line++;
1393 if (*line == '\0')
1394 break;
1395 }
1396 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001397 *line++ = '\0';
1398
1399 while (isspace((unsigned char)*line))
1400 line++;
1401
1402 args[++arg] = line;
1403 continue;
1404 }
1405
1406 line++;
1407 }
1408
1409 while (++arg <= MAX_STATS_ARGS)
1410 args[arg] = line;
1411
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001412 /* remove \ */
1413 arg = 0;
1414 while (*args[arg] != '\0') {
1415 j = 0;
1416 for (i=0; args[arg][i] != '\0'; i++) {
1417 if (args[arg][i] == '\\')
1418 continue;
1419 args[arg][j] = args[arg][i];
1420 j++;
1421 }
1422 args[arg][j] = '\0';
1423 arg++;
1424 }
1425
Willy Tarreau6bcb95d2015-05-04 18:07:56 +02001426 appctx->ctx.stats.scope_str = 0;
1427 appctx->ctx.stats.scope_len = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001428 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001429 if (strcmp(args[0], "show") == 0) {
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02001430 if (strcmp(args[1], "backend") == 0) {
1431 appctx->st2 = STAT_ST_INIT;
1432 appctx->st0 = STAT_CLI_O_BACKEND;
1433 }
Willy Tarreauae795722016-02-16 11:27:28 +01001434 else if (strcmp(args[1], "env") == 0) {
1435 extern char **environ;
1436
1437 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
1438 appctx->ctx.cli.msg = stats_permission_denied_msg;
1439 appctx->st0 = STAT_CLI_PRINT;
1440 return 1;
1441 }
1442 appctx->ctx.env.var = environ;
1443 appctx->st2 = STAT_ST_INIT;
1444 appctx->st0 = STAT_CLI_O_ENV; // stats_dump_env_to_buffer
1445
1446 if (*args[2]) {
1447 int len = strlen(args[2]);
1448
1449 for (; *appctx->ctx.env.var; appctx->ctx.env.var++) {
1450 if (strncmp(*appctx->ctx.env.var, args[2], len) == 0 &&
1451 (*appctx->ctx.env.var)[len] == '=')
1452 break;
1453 }
1454 if (!*appctx->ctx.env.var) {
1455 appctx->ctx.cli.msg = "Variable not found\n";
1456 appctx->st0 = STAT_CLI_PRINT;
1457 return 1;
1458 }
1459 appctx->st2 = STAT_ST_END;
1460 }
1461 }
1462 else if (strcmp(args[1], "stat") == 0) {
Baptiste Assmann3863f972015-05-17 00:33:24 +02001463 if (strcmp(args[2], "resolvers") == 0) {
1464 struct dns_resolvers *presolvers;
1465
Andrew Hayworth68d05342015-10-02 20:33:01 +00001466 if (*args[3]) {
1467 appctx->ctx.resolvers.ptr = NULL;
1468 list_for_each_entry(presolvers, &dns_resolvers, list) {
1469 if (strcmp(presolvers->id, args[3]) == 0) {
1470 appctx->ctx.resolvers.ptr = presolvers;
1471 break;
1472 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001473 }
Andrew Hayworth68d05342015-10-02 20:33:01 +00001474 if (appctx->ctx.resolvers.ptr == NULL) {
1475 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1476 appctx->st0 = STAT_CLI_PRINT;
1477 return 1;
1478 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001479 }
1480
1481 appctx->st2 = STAT_ST_INIT;
1482 appctx->st0 = STAT_CLI_O_RESOLVERS;
1483 return 1;
1484 }
1485 else if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001486 appctx->ctx.stats.flags |= STAT_BOUND;
1487 appctx->ctx.stats.iid = atoi(args[2]);
1488 appctx->ctx.stats.type = atoi(args[3]);
1489 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001490 }
1491
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001492 appctx->st2 = STAT_ST_INIT;
1493 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001494 }
1495 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001496 appctx->st2 = STAT_ST_INIT;
1497 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001498 }
Baptiste Assmann28289462015-07-03 08:01:20 +02001499 else if (strcmp(args[1], "servers") == 0 && strcmp(args[2], "state") == 0) {
1500 appctx->ctx.server_state.backend = NULL;
1501
1502 /* check if a backend name has been provided */
1503 if (*args[3]) {
1504 /* read server state from local file */
1505 appctx->ctx.server_state.backend = proxy_be_by_name(args[3]);
1506
1507 if (appctx->ctx.server_state.backend == NULL) {
1508 appctx->ctx.cli.msg = "Can't find backend.\n";
1509 appctx->st0 = STAT_CLI_PRINT;
1510 return 1;
1511 }
1512 }
1513 appctx->st2 = STAT_ST_INIT;
1514 appctx->st0 = STAT_CLI_O_SERVERS_STATE; // stats_dump_servers_state_to_buffer
1515 return 1;
1516 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001517 else if (strcmp(args[1], "pools") == 0) {
1518 appctx->st2 = STAT_ST_INIT;
1519 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1520 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001521 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001522 appctx->st2 = STAT_ST_INIT;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001523 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001524 appctx->ctx.cli.msg = stats_permission_denied_msg;
1525 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001526 return 1;
1527 }
Willy Tarreau76153662012-11-26 01:16:39 +01001528 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001529 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001530 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001531 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001532 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001533 appctx->ctx.sess.target = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001534 appctx->ctx.sess.section = 0; /* start with stream status */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001535 appctx->ctx.sess.pos = 0;
1536 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001537 }
1538 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001539 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001540 appctx->ctx.cli.msg = stats_permission_denied_msg;
1541 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001542 return 1;
1543 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001544 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001545 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001546 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001547 appctx->ctx.errors.iid = -1;
1548 appctx->ctx.errors.px = NULL;
1549 appctx->st2 = STAT_ST_INIT;
1550 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001551 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001552 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001553 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001554 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001555 else if (strcmp(args[1], "tls-keys") == 0) {
1556#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1557 appctx->st2 = STAT_ST_INIT;
1558 appctx->st0 = STAT_CLI_O_TLSK;
1559#else
1560 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1561 "that doesn't support specifying TLS ticket keys\n";
1562 appctx->st0 = STAT_CLI_PRINT;
1563#endif
1564 return 1;
1565 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001566 else if (strcmp(args[1], "map") == 0 ||
1567 strcmp(args[1], "acl") == 0) {
1568
1569 /* Set ACL or MAP flags. */
1570 if (args[1][0] == 'm')
1571 appctx->ctx.map.display_flags = PAT_REF_MAP;
1572 else
1573 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001574
1575 /* no parameter: display all map avalaible */
1576 if (!*args[2]) {
1577 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001578 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001579 return 1;
1580 }
1581
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001582 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001583 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001584 if (!appctx->ctx.map.ref ||
1585 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1586 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001587 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001588 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001589 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001590 appctx->st0 = STAT_CLI_PRINT;
1591 return 1;
1592 }
1593 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001594 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001595 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001596 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001597 return 0;
1598 }
1599 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001600 else if (strcmp(args[0], "clear") == 0) {
1601 if (strcmp(args[1], "counters") == 0) {
1602 struct proxy *px;
1603 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001604 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001605 int clrall = 0;
1606
1607 if (strcmp(args[2], "all") == 0)
1608 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001609
Willy Tarreau6162db22009-10-10 17:13:00 +02001610 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001611 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1612 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001613 appctx->ctx.cli.msg = stats_permission_denied_msg;
1614 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001615 return 1;
1616 }
1617
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001618 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001619 if (clrall) {
1620 memset(&px->be_counters, 0, sizeof(px->be_counters));
1621 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1622 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001623 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001624 px->be_counters.conn_max = 0;
1625 px->be_counters.p.http.rps_max = 0;
1626 px->be_counters.sps_max = 0;
1627 px->be_counters.cps_max = 0;
1628 px->be_counters.nbpend_max = 0;
1629
1630 px->fe_counters.conn_max = 0;
1631 px->fe_counters.p.http.rps_max = 0;
1632 px->fe_counters.sps_max = 0;
1633 px->fe_counters.cps_max = 0;
1634 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001635 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001636
1637 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001638 if (clrall)
1639 memset(&sv->counters, 0, sizeof(sv->counters));
1640 else {
1641 sv->counters.cur_sess_max = 0;
1642 sv->counters.nbpend_max = 0;
1643 sv->counters.sps_max = 0;
1644 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001645
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001646 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001647 if (li->counters) {
1648 if (clrall)
1649 memset(li->counters, 0, sizeof(*li->counters));
1650 else
1651 li->counters->conn_max = 0;
1652 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001653 }
1654
Willy Tarreau81c25d02011-09-07 15:17:21 +02001655 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001656 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001657 return 1;
1658 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001659 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001660 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001661 /* end of processing */
1662 return 1;
1663 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001664 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1665 /* Set ACL or MAP flags. */
1666 if (args[1][0] == 'm')
1667 appctx->ctx.map.display_flags = PAT_REF_MAP;
1668 else
1669 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001670
1671 /* no parameter */
1672 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001673 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1674 appctx->ctx.cli.msg = "Missing map identifier.\n";
1675 else
1676 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001677 appctx->st0 = STAT_CLI_PRINT;
1678 return 1;
1679 }
1680
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001681 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001682 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001683 if (!appctx->ctx.map.ref ||
1684 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1685 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001686 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001687 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001688 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001689 appctx->st0 = STAT_CLI_PRINT;
1690 return 1;
1691 }
1692
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001693 /* Clear all. */
1694 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001695
1696 /* return response */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001697 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001698 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001699 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001700 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001701 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001702 return 0;
1703 }
1704 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001705 else if (strcmp(args[0], "get") == 0) {
1706 if (strcmp(args[1], "weight") == 0) {
1707 struct proxy *px;
1708 struct server *sv;
1709
1710 /* split "backend/server" and make <line> point to server */
1711 for (line = args[2]; *line; line++)
1712 if (*line == '/') {
1713 *line++ = '\0';
1714 break;
1715 }
1716
1717 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001718 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1719 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001720 return 1;
1721 }
1722
1723 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001724 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1725 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001726 return 1;
1727 }
1728
1729 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001730 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001731 if (bi_putstr(si_ic(si), trash.str) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02001732 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001733
Willy Tarreau38338fa2009-10-10 18:37:29 +02001734 return 1;
1735 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001736 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1737 /* Set flags. */
1738 if (args[1][0] == 'm')
1739 appctx->ctx.map.display_flags = PAT_REF_MAP;
1740 else
1741 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001742
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001743 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001744 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001745 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1746 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1747 else
1748 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001749 appctx->st0 = STAT_CLI_PRINT;
1750 return 1;
1751 }
1752
1753 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001754 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001755 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001756 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001757 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001758 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001759 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001760 appctx->st0 = STAT_CLI_PRINT;
1761 return 1;
1762 }
1763
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001764 /* copy input string. The string must be allocated because
1765 * it may be used over multiple iterations. It's released
1766 * at the end and upon abort anyway.
1767 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001768 appctx->ctx.map.chunk.len = strlen(args[3]);
1769 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1770 appctx->ctx.map.chunk.str = strdup(args[3]);
1771 if (!appctx->ctx.map.chunk.str) {
1772 appctx->ctx.cli.msg = "Out of memory error.\n";
1773 appctx->st0 = STAT_CLI_PRINT;
1774 return 1;
1775 }
1776
1777 /* prepare response */
1778 appctx->st2 = STAT_ST_INIT;
1779 appctx->st0 = STAT_CLI_O_MLOOK;
1780 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001781 else { /* not "get weight" */
1782 return 0;
1783 }
1784 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001785 else if (strcmp(args[0], "set") == 0) {
1786 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001787 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001788 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001789
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001790 sv = expect_server_admin(s, si, args[2]);
1791 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001792 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001793
Simon Horman7d09b9a2013-02-12 10:45:51 +09001794 warning = server_parse_weight_change_request(sv, args[3]);
1795 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001796 appctx->ctx.cli.msg = warning;
1797 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001798 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001799 return 1;
1800 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001801 else if (strcmp(args[1], "server") == 0) {
1802 struct server *sv;
1803 const char *warning;
1804
1805 sv = expect_server_admin(s, si, args[2]);
1806 if (!sv)
1807 return 1;
1808
1809 if (strcmp(args[3], "weight") == 0) {
1810 warning = server_parse_weight_change_request(sv, args[4]);
1811 if (warning) {
1812 appctx->ctx.cli.msg = warning;
1813 appctx->st0 = STAT_CLI_PRINT;
1814 }
1815 }
1816 else if (strcmp(args[3], "state") == 0) {
1817 if (strcmp(args[4], "ready") == 0)
1818 srv_adm_set_ready(sv);
1819 else if (strcmp(args[4], "drain") == 0)
1820 srv_adm_set_drain(sv);
1821 else if (strcmp(args[4], "maint") == 0)
1822 srv_adm_set_maint(sv);
1823 else {
1824 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1825 appctx->st0 = STAT_CLI_PRINT;
1826 }
1827 }
1828 else if (strcmp(args[3], "health") == 0) {
1829 if (sv->track) {
1830 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1831 appctx->st0 = STAT_CLI_PRINT;
1832 }
1833 else if (strcmp(args[4], "up") == 0) {
1834 sv->check.health = sv->check.rise + sv->check.fall - 1;
1835 srv_set_running(sv, "changed from CLI");
1836 }
1837 else if (strcmp(args[4], "stopping") == 0) {
1838 sv->check.health = sv->check.rise + sv->check.fall - 1;
1839 srv_set_stopping(sv, "changed from CLI");
1840 }
1841 else if (strcmp(args[4], "down") == 0) {
1842 sv->check.health = 0;
1843 srv_set_stopped(sv, "changed from CLI");
1844 }
1845 else {
1846 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1847 appctx->st0 = STAT_CLI_PRINT;
1848 }
1849 }
1850 else if (strcmp(args[3], "agent") == 0) {
1851 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1852 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1853 appctx->st0 = STAT_CLI_PRINT;
1854 }
1855 else if (strcmp(args[4], "up") == 0) {
1856 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1857 srv_set_running(sv, "changed from CLI");
1858 }
1859 else if (strcmp(args[4], "down") == 0) {
1860 sv->agent.health = 0;
1861 srv_set_stopped(sv, "changed from CLI");
1862 }
1863 else {
1864 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1865 appctx->st0 = STAT_CLI_PRINT;
1866 }
1867 }
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001868 else if (strcmp(args[3], "addr") == 0) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001869 warning = server_parse_addr_change_request(sv, args[4], "stats command");
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001870 if (warning) {
1871 appctx->ctx.cli.msg = warning;
1872 appctx->st0 = STAT_CLI_PRINT;
1873 }
1874 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001875 else {
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001876 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight' and 'addr'.\n";
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001877 appctx->st0 = STAT_CLI_PRINT;
1878 }
1879 return 1;
1880 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001881 else if (strcmp(args[1], "timeout") == 0) {
1882 if (strcmp(args[2], "cli") == 0) {
1883 unsigned timeout;
1884 const char *res;
1885
1886 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001887 appctx->ctx.cli.msg = "Expects an integer value.\n";
1888 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001889 return 1;
1890 }
1891
1892 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1893 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001894 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1895 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001896 return 1;
1897 }
1898
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001899 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001900 return 1;
1901 }
1902 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001903 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1904 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001905 return 1;
1906 }
1907 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001908 else if (strcmp(args[1], "maxconn") == 0) {
1909 if (strcmp(args[2], "frontend") == 0) {
1910 struct proxy *px;
1911 struct listener *l;
1912 int v;
1913
Willy Tarreau532a4502011-09-07 22:37:44 +02001914 px = expect_frontend_admin(s, si, args[3]);
1915 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001916 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001917
1918 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001919 appctx->ctx.cli.msg = "Integer value expected.\n";
1920 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001921 return 1;
1922 }
1923
1924 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001925 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001926 appctx->ctx.cli.msg = "Value out of range.\n";
1927 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001928 return 1;
1929 }
1930
1931 /* OK, the value is fine, so we assign it to the proxy and to all of
1932 * its listeners. The blocked ones will be dequeued.
1933 */
1934 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001935 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001936 l->maxconn = v;
1937 if (l->state == LI_FULL)
1938 resume_listener(l);
1939 }
1940
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001941 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&strm_fe(s)->listener_queue))
1942 dequeue_all_listeners(&strm_fe(s)->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001943
1944 return 1;
1945 }
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001946 else if (strcmp(args[2], "server") == 0) {
1947 struct server *sv;
1948 int v;
1949
1950 sv = expect_server_admin(s, si, args[3]);
1951 if (!sv)
1952 return 1;
1953
1954 if (!*args[4]) {
1955 appctx->ctx.cli.msg = "Integer value expected.\n";
1956 appctx->st0 = STAT_CLI_PRINT;
1957 return 1;
1958 }
1959
1960 v = atoi(args[4]);
1961 if (v < 0) {
1962 appctx->ctx.cli.msg = "Value out of range.\n";
1963 appctx->st0 = STAT_CLI_PRINT;
1964 return 1;
1965 }
1966
1967 if (sv->maxconn == sv->minconn) { // static maxconn
1968 sv->maxconn = sv->minconn = v;
1969 } else { // dynamic maxconn
1970 sv->maxconn = v;
1971 }
1972
1973 if (may_dequeue_tasks(sv, sv->proxy))
1974 process_srv_queue(sv);
1975
1976 return 1;
1977 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001978 else if (strcmp(args[2], "global") == 0) {
1979 int v;
1980
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001981 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001982 appctx->ctx.cli.msg = stats_permission_denied_msg;
1983 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001984 return 1;
1985 }
1986
1987 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001988 appctx->ctx.cli.msg = "Expects an integer value.\n";
1989 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001990 return 1;
1991 }
1992
1993 v = atoi(args[3]);
1994 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001995 appctx->ctx.cli.msg = "Value out of range.\n";
1996 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001997 return 1;
1998 }
1999
2000 /* check for unlimited values */
2001 if (v <= 0)
2002 v = global.hardmaxconn;
2003
2004 global.maxconn = v;
2005
2006 /* Dequeues all of the listeners waiting for a resource */
2007 if (!LIST_ISEMPTY(&global_listener_queue))
2008 dequeue_all_listeners(&global_listener_queue);
2009
2010 return 1;
2011 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02002012 else {
Andrew Hayworthedb93a72015-10-27 21:46:25 +00002013 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend', 'server', and 'global'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002014 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02002015 return 1;
2016 }
2017 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02002018 else if (strcmp(args[1], "rate-limit") == 0) {
2019 if (strcmp(args[2], "connections") == 0) {
2020 if (strcmp(args[3], "global") == 0) {
2021 int v;
2022
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002023 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002024 appctx->ctx.cli.msg = stats_permission_denied_msg;
2025 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002026 return 1;
2027 }
2028
2029 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002030 appctx->ctx.cli.msg = "Expects an integer value.\n";
2031 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002032 return 1;
2033 }
2034
2035 v = atoi(args[4]);
2036 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002037 appctx->ctx.cli.msg = "Value out of range.\n";
2038 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002039 return 1;
2040 }
2041
2042 global.cps_lim = v;
2043
2044 /* Dequeues all of the listeners waiting for a resource */
2045 if (!LIST_ISEMPTY(&global_listener_queue))
2046 dequeue_all_listeners(&global_listener_queue);
2047
2048 return 1;
2049 }
2050 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002051 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
2052 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002053 return 1;
2054 }
2055 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02002056 else if (strcmp(args[2], "sessions") == 0) {
2057 if (strcmp(args[3], "global") == 0) {
2058 int v;
2059
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002060 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02002061 appctx->ctx.cli.msg = stats_permission_denied_msg;
2062 appctx->st0 = STAT_CLI_PRINT;
2063 return 1;
2064 }
2065
2066 if (!*args[4]) {
2067 appctx->ctx.cli.msg = "Expects an integer value.\n";
2068 appctx->st0 = STAT_CLI_PRINT;
2069 return 1;
2070 }
2071
2072 v = atoi(args[4]);
2073 if (v < 0) {
2074 appctx->ctx.cli.msg = "Value out of range.\n";
2075 appctx->st0 = STAT_CLI_PRINT;
2076 return 1;
2077 }
2078
2079 global.sps_lim = v;
2080
2081 /* Dequeues all of the listeners waiting for a resource */
2082 if (!LIST_ISEMPTY(&global_listener_queue))
2083 dequeue_all_listeners(&global_listener_queue);
2084
2085 return 1;
2086 }
2087 else {
2088 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
2089 appctx->st0 = STAT_CLI_PRINT;
2090 return 1;
2091 }
2092 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02002093#ifdef USE_OPENSSL
2094 else if (strcmp(args[2], "ssl-sessions") == 0) {
2095 if (strcmp(args[3], "global") == 0) {
2096 int v;
2097
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002098 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02002099 appctx->ctx.cli.msg = stats_permission_denied_msg;
2100 appctx->st0 = STAT_CLI_PRINT;
2101 return 1;
2102 }
2103
2104 if (!*args[4]) {
2105 appctx->ctx.cli.msg = "Expects an integer value.\n";
2106 appctx->st0 = STAT_CLI_PRINT;
2107 return 1;
2108 }
2109
2110 v = atoi(args[4]);
2111 if (v < 0) {
2112 appctx->ctx.cli.msg = "Value out of range.\n";
2113 appctx->st0 = STAT_CLI_PRINT;
2114 return 1;
2115 }
2116
2117 global.ssl_lim = v;
2118
2119 /* Dequeues all of the listeners waiting for a resource */
2120 if (!LIST_ISEMPTY(&global_listener_queue))
2121 dequeue_all_listeners(&global_listener_queue);
2122
2123 return 1;
2124 }
2125 else {
2126 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
2127 appctx->st0 = STAT_CLI_PRINT;
2128 return 1;
2129 }
2130 }
2131#endif
William Lallemandd85f9172012-11-09 17:05:39 +01002132 else if (strcmp(args[2], "http-compression") == 0) {
2133 if (strcmp(args[3], "global") == 0) {
2134 int v;
2135
Willy Tarreaua1c2b2c2015-11-26 18:32:39 +01002136 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
2137 appctx->ctx.cli.msg = stats_permission_denied_msg;
2138 appctx->st0 = STAT_CLI_PRINT;
2139 return 1;
2140 }
2141
Willy Tarreau85d47f92012-11-21 00:29:50 +01002142 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002143 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
2144 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01002145 return 1;
2146 }
2147
William Lallemandd85f9172012-11-09 17:05:39 +01002148 v = atoi(args[4]);
2149 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
2150 }
2151 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002152 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
2153 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01002154 return 1;
2155 }
2156 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02002157 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02002158 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002159 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002160 return 1;
2161 }
2162 }
Willy Tarreau654694e2012-06-07 01:03:16 +02002163 else if (strcmp(args[1], "table") == 0) {
2164 stats_sock_table_request(si, args, STAT_CLI_O_SET);
2165 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002166 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002167 char *err;
2168
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002169 /* Set flags. */
2170 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002171
2172 /* Expect three parameters: map name, key and new value. */
2173 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01002174 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002175 appctx->st0 = STAT_CLI_PRINT;
2176 return 1;
2177 }
2178
2179 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002180 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002181 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002182 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002183 appctx->st0 = STAT_CLI_PRINT;
2184 return 1;
2185 }
2186
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002187 /* If the entry identifier start with a '#', it is considered as
2188 * pointer id
2189 */
2190 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2191 struct pat_ref_elt *ref;
2192 long long int conv;
2193 char *error;
2194
2195 /* Convert argument to integer value. */
2196 conv = strtoll(&args[3][1], &error, 16);
2197 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002198 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002199 appctx->st0 = STAT_CLI_PRINT;
2200 return 1;
2201 }
2202
2203 /* Convert and check integer to pointer. */
2204 ref = (struct pat_ref_elt *)(long)conv;
2205 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002206 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002207 appctx->st0 = STAT_CLI_PRINT;
2208 return 1;
2209 }
2210
2211 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002212 err = NULL;
2213 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
2214 if (err)
2215 memprintf(&err, "%s.\n", err);
2216 appctx->ctx.cli.err = err;
2217 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002218 return 1;
2219 }
2220 }
2221 else {
2222 /* Else, use the entry identifier as pattern
2223 * string, and update the value.
2224 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002225 err = NULL;
2226 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
2227 if (err)
2228 memprintf(&err, "%s.\n", err);
2229 appctx->ctx.cli.err = err;
2230 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002231 return 1;
2232 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002233 }
2234
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002235 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002236 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002237 return 1;
2238 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002239#ifdef USE_OPENSSL
2240 else if (strcmp(args[1], "ssl") == 0) {
2241 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01002242#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02002243 char *err = NULL;
2244
Emeric Brunaf4ef742014-06-19 14:10:45 +02002245 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02002246 if (!*args[3]) {
2247 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
2248 appctx->st0 = STAT_CLI_PRINT;
2249 return 1;
2250 }
2251
2252 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
2253 if (trash.len < 0) {
2254 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
2255 appctx->st0 = STAT_CLI_PRINT;
2256 return 1;
2257 }
2258
2259 if (ssl_sock_update_ocsp_response(&trash, &err)) {
2260 if (err) {
2261 memprintf(&err, "%s.\n", err);
2262 appctx->ctx.cli.err = err;
2263 appctx->st0 = STAT_CLI_PRINT_FREE;
2264 }
2265 return 1;
2266 }
2267 appctx->ctx.cli.msg = "OCSP Response updated!";
2268 appctx->st0 = STAT_CLI_PRINT;
2269 return 1;
2270#else
2271 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
2272 appctx->st0 = STAT_CLI_PRINT;
2273 return 1;
2274#endif
2275 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002276 else if (strcmp(args[2], "tls-key") == 0) {
2277#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2278 /* Expect two parameters: the filename and the new new TLS key in encoding */
2279 if (!*args[3] || !*args[4]) {
2280 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
2281 appctx->st0 = STAT_CLI_PRINT;
2282 return 1;
2283 }
2284
2285 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[3]);
2286 if(!appctx->ctx.tlskeys.ref) {
2287 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
2288 appctx->st0 = STAT_CLI_PRINT;
2289 return 1;
2290 }
2291
2292 trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size);
2293 if (trash.len != sizeof(struct tls_sess_key)) {
2294 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
2295 appctx->st0 = STAT_CLI_PRINT;
2296 return 1;
2297 }
2298
Pradeep Jindalcc79b002015-08-20 18:25:17 +05302299 memcpy(appctx->ctx.tlskeys.ref->tlskeys + ((appctx->ctx.tlskeys.ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len);
2300 appctx->ctx.tlskeys.ref->tls_ticket_enc_index = (appctx->ctx.tlskeys.ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002301
2302 appctx->ctx.cli.msg = "TLS ticket key updated!";
2303 appctx->st0 = STAT_CLI_PRINT;
2304 return 1;
2305#else
2306 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
2307 "that doesn't support specifying TLS ticket keys\n";
2308 appctx->st0 = STAT_CLI_PRINT;
2309 return 1;
2310#endif
2311 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002312 else {
2313 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
2314 appctx->st0 = STAT_CLI_PRINT;
2315 return 1;
2316 }
2317 }
2318#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01002319 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02002320 return 0;
2321 }
2322 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002323 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002324 if (strcmp(args[1], "agent") == 0) {
2325 struct server *sv;
2326
2327 sv = expect_server_admin(s, si, args[2]);
2328 if (!sv)
2329 return 1;
2330
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002331 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
2332 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
2333 appctx->st0 = STAT_CLI_PRINT;
2334 return 1;
2335 }
2336
2337 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002338 return 1;
2339 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002340 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002341 struct server *sv;
2342
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002343 sv = expect_server_admin(s, si, args[2]);
2344 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002345 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002346
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002347 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
2348 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
2349 appctx->st0 = STAT_CLI_PRINT;
2350 return 1;
2351 }
2352
2353 sv->check.state |= CHK_ST_ENABLED;
2354 return 1;
2355 }
2356 else if (strcmp(args[1], "server") == 0) {
2357 struct server *sv;
2358
2359 sv = expect_server_admin(s, si, args[2]);
2360 if (!sv)
2361 return 1;
2362
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002363 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002364 return 1;
2365 }
2366 else if (strcmp(args[1], "frontend") == 0) {
2367 struct proxy *px;
2368
2369 px = expect_frontend_admin(s, si, args[2]);
2370 if (!px)
2371 return 1;
2372
2373 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002374 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
2375 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002376 return 1;
2377 }
2378
2379 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002380 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
2381 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002382 return 1;
2383 }
2384
2385 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002386 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
2387 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002388 return 1;
2389 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002390 return 1;
2391 }
2392 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002393 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002394 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002395 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002396 }
2397 }
2398 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002399 if (strcmp(args[1], "agent") == 0) {
2400 struct server *sv;
2401
2402 sv = expect_server_admin(s, si, args[2]);
2403 if (!sv)
2404 return 1;
2405
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002406 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002407 return 1;
2408 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002409 else if (strcmp(args[1], "health") == 0) {
2410 struct server *sv;
2411
2412 sv = expect_server_admin(s, si, args[2]);
2413 if (!sv)
2414 return 1;
2415
2416 sv->check.state &= ~CHK_ST_ENABLED;
2417 return 1;
2418 }
Simon Horman671b6f02013-11-25 10:46:39 +09002419 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002420 struct server *sv;
2421
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002422 sv = expect_server_admin(s, si, args[2]);
2423 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002424 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002425
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002426 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002427 return 1;
2428 }
2429 else if (strcmp(args[1], "frontend") == 0) {
2430 struct proxy *px;
2431
2432 px = expect_frontend_admin(s, si, args[2]);
2433 if (!px)
2434 return 1;
2435
2436 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002437 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
2438 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002439 return 1;
2440 }
2441
2442 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002443 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
2444 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002445 return 1;
2446 }
2447
2448 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002449 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
2450 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002451 return 1;
2452 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002453 return 1;
2454 }
2455 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002456 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002457 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002458 return 1;
2459 }
2460 }
2461 else if (strcmp(args[0], "shutdown") == 0) {
2462 if (strcmp(args[1], "frontend") == 0) {
2463 struct proxy *px;
2464
2465 px = expect_frontend_admin(s, si, args[2]);
2466 if (!px)
2467 return 1;
2468
2469 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002470 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
2471 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002472 return 1;
2473 }
2474
2475 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2476 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2477 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2478 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2479 stop_proxy(px);
2480 return 1;
2481 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002482 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002483 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002484
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002485 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002486 appctx->ctx.cli.msg = stats_permission_denied_msg;
2487 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002488 return 1;
2489 }
2490
2491 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002492 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2493 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002494 return 1;
2495 }
2496
2497 ptr = (void *)strtoul(args[2], NULL, 0);
2498
Willy Tarreau87b09662015-04-03 00:22:06 +02002499 /* first, look for the requested stream in the stream table */
2500 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002501 if (sess == ptr)
2502 break;
2503 }
2504
Willy Tarreau87b09662015-04-03 00:22:06 +02002505 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002506 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002507 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2508 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002509 return 1;
2510 }
2511
Willy Tarreaue7dff022015-04-03 01:14:29 +02002512 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002513 return 1;
2514 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002515 else if (strcmp(args[1], "sessions") == 0) {
2516 if (strcmp(args[2], "server") == 0) {
2517 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002518 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002519
2520 sv = expect_server_admin(s, si, args[3]);
2521 if (!sv)
2522 return 1;
2523
Willy Tarreau87b09662015-04-03 00:22:06 +02002524 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002525 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2526 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002527 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002528
2529 return 1;
2530 }
2531 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002532 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2533 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002534 return 1;
2535 }
2536 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002537 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002538 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2539 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002540 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002541 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002542 }
2543 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002544 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2545 if (args[1][0] == 'm')
2546 appctx->ctx.map.display_flags = PAT_REF_MAP;
2547 else
2548 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002549
2550 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002551 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2552 if (!*args[2] || !*args[3]) {
2553 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2554 appctx->st0 = STAT_CLI_PRINT;
2555 return 1;
2556 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002557 }
2558
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002559 else {
2560 if (!*args[2] || !*args[3]) {
2561 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2562 appctx->st0 = STAT_CLI_PRINT;
2563 return 1;
2564 }
2565 }
2566
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002567 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002568 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002569 if (!appctx->ctx.map.ref ||
2570 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002571 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002572 appctx->st0 = STAT_CLI_PRINT;
2573 return 1;
2574 }
2575
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002576 /* If the entry identifier start with a '#', it is considered as
2577 * pointer id
2578 */
2579 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2580 struct pat_ref_elt *ref;
2581 long long int conv;
2582 char *error;
2583
2584 /* Convert argument to integer value. */
2585 conv = strtoll(&args[3][1], &error, 16);
2586 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002587 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002588 appctx->st0 = STAT_CLI_PRINT;
2589 return 1;
2590 }
2591
2592 /* Convert and check integer to pointer. */
2593 ref = (struct pat_ref_elt *)(long)conv;
2594 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002595 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002596 appctx->st0 = STAT_CLI_PRINT;
2597 return 1;
2598 }
2599
2600 /* Try to delete the entry. */
2601 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2602 /* The entry is not found, send message. */
2603 appctx->ctx.cli.msg = "Key not found.\n";
2604 appctx->st0 = STAT_CLI_PRINT;
2605 return 1;
2606 }
2607 }
2608 else {
2609 /* Else, use the entry identifier as pattern
2610 * string and try to delete the entry.
2611 */
2612 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2613 /* The entry is not found, send message. */
2614 appctx->ctx.cli.msg = "Key not found.\n";
2615 appctx->st0 = STAT_CLI_PRINT;
2616 return 1;
2617 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002618 }
2619
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002620 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002621 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002622 return 1;
2623 }
2624 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002625 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002626 appctx->st0 = STAT_CLI_PRINT;
2627 return 1;
2628 }
2629 }
2630 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002631 if (strcmp(args[1], "map") == 0 ||
2632 strcmp(args[1], "acl") == 0) {
2633 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002634 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002635
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002636 /* Set flags. */
2637 if (args[1][0] == 'm')
2638 appctx->ctx.map.display_flags = PAT_REF_MAP;
2639 else
2640 appctx->ctx.map.display_flags = PAT_REF_ACL;
2641
2642 /* If the keywork is "map", we expect three parameters, if it
2643 * is "acl", we expect only two parameters
2644 */
2645 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2646 if (!*args[2] || !*args[3] || !*args[4]) {
2647 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2648 appctx->st0 = STAT_CLI_PRINT;
2649 return 1;
2650 }
2651 }
2652 else {
2653 if (!*args[2] || !*args[3]) {
2654 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2655 appctx->st0 = STAT_CLI_PRINT;
2656 return 1;
2657 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002658 }
2659
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002660 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002661 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002662 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002663 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002664 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002665 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002666 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002667 appctx->st0 = STAT_CLI_PRINT;
2668 return 1;
2669 }
2670
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002671 /* The command "add acl" is prohibited if the reference
2672 * use samples.
2673 */
2674 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2675 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2676 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2677 "You must use the command 'add map' to add values.\n";
2678 appctx->st0 = STAT_CLI_PRINT;
2679 return 1;
2680 }
2681
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002682 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002683 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002684 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002685 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002686 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002687 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002688 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002689 if (err)
2690 memprintf(&err, "%s.\n", err);
2691 appctx->ctx.cli.err = err;
2692 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002693 return 1;
2694 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002695
2696 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002697 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002698 return 1;
2699 }
2700 else { /* unknown "del" parameter */
2701 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2702 appctx->st0 = STAT_CLI_PRINT;
2703 return 1;
2704 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002705 }
2706 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002707 return 0;
2708 }
2709 return 1;
2710}
2711
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002712/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002713 * used to processes I/O from/to the stats unix socket. The system relies on a
2714 * state machine handling requests and various responses. We read a request,
2715 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002716 * Then we can read again. The state is stored in appctx->st0 and is one of the
2717 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002718 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002719 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002720static void cli_io_handler(struct appctx *appctx)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002721{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002722 struct stream_interface *si = appctx->owner;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002723 struct channel *req = si_oc(si);
2724 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002725 int reql;
2726 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002727
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002728 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2729 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002730
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002731 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002732 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002733 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002734 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2735 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002736 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002737 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002738 /* Let's close for real now. We just close the request
2739 * side, the conditions below will complete if needed.
2740 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002741 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002742 break;
2743 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002744 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002745 /* ensure we have some output room left in the event we
2746 * would want to return some info right after parsing.
2747 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002748 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002749 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02002750 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002751 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002752
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002753 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002754 if (reql <= 0) { /* closed or EOL not found */
2755 if (reql == 0)
2756 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002757 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002758 continue;
2759 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002760
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002761 /* seek for a possible semi-colon. If we find one, we
2762 * replace it with an LF and skip only this part.
2763 */
2764 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002765 if (trash.str[len] == ';') {
2766 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002767 reql = len + 1;
2768 break;
2769 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002770
Willy Tarreau816fc222009-10-04 07:36:58 +02002771 /* now it is time to check that we have a full line,
2772 * remove the trailing \n and possibly \r, then cut the
2773 * line.
2774 */
2775 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002776 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002777 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002778 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002779 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002780
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002781 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002782 len--;
2783
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002784 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002785
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002786 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002787 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002788 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002789 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002790 continue;
2791 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002792 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002793 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002794 else if (strcmp(trash.str, "help") == 0 ||
2795 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002796 appctx->ctx.cli.msg = stats_sock_usage_msg;
2797 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002798 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002799 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002800 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002801 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002802 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002803 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002804 /* if prompt is disabled, print help on empty lines,
2805 * so that the user at least knows how to enable
2806 * prompt and find help.
2807 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002808 appctx->ctx.cli.msg = stats_sock_usage_msg;
2809 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002810 }
2811
2812 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002813 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002814 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002815 }
Willy Tarreau68c00c72015-09-25 19:21:19 +02002816 else { /* output functions */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002817 switch (appctx->st0) {
Andrew Hayworthe32d1862015-10-02 15:08:10 +00002818 case STAT_CLI_PROMPT:
2819 break;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002820 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002821 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002822 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002823 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002824 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002825 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002826 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002827 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002828 free(appctx->ctx.cli.err);
2829 appctx->st0 = STAT_CLI_PROMPT;
2830 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002831 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002832 si_applet_cant_put(si);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002833 break;
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02002834 case STAT_CLI_O_BACKEND:
2835 if (stats_dump_backend_to_buffer(si))
2836 appctx->st0 = STAT_CLI_PROMPT;
2837 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002838 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002839 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002840 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002841 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02002842 case STAT_CLI_O_SERVERS_STATE:
2843 if (stats_dump_servers_state_to_buffer(si))
2844 appctx->st0 = STAT_CLI_PROMPT;
2845 break;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002846 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002847 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002848 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002849 break;
Baptiste Assmann3863f972015-05-17 00:33:24 +02002850 case STAT_CLI_O_RESOLVERS:
2851 if (stats_dump_resolvers_to_buffer(si))
2852 appctx->st0 = STAT_CLI_PROMPT;
2853 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002854 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002855 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002856 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002857 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002858 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002859 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002860 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002861 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002862 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002863 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002864 if (stats_table_request(si, appctx->st0))
2865 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002866 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002867 case STAT_CLI_O_PATS:
2868 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002869 appctx->st0 = STAT_CLI_PROMPT;
2870 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002871 case STAT_CLI_O_PAT:
2872 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002873 appctx->st0 = STAT_CLI_PROMPT;
2874 break;
2875 case STAT_CLI_O_MLOOK:
2876 if (stats_map_lookup(si))
2877 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002878 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002879 case STAT_CLI_O_POOLS:
2880 if (stats_dump_pools_to_buffer(si))
2881 appctx->st0 = STAT_CLI_PROMPT;
2882 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002883#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2884 case STAT_CLI_O_TLSK:
2885 if (stats_tlskeys_list(si))
2886 appctx->st0 = STAT_CLI_PROMPT;
2887 break;
2888#endif
Willy Tarreauae795722016-02-16 11:27:28 +01002889 case STAT_CLI_O_ENV: /* environment dump */
2890 if (stats_dump_env_to_buffer(si))
2891 appctx->st0 = STAT_CLI_PROMPT;
2892 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002893 default: /* abnormal state */
Willy Tarreau5cfa3bc2015-09-25 20:08:51 +02002894 si->flags |= SI_FL_ERR;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002895 break;
2896 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002897
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002898 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002899 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002900 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002901 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002902 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002903 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002904 }
2905
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002906 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002907 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002908 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002909
2910 /* Now we close the output if one of the writers did so,
2911 * or if we're not in interactive mode and the request
2912 * buffer is empty. This still allows pipelined requests
2913 * to be sent in non-interactive mode.
2914 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002915 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2916 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002917 continue;
2918 }
2919
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002920 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002921 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002922 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002923 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002924
Willy Tarreauc9e930a2015-09-25 20:06:08 +02002925 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002926 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2927 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002928 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002929 * and nothing more to consume. This is comparable to a broken pipe, so
2930 * we forward the close to the request side so that it flows upstream to
2931 * the client.
2932 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002933 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002934 }
2935
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002936 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002937 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2938 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2939 /* We have no more processing to do, and nothing more to send, and
2940 * the client side has closed. So we'll forward this state downstream
2941 * on the response buffer.
2942 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002943 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002944 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002945 }
2946
Willy Tarreau828824a2015-04-19 17:20:03 +02002947 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002948 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 +02002949 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002950 si->state, req->flags, res->flags, req->buf->i, req->buf->o, res->buf->i, res->buf->o);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002951}
2952
Willy Tarreau638d40a2016-02-24 23:11:01 +01002953/* Emits a stats field without any surrounding element and properly encoded to
2954 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
2955 */
2956static int stats_emit_raw_data_field(struct chunk *out, const struct field *f)
2957{
2958 switch (field_format(f, 0)) {
2959 case FF_EMPTY: return 1;
2960 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
2961 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
2962 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
2963 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
2964 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
2965 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
2966 }
2967}
2968
Willy Tarreaubf95cba2016-01-11 18:27:29 +01002969/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
2970static int stats_dump_info_fields(struct chunk *out, const struct field *info)
2971{
2972 int field;
2973
2974 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2975 if (!field_format(info, field))
2976 continue;
2977
2978 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
2979 return 0;
2980 if (!stats_emit_raw_data_field(out, &info[field]))
2981 return 0;
2982 if (!chunk_strcat(out, "\n"))
2983 return 0;
2984 }
2985 return 1;
2986}
2987
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002988/* This function dumps information onto the stream interface's read buffer.
2989 * It returns 0 as long as it does not complete, non-zero upon completion.
2990 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002991 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002992static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002993{
2994 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau4529c072016-01-11 18:17:07 +01002995 struct chunk *out = get_trash_chunk();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002996
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002997#ifdef USE_OPENSSL
2998 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2999 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3000 int ssl_reuse = 0;
3001
3002 if (ssl_key_rate < ssl_sess_rate) {
3003 /* count the ssl reuse ratio and avoid overflows in both directions */
3004 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3005 }
3006#endif
3007
Willy Tarreau4529c072016-01-11 18:17:07 +01003008 chunk_reset(out);
3009 memset(&info, 0, sizeof(info));
3010
3011 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
3012 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
3013 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
3014
3015 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3016 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3017 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3018
3019 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3020 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3021
3022 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3023 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3024 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3025 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3026 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3027 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3028 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3029 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3030 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3031 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3032 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3033 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3034#ifdef USE_OPENSSL
3035 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3036 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3037 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3038#endif
3039 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3040 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3041 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3042 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3043 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3044 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3045 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3046 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3047 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3048
3049#ifdef USE_OPENSSL
3050 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3051 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3052 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3053 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3054 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3055 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3056 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3057 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3058 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3059 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3060#endif
3061 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3062 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3063 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3064#ifdef USE_ZLIB
3065 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3066 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3067#endif
3068 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
3069 info[INF_RUN_QUEUE] = mkf_u32(0, run_queue_cur);
3070 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3071 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3072 if (global.desc)
3073 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
3074
Willy Tarreau1b4ba1e2016-01-11 18:29:04 +01003075 chunk_reset(&trash);
3076 stats_dump_info_fields(&trash, info);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003077
Willy Tarreaubc18da12015-03-13 14:00:47 +01003078 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003079 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003080 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003081 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003082
3083 return 1;
3084}
3085
Baptiste Assmann28289462015-07-03 08:01:20 +02003086/* dumps server state information into <buf> for all the servers found in <backend>
3087 * These information are all the parameters which may change during HAProxy runtime.
3088 * By default, we only export to the last known server state file format.
3089 * These information can be used at next startup to recover same level of server state.
3090 */
3091static void dump_servers_state(struct proxy *backend, struct chunk *buf)
3092{
3093 struct server *srv;
3094 char srv_addr[INET6_ADDRSTRLEN + 1];
3095 time_t srv_time_since_last_change;
3096 int bk_f_forced_id, srv_f_forced_id;
3097
3098 /* we don't want to report any state if the backend is not enabled on this process */
3099 if (backend->bind_proc && !(backend->bind_proc & (1UL << (relative_pid - 1))))
3100 return;
3101
3102 srv = backend->srv;
3103
3104 while (srv) {
3105 srv_addr[0] = '\0';
3106 srv_time_since_last_change = 0;
3107 bk_f_forced_id = 0;
3108 srv_f_forced_id = 0;
3109
3110 switch (srv->addr.ss_family) {
3111 case AF_INET:
3112 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
3113 srv_addr, INET_ADDRSTRLEN + 1);
3114 break;
3115 case AF_INET6:
3116 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
3117 srv_addr, INET6_ADDRSTRLEN + 1);
3118 break;
3119 }
3120 srv_time_since_last_change = now.tv_sec - srv->last_change;
3121 bk_f_forced_id = backend->options & PR_O_FORCED_ID ? 1 : 0;
3122 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
3123
3124 chunk_appendf(buf,
3125 "%d %s "
3126 "%d %s %s "
3127 "%d %d %d %d %ld "
3128 "%d %d %d %d %d "
3129 "%d %d"
3130 "\n",
3131 backend->uuid, backend->id,
3132 srv->puid, srv->id, srv_addr,
David Carlier081b3362015-11-18 06:10:22 +00003133 srv->state, srv->admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
Baptiste Assmann28289462015-07-03 08:01:20 +02003134 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
3135 bk_f_forced_id, srv_f_forced_id);
3136
3137 srv = srv->next;
3138 }
3139}
3140
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003141/* Parses backend list and simply report backend names */
3142static int stats_dump_backend_to_buffer(struct stream_interface *si)
3143{
3144 extern struct proxy *proxy;
3145 struct proxy *curproxy;
3146
3147 chunk_reset(&trash);
3148 chunk_printf(&trash, "# name\n");
3149
3150 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3151 /* looking for backends only */
3152 if (!(curproxy->cap & PR_CAP_BE))
3153 continue;
3154
3155 /* we don't want to list a backend which is bound to this process */
3156 if (curproxy->bind_proc && !(curproxy->bind_proc & (1UL << (relative_pid - 1))))
3157 continue;
3158
3159 chunk_appendf(&trash, "%s\n", curproxy->id);
3160 }
3161
3162 if (bi_putchk(si_ic(si), &trash) == -1) {
3163 si_applet_cant_put(si);
3164 return 0;
3165 }
3166
3167 return 1;
3168}
3169
Baptiste Assmann28289462015-07-03 08:01:20 +02003170/* Parses backend list or simply use backend name provided by the user to return
3171 * states of servers to stdout.
3172 */
3173static int stats_dump_servers_state_to_buffer(struct stream_interface *si)
3174{
3175 struct appctx *appctx = __objt_appctx(si->end);
3176 extern struct proxy *proxy;
3177 struct proxy *curproxy;
3178
3179 chunk_reset(&trash);
3180
3181 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
3182
3183 if (appctx->ctx.server_state.backend) {
3184 dump_servers_state(appctx->ctx.server_state.backend, &trash);
3185 }
3186 else {
3187 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3188 /* servers are only in backends */
3189 if (!(curproxy->cap & PR_CAP_BE))
3190 continue;
3191
3192 dump_servers_state(curproxy, &trash);
3193 }
3194 }
3195
3196 if (bi_putchk(si_ic(si), &trash) == -1) {
3197 si_applet_cant_put(si);
3198 return 0;
3199 }
3200
3201 return 1;
3202}
3203
Willy Tarreau12833bb2014-01-28 16:49:56 +01003204/* This function dumps memory usage information onto the stream interface's
3205 * read buffer. It returns 0 as long as it does not complete, non-zero upon
3206 * completion. No state is used.
3207 */
3208static int stats_dump_pools_to_buffer(struct stream_interface *si)
3209{
3210 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01003211 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003212 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01003213 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003214 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01003215 return 1;
3216}
3217
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003218/* Dumps a frontend's line to the trash for the current proxy <px> and uses
3219 * the state from stream interface <si>. The caller is responsible for clearing
3220 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02003221 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003222static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02003223{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003224 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003225 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02003226
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003227 if (!(px->cap & PR_CAP_FE))
3228 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02003229
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003230 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003231 return 0;
3232
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003233 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003234 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003235 /* name, queue */
3236 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003237
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003238 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003239 /* Column sub-heading for Enable or Disable server */
3240 chunk_appendf(&trash, "<td></td>");
3241 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003242
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003243 chunk_appendf(&trash,
3244 "<td class=ac>"
3245 "<a name=\"%s/Frontend\"></a>"
3246 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
3247 "<td colspan=3></td>"
3248 "",
3249 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003250
Willy Tarreau466c9b52012-12-23 02:25:03 +01003251 chunk_appendf(&trash,
3252 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003253 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003254 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
3255 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
3256 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003257 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
3258 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
3259 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003260
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003261 if (px->mode == PR_MODE_HTTP)
3262 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003263 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003264 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003265
3266 chunk_appendf(&trash,
3267 "</table></div></u></td>"
3268 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003269 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003270 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
3271 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
3272 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003273 U2H(px->fe_counters.sps_max),
3274 U2H(px->fe_counters.cps_max),
3275 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003276
3277 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003278 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003279 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003280 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003281
3282 chunk_appendf(&trash,
3283 "</table></div></u></td>"
3284 /* sessions rate : limit */
3285 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003286 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003287
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003288 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003289 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003290 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003291 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003292 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
3293 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003294 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003295 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
3296 U2H(px->fe_counters.cum_sess),
3297 U2H(px->fe_counters.cum_conn),
3298 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003299
Willy Tarreau466c9b52012-12-23 02:25:03 +01003300 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003301 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003302 chunk_appendf(&trash,
3303 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3304 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3305 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3306 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3307 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3308 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3309 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3310 "<tr><th>- other responses:</th><td>%s</td></tr>"
3311 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3312 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003313 U2H(px->fe_counters.p.http.cum_req),
3314 U2H(px->fe_counters.p.http.rsp[1]),
3315 U2H(px->fe_counters.p.http.rsp[2]),
3316 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003317 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003318 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003319 U2H(px->fe_counters.p.http.rsp[3]),
3320 U2H(px->fe_counters.p.http.rsp[4]),
3321 U2H(px->fe_counters.p.http.rsp[5]),
3322 U2H(px->fe_counters.p.http.rsp[0]),
3323 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003324 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003325
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003326 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003327 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003328 /* sessions: lbtot, lastsess */
3329 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003330 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003331 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003332 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003333 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003334
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003335 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003336 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003337 "<td>%s%s<div class=tips><table class=det>"
3338 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3339 "<tr><th>Compression in:</th><td>%s</td></tr>"
3340 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3341 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3342 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3343 "</table></div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003344 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003345 U2H(px->fe_counters.bytes_out),
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003346 U2H(px->fe_counters.bytes_out),
3347 U2H(px->fe_counters.comp_in),
3348 U2H(px->fe_counters.comp_out),
3349 px->fe_counters.comp_in ? (int)(px->fe_counters.comp_out * 100 / px->fe_counters.comp_in) : 0,
3350 U2H(px->fe_counters.comp_byp),
3351 U2H(px->fe_counters.comp_in - px->fe_counters.comp_out),
3352 px->fe_counters.bytes_out ? (int)((px->fe_counters.comp_in - px->fe_counters.comp_out) * 100 / px->fe_counters.bytes_out) : 0,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003353 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003354
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003355 chunk_appendf(&trash,
3356 /* denied: req, resp */
3357 "<td>%s</td><td>%s</td>"
3358 /* errors : request, connect, response */
3359 "<td>%s</td><td></td><td></td>"
3360 /* warnings: retries, redispatches */
3361 "<td></td><td></td>"
3362 /* server status : reflect frontend status */
3363 "<td class=ac>%s</td>"
3364 /* rest of server: nothing */
3365 "<td class=ac colspan=8></td></tr>"
3366 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003367 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
3368 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003369 px->state == PR_STREADY ? "OPEN" :
3370 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003371 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003372 else { /* CSV mode */
3373 chunk_appendf(&trash,
3374 /* pxid, name, queue cur, queue max, */
3375 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003376 /* sessions : current, max, limit, total */
3377 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003378 /* bytes : in, out */
3379 "%lld,%lld,"
3380 /* denied: req, resp */
3381 "%lld,%lld,"
3382 /* errors : request, connect, response */
3383 "%lld,,,"
3384 /* warnings: retries, redispatches */
3385 ",,"
3386 /* server status : reflect frontend status */
3387 "%s,"
3388 /* rest of server: nothing */
3389 ",,,,,,,,"
3390 /* pid, iid, sid, throttle, lbtot, tracked, type */
3391 "%d,%d,0,,,,%d,"
3392 /* rate, rate_lim, rate_max */
3393 "%u,%u,%u,"
3394 /* check_status, check_code, check_duration */
3395 ",,,",
3396 px->id,
3397 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
3398 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
3399 px->fe_counters.denied_req, px->fe_counters.denied_resp,
3400 px->fe_counters.failed_req,
3401 px->state == PR_STREADY ? "OPEN" :
3402 px->state == PR_STFULL ? "FULL" : "STOP",
3403 relative_pid, px->uuid, STATS_TYPE_FE,
3404 read_freq_ctr(&px->fe_sess_per_sec),
3405 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003406
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003407 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3408 if (px->mode == PR_MODE_HTTP) {
3409 for (i=1; i<6; i++)
3410 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
3411 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
3412 }
3413 else
3414 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003415
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003416 /* failed health analyses */
3417 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003418
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003419 /* requests : req_rate, req_rate_max, req_tot, */
3420 chunk_appendf(&trash, "%u,%u,%lld,",
3421 read_freq_ctr(&px->fe_req_per_sec),
3422 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
3423
3424 /* errors: cli_aborts, srv_aborts */
3425 chunk_appendf(&trash, ",,");
3426
3427 /* compression: in, out, bypassed */
3428 chunk_appendf(&trash, "%lld,%lld,%lld,",
3429 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
3430
3431 /* compression: comp_rsp */
3432 chunk_appendf(&trash, "%lld,",
3433 px->fe_counters.p.http.comp_rsp);
3434
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003435 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
3436 chunk_appendf(&trash, ",,,,,,,");
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003437
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003438 /* finish with EOL */
3439 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003440 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003441 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003442}
3443
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003444/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
3445 * from stream interface <si>, and stats flags <flags>. The caller is responsible
3446 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
3447 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02003448 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003449static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02003450{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003451 struct appctx *appctx = __objt_appctx(si->end);
3452
3453 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003454 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003455 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003456 /* Column sub-heading for Enable or Disable server */
3457 chunk_appendf(&trash, "<td></td>");
3458 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003459 chunk_appendf(&trash,
3460 /* frontend name, listener name */
3461 "<td class=ac><a name=\"%s/+%s\"></a>%s"
3462 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
3463 "",
3464 px->id, l->name,
3465 (flags & ST_SHLGNDS)?"<u>":"",
3466 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02003467
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003468 if (flags & ST_SHLGNDS) {
3469 char str[INET6_ADDRSTRLEN];
3470 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02003471
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003472 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02003473
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003474 port = get_host_port(&l->addr);
3475 switch (addr_to_str(&l->addr, str, sizeof(str))) {
3476 case AF_INET:
3477 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
3478 break;
3479 case AF_INET6:
3480 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
3481 break;
3482 case AF_UNIX:
3483 chunk_appendf(&trash, "unix, ");
3484 break;
3485 case -1:
3486 chunk_appendf(&trash, "(%s), ", strerror(errno));
3487 break;
3488 }
Willy Tarreau91861262007-10-17 17:06:05 +02003489
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003490 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003491 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003492 }
Willy Tarreau91861262007-10-17 17:06:05 +02003493
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003494 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003495 /* queue */
3496 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003497 /* sessions rate: current, max, limit */
3498 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003499 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003500 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003501 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003502 /* bytes: in, out */
3503 "<td>%s</td><td>%s</td>"
3504 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003505 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003506 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
3507 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02003508
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003509 chunk_appendf(&trash,
3510 /* denied: req, resp */
3511 "<td>%s</td><td>%s</td>"
3512 /* errors: request, connect, response */
3513 "<td>%s</td><td></td><td></td>"
3514 /* warnings: retries, redispatches */
3515 "<td></td><td></td>"
3516 /* server status: reflect listener status */
3517 "<td class=ac>%s</td>"
3518 /* rest of server: nothing */
3519 "<td class=ac colspan=8></td></tr>"
3520 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003521 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
3522 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003523 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
3524 }
3525 else { /* CSV mode */
3526 chunk_appendf(&trash,
3527 /* pxid, name, queue cur, queue max, */
3528 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003529 /* sessions: current, max, limit, total */
3530 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003531 /* bytes: in, out */
3532 "%lld,%lld,"
3533 /* denied: req, resp */
3534 "%lld,%lld,"
3535 /* errors: request, connect, response */
3536 "%lld,,,"
3537 /* warnings: retries, redispatches */
3538 ",,"
3539 /* server status: reflect listener status */
3540 "%s,"
3541 /* rest of server: nothing */
3542 ",,,,,,,,"
3543 /* pid, iid, sid, throttle, lbtot, tracked, type */
3544 "%d,%d,%d,,,,%d,"
3545 /* rate, rate_lim, rate_max */
3546 ",,,"
3547 /* check_status, check_code, check_duration */
3548 ",,,"
3549 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3550 ",,,,,,"
3551 /* failed health analyses */
3552 ","
3553 /* requests : req_rate, req_rate_max, req_tot, */
3554 ",,,"
3555 /* errors: cli_aborts, srv_aborts */
3556 ",,"
3557 /* compression: in, out, bypassed, comp_rsp */
3558 ",,,,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003559 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
3560 ",,,,,,,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003561 "\n",
3562 px->id, l->name,
3563 l->nbconn, l->counters->conn_max,
3564 l->maxconn, l->counters->cum_conn,
3565 l->counters->bytes_in, l->counters->bytes_out,
3566 l->counters->denied_req, l->counters->denied_resp,
3567 l->counters->failed_req,
3568 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
3569 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
3570 }
3571 return 1;
3572}
Willy Tarreau91861262007-10-17 17:06:05 +02003573
Simon Horman4d2eab62015-04-23 14:51:26 +09003574enum srv_stats_state {
3575 SRV_STATS_STATE_DOWN = 0,
3576 SRV_STATS_STATE_DOWN_AGENT,
3577 SRV_STATS_STATE_GOING_UP,
3578 SRV_STATS_STATE_UP_GOING_DOWN,
3579 SRV_STATS_STATE_UP,
3580 SRV_STATS_STATE_NOLB_GOING_DOWN,
3581 SRV_STATS_STATE_NOLB,
3582 SRV_STATS_STATE_DRAIN_GOING_DOWN,
3583 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09003584 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09003585 SRV_STATS_STATE_NO_CHECK,
3586
3587 SRV_STATS_STATE_COUNT, /* Must be last */
3588};
3589
Simon Horman837bfa72015-04-23 14:51:27 +09003590enum srv_stats_colour {
3591 SRV_STATS_COLOUR_DOWN = 0,
3592 SRV_STATS_COLOUR_GOING_UP,
3593 SRV_STATS_COLOUR_GOING_DOWN,
3594 SRV_STATS_COLOUR_UP,
3595 SRV_STATS_COLOUR_NOLB,
3596 SRV_STATS_COLOUR_DRAINING,
3597 SRV_STATS_COLOUR_NO_CHECK,
3598
3599 SRV_STATS_COLOUR_COUNT, /* Must be last */
3600};
3601
3602static const char *srv_stats_colour_st[SRV_STATS_COLOUR_COUNT] = {
3603 [SRV_STATS_COLOUR_DOWN] = "down",
3604 [SRV_STATS_COLOUR_GOING_UP] = "going_up",
3605 [SRV_STATS_COLOUR_GOING_DOWN] = "going_down",
3606 [SRV_STATS_COLOUR_UP] = "up",
3607 [SRV_STATS_COLOUR_NOLB] = "nolb",
3608 [SRV_STATS_COLOUR_DRAINING] = "draining",
3609 [SRV_STATS_COLOUR_NO_CHECK] = "no_check",
3610};
3611
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003612/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
3613 * from stream interface <si>, stats flags <flags>, and server state <state>.
3614 * The caller is responsible for clearing the trash if needed. Returns non-zero
Simon Horman4d2eab62015-04-23 14:51:26 +09003615 * if it emits anything, zero otherwise.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003616 */
Simon Horman4d2eab62015-04-23 14:51:26 +09003617static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv,
Simon Horman837bfa72015-04-23 14:51:27 +09003618 enum srv_stats_state state, enum srv_stats_colour colour)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003619{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003620 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02003621 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003622 char str[INET6_ADDRSTRLEN];
3623 struct chunk src;
3624 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02003625
Willy Tarreau32091232014-05-16 13:52:00 +02003626 /* we have "via" which is the tracked server as described in the configuration,
3627 * and "ref" which is the checked server and the end of the chain.
3628 */
3629 via = sv->track ? sv->track : sv;
3630 ref = via;
3631 while (ref->track)
3632 ref = ref->track;
3633
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003634 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003635 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3636 [SRV_STATS_STATE_DOWN] = "DOWN",
3637 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
3638 [SRV_STATS_STATE_GOING_UP] = "DN %d/%d &uarr;",
3639 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d &darr;",
3640 [SRV_STATS_STATE_UP] = "UP",
3641 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d &darr;",
3642 [SRV_STATS_STATE_NOLB] = "NOLB",
3643 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d &darr;",
3644 [SRV_STATS_STATE_DRAIN] = "DRAIN",
Simon Hormanb167b6b2015-04-23 14:51:29 +09003645 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
Simon Horman4d2eab62015-04-23 14:51:26 +09003646 [SRV_STATS_STATE_NO_CHECK] = "<i>no check</i>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003647 };
Willy Tarreau91861262007-10-17 17:06:05 +02003648
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003649 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003650 chunk_appendf(&trash, "<tr class=\"maintain\">");
3651 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003652 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09003653 "<tr class=\"%s_%s\">",
3654 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", srv_stats_colour_st[colour]);
Willy Tarreau91861262007-10-17 17:06:05 +02003655
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003656 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003657 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003658 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3659 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003660
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003661 chunk_appendf(&trash,
3662 "<td class=ac><a name=\"%s/%s\"></a>%s"
3663 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3664 "",
3665 px->id, sv->id,
3666 (flags & ST_SHLGNDS) ? "<u>" : "",
3667 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02003668
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003669 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003670 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003671
3672 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
3673 case AF_INET:
3674 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
3675 break;
3676 case AF_INET6:
3677 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
3678 break;
3679 case AF_UNIX:
3680 chunk_appendf(&trash, "unix, ");
3681 break;
3682 case -1:
3683 chunk_appendf(&trash, "(%s), ", strerror(errno));
3684 break;
3685 default: /* address family not supported */
3686 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02003687 }
Willy Tarreau91861262007-10-17 17:06:05 +02003688
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003689 /* id */
3690 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02003691
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003692 /* cookie */
3693 if (sv->cookie) {
3694 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02003695
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003696 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
3697 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003698
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003699 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003700 }
3701
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003702 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003703 }
Willy Tarreau91861262007-10-17 17:06:05 +02003704
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003705 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003706 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003707 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003708 /* sessions rate : current, max, limit */
3709 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003710 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003711 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003712 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
3713 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02003714
Willy Tarreau466c9b52012-12-23 02:25:03 +01003715
3716 chunk_appendf(&trash,
3717 /* sessions: current, max, limit, total */
3718 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003719 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003720 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3721 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003722 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
3723 U2H(sv->counters.cum_sess),
3724 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02003725
Willy Tarreau466c9b52012-12-23 02:25:03 +01003726 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3727 if (px->mode == PR_MODE_HTTP) {
3728 unsigned long long tot;
3729 for (tot = i = 0; i < 6; i++)
3730 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003731
Willy Tarreau466c9b52012-12-23 02:25:03 +01003732 chunk_appendf(&trash,
3733 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3734 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3735 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3736 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3737 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3738 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3739 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3740 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003741 U2H(tot),
3742 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
3743 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
3744 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
3745 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
3746 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
3747 U2H(sv->counters.p.http.rsp[0]), tot ? (int)(100*sv->counters.p.http.rsp[0] / tot) : 0);
Willy Tarreau91861262007-10-17 17:06:05 +02003748 }
Willy Tarreau91861262007-10-17 17:06:05 +02003749
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003750 chunk_appendf(&trash, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3751 chunk_appendf(&trash, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES)));
3752 chunk_appendf(&trash, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES)));
3753 if (px->mode == PR_MODE_HTTP)
3754 chunk_appendf(&trash, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES)));
3755 chunk_appendf(&trash, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES)));
3756
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003757 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003758 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003759 /* sessions: lbtot, last */
3760 "<td>%s</td><td>%s</td>",
3761 U2H(sv->counters.cum_lbconn),
3762 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003763
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003764 chunk_appendf(&trash,
3765 /* bytes : in, out */
3766 "<td>%s</td><td>%s</td>"
3767 /* denied: req, resp */
3768 "<td></td><td>%s</td>"
3769 /* errors : request, connect */
3770 "<td></td><td>%s</td>"
3771 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003772 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003773 /* warnings: retries, redispatches */
3774 "<td>%lld</td><td>%lld</td>"
3775 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003776 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
3777 U2H(sv->counters.failed_secu),
3778 U2H(sv->counters.failed_conns),
3779 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003780 sv->counters.cli_aborts,
3781 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003782 sv->counters.retries, sv->counters.redispatches);
3783
3784 /* status, lest check */
3785 chunk_appendf(&trash, "<td class=ac>");
3786
Willy Tarreau20125212014-05-13 19:44:56 +02003787 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003788 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
3789 chunk_appendf(&trash, "MAINT");
3790 }
Simon Horman0766e442014-11-12 15:55:54 +09003791 else if ((ref->agent.state & CHK_ST_ENABLED) && !(sv->agent.health) && (ref->state == SRV_ST_STOPPED)) {
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003792 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
Willy Tarreauac497072014-05-29 01:04:35 +02003793 /* DOWN (agent) */
3794 chunk_appendf(&trash, srv_hlt_st[1], "GCC: your -Werror=format-security is bogus, annoying, and hides real bugs, I don't thank you, really!");
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003795 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01003796 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003797 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
3798 chunk_appendf(&trash,
3799 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003800 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3801 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02003802 }
Willy Tarreau91861262007-10-17 17:06:05 +02003803
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003804 if ((sv->state == SRV_ST_STOPPED) &&
3805 ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) && !(sv->agent.health)) {
3806 chunk_appendf(&trash,
3807 "</td><td class=ac><u> %s%s",
3808 (sv->agent.state & CHK_ST_INPROGRESS) ? "* " : "",
3809 get_check_status_info(sv->agent.status));
3810
3811 if (sv->agent.status >= HCHK_STATUS_L57DATA)
3812 chunk_appendf(&trash, "/%d", sv->agent.code);
3813
3814 if (sv->agent.status >= HCHK_STATUS_CHECKED && sv->agent.duration >= 0)
3815 chunk_appendf(&trash, " in %lums", sv->agent.duration);
3816
3817 chunk_appendf(&trash, "<div class=tips>%s",
3818 get_check_status_description(sv->agent.status));
3819 if (*sv->agent.desc) {
3820 chunk_appendf(&trash, ": ");
3821 chunk_initlen(&src, sv->agent.desc, 0, strlen(sv->agent.desc));
3822 chunk_htmlencode(&trash, &src);
3823 }
3824 chunk_appendf(&trash, "</div></u>");
3825 }
3826 else if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003827 chunk_appendf(&trash,
3828 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01003829 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003830 get_check_status_info(sv->check.status));
3831
3832 if (sv->check.status >= HCHK_STATUS_L57DATA)
3833 chunk_appendf(&trash, "/%d", sv->check.code);
3834
3835 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003836 chunk_appendf(&trash, " in %lums", sv->check.duration);
3837
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003838 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003839 get_check_status_description(sv->check.status));
3840 if (*sv->check.desc) {
3841 chunk_appendf(&trash, ": ");
3842 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
3843 chunk_htmlencode(&trash, &src);
3844 }
3845 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003846 }
3847 else
3848 chunk_appendf(&trash, "</td><td>");
3849
3850 chunk_appendf(&trash,
3851 /* weight */
3852 "</td><td class=ac>%d</td>"
3853 /* act, bck */
3854 "<td class=ac>%s</td><td class=ac>%s</td>"
3855 "",
3856 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003857 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
3858 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003859
3860 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003861 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003862 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003863
3864 if (ref->observe)
3865 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
3866
3867 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003868 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003869 "<td>%lld</td><td>%s</td>"
3870 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003871 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003872 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
3873 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003874 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
3875 /* tracking a server */
3876 chunk_appendf(&trash,
3877 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02003878 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01003879 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003880 else
3881 chunk_appendf(&trash, "<td colspan=3></td>");
3882
3883 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003884 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003885 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003886 else
3887 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3888 }
3889 else { /* CSV mode */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003890 struct chunk *out = get_trash_chunk();
Simon Horman4d2eab62015-04-23 14:51:26 +09003891 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3892 [SRV_STATS_STATE_DOWN] = "DOWN,",
3893 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent),",
3894 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d,",
3895 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d,",
3896 [SRV_STATS_STATE_UP] = "UP,",
3897 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d,",
3898 [SRV_STATS_STATE_NOLB] = "NOLB,",
3899 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d,",
3900 [SRV_STATS_STATE_DRAIN] = "DRAIN,",
Raghu Udiyar0d6b7a42016-02-05 22:30:11 +05303901 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent),",
Simon Horman4d2eab62015-04-23 14:51:26 +09003902 [SRV_STATS_STATE_NO_CHECK] = "no check,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003903 };
3904
3905 chunk_appendf(&trash,
3906 /* pxid, name */
3907 "%s,%s,"
3908 /* queue : current, max */
3909 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003910 /* sessions : current, max, limit, total */
3911 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003912 /* bytes : in, out */
3913 "%lld,%lld,"
3914 /* denied: req, resp */
3915 ",%lld,"
3916 /* errors : request, connect, response */
3917 ",%lld,%lld,"
3918 /* warnings: retries, redispatches */
3919 "%lld,%lld,"
3920 "",
3921 px->id, sv->id,
3922 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003923 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003924 sv->counters.bytes_in, sv->counters.bytes_out,
3925 sv->counters.failed_secu,
3926 sv->counters.failed_conns, sv->counters.failed_resp,
3927 sv->counters.retries, sv->counters.redispatches);
3928
3929 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003930 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003931 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003932 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003933 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003934 else
3935 chunk_appendf(&trash,
3936 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003937 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3938 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003939
3940 chunk_appendf(&trash,
3941 /* weight, active, backup */
3942 "%d,%d,%d,"
3943 "",
3944 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003945 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3946 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003947
3948 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003949 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003950 chunk_appendf(&trash,
3951 "%lld,%lld,%d,%d,",
3952 sv->counters.failed_checks, sv->counters.down_trans,
3953 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3954 else
3955 chunk_appendf(&trash, ",,,,");
3956
3957 /* queue limit, pid, iid, sid, */
3958 chunk_appendf(&trash,
3959 "%s,"
3960 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003961 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003962 relative_pid, px->uuid, sv->puid);
3963
3964 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003965 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003966 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003967
3968 /* sessions: lbtot */
3969 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3970
3971 /* tracked */
3972 if (sv->track)
3973 chunk_appendf(&trash, "%s/%s,",
3974 sv->track->proxy->id, sv->track->id);
3975 else
3976 chunk_appendf(&trash, ",");
3977
3978 /* type */
3979 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3980
3981 /* rate */
3982 chunk_appendf(&trash, "%u,,%u,",
3983 read_freq_ctr(&sv->sess_per_sec),
3984 sv->counters.sps_max);
3985
Willy Tarreauff5ae352013-12-11 20:36:34 +01003986 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003987 /* check_status */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003988 chunk_appendf(&trash, "%s,", csv_enc(get_check_status_info(sv->check.status), 1, out));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003989
3990 /* check_code */
3991 if (sv->check.status >= HCHK_STATUS_L57DATA)
3992 chunk_appendf(&trash, "%u,", sv->check.code);
3993 else
3994 chunk_appendf(&trash, ",");
3995
3996 /* check_duration */
3997 if (sv->check.status >= HCHK_STATUS_CHECKED)
3998 chunk_appendf(&trash, "%lu,", sv->check.duration);
3999 else
4000 chunk_appendf(&trash, ",");
4001
4002 }
4003 else
4004 chunk_appendf(&trash, ",,,");
4005
4006 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4007 if (px->mode == PR_MODE_HTTP) {
4008 for (i=1; i<6; i++)
4009 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
4010
4011 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
4012 }
4013 else
4014 chunk_appendf(&trash, ",,,,,,");
4015
4016 /* failed health analyses */
4017 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
4018
4019 /* requests : req_rate, req_rate_max, req_tot, */
4020 chunk_appendf(&trash, ",,,");
4021
4022 /* errors: cli_aborts, srv_aborts */
4023 chunk_appendf(&trash, "%lld,%lld,",
4024 sv->counters.cli_aborts, sv->counters.srv_aborts);
4025
4026 /* compression: in, out, bypassed, comp_rsp */
4027 chunk_appendf(&trash, ",,,,");
4028
Willy Tarreauf522f3d2014-02-10 22:22:49 +01004029 /* lastsess */
4030 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
4031
Willy Tarreaua28df3e2014-06-16 16:40:14 +02004032 /* capture of last check and agent statuses */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02004033 chunk_appendf(&trash, "%s,", ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? csv_enc(cstr(sv->check.desc), 1, out) : "");
4034 chunk_appendf(&trash, "%s,", ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? csv_enc(cstr(sv->agent.desc), 1, out) : "");
Willy Tarreaua28df3e2014-06-16 16:40:14 +02004035
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004036 /* qtime, ctime, rtime, ttime, */
4037 chunk_appendf(&trash, "%u,%u,%u,%u,",
4038 swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES),
4039 swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES),
4040 swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES),
4041 swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
4042
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004043 /* finish with EOL */
4044 chunk_appendf(&trash, "\n");
4045 }
4046 return 1;
4047}
4048
4049/* Dumps a line for backend <px> to the trash for and uses the state from stream
4050 * interface <si> and stats flags <flags>. The caller is responsible for clearing
4051 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
4052 */
4053static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
4054{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004055 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004056 struct chunk src;
4057 int i;
4058
4059 if (!(px->cap & PR_CAP_BE))
4060 return 0;
4061
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004062 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004063 return 0;
4064
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004065 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004066 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004067 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004068 /* Column sub-heading for Enable or Disable server */
4069 chunk_appendf(&trash, "<td></td>");
4070 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004071 chunk_appendf(&trash,
4072 "<td class=ac>"
4073 /* name */
4074 "%s<a name=\"%s/Backend\"></a>"
4075 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
4076 "",
4077 (flags & ST_SHLGNDS)?"<u>":"",
4078 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004079
4080 if (flags & ST_SHLGNDS) {
4081 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004082 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004083 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
4084
4085 /* cookie */
4086 if (px->cookie_name) {
4087 chunk_appendf(&trash, ", cookie: '");
4088 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
4089 chunk_htmlencode(&trash, &src);
4090 chunk_appendf(&trash, "'");
4091 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004092 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004093 }
4094
4095 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004096 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004097 /* queue : current, max */
4098 "<td>%s</td><td>%s</td><td></td>"
4099 /* sessions rate : current, max, limit */
4100 "<td>%s</td><td>%s</td><td></td>"
4101 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004102 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01004103 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
4104 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004105
4106 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004107 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004108 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004109 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004110 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004111 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01004112 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
4113 U2H(px->be_counters.cum_conn),
4114 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004115
Willy Tarreau466c9b52012-12-23 02:25:03 +01004116 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004117 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01004118 chunk_appendf(&trash,
4119 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
4120 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
4121 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
4122 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
4123 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
4124 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
4125 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
4126 "<tr><th>- other responses:</th><td>%s</td></tr>"
4127 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004128 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004129 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01004130 U2H(px->be_counters.p.http.cum_req),
4131 U2H(px->be_counters.p.http.rsp[1]),
4132 U2H(px->be_counters.p.http.rsp[2]),
4133 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004134 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01004135 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01004136 U2H(px->be_counters.p.http.rsp[3]),
4137 U2H(px->be_counters.p.http.rsp[4]),
4138 U2H(px->be_counters.p.http.rsp[5]),
4139 U2H(px->be_counters.p.http.rsp[0]),
4140 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004141 }
4142
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004143 chunk_appendf(&trash, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES)));
4144 chunk_appendf(&trash, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES)));
4145 if (px->mode == PR_MODE_HTTP)
4146 chunk_appendf(&trash, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES)));
4147 chunk_appendf(&trash, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES)));
4148
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004149 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01004150 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004151 /* sessions: lbtot, last */
4152 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004153 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004154 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004155 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01004156 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004157 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01004158 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004159
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004160 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004161 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02004162 "<td>%s%s<div class=tips><table class=det>"
4163 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
4164 "<tr><th>Compression in:</th><td>%s</td></tr>"
4165 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
4166 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
4167 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
4168 "</table></div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004169 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01004170 U2H(px->be_counters.bytes_out),
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02004171 U2H(px->be_counters.bytes_out),
4172 U2H(px->be_counters.comp_in),
4173 U2H(px->be_counters.comp_out),
4174 px->be_counters.comp_in ? (int)(px->be_counters.comp_out * 100 / px->be_counters.comp_in) : 0,
4175 U2H(px->be_counters.comp_byp),
4176 U2H(px->be_counters.comp_in - px->be_counters.comp_out),
4177 px->be_counters.bytes_out ? (int)((px->be_counters.comp_in - px->be_counters.comp_out) * 100 / px->be_counters.bytes_out) : 0,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004178 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
4179
4180 chunk_appendf(&trash,
4181 /* denied: req, resp */
4182 "<td>%s</td><td>%s</td>"
4183 /* errors : request, connect */
4184 "<td></td><td>%s</td>"
4185 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004186 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004187 /* warnings: retries, redispatches */
4188 "<td>%lld</td><td>%lld</td>"
4189 /* backend status: reflect backend status (up/down): we display UP
4190 * if the backend has known working servers or if it has no server at
4191 * all (eg: for stats). Then we display the total weight, number of
4192 * active and backups. */
4193 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
4194 "<td class=ac>%d</td><td class=ac>%d</td>"
4195 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01004196 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
4197 U2H(px->be_counters.failed_conns),
4198 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004199 px->be_counters.cli_aborts,
4200 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004201 px->be_counters.retries, px->be_counters.redispatches,
4202 human_time(now.tv_sec - px->last_change, 1),
4203 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
4204 "<font color=\"red\"><b>DOWN</b></font>",
4205 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
4206 px->srv_act, px->srv_bck);
4207
4208 chunk_appendf(&trash,
4209 /* rest of backend: nothing, down transitions, total downtime, throttle */
4210 "<td class=ac>&nbsp;</td><td>%d</td>"
4211 "<td>%s</td>"
4212 "<td></td>"
4213 "</tr>",
4214 px->down_trans,
4215 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
4216 }
4217 else { /* CSV mode */
4218 chunk_appendf(&trash,
4219 /* pxid, name */
4220 "%s,BACKEND,"
4221 /* queue : current, max */
4222 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01004223 /* sessions : current, max, limit, total */
4224 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004225 /* bytes : in, out */
4226 "%lld,%lld,"
4227 /* denied: req, resp */
4228 "%lld,%lld,"
4229 /* errors : request, connect, response */
4230 ",%lld,%lld,"
4231 /* warnings: retries, redispatches */
4232 "%lld,%lld,"
4233 /* backend status: reflect backend status (up/down): we display UP
4234 * if the backend has known working servers or if it has no server at
4235 * all (eg: for stats). Then we display the total weight, number of
4236 * active and backups. */
4237 "%s,"
4238 "%d,%d,%d,"
4239 /* rest of backend: nothing, down transitions, last change, total downtime */
4240 ",%d,%d,%d,,"
4241 /* pid, iid, sid, throttle, lbtot, tracked, type */
4242 "%d,%d,0,,%lld,,%d,"
4243 /* rate, rate_lim, rate_max, */
4244 "%u,,%u,"
4245 /* check_status, check_code, check_duration */
4246 ",,,",
4247 px->id,
4248 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
4249 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
4250 px->be_counters.bytes_in, px->be_counters.bytes_out,
4251 px->be_counters.denied_req, px->be_counters.denied_resp,
4252 px->be_counters.failed_conns, px->be_counters.failed_resp,
4253 px->be_counters.retries, px->be_counters.redispatches,
4254 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
4255 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
4256 px->srv_act, px->srv_bck,
4257 px->down_trans, (int)(now.tv_sec - px->last_change),
4258 px->srv?be_downtime(px):0,
4259 relative_pid, px->uuid,
4260 px->be_counters.cum_lbconn, STATS_TYPE_BE,
4261 read_freq_ctr(&px->be_sess_per_sec),
4262 px->be_counters.sps_max);
4263
4264 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4265 if (px->mode == PR_MODE_HTTP) {
4266 for (i=1; i<6; i++)
4267 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
4268 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
4269 }
4270 else
4271 chunk_appendf(&trash, ",,,,,,");
4272
4273 /* failed health analyses */
4274 chunk_appendf(&trash, ",");
4275
4276 /* requests : req_rate, req_rate_max, req_tot, */
4277 chunk_appendf(&trash, ",,,");
4278
4279 /* errors: cli_aborts, srv_aborts */
4280 chunk_appendf(&trash, "%lld,%lld,",
4281 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
4282
4283 /* compression: in, out, bypassed */
4284 chunk_appendf(&trash, "%lld,%lld,%lld,",
4285 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
4286
4287 /* compression: comp_rsp */
4288 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
4289
Willy Tarreaua28df3e2014-06-16 16:40:14 +02004290 /* lastsess, last_chk, last_agt, */
4291 chunk_appendf(&trash, "%d,,,", be_lastsession(px));
Willy Tarreauf522f3d2014-02-10 22:22:49 +01004292
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004293 /* qtime, ctime, rtime, ttime, */
4294 chunk_appendf(&trash, "%u,%u,%u,%u,",
4295 swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES),
4296 swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES),
4297 swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES),
4298 swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
4299
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004300 /* finish with EOL */
4301 chunk_appendf(&trash, "\n");
4302 }
4303 return 1;
4304}
4305
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004306/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004307 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004308 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004309 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004310static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004311{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004312 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004313 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4314
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004315 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004316 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004317
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004318 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004319 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004320 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004321 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004322 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004323 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004324 }
4325
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004326 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004327 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004328 }
4329
4330 /* print a new table */
4331 chunk_appendf(&trash,
4332 "<table class=\"tbl\" width=\"100%%\">\n"
4333 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004334 "<th class=\"pxname\" width=\"10%%\">");
4335
4336 chunk_appendf(&trash,
4337 "<a name=\"%s\"></a>%s"
4338 "<a class=px href=\"#%s\">%s</a>",
4339 px->id,
4340 (uri->flags & ST_SHLGNDS) ? "<u>":"",
4341 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004342
4343 if (uri->flags & ST_SHLGNDS) {
4344 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004345 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004346 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
4347 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004348 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004349 }
4350
4351 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004352 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004353 "<th class=\"%s\" width=\"90%%\">%s</th>"
4354 "</tr>\n"
4355 "</table>\n"
4356 "<table class=\"tbl\" width=\"100%%\">\n"
4357 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004358 (uri->flags & ST_SHLGNDS) ? "</u>":"",
4359 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
4360
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004361 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004362 /* Column heading for Enable or Disable server */
4363 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02004364 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004365
4366 chunk_appendf(&trash,
4367 "<th rowspan=2></th>"
4368 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004369 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004370 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
4371 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
4372 "<th colspan=9>Server</th>"
4373 "</tr>\n"
4374 "<tr class=\"titre\">"
4375 "<th>Cur</th><th>Max</th><th>Limit</th>"
4376 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004377 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004378 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
4379 "<th>Resp</th><th>Retr</th><th>Redis</th>"
4380 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
4381 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
4382 "<th>Thrtle</th>\n"
4383 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02004384}
4385
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004386/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004387 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004388 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004389static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004390{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004391 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004392 chunk_appendf(&trash, "</table>");
4393
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004394 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004395 /* close the form used to enable/disable this proxy servers */
4396 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004397 "Choose the action to perform on the checked servers : "
4398 "<select name=action>"
4399 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02004400 "<option value=\"ready\">Set state to READY</option>"
4401 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02004402 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02004403 "<option value=\"dhlth\">Health: disable checks</option>"
4404 "<option value=\"ehlth\">Health: enable checks</option>"
4405 "<option value=\"hrunn\">Health: force UP</option>"
4406 "<option value=\"hnolb\">Health: force NOLB</option>"
4407 "<option value=\"hdown\">Health: force DOWN</option>"
4408 "<option value=\"dagent\">Agent: disable checks</option>"
4409 "<option value=\"eagent\">Agent: enable checks</option>"
4410 "<option value=\"arunn\">Agent: force UP</option>"
4411 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004412 "<option value=\"shutdown\">Kill Sessions</option>"
4413 "</select>"
4414 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
4415 "&nbsp;<input type=\"submit\" value=\"Apply\">"
4416 "</form>",
4417 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004418 }
4419
4420 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004421}
Willy Tarreau91861262007-10-17 17:06:05 +02004422
4423/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004424 * Dumps statistics for a proxy. The output is sent to the stream interface's
4425 * input buffer. Returns 0 if it had to stop dumping data because of lack of
4426 * buffer space, or non-zero if everything completed. This function is used
4427 * both by the CLI and the HTTP entry points, and is able to dump the output
4428 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02004429 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004430static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02004431{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004432 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02004433 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004434 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02004435 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004436 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02004437
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004438 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02004439
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004440 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004441 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02004442 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02004443 if (uri && uri->scope) {
4444 /* we have a limited scope, we have to check the proxy name */
4445 struct stat_scope *scope;
4446 int len;
4447
4448 len = strlen(px->id);
4449 scope = uri->scope;
4450
4451 while (scope) {
4452 /* match exact proxy name */
4453 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
4454 break;
4455
4456 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02004457 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02004458 break;
4459 scope = scope->next;
4460 }
4461
4462 /* proxy name not found : don't dump anything */
4463 if (scope == NULL)
4464 return 1;
4465 }
4466
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004467 /* if the user has requested a limited output and the proxy
4468 * name does not match, skip it.
4469 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004470 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004471 strnistr(px->id, strlen(px->id), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len) == NULL)
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004472 return 1;
4473
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004474 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
4475 (appctx->ctx.stats.iid != -1) &&
4476 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004477 return 1;
4478
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004479 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02004480 /* fall through */
4481
Willy Tarreau295a8372011-03-10 11:25:07 +01004482 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004483 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004484 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004485 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004486 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02004487 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004488 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004489 }
Willy Tarreau91861262007-10-17 17:06:05 +02004490
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004491 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02004492 /* fall through */
4493
Willy Tarreau295a8372011-03-10 11:25:07 +01004494 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02004495 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004496 if (stats_dump_fe_stats(si, px)) {
4497 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004498 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004499 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004500 }
4501 }
Willy Tarreau91861262007-10-17 17:06:05 +02004502
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004503 appctx->ctx.stats.l = px->conf.listeners.n;
4504 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004505 /* fall through */
4506
Willy Tarreau295a8372011-03-10 11:25:07 +01004507 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004508 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004509 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004510 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004511 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004512 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004513 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004514
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004515 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004516 if (!l->counters)
4517 continue;
4518
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004519 if (appctx->ctx.stats.flags & STAT_BOUND) {
4520 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004521 break;
4522
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004523 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004524 continue;
4525 }
4526
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004527 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004528 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0)) {
4529 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004530 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004531 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004532 }
4533 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004534 }
4535
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004536 appctx->ctx.stats.sv = px->srv; /* may be NULL */
4537 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02004538 /* fall through */
4539
Willy Tarreau295a8372011-03-10 11:25:07 +01004540 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02004541 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004542 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004543 enum srv_stats_state sv_state;
Simon Horman837bfa72015-04-23 14:51:27 +09004544 enum srv_stats_colour sv_colour;
Willy Tarreau91861262007-10-17 17:06:05 +02004545
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004546 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004547 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004548 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004549 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004550
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004551 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02004552
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004553 if (appctx->ctx.stats.flags & STAT_BOUND) {
4554 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004555 break;
4556
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004557 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004558 continue;
4559 }
4560
Willy Tarreau32091232014-05-16 13:52:00 +02004561 svs = sv;
4562 while (svs->track)
4563 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004564
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004565 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004566 if ((svs->check.state & CHK_ST_ENABLED) &&
Simon Horman837bfa72015-04-23 14:51:27 +09004567 (svs->check.health < svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004568 sv_state = SRV_STATS_STATE_UP_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09004569 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
4570 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004571 sv_state = SRV_STATS_STATE_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09004572 sv_colour = SRV_STATS_COLOUR_UP;
4573 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01004574
Simon Horman4463d192015-04-23 14:51:28 +09004575 if (sv_state == SRV_STATS_STATE_UP && !svs->uweight)
4576 sv_colour = SRV_STATS_COLOUR_DRAINING;
4577
4578 if (sv->admin & SRV_ADMF_DRAIN) {
Simon Hormanb167b6b2015-04-23 14:51:29 +09004579 if (svs->agent.state & CHK_ST_ENABLED)
4580 sv_state = SRV_STATS_STATE_DRAIN_AGENT;
4581 else if (sv_state == SRV_STATS_STATE_UP_GOING_DOWN)
Simon Horman4d2eab62015-04-23 14:51:26 +09004582 sv_state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
Simon Horman4463d192015-04-23 14:51:28 +09004583 else
Simon Horman4d2eab62015-04-23 14:51:26 +09004584 sv_state = SRV_STATS_STATE_DRAIN;
4585 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004586
Simon Horman837bfa72015-04-23 14:51:27 +09004587 if (sv_state == SRV_STATS_STATE_UP && !(svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004588 sv_state = SRV_STATS_STATE_NO_CHECK;
Simon Horman837bfa72015-04-23 14:51:27 +09004589 sv_colour = SRV_STATS_COLOUR_NO_CHECK;
4590 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01004591 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004592 else if (sv->state == SRV_ST_STOPPING) {
4593 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
Simon Horman837bfa72015-04-23 14:51:27 +09004594 (svs->check.health == svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004595 sv_state = SRV_STATS_STATE_NOLB;
Simon Horman837bfa72015-04-23 14:51:27 +09004596 sv_colour = SRV_STATS_COLOUR_NOLB;
4597 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004598 sv_state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09004599 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
4600 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004601 }
4602 else { /* stopped */
Simon Horman837bfa72015-04-23 14:51:27 +09004603 if ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004604 sv_state = SRV_STATS_STATE_DOWN_AGENT;
Simon Horman837bfa72015-04-23 14:51:27 +09004605 sv_colour = SRV_STATS_COLOUR_DOWN;
4606 } else if ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004607 sv_state = SRV_STATS_STATE_DOWN; /* DOWN */
Simon Horman837bfa72015-04-23 14:51:27 +09004608 sv_colour = SRV_STATS_COLOUR_DOWN;
4609 } else if ((svs->agent.state & CHK_ST_ENABLED) || (svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004610 sv_state = SRV_STATS_STATE_GOING_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09004611 sv_colour = SRV_STATS_COLOUR_GOING_UP;
4612 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004613 sv_state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Simon Horman837bfa72015-04-23 14:51:27 +09004614 sv_colour = SRV_STATS_COLOUR_DOWN;
4615 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004616 }
Willy Tarreau91861262007-10-17 17:06:05 +02004617
Willy Tarreau9638efa2014-05-23 11:19:57 +02004618 if (((sv_state <= 1) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02004619 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004620 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02004621 continue;
4622 }
4623
Simon Horman837bfa72015-04-23 14:51:27 +09004624 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state, sv_colour)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004625 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004626 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004627 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004628 }
4629 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004630 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004631
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004632 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004633 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004634
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004635 case STAT_PX_ST_BE:
4636 /* print the backend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004637 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0)) {
4638 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004639 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004640 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004641 }
4642 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004643
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004644 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004645 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004646
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004647 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004648 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004649 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004650 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004651 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004652 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004653 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004654 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004655
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004656 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004657 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004658
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004659 case STAT_PX_ST_FIN:
4660 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004661
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004662 default:
4663 /* unknown state, we should put an abort() here ! */
4664 return 1;
4665 }
4666}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004667
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004668/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4669 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004670 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004671static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004672{
4673 /* WARNING! This must fit in the first buffer !!! */
4674 chunk_appendf(&trash,
4675 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4676 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4677 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4678 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4679 "<style type=\"text/css\"><!--\n"
4680 "body {"
4681 " font-family: arial, helvetica, sans-serif;"
4682 " font-size: 12px;"
4683 " font-weight: normal;"
4684 " color: black;"
4685 " background: white;"
4686 "}\n"
4687 "th,td {"
4688 " font-size: 10px;"
4689 "}\n"
4690 "h1 {"
4691 " font-size: x-large;"
4692 " margin-bottom: 0.5em;"
4693 "}\n"
4694 "h2 {"
4695 " font-family: helvetica, arial;"
4696 " font-size: x-large;"
4697 " font-weight: bold;"
4698 " font-style: italic;"
4699 " color: #6020a0;"
4700 " margin-top: 0em;"
4701 " margin-bottom: 0em;"
4702 "}\n"
4703 "h3 {"
4704 " font-family: helvetica, arial;"
4705 " font-size: 16px;"
4706 " font-weight: bold;"
4707 " color: #b00040;"
4708 " background: #e8e8d0;"
4709 " margin-top: 0em;"
4710 " margin-bottom: 0em;"
4711 "}\n"
4712 "li {"
4713 " margin-top: 0.25em;"
4714 " margin-right: 2em;"
4715 "}\n"
4716 ".hr {margin-top: 0.25em;"
4717 " border-color: black;"
4718 " border-bottom-style: solid;"
4719 "}\n"
4720 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4721 ".total {background: #20D0D0;color: #ffff80;}\n"
4722 ".frontend {background: #e8e8d0;}\n"
4723 ".socket {background: #d0d0d0;}\n"
4724 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004725 ".active_down {background: #ff9090;}\n"
4726 ".active_going_up {background: #ffd020;}\n"
4727 ".active_going_down {background: #ffffa0;}\n"
4728 ".active_up {background: #c0ffc0;}\n"
4729 ".active_nolb {background: #20a0ff;}\n"
4730 ".active_draining {background: #20a0FF;}\n"
4731 ".active_no_check {background: #e0e0e0;}\n"
4732 ".backup_down {background: #ff9090;}\n"
4733 ".backup_going_up {background: #ff80ff;}\n"
4734 ".backup_going_down {background: #c060ff;}\n"
4735 ".backup_up {background: #b0d0ff;}\n"
4736 ".backup_nolb {background: #90b0e0;}\n"
4737 ".backup_draining {background: #cc9900;}\n"
4738 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004739 ".maintain {background: #c07820;}\n"
4740 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4741 "\n"
4742 "a.px:link {color: #ffff40; text-decoration: none;}"
4743 "a.px:visited {color: #ffff40; text-decoration: none;}"
4744 "a.px:hover {color: #ffffff; text-decoration: none;}"
4745 "a.lfsb:link {color: #000000; text-decoration: none;}"
4746 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4747 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4748 "\n"
4749 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4750 "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"
4751 "table.tbl td.ac { text-align: center;}\n"
4752 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4753 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4754 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4755 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4756 "\n"
4757 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4758 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4759 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004760 "table.det { border-collapse: collapse; border-style: none; }\n"
4761 "table.det th { text-align: left; border-width: 0px; padding: 0px 1px 0px 0px; font-style:normal;font-size:11px;font-weight:bold;font-family: sans-serif;}\n"
Willy Tarreau6b9d3a82013-12-16 09:00:35 +01004762 "table.det td { text-align: right; border-width: 0px; padding: 0px 0px 0px 4px; white-space: nowrap; font-style:normal;font-size:11px;font-weight:normal;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004763 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004764 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004765 " display:block;\n"
4766 " visibility:hidden;\n"
4767 " z-index:2147483647;\n"
4768 " position:absolute;\n"
4769 " padding:2px 4px 3px;\n"
4770 " background:#f0f060; color:#000000;\n"
4771 " border:1px solid #7040c0;\n"
4772 " white-space:nowrap;\n"
4773 " font-style:normal;font-size:11px;font-weight:normal;\n"
4774 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4775 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4776 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004777 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004778 "-->\n"
4779 "</style></head>\n",
4780 (uri->flags & ST_SHNODE) ? " on " : "",
4781 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4782 );
4783}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004784
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004785/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004786 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004787 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004788 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004789static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004790{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004791 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004792 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004793 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004794
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004795 /* WARNING! this has to fit the first packet too.
4796 * We are around 3.5 kB, add adding entries will
4797 * become tricky if we want to support 4kB buffers !
4798 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004799 chunk_appendf(&trash,
4800 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4801 PRODUCT_NAME "%s</a></h1>\n"
4802 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4803 "<hr width=\"100%%\" class=\"hr\">\n"
4804 "<h3>&gt; General process information</h3>\n"
4805 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4806 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4807 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4808 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4809 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4810 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4811 "Running tasks: %d/%d; idle = %d %%<br>\n"
4812 "</td><td align=\"center\" nowrap>\n"
4813 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004814 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4815 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004816 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004817 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4818 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004819 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004820 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
4821 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004822 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004823 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
4824 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004825 "</tr><tr>\n"
4826 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
Geoff Bucarcc8bb922013-04-18 13:53:16 -07004827 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004828 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004829 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004830 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004831 "</td>"
4832 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4833 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4834 "",
4835 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4836 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4837 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4838 (uri->flags & ST_SHDESC) ? ": " : "",
4839 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4840 pid, relative_pid, global.nbproc,
4841 up / 86400, (up % 86400) / 3600,
4842 (up % 3600) / 60, (up % 60),
4843 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4844 global.rlimit_memmax ? " MB" : "",
4845 global.rlimit_nofile,
4846 global.maxsock, global.maxconn, global.maxpipes,
4847 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4848 run_queue_cur, nb_tasks_cur, idle_pct
4849 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004850
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004851 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004852 memcpy(scope_txt, bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004853 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004854
4855 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004856 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004857 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004858 STAT_SCOPE_TXT_MAXLEN);
4859
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004860 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004861 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004862 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004863 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004864 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004865 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004866 }
4867
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004868 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004869 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004870 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004871 uri->uri_prefix,
4872 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004873 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004874 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004875 else
4876 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004877 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004878 uri->uri_prefix,
4879 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004880 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004881 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004882
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004883 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004884 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004885 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004886 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004887 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004888 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004889 "",
4890 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004891 else
4892 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004893 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004894 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004895 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004896 ";norefresh",
4897 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004898 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004899
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004900 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004901 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004902 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004903 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4904 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004905 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004906
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004907 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004908 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004909 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004910 (uri->refresh > 0) ? ";norefresh" : "",
4911 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004912
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004913 chunk_appendf(&trash,
4914 "</ul></td>"
4915 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4916 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4917 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4918 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4919 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4920 "</ul>"
4921 "</td>"
4922 "</tr></table>\n"
4923 ""
4924 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004925
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004926 if (appctx->ctx.stats.st_code) {
4927 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004928 case STAT_STATUS_DONE:
4929 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004930 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004931 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004932 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02004933 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004934 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4935 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004936 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004937 break;
4938 case STAT_STATUS_NONE:
4939 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004940 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004941 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004942 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02004943 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004944 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4945 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004946 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004947 break;
4948 case STAT_STATUS_PART:
4949 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004950 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004951 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004952 "Action partially processed.<br>"
4953 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004954 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004955 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4956 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004957 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004958 break;
4959 case STAT_STATUS_ERRP:
4960 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004961 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004962 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004963 "Action not processed because of invalid parameters."
4964 "<ul>"
4965 "<li>The action is maybe unknown.</li>"
4966 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
4967 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4968 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004969 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004970 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4971 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004972 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004973 break;
4974 case STAT_STATUS_EXCD:
4975 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004976 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004977 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004978 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4979 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004980 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004981 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4982 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004983 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004984 break;
4985 case STAT_STATUS_DENY:
4986 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004987 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004988 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004989 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004990 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004991 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4992 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004993 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004994 break;
4995 default:
4996 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004997 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004998 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004999 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02005000 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005001 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5002 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005003 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005004 }
5005 chunk_appendf(&trash, "<p>\n");
5006 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005007}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01005008
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005009/* Dumps the HTML stats trailer block to the trash. The caller is responsible
5010 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005011 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005012static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005013{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005014 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005015}
Willy Tarreau7f062c42009-03-05 18:43:00 +01005016
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005017/* This function dumps statistics onto the stream interface's read buffer in
5018 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02005019 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
5020 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
Willy Tarreau87b09662015-04-03 00:22:06 +02005021 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02005022 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005023 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005024static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005025{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005026 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005027 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005028 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01005029
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005030 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005031
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005032 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005033 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005034 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005035 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005036
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005037 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005038 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005039 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01005040 else
5041 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01005042
Willy Tarreaubc18da12015-03-13 14:00:47 +01005043 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005044 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005045 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005046 }
Willy Tarreauae526782010-03-04 20:34:23 +01005047
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005048 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005049 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005050
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005051 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005052 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005053 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01005054 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005055 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005056 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005057 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005058 }
Willy Tarreau91861262007-10-17 17:06:05 +02005059
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005060 appctx->ctx.stats.px = proxy;
5061 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
5062 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02005063 /* fall through */
5064
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005065 case STAT_ST_LIST:
5066 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005067 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005068 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005069 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005070 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005071 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005072
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005073 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005074 /* skip the disabled proxies, global frontend and non-networked ones */
5075 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005076 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005077 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005078
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005079 appctx->ctx.stats.px = px->next;
5080 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005081 }
5082 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005083
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005084 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005085 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005086
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005087 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005088 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005089 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01005090 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005091 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005092 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005093 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005094 }
Willy Tarreau55058a72012-11-21 08:27:21 +01005095
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005096 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005097 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02005098
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005099 case STAT_ST_FIN:
5100 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01005101
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005102 default:
5103 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005104 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005105 return -1;
5106 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005107}
Willy Tarreauae526782010-03-04 20:34:23 +01005108
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005109/* We reached the stats page through a POST request. The appctx is
5110 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01005111 * Parse the posted data and enable/disable servers if necessary.
5112 * Returns 1 if request was parsed or zero if it needs more data.
5113 */
5114static int stats_process_http_post(struct stream_interface *si)
5115{
Willy Tarreau87b09662015-04-03 00:22:06 +02005116 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005117 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005118
5119 struct proxy *px = NULL;
5120 struct server *sv = NULL;
5121
5122 char key[LINESIZE];
5123 int action = ST_ADM_ACTION_NONE;
5124 int reprocess = 0;
5125
5126 int total_servers = 0;
5127 int altered_servers = 0;
5128
5129 char *first_param, *cur_param, *next_param, *end_params;
5130 char *st_cur_param = NULL;
5131 char *st_next_param = NULL;
5132
5133 struct chunk *temp;
5134 int reql;
5135
5136 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02005137 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005138 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005139 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005140 goto out;
5141 }
5142
Willy Tarreaueee5b512015-04-03 23:46:31 +02005143 reql = bo_getblk(si_oc(si), temp->str, s->txn->req.body_len, s->txn->req.eoh + 2);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005144 if (reql <= 0) {
5145 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005146 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005147 return 0;
5148 }
5149
5150 first_param = temp->str;
5151 end_params = temp->str + reql;
5152 cur_param = next_param = end_params;
5153 *end_params = '\0';
5154
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005155 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005156
5157 /*
5158 * Parse the parameters in reverse order to only store the last value.
5159 * From the html form, the backend and the action are at the end.
5160 */
5161 while (cur_param > first_param) {
5162 char *value;
5163 int poffset, plen;
5164
5165 cur_param--;
5166
5167 if ((*cur_param == '&') || (cur_param == first_param)) {
5168 reprocess_servers:
5169 /* Parse the key */
5170 poffset = (cur_param != first_param ? 1 : 0);
5171 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
5172 if ((plen > 0) && (plen <= sizeof(key))) {
5173 strncpy(key, cur_param + poffset, plen);
5174 key[plen - 1] = '\0';
5175 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005176 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005177 goto out;
5178 }
5179
5180 /* Parse the value */
5181 value = key;
5182 while (*value != '\0' && *value != '=') {
5183 value++;
5184 }
5185 if (*value == '=') {
5186 /* Ok, a value is found, we can mark the end of the key */
5187 *value++ = '\0';
5188 }
5189 if (url_decode(key) < 0 || url_decode(value) < 0)
5190 break;
5191
5192 /* Now we can check the key to see what to do */
5193 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02005194 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005195 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005196 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005197 goto out;
5198 }
5199 }
5200 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02005201 if (strcmp(value, "ready") == 0) {
5202 action = ST_ADM_ACTION_READY;
5203 }
5204 else if (strcmp(value, "drain") == 0) {
5205 action = ST_ADM_ACTION_DRAIN;
5206 }
5207 else if (strcmp(value, "maint") == 0) {
5208 action = ST_ADM_ACTION_MAINT;
5209 }
5210 else if (strcmp(value, "shutdown") == 0) {
5211 action = ST_ADM_ACTION_SHUTDOWN;
5212 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02005213 else if (strcmp(value, "dhlth") == 0) {
5214 action = ST_ADM_ACTION_DHLTH;
5215 }
5216 else if (strcmp(value, "ehlth") == 0) {
5217 action = ST_ADM_ACTION_EHLTH;
5218 }
5219 else if (strcmp(value, "hrunn") == 0) {
5220 action = ST_ADM_ACTION_HRUNN;
5221 }
5222 else if (strcmp(value, "hnolb") == 0) {
5223 action = ST_ADM_ACTION_HNOLB;
5224 }
5225 else if (strcmp(value, "hdown") == 0) {
5226 action = ST_ADM_ACTION_HDOWN;
5227 }
5228 else if (strcmp(value, "dagent") == 0) {
5229 action = ST_ADM_ACTION_DAGENT;
5230 }
5231 else if (strcmp(value, "eagent") == 0) {
5232 action = ST_ADM_ACTION_EAGENT;
5233 }
5234 else if (strcmp(value, "arunn") == 0) {
5235 action = ST_ADM_ACTION_ARUNN;
5236 }
5237 else if (strcmp(value, "adown") == 0) {
5238 action = ST_ADM_ACTION_ADOWN;
5239 }
Willy Tarreaued7df902014-05-22 18:04:49 +02005240 /* else these are the old supported methods */
5241 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005242 action = ST_ADM_ACTION_DISABLE;
5243 }
5244 else if (strcmp(value, "enable") == 0) {
5245 action = ST_ADM_ACTION_ENABLE;
5246 }
5247 else if (strcmp(value, "stop") == 0) {
5248 action = ST_ADM_ACTION_STOP;
5249 }
5250 else if (strcmp(value, "start") == 0) {
5251 action = ST_ADM_ACTION_START;
5252 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005253 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005254 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005255 goto out;
5256 }
5257 }
5258 else if (strcmp(key, "s") == 0) {
5259 if (!(px && action)) {
5260 /*
5261 * Indicates that we'll need to reprocess the parameters
5262 * as soon as backend and action are known
5263 */
5264 if (!reprocess) {
5265 st_cur_param = cur_param;
5266 st_next_param = next_param;
5267 }
5268 reprocess = 1;
5269 }
5270 else if ((sv = findserver(px, value)) != NULL) {
5271 switch (action) {
5272 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005273 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005274 altered_servers++;
5275 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005276 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005277 }
5278 break;
5279 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005280 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005281 altered_servers++;
5282 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005283 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005284 }
5285 break;
5286 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005287 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
5288 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
5289 altered_servers++;
5290 total_servers++;
5291 }
5292 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005293 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005294 if (sv->admin & SRV_ADMF_FDRAIN) {
5295 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
5296 altered_servers++;
5297 total_servers++;
5298 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005299 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02005300 case ST_ADM_ACTION_DHLTH:
5301 if (sv->check.state & CHK_ST_CONFIGURED) {
5302 sv->check.state &= ~CHK_ST_ENABLED;
5303 altered_servers++;
5304 total_servers++;
5305 }
5306 break;
5307 case ST_ADM_ACTION_EHLTH:
5308 if (sv->check.state & CHK_ST_CONFIGURED) {
5309 sv->check.state |= CHK_ST_ENABLED;
5310 altered_servers++;
5311 total_servers++;
5312 }
5313 break;
5314 case ST_ADM_ACTION_HRUNN:
5315 if (!(sv->track)) {
5316 sv->check.health = sv->check.rise + sv->check.fall - 1;
5317 srv_set_running(sv, "changed from Web interface");
5318 altered_servers++;
5319 total_servers++;
5320 }
5321 break;
5322 case ST_ADM_ACTION_HNOLB:
5323 if (!(sv->track)) {
5324 sv->check.health = sv->check.rise + sv->check.fall - 1;
5325 srv_set_stopping(sv, "changed from Web interface");
5326 altered_servers++;
5327 total_servers++;
5328 }
5329 break;
5330 case ST_ADM_ACTION_HDOWN:
5331 if (!(sv->track)) {
5332 sv->check.health = 0;
5333 srv_set_stopped(sv, "changed from Web interface");
5334 altered_servers++;
5335 total_servers++;
5336 }
5337 break;
5338 case ST_ADM_ACTION_DAGENT:
5339 if (sv->agent.state & CHK_ST_CONFIGURED) {
5340 sv->agent.state &= ~CHK_ST_ENABLED;
5341 altered_servers++;
5342 total_servers++;
5343 }
5344 break;
5345 case ST_ADM_ACTION_EAGENT:
5346 if (sv->agent.state & CHK_ST_CONFIGURED) {
5347 sv->agent.state |= CHK_ST_ENABLED;
5348 altered_servers++;
5349 total_servers++;
5350 }
5351 break;
5352 case ST_ADM_ACTION_ARUNN:
5353 if (sv->agent.state & CHK_ST_ENABLED) {
5354 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
5355 srv_set_running(sv, "changed from Web interface");
5356 altered_servers++;
5357 total_servers++;
5358 }
5359 break;
5360 case ST_ADM_ACTION_ADOWN:
5361 if (sv->agent.state & CHK_ST_ENABLED) {
5362 sv->agent.health = 0;
5363 srv_set_stopped(sv, "changed from Web interface");
5364 altered_servers++;
5365 total_servers++;
5366 }
5367 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02005368 case ST_ADM_ACTION_READY:
5369 srv_adm_set_ready(sv);
5370 altered_servers++;
5371 total_servers++;
5372 break;
5373 case ST_ADM_ACTION_DRAIN:
5374 srv_adm_set_drain(sv);
5375 altered_servers++;
5376 total_servers++;
5377 break;
5378 case ST_ADM_ACTION_MAINT:
5379 srv_adm_set_maint(sv);
5380 altered_servers++;
5381 total_servers++;
5382 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005383 case ST_ADM_ACTION_SHUTDOWN:
5384 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005385 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005386
5387 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
5388 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005389 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005390
5391 altered_servers++;
5392 total_servers++;
5393 }
5394 break;
5395 }
5396 } else {
5397 /* the server name is unknown or ambiguous (duplicate names) */
5398 total_servers++;
5399 }
5400 }
5401 if (reprocess && px && action) {
5402 /* Now, we know the backend and the action chosen by the user.
5403 * We can safely restart from the first server parameter
5404 * to reprocess them
5405 */
5406 cur_param = st_cur_param;
5407 next_param = st_next_param;
5408 reprocess = 0;
5409 goto reprocess_servers;
5410 }
5411
5412 next_param = cur_param;
5413 }
5414 }
5415
5416 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005417 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005418 }
5419 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005420 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005421 }
5422 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005423 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005424 }
5425 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005426 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005427 }
5428 out:
5429 return 1;
5430}
5431
5432
5433static int stats_send_http_headers(struct stream_interface *si)
5434{
Willy Tarreau87b09662015-04-03 00:22:06 +02005435 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005436 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005437 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005438
5439 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02005440 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005441 "Cache-Control: no-cache\r\n"
5442 "Connection: close\r\n"
5443 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005444 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005445
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005446 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01005447 chunk_appendf(&trash, "Refresh: %d\r\n",
5448 uri->refresh);
5449
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005450 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
5451
5452 if (appctx->ctx.stats.flags & STAT_CHUNKED)
5453 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
5454 else
5455 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005456
Willy Tarreaueee5b512015-04-03 23:46:31 +02005457 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005458 s->logs.tv_request = now;
5459
Willy Tarreaubc18da12015-03-13 14:00:47 +01005460 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005461 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005462 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005463 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005464
5465 return 1;
5466}
5467
5468static int stats_send_http_redirect(struct stream_interface *si)
5469{
5470 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005471 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005472 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005473 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005474
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005475 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01005476 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005477 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005478 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005479 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005480 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005481 }
5482
5483 /* We don't want to land on the posted stats page because a refresh will
5484 * repost the data. We don't want this to happen on accident so we redirect
5485 * the browse to the stats page with a GET.
5486 */
5487 chunk_printf(&trash,
5488 "HTTP/1.1 303 See Other\r\n"
5489 "Cache-Control: no-cache\r\n"
5490 "Content-Type: text/plain\r\n"
5491 "Connection: close\r\n"
5492 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreaufdfcc9d2016-01-26 13:57:29 +01005493 "Content-length: 0\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005494 "\r\n",
5495 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005496 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
5497 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
5498 stat_status_codes[appctx->ctx.stats.st_code]) ?
5499 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01005500 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005501 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5502 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01005503 scope_txt);
5504
Willy Tarreaueee5b512015-04-03 23:46:31 +02005505 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005506 s->logs.tv_request = now;
5507
Willy Tarreaubc18da12015-03-13 14:00:47 +01005508 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005509 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005510 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005511 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005512
5513 return 1;
5514}
5515
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005516/* This I/O handler runs as an applet embedded in a stream interface. It is
5517 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005518 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005519 * automatically unregisters itself once transfer is complete.
5520 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005521static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005522{
Willy Tarreau00a37f02015-04-13 12:05:19 +02005523 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02005524 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005525 struct channel *req = si_oc(si);
5526 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01005527
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005528 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
5529 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005530
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005531 /* check that the output is not closed */
5532 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005533 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005534
Willy Tarreau347a35d2013-11-22 17:51:09 +01005535 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005536 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005537 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02005538 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005539 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005540 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005541 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005542 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005543 }
5544
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005545 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005546 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005547 unsigned int data_len;
5548 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02005549 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005550
5551 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5552 /* One difficulty we're facing is that we must prevent
5553 * the input data from being automatically forwarded to
5554 * the output area. For this, we temporarily disable
5555 * forwarding on the channel.
5556 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005557 last_fwd = si_ic(si)->to_forward;
5558 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005559 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005560 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005561 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005562 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02005563 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005564 }
5565 }
5566
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005567 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005568 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005569 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005570
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005571 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005572
5573 /* Now we must either adjust or remove the chunk size. This is
5574 * not easy because the chunk size might wrap at the end of the
5575 * buffer, so we pretend we have nothing in the buffer, we write
5576 * the size, then restore the buffer's contents. Note that we can
5577 * only do that because no forwarding is scheduled on the stats
5578 * applet.
5579 */
5580 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005581 si_ic(si)->total -= (last_len - prev_len);
5582 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005583
5584 if (last_len != data_len) {
5585 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005586 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005587 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005588
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005589 si_ic(si)->total += (last_len - data_len);
5590 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005591 }
5592 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005593 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005594 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005595 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005596
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005597 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005598 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005599 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005600 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005601 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005602 }
5603
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005604 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005605 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005606 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005607 }
5608
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005609 if (appctx->st0 == STAT_HTTP_DONE) {
5610 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5611 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005612 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005613 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005614 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005615 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005616 }
5617 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005618 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005619 res->flags |= CF_READ_NULL;
5620 si_shutr(si);
5621 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005622
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005623 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5624 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005625
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005626 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005627 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5628 si_shutr(si);
5629 res->flags |= CF_READ_NULL;
5630 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005631 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005632 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005633 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005634}
5635
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005636
Willy Tarreau909d5172012-11-26 03:04:41 +01005637static inline const char *get_conn_ctrl_name(const struct connection *conn)
5638{
Willy Tarreau3c728722014-01-23 13:50:42 +01005639 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005640 return "NONE";
5641 return conn->ctrl->name;
5642}
5643
5644static inline const char *get_conn_xprt_name(const struct connection *conn)
5645{
5646 static char ptr[17];
5647
Willy Tarreauaad69382014-01-23 14:21:42 +01005648 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005649 return "NONE";
5650
5651 if (conn->xprt == &raw_sock)
5652 return "RAW";
5653
5654#ifdef USE_OPENSSL
5655 if (conn->xprt == &ssl_sock)
5656 return "SSL";
5657#endif
5658 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5659 return ptr;
5660}
5661
5662static inline const char *get_conn_data_name(const struct connection *conn)
5663{
5664 static char ptr[17];
5665
5666 if (!conn->data)
5667 return "NONE";
5668
5669 if (conn->data == &sess_conn_cb)
5670 return "SESS";
5671
5672 if (conn->data == &si_conn_cb)
5673 return "STRM";
5674
5675 if (conn->data == &check_conn_cb)
5676 return "CHCK";
5677
5678 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5679 return ptr;
5680}
5681
Willy Tarreau87b09662015-04-03 00:22:06 +02005682/* This function dumps a complete stream state onto the stream interface's
5683 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005684 * 0 if the output buffer is full and it needs to be called again, otherwise
5685 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005686 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005687static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005688{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005689 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005690 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005691 extern const char *monthname[12];
5692 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005693 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005694 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005695
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005696 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005697
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005698 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005699 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005700 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005701 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005702 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005703 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005704 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005705 appctx->ctx.sess.uid = 0;
5706 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005707 return 1;
5708 }
5709
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005710 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005711 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005712 appctx->ctx.sess.uid = sess->uniq_id;
5713 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005714 /* fall through */
5715
5716 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005717 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005718 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005719 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005720 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005721 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5722 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005723 sess->uniq_id,
Willy Tarreau666f5042015-06-17 19:49:52 +02005724 strm_li(sess) ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005725
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005726 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005727 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005728 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005729 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005730 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005731 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005732 break;
5733 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005734 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005735 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005736 default:
5737 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005738 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005739 break;
5740 }
5741
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005742 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005743 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005744 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005745
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005746 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005747 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005748 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5749 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5750 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005751
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005752 if (conn)
5753 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005754
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005755 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005756 case AF_INET:
5757 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005758 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005759 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005760 break;
5761 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005762 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005763 break;
5764 default:
5765 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005766 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005767 break;
5768 }
5769
Willy Tarreau50943332011-09-02 17:33:05 +02005770 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005771 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005772 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005773 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005774 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5775 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005776 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005777
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005778 conn = objt_conn(sess->si[1].end);
5779 if (conn)
5780 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005781
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005782 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005783 case AF_INET:
5784 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005785 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005786 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005787 break;
5788 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005789 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005790 break;
5791 default:
5792 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005793 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005794 break;
5795 }
5796
5797 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005798 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005799 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005800 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5801 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005802 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005803 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005804
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005805 if (conn)
5806 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005807
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005808 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005809 case AF_INET:
5810 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005811 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005812 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005813 break;
5814 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005815 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005816 break;
5817 default:
5818 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005819 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005820 break;
5821 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005822
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005823 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005824 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005825 sess->task,
5826 sess->task->state,
5827 sess->task->nice, sess->task->calls,
5828 sess->task->expire ?
5829 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5830 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5831 TICKS_TO_MS(1000)) : "<NEVER>",
5832 task_in_rq(sess->task) ? ", running" : "");
5833
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005834 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005835 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005836 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5837
Willy Tarreaueee5b512015-04-03 23:46:31 +02005838 if (sess->txn)
5839 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005840 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s waiting=%d\n",
Willy Tarreaueee5b512015-04-03 23:46:31 +02005841 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
5842 http_msg_state_str(sess->txn->req.msg_state), http_msg_state_str(sess->txn->rsp.msg_state), !LIST_ISEMPTY(&sess->buffer_wait));
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005843
5844 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005845 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005846 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005847 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005848 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005849 obj_type_name(sess->si[0].end),
5850 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005851 sess->si[0].exp ?
5852 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5853 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5854 TICKS_TO_MS(1000)) : "<NEVER>",
5855 sess->si[0].err_type);
5856
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005857 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005858 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005859 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005860 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005861 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005862 obj_type_name(sess->si[1].end),
5863 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005864 sess->si[1].exp ?
5865 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5866 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5867 TICKS_TO_MS(1000)) : "<NEVER>",
5868 sess->si[1].err_type);
5869
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005870 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005871 chunk_appendf(&trash,
5872 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005873 conn,
5874 get_conn_ctrl_name(conn),
5875 get_conn_xprt_name(conn),
5876 get_conn_data_name(conn),
5877 obj_type_name(conn->target),
5878 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005879
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005880 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005881 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005882 conn->flags,
5883 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005884 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005885 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005886 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005887 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005888 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5889 chunk_appendf(&trash,
5890 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5891 tmpctx,
5892 tmpctx->st0,
5893 tmpctx->st1,
5894 tmpctx->st2,
5895 tmpctx->applet->name);
5896 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005897
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005898 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005899 chunk_appendf(&trash,
5900 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005901 conn,
5902 get_conn_ctrl_name(conn),
5903 get_conn_xprt_name(conn),
5904 get_conn_data_name(conn),
5905 obj_type_name(conn->target),
5906 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005907
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005908 chunk_appendf(&trash,
Willy Tarreauceeafb52016-01-25 15:27:17 +01005909 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005910 conn->flags,
5911 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005912 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005913 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005914 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005915 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005916 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
5917 chunk_appendf(&trash,
5918 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
5919 tmpctx,
5920 tmpctx->st0,
5921 tmpctx->st1,
5922 tmpctx->st2,
5923 tmpctx->applet->name);
5924 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005925
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005926 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005927 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005928 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005929 &sess->req,
5930 sess->req.flags, sess->req.analysers,
5931 sess->req.pipe ? sess->req.pipe->data : 0,
5932 sess->req.to_forward, sess->req.total,
5933 sess->req.analyse_exp ?
5934 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005935 TICKS_TO_MS(1000)) : "<NEVER>");
5936
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005937 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005938 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005939 sess->req.rex ?
5940 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005941 TICKS_TO_MS(1000)) : "<NEVER>");
5942
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005943 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005944 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005945 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005946 sess->req.wex ?
5947 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005948 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005949 sess->req.buf,
5950 sess->req.buf->data, sess->req.buf->o,
5951 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005952 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005953 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005954
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005955 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005956 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005957 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005958 &sess->res,
5959 sess->res.flags, sess->res.analysers,
5960 sess->res.pipe ? sess->res.pipe->data : 0,
5961 sess->res.to_forward, sess->res.total,
5962 sess->res.analyse_exp ?
5963 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005964 TICKS_TO_MS(1000)) : "<NEVER>");
5965
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005966 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005967 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005968 sess->res.rex ?
5969 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005970 TICKS_TO_MS(1000)) : "<NEVER>");
5971
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005972 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005973 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005974 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005975 sess->res.wex ?
5976 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005977 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005978 sess->res.buf,
5979 sess->res.buf->data, sess->res.buf->o,
5980 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005981 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005982 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005983
Willy Tarreaubc18da12015-03-13 14:00:47 +01005984 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005985 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005986 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005987 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005988
5989 /* use other states to dump the contents */
5990 }
5991 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005992 appctx->ctx.sess.uid = 0;
5993 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005994 return 1;
5995}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005996
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02005997#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
5998static int stats_tlskeys_list(struct stream_interface *si) {
5999 struct appctx *appctx = __objt_appctx(si->end);
6000
6001 switch (appctx->st2) {
6002 case STAT_ST_INIT:
6003 /* Display the column headers. If the message cannot be sent,
6004 * quit the fucntion with returning 0. The function is called
6005 * later and restart at the state "STAT_ST_INIT".
6006 */
6007 chunk_reset(&trash);
6008 chunk_appendf(&trash, "# id (file)\n");
6009 if (bi_putchk(si_ic(si), &trash) == -1) {
6010 si_applet_cant_put(si);
6011 return 0;
6012 }
6013
6014 /* Now, we start the browsing of the references lists.
6015 * Note that the following call to LIST_ELEM return bad pointer. The only
6016 * avalaible field of this pointer is <list>. It is used with the function
6017 * tlskeys_list_get_next() for retruning the first avalaible entry
6018 */
6019 appctx->ctx.tlskeys.ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
6020 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
6021
6022 appctx->st2 = STAT_ST_LIST;
6023 /* fall through */
6024
6025 case STAT_ST_LIST:
6026 while (appctx->ctx.tlskeys.ref) {
6027 chunk_reset(&trash);
6028
6029 chunk_appendf(&trash, "%d (%s)\n", appctx->ctx.tlskeys.ref->unique_id,
6030 appctx->ctx.tlskeys.ref->filename);
6031
6032 if (bi_putchk(si_ic(si), &trash) == -1) {
6033 /* let's try again later from this stream. We add ourselves into
6034 * this stream's users so that it can remove us upon termination.
6035 */
6036 si_applet_cant_put(si);
6037 return 0;
6038 }
6039
6040 /* get next list entry and check the end of the list */
6041 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
6042 }
6043
6044 appctx->st2 = STAT_ST_FIN;
6045 /* fall through */
6046
6047 default:
6048 appctx->st2 = STAT_ST_FIN;
6049 return 1;
6050 }
6051 return 0;
6052}
6053#endif
6054
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006055static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006056{
6057 struct appctx *appctx = __objt_appctx(si->end);
6058
6059 switch (appctx->st2) {
6060 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006061 /* Display the column headers. If the message cannot be sent,
6062 * quit the fucntion with returning 0. The function is called
6063 * later and restart at the state "STAT_ST_INIT".
6064 */
6065 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01006066 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006067 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006068 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006069 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006070 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006071
6072 /* Now, we start the browsing of the references lists.
6073 * Note that the following call to LIST_ELEM return bad pointer. The only
6074 * avalaible field of this pointer is <list>. It is used with the function
6075 * pat_list_get_next() for retruning the first avalaible entry
6076 */
6077 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
6078 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6079 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006080 appctx->st2 = STAT_ST_LIST;
6081 /* fall through */
6082
6083 case STAT_ST_LIST:
6084 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006085 chunk_reset(&trash);
6086
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006087 /* Build messages. If the reference is used by another category than
6088 * the listed categorie, display the information in the massage.
6089 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01006090 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01006091 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
6092 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006093
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006094 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006095 /* let's try again later from this stream. We add ourselves into
6096 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006097 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006098 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006099 return 0;
6100 }
6101
6102 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006103 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6104 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006105 }
6106
6107 appctx->st2 = STAT_ST_FIN;
6108 /* fall through */
6109
6110 default:
6111 appctx->st2 = STAT_ST_FIN;
6112 return 1;
6113 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006114 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006115}
6116
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006117static int stats_map_lookup(struct stream_interface *si)
6118{
6119 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006120 struct sample sample;
6121 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006122 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006123
6124 switch (appctx->st2) {
6125 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006126 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01006127 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006128 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006129 appctx->st2 = STAT_ST_LIST;
6130 /* fall through */
6131
6132 case STAT_ST_LIST:
6133 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006134 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006135 /* initialise chunk to build new message */
6136 chunk_reset(&trash);
6137
6138 /* execute pattern matching */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006139 sample.data.type = SMP_T_STR;
Andreas Seltenreich9727cf42016-03-03 19:32:25 +01006140 sample.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006141 sample.data.u.str.len = appctx->ctx.map.chunk.len;
6142 sample.data.u.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01006143 if (appctx->ctx.map.expr->pat_head->match &&
6144 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006145 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
6146 else
6147 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006148
6149 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006150 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
6151 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
6152 break;
6153 if (match_method >= PAT_MATCH_NUM)
6154 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
6155 else
6156 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006157
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006158 /* case sensitive */
6159 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
6160 chunk_appendf(&trash, ", case=insensitive");
6161 else
6162 chunk_appendf(&trash, ", case=sensitive");
6163
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006164 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006165 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006166 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6167 chunk_appendf(&trash, ", found=no");
6168 else
6169 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006170 }
6171
6172 /* Display match and match info */
6173 else {
6174 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006175 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6176 chunk_appendf(&trash, ", found=yes");
6177 else
6178 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006179
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006180 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006181 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006182 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006183 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006184 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006185
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006186 /* display pattern */
6187 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
6188 if (pat->ref && pat->ref->pattern)
6189 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
6190 else
6191 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006192 }
6193 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006194 if (pat->ref && pat->ref->pattern)
6195 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
6196 else
6197 chunk_appendf(&trash, ", pattern=unknown");
6198 }
6199
6200 /* display return value */
6201 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02006202 if (pat->data && pat->ref && pat->ref->sample)
Thierry FOURNIER03d0e452015-07-23 18:33:41 +02006203 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
Thierry FOURNIER7d4335c2015-08-19 09:05:25 +02006204 smp_to_type[pat->data->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006205 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006206 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006207 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006208 }
6209
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006210 chunk_appendf(&trash, "\n");
6211
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006212 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006213 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006214 /* let's try again later from this stream. We add ourselves into
6215 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006216 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006217 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006218 return 0;
6219 }
6220
6221 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006222 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
6223 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006224 }
6225
6226 appctx->st2 = STAT_ST_FIN;
6227 /* fall through */
6228
6229 default:
6230 appctx->st2 = STAT_ST_FIN;
6231 free(appctx->ctx.map.chunk.str);
6232 return 1;
6233 }
6234}
6235
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006236static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006237{
6238 struct appctx *appctx = __objt_appctx(si->end);
6239
6240 switch (appctx->st2) {
6241
6242 case STAT_ST_INIT:
6243 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006244 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
6245 struct pat_ref_elt *, list);
6246 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
6247 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006248 appctx->st2 = STAT_ST_LIST;
6249 /* fall through */
6250
6251 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006252 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006253 chunk_reset(&trash);
6254
6255 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006256 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006257 chunk_appendf(&trash, "%p %s %s\n",
6258 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
6259 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006260 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006261 chunk_appendf(&trash, "%p %s\n",
6262 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006263
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006264 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006265 /* let's try again later from this stream. We add ourselves into
6266 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006267 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006268 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006269 return 0;
6270 }
6271
6272 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006273 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
6274 struct pat_ref_elt *, list);
6275 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006276 break;
6277 }
6278
6279 appctx->st2 = STAT_ST_FIN;
6280 /* fall through */
6281
6282 default:
6283 appctx->st2 = STAT_ST_FIN;
6284 return 1;
6285 }
6286}
6287
Willy Tarreau87b09662015-04-03 00:22:06 +02006288/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006289 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006290 * to be called again, otherwise non-zero. It is designed to be called
6291 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006292 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006293static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006294{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006295 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006296 struct connection *conn;
6297
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006298 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006299 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02006300 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006301 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006302 if (appctx->st2 == STAT_ST_LIST) {
6303 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6304 LIST_DEL(&appctx->ctx.sess.bref.users);
6305 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006306 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006307 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006308 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006309 }
6310
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006311 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006312
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006313 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006314 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006315 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02006316 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006317 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02006318 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006319 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02006320 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006321 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006322 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02006323 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006324 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006325 /* fall through */
6326
Willy Tarreau295a8372011-03-10 11:25:07 +01006327 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02006328 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006329 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6330 LIST_DEL(&appctx->ctx.sess.bref.users);
6331 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006332 }
6333
6334 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02006335 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01006336 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02006337 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006338
Willy Tarreau87b09662015-04-03 00:22:06 +02006339 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006340
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006341 if (appctx->ctx.sess.target) {
6342 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006343 goto next_sess;
6344
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006345 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006346 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01006347 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006348 return 0;
6349
Willy Tarreau87b09662015-04-03 00:22:06 +02006350 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006351 LIST_DEL(&appctx->ctx.sess.bref.users);
6352 LIST_INIT(&appctx->ctx.sess.bref.users);
6353 if (appctx->ctx.sess.target != (void *)-1) {
6354 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01006355 break;
6356 }
6357 else
6358 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006359 }
6360
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006361 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006362 "%p: proto=%s",
6363 curr_sess,
Willy Tarreauf1e02122015-09-23 12:16:43 +02006364 strm_li(curr_sess) ? strm_li(curr_sess)->proto->name : "?");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006365
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006366 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006367 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02006368 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006369 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006370 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006371 " src=%s:%d fe=%s be=%s srv=%s",
6372 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006373 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006374 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006375 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006376 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006377 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006378 break;
6379 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006380 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02006381 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006382 strm_li(curr_sess)->luid,
6383 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006384 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006385 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02006386 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006387 break;
6388 }
6389
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006390 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02006391 " ts=%02x age=%s calls=%d",
6392 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01006393 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
6394 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006395
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006396 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006397 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006398 curr_sess->req.flags,
6399 curr_sess->req.buf->i,
6400 curr_sess->req.analysers,
6401 curr_sess->req.rex ?
6402 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006403 TICKS_TO_MS(1000)) : "");
6404
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006405 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006406 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006407 curr_sess->req.wex ?
6408 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006409 TICKS_TO_MS(1000)) : "");
6410
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006411 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006412 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006413 curr_sess->req.analyse_exp ?
6414 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006415 TICKS_TO_MS(1000)) : "");
6416
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006417 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006418 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006419 curr_sess->res.flags,
6420 curr_sess->res.buf->i,
6421 curr_sess->res.analysers,
6422 curr_sess->res.rex ?
6423 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006424 TICKS_TO_MS(1000)) : "");
6425
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006426 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006427 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006428 curr_sess->res.wex ?
6429 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006430 TICKS_TO_MS(1000)) : "");
6431
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006432 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006433 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006434 curr_sess->res.analyse_exp ?
6435 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006436 TICKS_TO_MS(1000)) : "");
6437
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006438 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006439 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006440 " s0=[%d,%1xh,fd=%d,ex=%s]",
6441 curr_sess->si[0].state,
6442 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006443 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006444 curr_sess->si[0].exp ?
6445 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
6446 TICKS_TO_MS(1000)) : "");
6447
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006448 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006449 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006450 " s1=[%d,%1xh,fd=%d,ex=%s]",
6451 curr_sess->si[1].state,
6452 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006453 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006454 curr_sess->si[1].exp ?
6455 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
6456 TICKS_TO_MS(1000)) : "");
6457
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006458 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006459 " exp=%s",
6460 curr_sess->task->expire ?
6461 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
6462 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01006463 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006464 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006465
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006466 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006467
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006468 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006469 /* let's try again later from this stream. We add ourselves into
6470 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006471 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006472 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006473 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006474 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006475 }
6476
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006477 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006478 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006479 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006480
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006481 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006482 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006483 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006484 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006485 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006486 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006487
Willy Tarreaubc18da12015-03-13 14:00:47 +01006488 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006489 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006490 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006491 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006492
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006493 appctx->ctx.sess.target = NULL;
6494 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006495 return 1;
6496 }
6497
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006498 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006499 /* fall through */
6500
6501 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006502 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006503 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006504 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02006505}
6506
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006507/* This is called when the stream interface is closed. For instance, upon an
6508 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02006509 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006510 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02006511static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006512{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006513 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
6514 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
6515 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006516 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006517 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
6518 free(appctx->ctx.cli.err);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006519 appctx->ctx.cli.err = NULL;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006520 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006521 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
6522 free(appctx->ctx.map.chunk.str);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006523 appctx->ctx.map.chunk.str = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006524 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006525}
6526
Willy Tarreau20e99322013-04-13 09:22:25 +02006527/* This function is used to either dump tables states (when action is set
6528 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02006529 * It returns 0 if the output buffer is full and it needs to be called
6530 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02006531 */
Willy Tarreau20e99322013-04-13 09:22:25 +02006532static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02006533{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006534 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02006535 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006536 struct ebmb_node *eb;
6537 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01006538 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02006539 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006540
6541 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006542 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01006543 * - STAT_ST_INIT : the first call
6544 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02006545 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006546 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02006547 * and the entry pointer points to the next entry to be dumped,
6548 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006549 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02006550 * data though.
6551 */
6552
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006553 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006554 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006555 if (appctx->st2 == STAT_ST_LIST) {
6556 appctx->ctx.table.entry->ref_cnt--;
6557 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02006558 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02006559 return 1;
6560 }
6561
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006562 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006563
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006564 while (appctx->st2 != STAT_ST_FIN) {
6565 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006566 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006567 appctx->ctx.table.proxy = appctx->ctx.table.target;
6568 if (!appctx->ctx.table.proxy)
6569 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006570
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006571 appctx->ctx.table.entry = NULL;
6572 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006573 break;
6574
Willy Tarreau295a8372011-03-10 11:25:07 +01006575 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006576 if (!appctx->ctx.table.proxy ||
6577 (appctx->ctx.table.target &&
6578 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6579 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006580 break;
6581 }
6582
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006583 if (appctx->ctx.table.proxy->table.size) {
6584 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6585 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006586 return 0;
6587
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006588 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006589 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006590 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006591 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006592 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006593 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6594 appctx->ctx.table.entry->ref_cnt++;
6595 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006596 break;
6597 }
6598 }
6599 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006600 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006601 break;
6602
Willy Tarreau295a8372011-03-10 11:25:07 +01006603 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006604 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006605
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006606 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006607 /* we're filtering on some data contents */
6608 void *ptr;
6609 long long data;
6610
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006611 dt = appctx->ctx.table.data_type;
6612 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6613 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006614 dt);
6615
6616 data = 0;
6617 switch (stktable_data_types[dt].std_type) {
6618 case STD_T_SINT:
6619 data = stktable_data_cast(ptr, std_t_sint);
6620 break;
6621 case STD_T_UINT:
6622 data = stktable_data_cast(ptr, std_t_uint);
6623 break;
6624 case STD_T_ULL:
6625 data = stktable_data_cast(ptr, std_t_ull);
6626 break;
6627 case STD_T_FRQP:
6628 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006629 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006630 break;
6631 }
6632
6633 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006634 if ((data < appctx->ctx.table.value &&
6635 (appctx->ctx.table.data_op == STD_OP_EQ ||
6636 appctx->ctx.table.data_op == STD_OP_GT ||
6637 appctx->ctx.table.data_op == STD_OP_GE)) ||
6638 (data == appctx->ctx.table.value &&
6639 (appctx->ctx.table.data_op == STD_OP_NE ||
6640 appctx->ctx.table.data_op == STD_OP_GT ||
6641 appctx->ctx.table.data_op == STD_OP_LT)) ||
6642 (data > appctx->ctx.table.value &&
6643 (appctx->ctx.table.data_op == STD_OP_EQ ||
6644 appctx->ctx.table.data_op == STD_OP_LT ||
6645 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006646 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006647 }
6648
Simon Hormanc88b8872011-06-15 15:18:49 +09006649 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006650 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6651 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006652 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006653
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006654 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006655
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006656 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006657 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006658 struct stksess *old = appctx->ctx.table.entry;
6659 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006660 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006661 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6662 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6663 stksess_kill(&appctx->ctx.table.proxy->table, old);
6664 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006665 break;
6666 }
6667
Simon Hormanc88b8872011-06-15 15:18:49 +09006668
6669 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006670 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6671 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6672 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006673
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006674 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6675 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006676 break;
6677
Willy Tarreau295a8372011-03-10 11:25:07 +01006678 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006679 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006680 break;
6681 }
6682 }
6683 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006684}
6685
Willy Tarreaud426a182010-03-05 14:58:26 +01006686/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006687 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6688 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6689 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6690 * lines are respected within the limit of 70 output chars. Lines that are
6691 * continuation of a previous truncated line begin with "+" instead of " "
6692 * after the offset. The new pointer is returned.
6693 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006694static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6695 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006696{
6697 int end;
6698 unsigned char c;
6699
6700 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006701 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006702 return ptr;
6703
Willy Tarreau77804732012-10-29 16:14:26 +01006704 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006705
Willy Tarreaud426a182010-03-05 14:58:26 +01006706 while (ptr < len && ptr < bsize) {
6707 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006708 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006709 if (out->len > end - 2)
6710 break;
6711 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006712 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006713 if (out->len > end - 3)
6714 break;
6715 out->str[out->len++] = '\\';
6716 switch (c) {
6717 case '\t': c = 't'; break;
6718 case '\n': c = 'n'; break;
6719 case '\r': c = 'r'; break;
6720 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006721 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006722 }
6723 out->str[out->len++] = c;
6724 } else {
6725 if (out->len > end - 5)
6726 break;
6727 out->str[out->len++] = '\\';
6728 out->str[out->len++] = 'x';
6729 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6730 out->str[out->len++] = hextab[c & 0xF];
6731 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006732 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006733 /* we had a line break, let's return now */
6734 out->str[out->len++] = '\n';
6735 *line = ptr;
6736 return ptr;
6737 }
6738 }
6739 /* we have an incomplete line, we return it as-is */
6740 out->str[out->len++] = '\n';
6741 return ptr;
6742}
6743
Baptiste Assmann3863f972015-05-17 00:33:24 +02006744/* This function dumps counters from all resolvers section and associated name servers.
6745 * It returns 0 if the output buffer is full and it needs
6746 * to be called again, otherwise non-zero.
6747 */
6748static int stats_dump_resolvers_to_buffer(struct stream_interface *si)
6749{
6750 struct appctx *appctx = __objt_appctx(si->end);
6751 struct dns_resolvers *presolvers;
6752 struct dns_nameserver *pnameserver;
6753
6754 chunk_reset(&trash);
6755
6756 switch (appctx->st2) {
6757 case STAT_ST_INIT:
6758 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
6759 /* fall through */
6760
6761 case STAT_ST_LIST:
Andrew Hayworth68d05342015-10-02 20:33:01 +00006762 if (LIST_ISEMPTY(&dns_resolvers)) {
6763 chunk_appendf(&trash, "No resolvers found\n");
6764 }
6765 else {
6766 list_for_each_entry(presolvers, &dns_resolvers, list) {
6767 if (appctx->ctx.resolvers.ptr != NULL && appctx->ctx.resolvers.ptr != presolvers)
6768 continue;
6769
6770 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
6771 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
6772 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
6773 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
6774 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
6775 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
6776 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
6777 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
6778 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
6779 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
6780 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
6781 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
6782 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
6783 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
6784 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
6785 chunk_appendf(&trash, " truncated: %ld\n", pnameserver->counters.truncated);
6786 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
6787 }
6788 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02006789 }
6790
6791 /* display response */
6792 if (bi_putchk(si_ic(si), &trash) == -1) {
6793 /* let's try again later from this session. We add ourselves into
6794 * this session's users so that it can remove us upon termination.
6795 */
6796 si->flags |= SI_FL_WAIT_ROOM;
6797 return 0;
6798 }
6799
6800 appctx->st2 = STAT_ST_FIN;
6801 /* fall through */
6802
6803 default:
6804 appctx->st2 = STAT_ST_FIN;
6805 return 1;
6806 }
6807}
6808
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006809/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006810 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006811 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006812 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006813static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006814{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006815 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006816 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006817
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006818 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02006819 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006820
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006821 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006822
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006823 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006824 /* the function had not been called yet, let's prepare the
6825 * buffer for a response.
6826 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006827 struct tm tm;
6828
6829 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006830 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006831 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6832 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6833 error_snapshot_id);
6834
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006835 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01006836 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006837 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01006838 return 0;
6839 }
6840
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006841 appctx->ctx.errors.px = proxy;
6842 appctx->ctx.errors.buf = 0;
6843 appctx->ctx.errors.bol = 0;
6844 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006845 }
6846
6847 /* we have two inner loops here, one for the proxy, the other one for
6848 * the buffer.
6849 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006850 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006851 struct error_snapshot *es;
6852
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006853 if (appctx->ctx.errors.buf == 0)
6854 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006855 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006856 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006857
6858 if (!es->when.tv_sec)
6859 goto next;
6860
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006861 if (appctx->ctx.errors.iid >= 0 &&
6862 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6863 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006864 goto next;
6865
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006866 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006867 /* just print headers now */
6868
6869 char pn[INET6_ADDRSTRLEN];
6870 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006871 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006872
6873 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006874 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006875 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006876 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006877
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006878 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6879 case AF_INET:
6880 case AF_INET6:
6881 port = get_host_port(&es->src);
6882 break;
6883 default:
6884 port = 0;
6885 }
6886
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006887 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006888 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006889 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006890 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006891 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006892 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006893 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
6894 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006895 break;
6896 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006897 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02006898 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006899 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006900 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006901 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006902 break;
6903 }
6904
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006905 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006906 ", server %s (#%d), event #%u\n"
6907 " src %s:%d, session #%d, session flags 0x%08x\n"
6908 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
6909 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
6910 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
6911 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
6912 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
6913 es->ev_id,
6914 pn, port, es->sid, es->s_flags,
6915 es->state, es->m_flags, es->t_flags,
6916 es->m_clen, es->m_blen,
6917 es->b_flags, es->b_out, es->b_tot,
6918 es->len, es->b_wrap, es->pos);
6919
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006920 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006921 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006922 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006923 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006924 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006925 appctx->ctx.errors.ptr = 0;
6926 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006927 }
6928
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006929 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006930 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006931 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006932 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006933 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006934 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006935 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006936 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01006937 goto next;
6938 }
6939
6940 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006941 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006942 int newptr;
6943 int newline;
6944
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006945 newline = appctx->ctx.errors.bol;
6946 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
6947 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02006948 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006949
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006950 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006951 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006952 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006953 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006954 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006955 appctx->ctx.errors.ptr = newptr;
6956 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006957 };
6958 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006959 appctx->ctx.errors.bol = 0;
6960 appctx->ctx.errors.ptr = -1;
6961 appctx->ctx.errors.buf++;
6962 if (appctx->ctx.errors.buf > 1) {
6963 appctx->ctx.errors.buf = 0;
6964 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006965 }
Willy Tarreauae795722016-02-16 11:27:28 +01006966 }
6967
6968 /* dump complete */
6969 return 1;
6970}
6971
6972/* This function dumps all environmnent variables to the buffer. It returns 0
6973 * if the output buffer is full and it needs to be called again, otherwise
6974 * non-zero. Dumps only one entry if st2 == STAT_ST_END.
6975 */
6976static int stats_dump_env_to_buffer(struct stream_interface *si)
6977{
6978 struct appctx *appctx = __objt_appctx(si->end);
6979
6980 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
6981 return 1;
6982
6983 chunk_reset(&trash);
6984
6985 /* we have two inner loops here, one for the proxy, the other one for
6986 * the buffer.
6987 */
6988 while (*appctx->ctx.env.var) {
6989 chunk_printf(&trash, "%s\n", *appctx->ctx.env.var);
6990
6991 if (bi_putchk(si_ic(si), &trash) == -1) {
6992 si_applet_cant_put(si);
6993 return 0;
6994 }
6995 if (appctx->st2 == STAT_ST_END)
6996 break;
6997 appctx->ctx.env.var++;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006998 }
6999
7000 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02007001 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007002}
7003
Willy Tarreaud5781202012-09-22 19:32:35 +02007004/* parse the "level" argument on the bind lines */
7005static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7006{
7007 if (!*args[cur_arg + 1]) {
7008 memprintf(err, "'%s' : missing level", args[cur_arg]);
7009 return ERR_ALERT | ERR_FATAL;
7010 }
7011
7012 if (!strcmp(args[cur_arg+1], "user"))
7013 conf->level = ACCESS_LVL_USER;
7014 else if (!strcmp(args[cur_arg+1], "operator"))
7015 conf->level = ACCESS_LVL_OPER;
7016 else if (!strcmp(args[cur_arg+1], "admin"))
7017 conf->level = ACCESS_LVL_ADMIN;
7018 else {
7019 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
7020 args[cur_arg], args[cur_arg+1]);
7021 return ERR_ALERT | ERR_FATAL;
7022 }
7023
7024 return 0;
7025}
7026
Willy Tarreau30576452015-04-13 13:50:30 +02007027struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007028 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007029 .name = "<STATS>", /* used for logging */
7030 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07007031 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007032};
7033
Willy Tarreau30576452015-04-13 13:50:30 +02007034static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007035 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007036 .name = "<CLI>", /* used for logging */
7037 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01007038 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007039};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01007040
Willy Tarreaudc13c112013-06-21 23:16:39 +02007041static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02007042 { CFG_GLOBAL, "stats", stats_parse_global },
7043 { 0, NULL, NULL },
7044}};
7045
Willy Tarreaud5781202012-09-22 19:32:35 +02007046static struct bind_kw_list bind_kws = { "STAT", { }, {
7047 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
7048 { NULL, NULL, 0 },
7049}};
7050
Willy Tarreau10522fd2008-07-09 20:12:41 +02007051__attribute__((constructor))
7052static void __dumpstats_module_init(void)
7053{
7054 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02007055 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02007056}
7057
Willy Tarreau91861262007-10-17 17:06:05 +02007058/*
7059 * Local variables:
7060 * c-indent-level: 8
7061 * c-basic-offset: 8
7062 * End:
7063 */