blob: 45290f6578b04501a3cec467f54434d135b980a3 [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
Willy Tarreau82a86022016-01-04 19:04:18 +0100399/* one line of stats */
400static struct field stats[ST_F_TOTAL_FIELDS];
401
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200402static int stats_dump_backend_to_buffer(struct stream_interface *si);
Willy Tarreauae795722016-02-16 11:27:28 +0100403static int stats_dump_env_to_buffer(struct stream_interface *si);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100404static int stats_dump_info_to_buffer(struct stream_interface *si);
Baptiste Assmann28289462015-07-03 08:01:20 +0200405static int stats_dump_servers_state_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100406static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau87b09662015-04-03 00:22:06 +0200407static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900408static int stats_dump_sess_to_buffer(struct stream_interface *si);
409static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100410static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100411static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
412static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Baptiste Assmann3863f972015-05-17 00:33:24 +0200413static int stats_dump_resolvers_to_buffer(struct stream_interface *si);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100414static int stats_pats_list(struct stream_interface *si);
415static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100416static int stats_map_lookup(struct stream_interface *si);
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200417#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
418static int stats_tlskeys_list(struct stream_interface *si);
419#endif
Willy Tarreau00a37f02015-04-13 12:05:19 +0200420static void cli_release_handler(struct appctx *appctx);
Simon Horman9bd2c732011-06-15 15:18:44 +0900421
Baptiste Assmann28289462015-07-03 08:01:20 +0200422static void dump_servers_state(struct proxy *backend, struct chunk *buf);
423
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100424/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100425 * cli_io_handler()
426 * -> stats_dump_sess_to_buffer() // "show sess"
427 * -> stats_dump_errors_to_buffer() // "show errors"
428 * -> stats_dump_info_to_buffer() // "show info"
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200429 * -> stats_dump_backend_to_buffer() // "show backend"
Baptiste Assmann28289462015-07-03 08:01:20 +0200430 * -> stats_dump_servers_state_to_buffer() // "show servers state [<backend name>]"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100431 * -> stats_dump_stat_to_buffer() // "show stat"
Baptiste Assmann3863f972015-05-17 00:33:24 +0200432 * -> stats_dump_resolvers_to_buffer() // "show stat resolver <id>"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100433 * -> stats_dump_csv_header()
434 * -> stats_dump_proxy_to_buffer()
435 * -> stats_dump_fe_stats()
436 * -> stats_dump_li_stats()
437 * -> stats_dump_sv_stats()
438 * -> stats_dump_be_stats()
439 *
440 * http_stats_io_handler()
441 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
442 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
443 * -> stats_dump_html_head() // emits the HTML headers
444 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
445 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
446 * -> stats_dump_html_px_hdr()
447 * -> stats_dump_fe_stats()
448 * -> stats_dump_li_stats()
449 * -> stats_dump_sv_stats()
450 * -> stats_dump_be_stats()
451 * -> stats_dump_html_px_end()
452 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100453 */
454
Willy Tarreau30576452015-04-13 13:50:30 +0200455static struct applet cli_applet;
Simon Horman9bd2c732011-06-15 15:18:44 +0900456
457static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200458 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200459 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200460 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200461 " help : this message\n"
462 " prompt : toggle interactive mode with prompt\n"
463 " quit : disconnect\n"
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200464 " show backend : list backends in the current running config\n"
Willy Tarreauae795722016-02-16 11:27:28 +0100465 " show env [var] : dump environment variables known to the process\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200466 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100467 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200468 " show stat : report counters for each proxy and server\n"
469 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100470 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200471 " show table [id]: report table usage stats or dump this table's contents\n"
Baptiste Assmann28289462015-07-03 08:01:20 +0200472 " show servers state [id]: dump volatile server information (for backend <id>)\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200473 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200474 " set weight : change a server's weight\n"
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200475 " set server : change a server's state, weight or address\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200476 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100477 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200478 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200479 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200480 " disable : put a server or frontend in maintenance mode\n"
481 " enable : re-enable a server or frontend which is in maintenance mode\n"
482 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100483 " show acl [id] : report avalaible acls or dump an acl's contents\n"
484 " get acl : reports the patterns matching a sample for an ACL\n"
485 " add acl : add acl entry\n"
486 " del acl : delete acl entry\n"
487 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100488 " show map [id] : report avalaible maps or dump a map's contents\n"
489 " get map : reports the keys and values matching a sample for a map\n"
490 " set map : modify map entry\n"
491 " add map : add map entry\n"
492 " del map : delete map entry\n"
493 " clear map <id> : clear the content of this map\n"
Emeric Brun4147b2e2014-06-16 18:36:30 +0200494 " set ssl <stmt> : set statement for ssl\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200495 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200496
Simon Horman9bd2c732011-06-15 15:18:44 +0900497static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200498 "Permission denied\n"
499 "";
500
Willy Tarreau295a8372011-03-10 11:25:07 +0100501/* data transmission states for the stats responses */
502enum {
503 STAT_ST_INIT = 0,
504 STAT_ST_HEAD,
505 STAT_ST_INFO,
506 STAT_ST_LIST,
507 STAT_ST_END,
508 STAT_ST_FIN,
509};
510
511/* data transmission states for the stats responses inside a proxy */
512enum {
513 STAT_PX_ST_INIT = 0,
514 STAT_PX_ST_TH,
515 STAT_PX_ST_FE,
516 STAT_PX_ST_LI,
517 STAT_PX_ST_SV,
518 STAT_PX_ST_BE,
519 STAT_PX_ST_END,
520 STAT_PX_ST_FIN,
521};
522
Cyril Bonté19979e12012-04-04 12:57:21 +0200523extern const char *stat_status_codes[];
524
Willy Tarreau07e9e642010-08-17 21:48:17 +0200525/* allocate a new stats frontend named <name>, and return it
526 * (or NULL in case of lack of memory).
527 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200528static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200529{
530 struct proxy *fe;
531
532 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
533 if (!fe)
534 return NULL;
535
Willy Tarreau237250c2011-07-29 01:49:03 +0200536 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200537 fe->next = proxy;
538 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200539 fe->last_change = now.tv_sec;
540 fe->id = strdup("GLOBAL");
541 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200542 fe->maxconn = 10; /* default to 10 concurrent connections */
543 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200544 fe->conf.file = strdup(file);
545 fe->conf.line = line;
Willy Tarreau32b60d42015-03-13 16:14:57 +0100546 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +0100547 fe->default_target = &cli_applet.obj_type;
Willy Tarreau050536d2012-10-04 08:47:34 +0200548
549 /* the stats frontend is the only one able to assign ID #0 */
550 fe->conf.id.key = fe->uuid = 0;
551 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200552 return fe;
553}
554
Willy Tarreaufbee7132007-10-18 13:53:22 +0200555/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200556 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
557 * error message into the <err> buffer which will be preallocated. The trailing
558 * '\n' must not be written. The function must be called with <args> pointing to
559 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200560 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200561static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200562 struct proxy *defpx, const char *file, int line,
563 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200564{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200565 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200566 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200567
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200568 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200569 int cur_arg;
570
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200571 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200572 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 +0200573 return -1;
574 }
575
Willy Tarreau89a63132009-08-16 17:41:45 +0200576 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200577 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200578 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200579 return -1;
580 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200581 }
582
Willy Tarreau4348fad2012-09-20 16:48:07 +0200583 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200584 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200585
Willy Tarreauc53d4222012-09-20 20:19:28 +0200586 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
587 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
588 file, line, args[0], args[1], err && *err ? *err : "error");
589 return -1;
590 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200591
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200592 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200593 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200594 static int bind_dumped;
595 struct bind_kw *kw;
596
597 kw = bind_find_kw(args[cur_arg]);
598 if (kw) {
599 if (!kw->parse) {
600 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
601 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200602 return -1;
603 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200604
Willy Tarreau061b5de2015-10-13 15:06:57 +0200605 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200606 if (err && *err)
607 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
608 else
609 memprintf(err, "'%s %s' : error encountered while processing '%s'",
610 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200611 return -1;
612 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200613
614 cur_arg += 1 + kw->skip;
615 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200616 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200617
618 if (!bind_dumped) {
619 bind_dump_kws(err);
620 indent_msg(err, 4);
621 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200622 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200623
624 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
625 args[0], args[1], args[cur_arg],
626 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
627 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200628 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100629
Willy Tarreauc53d4222012-09-20 20:19:28 +0200630 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
631 l->maxconn = global.stats_fe->maxconn;
632 l->backlog = global.stats_fe->backlog;
Willy Tarreau9903f0e2015-04-04 18:50:31 +0200633 l->accept = session_accept_fd;
Willy Tarreau87b09662015-04-03 00:22:06 +0200634 l->handler = process_stream;
Willy Tarreau10b688f2015-03-13 16:43:12 +0100635 l->default_target = global.stats_fe->default_target;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200636 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
637 l->nice = -64; /* we want to boost priority for local stats */
638 global.maxsock += l->maxconn;
639 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200640 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200641 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100642 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200643 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100644
645 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200646 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100647 return -1;
648 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200649
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100650 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200651 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200652 return -1;
653 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200654 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200655 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200656 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200657 return -1;
658 }
659 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200660 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200661 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200662 else if (!strcmp(args[1], "maxconn")) {
663 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200664
665 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200666 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200667 return -1;
668 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200669
670 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200671 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200672 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200673 return -1;
674 }
675 }
676 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200677 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200678 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
679 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100680 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200681
Willy Tarreau91319572013-04-20 09:48:50 +0200682 if (!global.stats_fe) {
683 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
684 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
685 return -1;
686 }
687 }
688
Willy Tarreau35b7b162012-10-22 23:17:18 +0200689 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100690 unsigned int low, high;
691
Willy Tarreau35b7b162012-10-22 23:17:18 +0200692 if (strcmp(args[cur_arg], "all") == 0) {
693 set = 0;
694 break;
695 }
696 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100697 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200698 }
699 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100700 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200701 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100702 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100703 char *dash = strchr(args[cur_arg], '-');
704
705 low = high = str2uic(args[cur_arg]);
706 if (dash)
707 high = str2uic(dash + 1);
708
709 if (high < low) {
710 unsigned int swap = low;
711 low = high;
712 high = swap;
713 }
714
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100715 if (low < 1 || high > LONGBITS) {
716 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
717 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200718 return -1;
719 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100720 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100721 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100722 }
723 else {
724 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100725 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
726 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100727 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200728 }
729 cur_arg++;
730 }
731 global.stats_fe->bind_proc = set;
732 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200733 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200734 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200735 return -1;
736 }
737 return 0;
738}
739
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100740/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
741 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100742 * NOTE: Some tools happen to rely on the field position instead of its name,
743 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100744 */
745static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100746{
Willy Tarreauf6142292016-01-04 17:24:29 +0100747 int field;
748
749 chunk_appendf(&trash, "# ");
750 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
751 chunk_appendf(&trash, "%s,", stat_field_names[field]);
752
753 chunk_appendf(&trash, "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100754}
755
Simon Hormand9366582011-06-15 15:18:45 +0900756/* print a string of text buffer to <out>. The format is :
757 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
758 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
759 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
760 */
761static int dump_text(struct chunk *out, const char *buf, int bsize)
762{
763 unsigned char c;
764 int ptr = 0;
765
766 while (buf[ptr] && ptr < bsize) {
767 c = buf[ptr];
768 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
769 if (out->len > out->size - 1)
770 break;
771 out->str[out->len++] = c;
772 }
773 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
774 if (out->len > out->size - 2)
775 break;
776 out->str[out->len++] = '\\';
777 switch (c) {
778 case ' ': c = ' '; break;
779 case '\t': c = 't'; break;
780 case '\n': c = 'n'; break;
781 case '\r': c = 'r'; break;
782 case '\e': c = 'e'; break;
783 case '\\': c = '\\'; break;
784 }
785 out->str[out->len++] = c;
786 }
787 else {
788 if (out->len > out->size - 4)
789 break;
790 out->str[out->len++] = '\\';
791 out->str[out->len++] = 'x';
792 out->str[out->len++] = hextab[(c >> 4) & 0xF];
793 out->str[out->len++] = hextab[c & 0xF];
794 }
795 ptr++;
796 }
797
798 return ptr;
799}
800
801/* print a buffer in hexa.
802 * Print stopped if <bsize> is reached, or if no more place in the chunk.
803 */
804static int dump_binary(struct chunk *out, const char *buf, int bsize)
805{
806 unsigned char c;
807 int ptr = 0;
808
809 while (ptr < bsize) {
810 c = buf[ptr];
811
812 if (out->len > out->size - 2)
813 break;
814 out->str[out->len++] = hextab[(c >> 4) & 0xF];
815 out->str[out->len++] = hextab[c & 0xF];
816
817 ptr++;
818 }
819 return ptr;
820}
821
822/* Dump the status of a table to a stream interface's
823 * read buffer. It returns 0 if the output buffer is full
824 * and needs to be called again, otherwise non-zero.
825 */
826static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
827 struct proxy *proxy, struct proxy *target)
828{
Willy Tarreau87b09662015-04-03 00:22:06 +0200829 struct stream *s = si_strm(si);
Simon Hormand9366582011-06-15 15:18:45 +0900830
Willy Tarreau77804732012-10-29 16:14:26 +0100831 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900832 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
833
834 /* any other information should be dumped here */
835
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200836 if (target && strm_li(s)->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100837 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900838
Willy Tarreaubc18da12015-03-13 14:00:47 +0100839 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200840 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900841 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100842 }
Simon Hormand9366582011-06-15 15:18:45 +0900843
844 return 1;
845}
846
847/* Dump the a table entry to a stream interface's
848 * read buffer. It returns 0 if the output buffer is full
849 * and needs to be called again, otherwise non-zero.
850 */
851static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
852 struct proxy *proxy, struct stksess *entry)
853{
854 int dt;
855
Willy Tarreau77804732012-10-29 16:14:26 +0100856 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900857
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200858 if (proxy->table.type == SMP_T_IPV4) {
Simon Hormand9366582011-06-15 15:18:45 +0900859 char addr[INET_ADDRSTRLEN];
860 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100861 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900862 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200863 else if (proxy->table.type == SMP_T_IPV6) {
Simon Hormand9366582011-06-15 15:18:45 +0900864 char addr[INET6_ADDRSTRLEN];
865 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100866 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900867 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200868 else if (proxy->table.type == SMP_T_SINT) {
Willy Tarreau77804732012-10-29 16:14:26 +0100869 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900870 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200871 else if (proxy->table.type == SMP_T_STR) {
Willy Tarreau77804732012-10-29 16:14:26 +0100872 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900873 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
874 }
875 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100876 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900877 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
878 }
879
Willy Tarreau77804732012-10-29 16:14:26 +0100880 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900881
882 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
883 void *ptr;
884
885 if (proxy->table.data_ofs[dt] == 0)
886 continue;
887 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100888 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900889 else
Willy Tarreau77804732012-10-29 16:14:26 +0100890 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900891
892 ptr = stktable_data_ptr(&proxy->table, entry, dt);
893 switch (stktable_data_types[dt].std_type) {
894 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100895 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900896 break;
897 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100898 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900899 break;
900 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100901 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900902 break;
903 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100904 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900905 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
906 proxy->table.data_arg[dt].u));
907 break;
908 }
909 }
Willy Tarreau77804732012-10-29 16:14:26 +0100910 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900911
Willy Tarreaubc18da12015-03-13 14:00:47 +0100912 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200913 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900914 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100915 }
Simon Hormand9366582011-06-15 15:18:45 +0900916
917 return 1;
918}
919
Willy Tarreaudec98142012-06-06 23:37:08 +0200920static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900921{
Willy Tarreau87b09662015-04-03 00:22:06 +0200922 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100923 struct appctx *appctx = __objt_appctx(si->end);
924 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900925 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900926 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900927 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200928 long long value;
929 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200930 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200931 void *ptr;
932 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900933
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100934 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900935
936 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100937 appctx->ctx.cli.msg = "Key value expected\n";
938 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900939 return;
940 }
941
Simon Hormanc5b89f62011-06-15 15:18:50 +0900942 switch (px->table.type) {
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200943 case SMP_T_IPV4:
Simon Hormancec9a222011-06-15 15:18:51 +0900944 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100945 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900946 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200947 case SMP_T_IPV6:
Simon Hormanc5b89f62011-06-15 15:18:50 +0900948 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100949 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900950 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200951 case SMP_T_SINT:
Simon Hormancec9a222011-06-15 15:18:51 +0900952 {
953 char *endptr;
954 unsigned long val;
955 errno = 0;
956 val = strtoul(args[4], &endptr, 10);
957 if ((errno == ERANGE && val == ULONG_MAX) ||
958 (errno != 0 && val == 0) || endptr == args[4] ||
959 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100960 appctx->ctx.cli.msg = "Invalid key\n";
961 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900962 return;
963 }
964 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100965 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900966 break;
967 }
968 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200969 case SMP_T_STR:
Willy Tarreau07115412012-10-29 21:56:59 +0100970 static_table_key->key = args[4];
971 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900972 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900973 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200974 switch (action) {
975 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100976 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 +0200977 break;
978 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100979 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 +0200980 break;
981 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100982 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200983 break;
984 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100985 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900986 return;
987 }
988
989 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200990 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100991 appctx->ctx.cli.msg = stats_permission_denied_msg;
992 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900993 return;
994 }
995
Willy Tarreau07115412012-10-29 21:56:59 +0100996 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900997
Willy Tarreaudec98142012-06-06 23:37:08 +0200998 switch (action) {
999 case STAT_CLI_O_TAB:
1000 if (!ts)
1001 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001002 chunk_reset(&trash);
1003 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +09001004 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001005 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +09001006 return;
Willy Tarreaudec98142012-06-06 23:37:08 +02001007
1008 case STAT_CLI_O_CLR:
1009 if (!ts)
1010 return;
1011 if (ts->ref_cnt) {
1012 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001013 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
1014 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +02001015 return;
1016 }
1017 stksess_kill(&px->table, ts);
1018 break;
Simon Horman17bce342011-06-15 15:18:47 +09001019
Willy Tarreau654694e2012-06-07 01:03:16 +02001020 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +02001021 if (ts)
1022 stktable_touch(&px->table, ts, 1);
1023 else {
Willy Tarreau07115412012-10-29 21:56:59 +01001024 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +02001025 if (!ts) {
1026 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001027 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
1028 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +02001029 return;
1030 }
1031 stktable_store(&px->table, ts, 1);
1032 }
1033
Willy Tarreau47060b62013-08-01 21:11:42 +02001034 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
1035 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001036 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
1037 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +02001038 return;
1039 }
1040
1041 data_type = stktable_get_data_type(args[cur_arg] + 5);
1042 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001043 appctx->ctx.cli.msg = "Unknown data type\n";
1044 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +02001045 return;
1046 }
1047
1048 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001049 appctx->ctx.cli.msg = "Data type not stored in this table\n";
1050 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +02001051 return;
1052 }
1053
1054 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001055 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
1056 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +02001057 return;
1058 }
1059
1060 ptr = stktable_data_ptr(&px->table, ts, data_type);
1061
1062 switch (stktable_data_types[data_type].std_type) {
1063 case STD_T_SINT:
1064 stktable_data_cast(ptr, std_t_sint) = value;
1065 break;
1066 case STD_T_UINT:
1067 stktable_data_cast(ptr, std_t_uint) = value;
1068 break;
1069 case STD_T_ULL:
1070 stktable_data_cast(ptr, std_t_ull) = value;
1071 break;
1072 case STD_T_FRQP:
1073 /* We set both the current and previous values. That way
1074 * the reported frequency is stable during all the period
1075 * then slowly fades out. This allows external tools to
1076 * push measures without having to update them too often.
1077 */
1078 frqp = &stktable_data_cast(ptr, std_t_frqp);
1079 frqp->curr_tick = now_ms;
1080 frqp->prev_ctr = 0;
1081 frqp->curr_ctr = value;
1082 break;
1083 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001084 }
1085 break;
1086
Willy Tarreaudec98142012-06-06 23:37:08 +02001087 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001088 appctx->ctx.cli.msg = "Unknown action\n";
1089 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +02001090 break;
Simon Horman121f3052011-06-15 15:18:46 +09001091 }
Simon Horman121f3052011-06-15 15:18:46 +09001092}
1093
Willy Tarreau654694e2012-06-07 01:03:16 +02001094static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001095{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001096 struct appctx *appctx = __objt_appctx(si->end);
1097
Willy Tarreau04b3a192013-04-13 09:41:37 +02001098 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001099 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
1100 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +02001101 return;
1102 }
1103
Simon Hormand5b9fd92011-06-15 15:18:48 +09001104 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001105 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
1106 if (appctx->ctx.table.data_type < 0) {
1107 appctx->ctx.cli.msg = "Unknown data type\n";
1108 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001109 return;
1110 }
1111
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001112 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
1113 appctx->ctx.cli.msg = "Data type not stored in this table\n";
1114 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001115 return;
1116 }
1117
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001118 appctx->ctx.table.data_op = get_std_op(args[4]);
1119 if (appctx->ctx.table.data_op < 0) {
1120 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
1121 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001122 return;
1123 }
1124
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001125 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
1126 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
1127 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001128 return;
1129 }
1130}
1131
Willy Tarreaudec98142012-06-06 23:37:08 +02001132static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001133{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001134 struct appctx *appctx = __objt_appctx(si->end);
1135
1136 appctx->ctx.table.data_type = -1;
1137 appctx->st2 = STAT_ST_INIT;
1138 appctx->ctx.table.target = NULL;
1139 appctx->ctx.table.proxy = NULL;
1140 appctx->ctx.table.entry = NULL;
1141 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001142
1143 if (*args[2]) {
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001144 appctx->ctx.table.target = proxy_tbl_by_name(args[2]);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001145 if (!appctx->ctx.table.target) {
1146 appctx->ctx.cli.msg = "No such table\n";
1147 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001148 return;
1149 }
1150 }
1151 else {
Willy Tarreaudec98142012-06-06 23:37:08 +02001152 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001153 goto err_args;
1154 return;
1155 }
1156
1157 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +02001158 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +09001159 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +02001160 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +09001161 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +09001162 goto err_args;
1163
1164 return;
1165
1166err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +02001167 switch (action) {
1168 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001169 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 +02001170 break;
1171 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001172 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 +02001173 break;
1174 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001175 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +02001176 break;
1177 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001178 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001179}
1180
Willy Tarreau532a4502011-09-07 22:37:44 +02001181/* Expects to find a frontend named <arg> and returns it, otherwise displays various
Willy Tarreau87b09662015-04-03 00:22:06 +02001182 * adequate error messages and returns NULL. This function also expects the stream
Willy Tarreau532a4502011-09-07 22:37:44 +02001183 * level to be admin.
1184 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001185static struct proxy *expect_frontend_admin(struct stream *s, struct stream_interface *si, const char *arg)
Willy Tarreau532a4502011-09-07 22:37:44 +02001186{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001187 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +02001188 struct proxy *px;
1189
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001190 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001191 appctx->ctx.cli.msg = stats_permission_denied_msg;
1192 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001193 return NULL;
1194 }
1195
1196 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001197 appctx->ctx.cli.msg = "A frontend name is expected.\n";
1198 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001199 return NULL;
1200 }
1201
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001202 px = proxy_fe_by_name(arg);
Willy Tarreau532a4502011-09-07 22:37:44 +02001203 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001204 appctx->ctx.cli.msg = "No such frontend.\n";
1205 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001206 return NULL;
1207 }
1208 return px;
1209}
1210
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001211/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
1212 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau87b09662015-04-03 00:22:06 +02001213 * and returns NULL. This function also expects the stream level to be admin. Note:
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001214 * the <arg> is modified to remove the '/'.
1215 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001216static struct server *expect_server_admin(struct stream *s, struct stream_interface *si, char *arg)
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001217{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001218 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001219 struct proxy *px;
1220 struct server *sv;
1221 char *line;
1222
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001223 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001224 appctx->ctx.cli.msg = stats_permission_denied_msg;
1225 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001226 return NULL;
1227 }
1228
1229 /* split "backend/server" and make <line> point to server */
1230 for (line = arg; *line; line++)
1231 if (*line == '/') {
1232 *line++ = '\0';
1233 break;
1234 }
1235
1236 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001237 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1238 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001239 return NULL;
1240 }
1241
1242 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001243 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1244 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001245 return NULL;
1246 }
1247
1248 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001249 appctx->ctx.cli.msg = "Proxy is disabled.\n";
1250 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001251 return NULL;
1252 }
1253
1254 return sv;
1255}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001256
1257/* This function is used with TLS ticket keys management. It permits to browse
1258 * each reference. The variable <getnext> must contain the current node,
1259 * <end> point to the root node.
1260 */
1261#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1262static inline
1263struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
1264{
1265 struct tls_keys_ref *ref = getnext;
1266
1267 while (1) {
1268
1269 /* Get next list entry. */
1270 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
1271
1272 /* If the entry is the last of the list, return NULL. */
1273 if (&ref->list == end)
1274 return NULL;
1275
1276 return ref;
1277 }
1278}
1279
1280static inline
1281struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
1282{
1283 int id;
1284 char *error;
1285
1286 /* If the reference starts by a '#', this is numeric id. */
1287 if (reference[0] == '#') {
1288 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1289 id = strtol(reference + 1, &error, 10);
1290 if (*error != '\0')
1291 return NULL;
1292
1293 /* Perform the unique id lookup. */
1294 return tlskeys_ref_lookupid(id);
1295 }
1296
1297 /* Perform the string lookup. */
1298 return tlskeys_ref_lookup(reference);
1299}
1300#endif
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001301
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001302/* This function is used with map and acl management. It permits to browse
1303 * each reference. The variable <getnext> must contain the current node,
1304 * <end> point to the root node and the <flags> permit to filter required
1305 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001306 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001307static inline
1308struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
1309 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001310{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001311 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001312
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001313 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001314
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001315 /* Get next list entry. */
1316 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001317
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001318 /* If the entry is the last of the list, return NULL. */
1319 if (&ref->list == end)
1320 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001321
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001322 /* If the entry match the flag, return it. */
1323 if (ref->flags & flags)
1324 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001325 }
1326}
1327
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001328static inline
1329struct pat_ref *pat_ref_lookup_ref(const char *reference)
1330{
1331 int id;
1332 char *error;
1333
1334 /* If the reference starts by a '#', this is numeric id. */
1335 if (reference[0] == '#') {
1336 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1337 id = strtol(reference + 1, &error, 10);
1338 if (*error != '\0')
1339 return NULL;
1340
1341 /* Perform the unique id lookup. */
1342 return pat_ref_lookupid(id);
1343 }
1344
1345 /* Perform the string lookup. */
1346 return pat_ref_lookup(reference);
1347}
1348
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001349/* This function is used with map and acl management. It permits to browse
1350 * each reference.
1351 */
1352static inline
1353struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1354{
1355 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001356 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1357 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001358 return NULL;
1359 return expr;
1360}
1361
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001362/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001363 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001364 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001365 * designating the function which will have to process the request, which can
1366 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001367 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001368static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001369{
Willy Tarreau87b09662015-04-03 00:22:06 +02001370 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001371 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001372 char *args[MAX_STATS_ARGS + 1];
1373 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001374 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001375
1376 while (isspace((unsigned char)*line))
1377 line++;
1378
1379 arg = 0;
1380 args[arg] = line;
1381
1382 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001383 if (*line == '\\') {
1384 line++;
1385 if (*line == '\0')
1386 break;
1387 }
1388 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001389 *line++ = '\0';
1390
1391 while (isspace((unsigned char)*line))
1392 line++;
1393
1394 args[++arg] = line;
1395 continue;
1396 }
1397
1398 line++;
1399 }
1400
1401 while (++arg <= MAX_STATS_ARGS)
1402 args[arg] = line;
1403
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001404 /* remove \ */
1405 arg = 0;
1406 while (*args[arg] != '\0') {
1407 j = 0;
1408 for (i=0; args[arg][i] != '\0'; i++) {
1409 if (args[arg][i] == '\\')
1410 continue;
1411 args[arg][j] = args[arg][i];
1412 j++;
1413 }
1414 args[arg][j] = '\0';
1415 arg++;
1416 }
1417
Willy Tarreau6bcb95d2015-05-04 18:07:56 +02001418 appctx->ctx.stats.scope_str = 0;
1419 appctx->ctx.stats.scope_len = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001420 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001421 if (strcmp(args[0], "show") == 0) {
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02001422 if (strcmp(args[1], "backend") == 0) {
1423 appctx->st2 = STAT_ST_INIT;
1424 appctx->st0 = STAT_CLI_O_BACKEND;
1425 }
Willy Tarreauae795722016-02-16 11:27:28 +01001426 else if (strcmp(args[1], "env") == 0) {
1427 extern char **environ;
1428
1429 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
1430 appctx->ctx.cli.msg = stats_permission_denied_msg;
1431 appctx->st0 = STAT_CLI_PRINT;
1432 return 1;
1433 }
1434 appctx->ctx.env.var = environ;
1435 appctx->st2 = STAT_ST_INIT;
1436 appctx->st0 = STAT_CLI_O_ENV; // stats_dump_env_to_buffer
1437
1438 if (*args[2]) {
1439 int len = strlen(args[2]);
1440
1441 for (; *appctx->ctx.env.var; appctx->ctx.env.var++) {
1442 if (strncmp(*appctx->ctx.env.var, args[2], len) == 0 &&
1443 (*appctx->ctx.env.var)[len] == '=')
1444 break;
1445 }
1446 if (!*appctx->ctx.env.var) {
1447 appctx->ctx.cli.msg = "Variable not found\n";
1448 appctx->st0 = STAT_CLI_PRINT;
1449 return 1;
1450 }
1451 appctx->st2 = STAT_ST_END;
1452 }
1453 }
1454 else if (strcmp(args[1], "stat") == 0) {
Baptiste Assmann3863f972015-05-17 00:33:24 +02001455 if (strcmp(args[2], "resolvers") == 0) {
1456 struct dns_resolvers *presolvers;
1457
Andrew Hayworth68d05342015-10-02 20:33:01 +00001458 if (*args[3]) {
1459 appctx->ctx.resolvers.ptr = NULL;
1460 list_for_each_entry(presolvers, &dns_resolvers, list) {
1461 if (strcmp(presolvers->id, args[3]) == 0) {
1462 appctx->ctx.resolvers.ptr = presolvers;
1463 break;
1464 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001465 }
Andrew Hayworth68d05342015-10-02 20:33:01 +00001466 if (appctx->ctx.resolvers.ptr == NULL) {
1467 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1468 appctx->st0 = STAT_CLI_PRINT;
1469 return 1;
1470 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001471 }
1472
1473 appctx->st2 = STAT_ST_INIT;
1474 appctx->st0 = STAT_CLI_O_RESOLVERS;
1475 return 1;
1476 }
1477 else if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001478 appctx->ctx.stats.flags |= STAT_BOUND;
1479 appctx->ctx.stats.iid = atoi(args[2]);
1480 appctx->ctx.stats.type = atoi(args[3]);
1481 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001482 }
1483
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001484 appctx->st2 = STAT_ST_INIT;
1485 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001486 }
1487 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001488 appctx->st2 = STAT_ST_INIT;
1489 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001490 }
Baptiste Assmann28289462015-07-03 08:01:20 +02001491 else if (strcmp(args[1], "servers") == 0 && strcmp(args[2], "state") == 0) {
1492 appctx->ctx.server_state.backend = NULL;
1493
1494 /* check if a backend name has been provided */
1495 if (*args[3]) {
1496 /* read server state from local file */
1497 appctx->ctx.server_state.backend = proxy_be_by_name(args[3]);
1498
1499 if (appctx->ctx.server_state.backend == NULL) {
1500 appctx->ctx.cli.msg = "Can't find backend.\n";
1501 appctx->st0 = STAT_CLI_PRINT;
1502 return 1;
1503 }
1504 }
1505 appctx->st2 = STAT_ST_INIT;
1506 appctx->st0 = STAT_CLI_O_SERVERS_STATE; // stats_dump_servers_state_to_buffer
1507 return 1;
1508 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001509 else if (strcmp(args[1], "pools") == 0) {
1510 appctx->st2 = STAT_ST_INIT;
1511 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1512 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001513 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001514 appctx->st2 = STAT_ST_INIT;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001515 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001516 appctx->ctx.cli.msg = stats_permission_denied_msg;
1517 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001518 return 1;
1519 }
Willy Tarreau76153662012-11-26 01:16:39 +01001520 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001521 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001522 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001523 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001524 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001525 appctx->ctx.sess.target = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001526 appctx->ctx.sess.section = 0; /* start with stream status */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001527 appctx->ctx.sess.pos = 0;
1528 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001529 }
1530 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001531 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001532 appctx->ctx.cli.msg = stats_permission_denied_msg;
1533 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001534 return 1;
1535 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001536 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001537 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001538 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001539 appctx->ctx.errors.iid = -1;
1540 appctx->ctx.errors.px = NULL;
1541 appctx->st2 = STAT_ST_INIT;
1542 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001543 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001544 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001545 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001546 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001547 else if (strcmp(args[1], "tls-keys") == 0) {
1548#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1549 appctx->st2 = STAT_ST_INIT;
1550 appctx->st0 = STAT_CLI_O_TLSK;
1551#else
1552 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1553 "that doesn't support specifying TLS ticket keys\n";
1554 appctx->st0 = STAT_CLI_PRINT;
1555#endif
1556 return 1;
1557 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001558 else if (strcmp(args[1], "map") == 0 ||
1559 strcmp(args[1], "acl") == 0) {
1560
1561 /* Set ACL or MAP flags. */
1562 if (args[1][0] == 'm')
1563 appctx->ctx.map.display_flags = PAT_REF_MAP;
1564 else
1565 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001566
1567 /* no parameter: display all map avalaible */
1568 if (!*args[2]) {
1569 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001570 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001571 return 1;
1572 }
1573
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001574 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001575 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001576 if (!appctx->ctx.map.ref ||
1577 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1578 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001579 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001580 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001581 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001582 appctx->st0 = STAT_CLI_PRINT;
1583 return 1;
1584 }
1585 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001586 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001587 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001588 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001589 return 0;
1590 }
1591 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001592 else if (strcmp(args[0], "clear") == 0) {
1593 if (strcmp(args[1], "counters") == 0) {
1594 struct proxy *px;
1595 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001596 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001597 int clrall = 0;
1598
1599 if (strcmp(args[2], "all") == 0)
1600 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001601
Willy Tarreau6162db22009-10-10 17:13:00 +02001602 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001603 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1604 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001605 appctx->ctx.cli.msg = stats_permission_denied_msg;
1606 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001607 return 1;
1608 }
1609
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001610 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001611 if (clrall) {
1612 memset(&px->be_counters, 0, sizeof(px->be_counters));
1613 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1614 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001615 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001616 px->be_counters.conn_max = 0;
1617 px->be_counters.p.http.rps_max = 0;
1618 px->be_counters.sps_max = 0;
1619 px->be_counters.cps_max = 0;
1620 px->be_counters.nbpend_max = 0;
1621
1622 px->fe_counters.conn_max = 0;
1623 px->fe_counters.p.http.rps_max = 0;
1624 px->fe_counters.sps_max = 0;
1625 px->fe_counters.cps_max = 0;
1626 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001627 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001628
1629 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001630 if (clrall)
1631 memset(&sv->counters, 0, sizeof(sv->counters));
1632 else {
1633 sv->counters.cur_sess_max = 0;
1634 sv->counters.nbpend_max = 0;
1635 sv->counters.sps_max = 0;
1636 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001637
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001638 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001639 if (li->counters) {
1640 if (clrall)
1641 memset(li->counters, 0, sizeof(*li->counters));
1642 else
1643 li->counters->conn_max = 0;
1644 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001645 }
1646
Willy Tarreau81c25d02011-09-07 15:17:21 +02001647 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001648 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001649 return 1;
1650 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001651 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001652 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001653 /* end of processing */
1654 return 1;
1655 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001656 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1657 /* Set ACL or MAP flags. */
1658 if (args[1][0] == 'm')
1659 appctx->ctx.map.display_flags = PAT_REF_MAP;
1660 else
1661 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001662
1663 /* no parameter */
1664 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001665 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1666 appctx->ctx.cli.msg = "Missing map identifier.\n";
1667 else
1668 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001669 appctx->st0 = STAT_CLI_PRINT;
1670 return 1;
1671 }
1672
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001673 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001674 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001675 if (!appctx->ctx.map.ref ||
1676 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1677 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001678 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001679 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001680 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001681 appctx->st0 = STAT_CLI_PRINT;
1682 return 1;
1683 }
1684
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001685 /* Clear all. */
1686 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001687
1688 /* return response */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001689 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001690 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001691 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001692 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001693 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001694 return 0;
1695 }
1696 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001697 else if (strcmp(args[0], "get") == 0) {
1698 if (strcmp(args[1], "weight") == 0) {
1699 struct proxy *px;
1700 struct server *sv;
1701
1702 /* split "backend/server" and make <line> point to server */
1703 for (line = args[2]; *line; line++)
1704 if (*line == '/') {
1705 *line++ = '\0';
1706 break;
1707 }
1708
1709 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001710 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1711 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001712 return 1;
1713 }
1714
1715 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001716 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1717 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001718 return 1;
1719 }
1720
1721 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001722 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001723 if (bi_putstr(si_ic(si), trash.str) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02001724 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001725
Willy Tarreau38338fa2009-10-10 18:37:29 +02001726 return 1;
1727 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001728 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1729 /* Set flags. */
1730 if (args[1][0] == 'm')
1731 appctx->ctx.map.display_flags = PAT_REF_MAP;
1732 else
1733 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001734
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001735 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001736 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001737 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1738 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1739 else
1740 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001741 appctx->st0 = STAT_CLI_PRINT;
1742 return 1;
1743 }
1744
1745 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001746 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001747 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001748 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001749 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001750 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001751 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001752 appctx->st0 = STAT_CLI_PRINT;
1753 return 1;
1754 }
1755
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001756 /* copy input string. The string must be allocated because
1757 * it may be used over multiple iterations. It's released
1758 * at the end and upon abort anyway.
1759 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001760 appctx->ctx.map.chunk.len = strlen(args[3]);
1761 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1762 appctx->ctx.map.chunk.str = strdup(args[3]);
1763 if (!appctx->ctx.map.chunk.str) {
1764 appctx->ctx.cli.msg = "Out of memory error.\n";
1765 appctx->st0 = STAT_CLI_PRINT;
1766 return 1;
1767 }
1768
1769 /* prepare response */
1770 appctx->st2 = STAT_ST_INIT;
1771 appctx->st0 = STAT_CLI_O_MLOOK;
1772 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001773 else { /* not "get weight" */
1774 return 0;
1775 }
1776 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001777 else if (strcmp(args[0], "set") == 0) {
1778 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001779 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001780 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001781
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001782 sv = expect_server_admin(s, si, args[2]);
1783 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001784 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001785
Simon Horman7d09b9a2013-02-12 10:45:51 +09001786 warning = server_parse_weight_change_request(sv, args[3]);
1787 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001788 appctx->ctx.cli.msg = warning;
1789 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001790 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001791 return 1;
1792 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001793 else if (strcmp(args[1], "server") == 0) {
1794 struct server *sv;
1795 const char *warning;
1796
1797 sv = expect_server_admin(s, si, args[2]);
1798 if (!sv)
1799 return 1;
1800
1801 if (strcmp(args[3], "weight") == 0) {
1802 warning = server_parse_weight_change_request(sv, args[4]);
1803 if (warning) {
1804 appctx->ctx.cli.msg = warning;
1805 appctx->st0 = STAT_CLI_PRINT;
1806 }
1807 }
1808 else if (strcmp(args[3], "state") == 0) {
1809 if (strcmp(args[4], "ready") == 0)
1810 srv_adm_set_ready(sv);
1811 else if (strcmp(args[4], "drain") == 0)
1812 srv_adm_set_drain(sv);
1813 else if (strcmp(args[4], "maint") == 0)
1814 srv_adm_set_maint(sv);
1815 else {
1816 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1817 appctx->st0 = STAT_CLI_PRINT;
1818 }
1819 }
1820 else if (strcmp(args[3], "health") == 0) {
1821 if (sv->track) {
1822 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1823 appctx->st0 = STAT_CLI_PRINT;
1824 }
1825 else if (strcmp(args[4], "up") == 0) {
1826 sv->check.health = sv->check.rise + sv->check.fall - 1;
1827 srv_set_running(sv, "changed from CLI");
1828 }
1829 else if (strcmp(args[4], "stopping") == 0) {
1830 sv->check.health = sv->check.rise + sv->check.fall - 1;
1831 srv_set_stopping(sv, "changed from CLI");
1832 }
1833 else if (strcmp(args[4], "down") == 0) {
1834 sv->check.health = 0;
1835 srv_set_stopped(sv, "changed from CLI");
1836 }
1837 else {
1838 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1839 appctx->st0 = STAT_CLI_PRINT;
1840 }
1841 }
1842 else if (strcmp(args[3], "agent") == 0) {
1843 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1844 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1845 appctx->st0 = STAT_CLI_PRINT;
1846 }
1847 else if (strcmp(args[4], "up") == 0) {
1848 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1849 srv_set_running(sv, "changed from CLI");
1850 }
1851 else if (strcmp(args[4], "down") == 0) {
1852 sv->agent.health = 0;
1853 srv_set_stopped(sv, "changed from CLI");
1854 }
1855 else {
1856 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1857 appctx->st0 = STAT_CLI_PRINT;
1858 }
1859 }
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001860 else if (strcmp(args[3], "addr") == 0) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001861 warning = server_parse_addr_change_request(sv, args[4], "stats command");
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001862 if (warning) {
1863 appctx->ctx.cli.msg = warning;
1864 appctx->st0 = STAT_CLI_PRINT;
1865 }
1866 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001867 else {
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001868 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight' and 'addr'.\n";
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001869 appctx->st0 = STAT_CLI_PRINT;
1870 }
1871 return 1;
1872 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001873 else if (strcmp(args[1], "timeout") == 0) {
1874 if (strcmp(args[2], "cli") == 0) {
1875 unsigned timeout;
1876 const char *res;
1877
1878 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001879 appctx->ctx.cli.msg = "Expects an integer value.\n";
1880 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001881 return 1;
1882 }
1883
1884 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1885 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001886 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1887 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001888 return 1;
1889 }
1890
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001891 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001892 return 1;
1893 }
1894 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001895 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1896 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001897 return 1;
1898 }
1899 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001900 else if (strcmp(args[1], "maxconn") == 0) {
1901 if (strcmp(args[2], "frontend") == 0) {
1902 struct proxy *px;
1903 struct listener *l;
1904 int v;
1905
Willy Tarreau532a4502011-09-07 22:37:44 +02001906 px = expect_frontend_admin(s, si, args[3]);
1907 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001908 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001909
1910 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001911 appctx->ctx.cli.msg = "Integer value expected.\n";
1912 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001913 return 1;
1914 }
1915
1916 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001917 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001918 appctx->ctx.cli.msg = "Value out of range.\n";
1919 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001920 return 1;
1921 }
1922
1923 /* OK, the value is fine, so we assign it to the proxy and to all of
1924 * its listeners. The blocked ones will be dequeued.
1925 */
1926 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001927 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001928 l->maxconn = v;
1929 if (l->state == LI_FULL)
1930 resume_listener(l);
1931 }
1932
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001933 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&strm_fe(s)->listener_queue))
1934 dequeue_all_listeners(&strm_fe(s)->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001935
1936 return 1;
1937 }
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001938 else if (strcmp(args[2], "server") == 0) {
1939 struct server *sv;
1940 int v;
1941
1942 sv = expect_server_admin(s, si, args[3]);
1943 if (!sv)
1944 return 1;
1945
1946 if (!*args[4]) {
1947 appctx->ctx.cli.msg = "Integer value expected.\n";
1948 appctx->st0 = STAT_CLI_PRINT;
1949 return 1;
1950 }
1951
1952 v = atoi(args[4]);
1953 if (v < 0) {
1954 appctx->ctx.cli.msg = "Value out of range.\n";
1955 appctx->st0 = STAT_CLI_PRINT;
1956 return 1;
1957 }
1958
1959 if (sv->maxconn == sv->minconn) { // static maxconn
1960 sv->maxconn = sv->minconn = v;
1961 } else { // dynamic maxconn
1962 sv->maxconn = v;
1963 }
1964
1965 if (may_dequeue_tasks(sv, sv->proxy))
1966 process_srv_queue(sv);
1967
1968 return 1;
1969 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001970 else if (strcmp(args[2], "global") == 0) {
1971 int v;
1972
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001973 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001974 appctx->ctx.cli.msg = stats_permission_denied_msg;
1975 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001976 return 1;
1977 }
1978
1979 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001980 appctx->ctx.cli.msg = "Expects an integer value.\n";
1981 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001982 return 1;
1983 }
1984
1985 v = atoi(args[3]);
1986 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001987 appctx->ctx.cli.msg = "Value out of range.\n";
1988 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001989 return 1;
1990 }
1991
1992 /* check for unlimited values */
1993 if (v <= 0)
1994 v = global.hardmaxconn;
1995
1996 global.maxconn = v;
1997
1998 /* Dequeues all of the listeners waiting for a resource */
1999 if (!LIST_ISEMPTY(&global_listener_queue))
2000 dequeue_all_listeners(&global_listener_queue);
2001
2002 return 1;
2003 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02002004 else {
Andrew Hayworthedb93a72015-10-27 21:46:25 +00002005 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend', 'server', and 'global'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002006 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02002007 return 1;
2008 }
2009 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02002010 else if (strcmp(args[1], "rate-limit") == 0) {
2011 if (strcmp(args[2], "connections") == 0) {
2012 if (strcmp(args[3], "global") == 0) {
2013 int v;
2014
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002015 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002016 appctx->ctx.cli.msg = stats_permission_denied_msg;
2017 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002018 return 1;
2019 }
2020
2021 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002022 appctx->ctx.cli.msg = "Expects an integer value.\n";
2023 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002024 return 1;
2025 }
2026
2027 v = atoi(args[4]);
2028 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002029 appctx->ctx.cli.msg = "Value out of range.\n";
2030 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002031 return 1;
2032 }
2033
2034 global.cps_lim = v;
2035
2036 /* Dequeues all of the listeners waiting for a resource */
2037 if (!LIST_ISEMPTY(&global_listener_queue))
2038 dequeue_all_listeners(&global_listener_queue);
2039
2040 return 1;
2041 }
2042 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002043 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
2044 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002045 return 1;
2046 }
2047 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02002048 else if (strcmp(args[2], "sessions") == 0) {
2049 if (strcmp(args[3], "global") == 0) {
2050 int v;
2051
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002052 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02002053 appctx->ctx.cli.msg = stats_permission_denied_msg;
2054 appctx->st0 = STAT_CLI_PRINT;
2055 return 1;
2056 }
2057
2058 if (!*args[4]) {
2059 appctx->ctx.cli.msg = "Expects an integer value.\n";
2060 appctx->st0 = STAT_CLI_PRINT;
2061 return 1;
2062 }
2063
2064 v = atoi(args[4]);
2065 if (v < 0) {
2066 appctx->ctx.cli.msg = "Value out of range.\n";
2067 appctx->st0 = STAT_CLI_PRINT;
2068 return 1;
2069 }
2070
2071 global.sps_lim = v;
2072
2073 /* Dequeues all of the listeners waiting for a resource */
2074 if (!LIST_ISEMPTY(&global_listener_queue))
2075 dequeue_all_listeners(&global_listener_queue);
2076
2077 return 1;
2078 }
2079 else {
2080 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
2081 appctx->st0 = STAT_CLI_PRINT;
2082 return 1;
2083 }
2084 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02002085#ifdef USE_OPENSSL
2086 else if (strcmp(args[2], "ssl-sessions") == 0) {
2087 if (strcmp(args[3], "global") == 0) {
2088 int v;
2089
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002090 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02002091 appctx->ctx.cli.msg = stats_permission_denied_msg;
2092 appctx->st0 = STAT_CLI_PRINT;
2093 return 1;
2094 }
2095
2096 if (!*args[4]) {
2097 appctx->ctx.cli.msg = "Expects an integer value.\n";
2098 appctx->st0 = STAT_CLI_PRINT;
2099 return 1;
2100 }
2101
2102 v = atoi(args[4]);
2103 if (v < 0) {
2104 appctx->ctx.cli.msg = "Value out of range.\n";
2105 appctx->st0 = STAT_CLI_PRINT;
2106 return 1;
2107 }
2108
2109 global.ssl_lim = v;
2110
2111 /* Dequeues all of the listeners waiting for a resource */
2112 if (!LIST_ISEMPTY(&global_listener_queue))
2113 dequeue_all_listeners(&global_listener_queue);
2114
2115 return 1;
2116 }
2117 else {
2118 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
2119 appctx->st0 = STAT_CLI_PRINT;
2120 return 1;
2121 }
2122 }
2123#endif
William Lallemandd85f9172012-11-09 17:05:39 +01002124 else if (strcmp(args[2], "http-compression") == 0) {
2125 if (strcmp(args[3], "global") == 0) {
2126 int v;
2127
Willy Tarreaua1c2b2c2015-11-26 18:32:39 +01002128 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
2129 appctx->ctx.cli.msg = stats_permission_denied_msg;
2130 appctx->st0 = STAT_CLI_PRINT;
2131 return 1;
2132 }
2133
Willy Tarreau85d47f92012-11-21 00:29:50 +01002134 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002135 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
2136 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01002137 return 1;
2138 }
2139
William Lallemandd85f9172012-11-09 17:05:39 +01002140 v = atoi(args[4]);
2141 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
2142 }
2143 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002144 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
2145 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01002146 return 1;
2147 }
2148 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02002149 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02002150 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002151 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002152 return 1;
2153 }
2154 }
Willy Tarreau654694e2012-06-07 01:03:16 +02002155 else if (strcmp(args[1], "table") == 0) {
2156 stats_sock_table_request(si, args, STAT_CLI_O_SET);
2157 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002158 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002159 char *err;
2160
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002161 /* Set flags. */
2162 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002163
2164 /* Expect three parameters: map name, key and new value. */
2165 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01002166 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002167 appctx->st0 = STAT_CLI_PRINT;
2168 return 1;
2169 }
2170
2171 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002172 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002173 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002174 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002175 appctx->st0 = STAT_CLI_PRINT;
2176 return 1;
2177 }
2178
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002179 /* If the entry identifier start with a '#', it is considered as
2180 * pointer id
2181 */
2182 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2183 struct pat_ref_elt *ref;
2184 long long int conv;
2185 char *error;
2186
2187 /* Convert argument to integer value. */
2188 conv = strtoll(&args[3][1], &error, 16);
2189 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002190 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002191 appctx->st0 = STAT_CLI_PRINT;
2192 return 1;
2193 }
2194
2195 /* Convert and check integer to pointer. */
2196 ref = (struct pat_ref_elt *)(long)conv;
2197 if ((long long int)(long)ref != conv) {
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 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002204 err = NULL;
2205 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
2206 if (err)
2207 memprintf(&err, "%s.\n", err);
2208 appctx->ctx.cli.err = err;
2209 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002210 return 1;
2211 }
2212 }
2213 else {
2214 /* Else, use the entry identifier as pattern
2215 * string, and update the value.
2216 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002217 err = NULL;
2218 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
2219 if (err)
2220 memprintf(&err, "%s.\n", err);
2221 appctx->ctx.cli.err = err;
2222 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002223 return 1;
2224 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002225 }
2226
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002227 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002228 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002229 return 1;
2230 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002231#ifdef USE_OPENSSL
2232 else if (strcmp(args[1], "ssl") == 0) {
2233 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01002234#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02002235 char *err = NULL;
2236
Emeric Brunaf4ef742014-06-19 14:10:45 +02002237 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02002238 if (!*args[3]) {
2239 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
2240 appctx->st0 = STAT_CLI_PRINT;
2241 return 1;
2242 }
2243
2244 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
2245 if (trash.len < 0) {
2246 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
2247 appctx->st0 = STAT_CLI_PRINT;
2248 return 1;
2249 }
2250
2251 if (ssl_sock_update_ocsp_response(&trash, &err)) {
2252 if (err) {
2253 memprintf(&err, "%s.\n", err);
2254 appctx->ctx.cli.err = err;
2255 appctx->st0 = STAT_CLI_PRINT_FREE;
2256 }
2257 return 1;
2258 }
2259 appctx->ctx.cli.msg = "OCSP Response updated!";
2260 appctx->st0 = STAT_CLI_PRINT;
2261 return 1;
2262#else
2263 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
2264 appctx->st0 = STAT_CLI_PRINT;
2265 return 1;
2266#endif
2267 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002268 else if (strcmp(args[2], "tls-key") == 0) {
2269#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2270 /* Expect two parameters: the filename and the new new TLS key in encoding */
2271 if (!*args[3] || !*args[4]) {
2272 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
2273 appctx->st0 = STAT_CLI_PRINT;
2274 return 1;
2275 }
2276
2277 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[3]);
2278 if(!appctx->ctx.tlskeys.ref) {
2279 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
2280 appctx->st0 = STAT_CLI_PRINT;
2281 return 1;
2282 }
2283
2284 trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size);
2285 if (trash.len != sizeof(struct tls_sess_key)) {
2286 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
2287 appctx->st0 = STAT_CLI_PRINT;
2288 return 1;
2289 }
2290
Pradeep Jindalcc79b002015-08-20 18:25:17 +05302291 memcpy(appctx->ctx.tlskeys.ref->tlskeys + ((appctx->ctx.tlskeys.ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len);
2292 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 +02002293
2294 appctx->ctx.cli.msg = "TLS ticket key updated!";
2295 appctx->st0 = STAT_CLI_PRINT;
2296 return 1;
2297#else
2298 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
2299 "that doesn't support specifying TLS ticket keys\n";
2300 appctx->st0 = STAT_CLI_PRINT;
2301 return 1;
2302#endif
2303 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002304 else {
2305 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
2306 appctx->st0 = STAT_CLI_PRINT;
2307 return 1;
2308 }
2309 }
2310#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01002311 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02002312 return 0;
2313 }
2314 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002315 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002316 if (strcmp(args[1], "agent") == 0) {
2317 struct server *sv;
2318
2319 sv = expect_server_admin(s, si, args[2]);
2320 if (!sv)
2321 return 1;
2322
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002323 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
2324 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
2325 appctx->st0 = STAT_CLI_PRINT;
2326 return 1;
2327 }
2328
2329 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002330 return 1;
2331 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002332 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002333 struct server *sv;
2334
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002335 sv = expect_server_admin(s, si, args[2]);
2336 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002337 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002338
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002339 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
2340 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
2341 appctx->st0 = STAT_CLI_PRINT;
2342 return 1;
2343 }
2344
2345 sv->check.state |= CHK_ST_ENABLED;
2346 return 1;
2347 }
2348 else if (strcmp(args[1], "server") == 0) {
2349 struct server *sv;
2350
2351 sv = expect_server_admin(s, si, args[2]);
2352 if (!sv)
2353 return 1;
2354
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002355 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002356 return 1;
2357 }
2358 else if (strcmp(args[1], "frontend") == 0) {
2359 struct proxy *px;
2360
2361 px = expect_frontend_admin(s, si, args[2]);
2362 if (!px)
2363 return 1;
2364
2365 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002366 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
2367 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002368 return 1;
2369 }
2370
2371 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002372 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
2373 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002374 return 1;
2375 }
2376
2377 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002378 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
2379 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002380 return 1;
2381 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002382 return 1;
2383 }
2384 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002385 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002386 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002387 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002388 }
2389 }
2390 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002391 if (strcmp(args[1], "agent") == 0) {
2392 struct server *sv;
2393
2394 sv = expect_server_admin(s, si, args[2]);
2395 if (!sv)
2396 return 1;
2397
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002398 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002399 return 1;
2400 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002401 else if (strcmp(args[1], "health") == 0) {
2402 struct server *sv;
2403
2404 sv = expect_server_admin(s, si, args[2]);
2405 if (!sv)
2406 return 1;
2407
2408 sv->check.state &= ~CHK_ST_ENABLED;
2409 return 1;
2410 }
Simon Horman671b6f02013-11-25 10:46:39 +09002411 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002412 struct server *sv;
2413
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002414 sv = expect_server_admin(s, si, args[2]);
2415 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002416 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002417
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002418 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002419 return 1;
2420 }
2421 else if (strcmp(args[1], "frontend") == 0) {
2422 struct proxy *px;
2423
2424 px = expect_frontend_admin(s, si, args[2]);
2425 if (!px)
2426 return 1;
2427
2428 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002429 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
2430 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002431 return 1;
2432 }
2433
2434 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002435 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
2436 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002437 return 1;
2438 }
2439
2440 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002441 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
2442 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002443 return 1;
2444 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002445 return 1;
2446 }
2447 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002448 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002449 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002450 return 1;
2451 }
2452 }
2453 else if (strcmp(args[0], "shutdown") == 0) {
2454 if (strcmp(args[1], "frontend") == 0) {
2455 struct proxy *px;
2456
2457 px = expect_frontend_admin(s, si, args[2]);
2458 if (!px)
2459 return 1;
2460
2461 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002462 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
2463 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002464 return 1;
2465 }
2466
2467 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2468 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2469 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2470 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2471 stop_proxy(px);
2472 return 1;
2473 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002474 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002475 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002476
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002477 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002478 appctx->ctx.cli.msg = stats_permission_denied_msg;
2479 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002480 return 1;
2481 }
2482
2483 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002484 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2485 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002486 return 1;
2487 }
2488
2489 ptr = (void *)strtoul(args[2], NULL, 0);
2490
Willy Tarreau87b09662015-04-03 00:22:06 +02002491 /* first, look for the requested stream in the stream table */
2492 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002493 if (sess == ptr)
2494 break;
2495 }
2496
Willy Tarreau87b09662015-04-03 00:22:06 +02002497 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002498 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002499 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2500 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002501 return 1;
2502 }
2503
Willy Tarreaue7dff022015-04-03 01:14:29 +02002504 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002505 return 1;
2506 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002507 else if (strcmp(args[1], "sessions") == 0) {
2508 if (strcmp(args[2], "server") == 0) {
2509 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002510 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002511
2512 sv = expect_server_admin(s, si, args[3]);
2513 if (!sv)
2514 return 1;
2515
Willy Tarreau87b09662015-04-03 00:22:06 +02002516 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002517 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2518 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002519 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002520
2521 return 1;
2522 }
2523 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002524 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2525 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002526 return 1;
2527 }
2528 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002529 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002530 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2531 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002532 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002533 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002534 }
2535 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002536 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2537 if (args[1][0] == 'm')
2538 appctx->ctx.map.display_flags = PAT_REF_MAP;
2539 else
2540 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002541
2542 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002543 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2544 if (!*args[2] || !*args[3]) {
2545 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2546 appctx->st0 = STAT_CLI_PRINT;
2547 return 1;
2548 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002549 }
2550
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002551 else {
2552 if (!*args[2] || !*args[3]) {
2553 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2554 appctx->st0 = STAT_CLI_PRINT;
2555 return 1;
2556 }
2557 }
2558
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002559 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002560 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002561 if (!appctx->ctx.map.ref ||
2562 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002563 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002564 appctx->st0 = STAT_CLI_PRINT;
2565 return 1;
2566 }
2567
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002568 /* If the entry identifier start with a '#', it is considered as
2569 * pointer id
2570 */
2571 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2572 struct pat_ref_elt *ref;
2573 long long int conv;
2574 char *error;
2575
2576 /* Convert argument to integer value. */
2577 conv = strtoll(&args[3][1], &error, 16);
2578 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002579 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002580 appctx->st0 = STAT_CLI_PRINT;
2581 return 1;
2582 }
2583
2584 /* Convert and check integer to pointer. */
2585 ref = (struct pat_ref_elt *)(long)conv;
2586 if ((long long int)(long)ref != conv) {
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 /* Try to delete the entry. */
2593 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2594 /* The entry is not found, send message. */
2595 appctx->ctx.cli.msg = "Key not found.\n";
2596 appctx->st0 = STAT_CLI_PRINT;
2597 return 1;
2598 }
2599 }
2600 else {
2601 /* Else, use the entry identifier as pattern
2602 * string and try to delete the entry.
2603 */
2604 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2605 /* The entry is not found, send message. */
2606 appctx->ctx.cli.msg = "Key not found.\n";
2607 appctx->st0 = STAT_CLI_PRINT;
2608 return 1;
2609 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002610 }
2611
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002612 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002613 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002614 return 1;
2615 }
2616 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002617 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002618 appctx->st0 = STAT_CLI_PRINT;
2619 return 1;
2620 }
2621 }
2622 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002623 if (strcmp(args[1], "map") == 0 ||
2624 strcmp(args[1], "acl") == 0) {
2625 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002626 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002627
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002628 /* Set flags. */
2629 if (args[1][0] == 'm')
2630 appctx->ctx.map.display_flags = PAT_REF_MAP;
2631 else
2632 appctx->ctx.map.display_flags = PAT_REF_ACL;
2633
2634 /* If the keywork is "map", we expect three parameters, if it
2635 * is "acl", we expect only two parameters
2636 */
2637 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2638 if (!*args[2] || !*args[3] || !*args[4]) {
2639 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2640 appctx->st0 = STAT_CLI_PRINT;
2641 return 1;
2642 }
2643 }
2644 else {
2645 if (!*args[2] || !*args[3]) {
2646 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2647 appctx->st0 = STAT_CLI_PRINT;
2648 return 1;
2649 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002650 }
2651
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002652 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002653 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002654 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002655 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002656 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002657 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002658 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002659 appctx->st0 = STAT_CLI_PRINT;
2660 return 1;
2661 }
2662
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002663 /* The command "add acl" is prohibited if the reference
2664 * use samples.
2665 */
2666 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2667 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2668 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2669 "You must use the command 'add map' to add values.\n";
2670 appctx->st0 = STAT_CLI_PRINT;
2671 return 1;
2672 }
2673
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002674 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002675 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002676 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002677 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002678 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002679 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002680 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002681 if (err)
2682 memprintf(&err, "%s.\n", err);
2683 appctx->ctx.cli.err = err;
2684 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002685 return 1;
2686 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002687
2688 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002689 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002690 return 1;
2691 }
2692 else { /* unknown "del" parameter */
2693 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2694 appctx->st0 = STAT_CLI_PRINT;
2695 return 1;
2696 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002697 }
2698 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002699 return 0;
2700 }
2701 return 1;
2702}
2703
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002704/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002705 * used to processes I/O from/to the stats unix socket. The system relies on a
2706 * state machine handling requests and various responses. We read a request,
2707 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002708 * Then we can read again. The state is stored in appctx->st0 and is one of the
2709 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002710 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002711 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002712static void cli_io_handler(struct appctx *appctx)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002713{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002714 struct stream_interface *si = appctx->owner;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002715 struct channel *req = si_oc(si);
2716 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002717 int reql;
2718 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002719
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002720 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2721 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002722
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002723 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002724 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002725 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002726 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2727 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002728 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002729 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002730 /* Let's close for real now. We just close the request
2731 * side, the conditions below will complete if needed.
2732 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002733 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002734 break;
2735 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002736 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002737 /* ensure we have some output room left in the event we
2738 * would want to return some info right after parsing.
2739 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002740 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002741 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02002742 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002743 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002744
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002745 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002746 if (reql <= 0) { /* closed or EOL not found */
2747 if (reql == 0)
2748 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002749 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002750 continue;
2751 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002752
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002753 /* seek for a possible semi-colon. If we find one, we
2754 * replace it with an LF and skip only this part.
2755 */
2756 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002757 if (trash.str[len] == ';') {
2758 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002759 reql = len + 1;
2760 break;
2761 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002762
Willy Tarreau816fc222009-10-04 07:36:58 +02002763 /* now it is time to check that we have a full line,
2764 * remove the trailing \n and possibly \r, then cut the
2765 * line.
2766 */
2767 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002768 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002769 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002770 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002771 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002772
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002773 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002774 len--;
2775
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002776 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002777
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002778 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002779 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002780 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002781 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002782 continue;
2783 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002784 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002785 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002786 else if (strcmp(trash.str, "help") == 0 ||
2787 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002788 appctx->ctx.cli.msg = stats_sock_usage_msg;
2789 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002790 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002791 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002792 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002793 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002794 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002795 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002796 /* if prompt is disabled, print help on empty lines,
2797 * so that the user at least knows how to enable
2798 * prompt and find help.
2799 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002800 appctx->ctx.cli.msg = stats_sock_usage_msg;
2801 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002802 }
2803
2804 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002805 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002806 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002807 }
Willy Tarreau68c00c72015-09-25 19:21:19 +02002808 else { /* output functions */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002809 switch (appctx->st0) {
Andrew Hayworthe32d1862015-10-02 15:08:10 +00002810 case STAT_CLI_PROMPT:
2811 break;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002812 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002813 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002814 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002815 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002816 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002817 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002818 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002819 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002820 free(appctx->ctx.cli.err);
2821 appctx->st0 = STAT_CLI_PROMPT;
2822 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002823 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002824 si_applet_cant_put(si);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002825 break;
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02002826 case STAT_CLI_O_BACKEND:
2827 if (stats_dump_backend_to_buffer(si))
2828 appctx->st0 = STAT_CLI_PROMPT;
2829 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002830 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002831 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002832 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002833 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02002834 case STAT_CLI_O_SERVERS_STATE:
2835 if (stats_dump_servers_state_to_buffer(si))
2836 appctx->st0 = STAT_CLI_PROMPT;
2837 break;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002838 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002839 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002840 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002841 break;
Baptiste Assmann3863f972015-05-17 00:33:24 +02002842 case STAT_CLI_O_RESOLVERS:
2843 if (stats_dump_resolvers_to_buffer(si))
2844 appctx->st0 = STAT_CLI_PROMPT;
2845 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002846 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002847 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002848 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002849 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002850 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002851 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002852 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002853 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002854 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002855 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002856 if (stats_table_request(si, appctx->st0))
2857 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002858 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002859 case STAT_CLI_O_PATS:
2860 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002861 appctx->st0 = STAT_CLI_PROMPT;
2862 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002863 case STAT_CLI_O_PAT:
2864 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002865 appctx->st0 = STAT_CLI_PROMPT;
2866 break;
2867 case STAT_CLI_O_MLOOK:
2868 if (stats_map_lookup(si))
2869 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002870 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002871 case STAT_CLI_O_POOLS:
2872 if (stats_dump_pools_to_buffer(si))
2873 appctx->st0 = STAT_CLI_PROMPT;
2874 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002875#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2876 case STAT_CLI_O_TLSK:
2877 if (stats_tlskeys_list(si))
2878 appctx->st0 = STAT_CLI_PROMPT;
2879 break;
2880#endif
Willy Tarreauae795722016-02-16 11:27:28 +01002881 case STAT_CLI_O_ENV: /* environment dump */
2882 if (stats_dump_env_to_buffer(si))
2883 appctx->st0 = STAT_CLI_PROMPT;
2884 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002885 default: /* abnormal state */
Willy Tarreau5cfa3bc2015-09-25 20:08:51 +02002886 si->flags |= SI_FL_ERR;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002887 break;
2888 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002889
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002890 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002891 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002892 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002893 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002894 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002895 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002896 }
2897
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002898 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002899 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002900 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002901
2902 /* Now we close the output if one of the writers did so,
2903 * or if we're not in interactive mode and the request
2904 * buffer is empty. This still allows pipelined requests
2905 * to be sent in non-interactive mode.
2906 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002907 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2908 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002909 continue;
2910 }
2911
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002912 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002913 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002914 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002915 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002916
Willy Tarreauc9e930a2015-09-25 20:06:08 +02002917 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002918 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2919 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002920 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002921 * and nothing more to consume. This is comparable to a broken pipe, so
2922 * we forward the close to the request side so that it flows upstream to
2923 * the client.
2924 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002925 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002926 }
2927
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002928 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002929 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2930 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2931 /* We have no more processing to do, and nothing more to send, and
2932 * the client side has closed. So we'll forward this state downstream
2933 * on the response buffer.
2934 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002935 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002936 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002937 }
2938
Willy Tarreau828824a2015-04-19 17:20:03 +02002939 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002940 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 +02002941 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002942 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 +02002943}
2944
Willy Tarreau638d40a2016-02-24 23:11:01 +01002945/* Emits a stats field without any surrounding element and properly encoded to
2946 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
2947 */
2948static int stats_emit_raw_data_field(struct chunk *out, const struct field *f)
2949{
2950 switch (field_format(f, 0)) {
2951 case FF_EMPTY: return 1;
2952 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
2953 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
2954 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
2955 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
2956 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
2957 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
2958 }
2959}
2960
Willy Tarreaubf95cba2016-01-11 18:27:29 +01002961/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
2962static int stats_dump_info_fields(struct chunk *out, const struct field *info)
2963{
2964 int field;
2965
2966 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2967 if (!field_format(info, field))
2968 continue;
2969
2970 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
2971 return 0;
2972 if (!stats_emit_raw_data_field(out, &info[field]))
2973 return 0;
2974 if (!chunk_strcat(out, "\n"))
2975 return 0;
2976 }
2977 return 1;
2978}
2979
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002980/* This function dumps information onto the stream interface's read buffer.
2981 * It returns 0 as long as it does not complete, non-zero upon completion.
2982 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002983 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002984static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002985{
2986 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau4529c072016-01-11 18:17:07 +01002987 struct chunk *out = get_trash_chunk();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002988
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002989#ifdef USE_OPENSSL
2990 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2991 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2992 int ssl_reuse = 0;
2993
2994 if (ssl_key_rate < ssl_sess_rate) {
2995 /* count the ssl reuse ratio and avoid overflows in both directions */
2996 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2997 }
2998#endif
2999
Willy Tarreau4529c072016-01-11 18:17:07 +01003000 chunk_reset(out);
3001 memset(&info, 0, sizeof(info));
3002
3003 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
3004 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
3005 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
3006
3007 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3008 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3009 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3010
3011 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3012 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3013
3014 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3015 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3016 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3017 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3018 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3019 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3020 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3021 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3022 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3023 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3024 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3025 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3026#ifdef USE_OPENSSL
3027 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3028 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3029 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3030#endif
3031 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3032 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3033 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3034 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3035 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3036 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3037 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3038 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3039 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3040
3041#ifdef USE_OPENSSL
3042 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3043 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3044 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3045 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3046 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3047 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3048 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3049 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3050 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3051 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3052#endif
3053 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3054 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3055 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3056#ifdef USE_ZLIB
3057 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3058 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3059#endif
3060 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
3061 info[INF_RUN_QUEUE] = mkf_u32(0, run_queue_cur);
3062 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3063 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3064 if (global.desc)
3065 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
3066
Willy Tarreau1b4ba1e2016-01-11 18:29:04 +01003067 chunk_reset(&trash);
3068 stats_dump_info_fields(&trash, info);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003069
Willy Tarreaubc18da12015-03-13 14:00:47 +01003070 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003071 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003072 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003073 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003074
3075 return 1;
3076}
3077
Baptiste Assmann28289462015-07-03 08:01:20 +02003078/* dumps server state information into <buf> for all the servers found in <backend>
3079 * These information are all the parameters which may change during HAProxy runtime.
3080 * By default, we only export to the last known server state file format.
3081 * These information can be used at next startup to recover same level of server state.
3082 */
3083static void dump_servers_state(struct proxy *backend, struct chunk *buf)
3084{
3085 struct server *srv;
3086 char srv_addr[INET6_ADDRSTRLEN + 1];
3087 time_t srv_time_since_last_change;
3088 int bk_f_forced_id, srv_f_forced_id;
3089
3090 /* we don't want to report any state if the backend is not enabled on this process */
3091 if (backend->bind_proc && !(backend->bind_proc & (1UL << (relative_pid - 1))))
3092 return;
3093
3094 srv = backend->srv;
3095
3096 while (srv) {
3097 srv_addr[0] = '\0';
3098 srv_time_since_last_change = 0;
3099 bk_f_forced_id = 0;
3100 srv_f_forced_id = 0;
3101
3102 switch (srv->addr.ss_family) {
3103 case AF_INET:
3104 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
3105 srv_addr, INET_ADDRSTRLEN + 1);
3106 break;
3107 case AF_INET6:
3108 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
3109 srv_addr, INET6_ADDRSTRLEN + 1);
3110 break;
3111 }
3112 srv_time_since_last_change = now.tv_sec - srv->last_change;
3113 bk_f_forced_id = backend->options & PR_O_FORCED_ID ? 1 : 0;
3114 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
3115
3116 chunk_appendf(buf,
3117 "%d %s "
3118 "%d %s %s "
3119 "%d %d %d %d %ld "
3120 "%d %d %d %d %d "
3121 "%d %d"
3122 "\n",
3123 backend->uuid, backend->id,
3124 srv->puid, srv->id, srv_addr,
David Carlier081b3362015-11-18 06:10:22 +00003125 srv->state, srv->admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
Baptiste Assmann28289462015-07-03 08:01:20 +02003126 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
3127 bk_f_forced_id, srv_f_forced_id);
3128
3129 srv = srv->next;
3130 }
3131}
3132
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003133/* Parses backend list and simply report backend names */
3134static int stats_dump_backend_to_buffer(struct stream_interface *si)
3135{
3136 extern struct proxy *proxy;
3137 struct proxy *curproxy;
3138
3139 chunk_reset(&trash);
3140 chunk_printf(&trash, "# name\n");
3141
3142 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3143 /* looking for backends only */
3144 if (!(curproxy->cap & PR_CAP_BE))
3145 continue;
3146
3147 /* we don't want to list a backend which is bound to this process */
3148 if (curproxy->bind_proc && !(curproxy->bind_proc & (1UL << (relative_pid - 1))))
3149 continue;
3150
3151 chunk_appendf(&trash, "%s\n", curproxy->id);
3152 }
3153
3154 if (bi_putchk(si_ic(si), &trash) == -1) {
3155 si_applet_cant_put(si);
3156 return 0;
3157 }
3158
3159 return 1;
3160}
3161
Baptiste Assmann28289462015-07-03 08:01:20 +02003162/* Parses backend list or simply use backend name provided by the user to return
3163 * states of servers to stdout.
3164 */
3165static int stats_dump_servers_state_to_buffer(struct stream_interface *si)
3166{
3167 struct appctx *appctx = __objt_appctx(si->end);
3168 extern struct proxy *proxy;
3169 struct proxy *curproxy;
3170
3171 chunk_reset(&trash);
3172
3173 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
3174
3175 if (appctx->ctx.server_state.backend) {
3176 dump_servers_state(appctx->ctx.server_state.backend, &trash);
3177 }
3178 else {
3179 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3180 /* servers are only in backends */
3181 if (!(curproxy->cap & PR_CAP_BE))
3182 continue;
3183
3184 dump_servers_state(curproxy, &trash);
3185 }
3186 }
3187
3188 if (bi_putchk(si_ic(si), &trash) == -1) {
3189 si_applet_cant_put(si);
3190 return 0;
3191 }
3192
3193 return 1;
3194}
3195
Willy Tarreau12833bb2014-01-28 16:49:56 +01003196/* This function dumps memory usage information onto the stream interface's
3197 * read buffer. It returns 0 as long as it does not complete, non-zero upon
3198 * completion. No state is used.
3199 */
3200static int stats_dump_pools_to_buffer(struct stream_interface *si)
3201{
3202 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01003203 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003204 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01003205 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003206 }
Willy Tarreau82a86022016-01-04 19:04:18 +01003207 return 1;
3208}
3209
3210/* Dump all fields from <stats> into <out> using CSV format */
3211static int stats_dump_fields_csv(struct chunk *out, const struct field *stats)
3212{
3213 int field;
3214
3215 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
3216 if (!stats_emit_raw_data_field(out, &stats[field]))
3217 return 0;
3218 if (!chunk_strcat(out, ","))
3219 return 0;
3220 }
3221 chunk_strcat(&trash, "\n");
Willy Tarreau12833bb2014-01-28 16:49:56 +01003222 return 1;
3223}
3224
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003225/* Dumps a frontend's line to the trash for the current proxy <px> and uses
3226 * the state from stream interface <si>. The caller is responsible for clearing
3227 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02003228 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003229static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02003230{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003231 struct appctx *appctx = __objt_appctx(si->end);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003232
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003233 if (!(px->cap & PR_CAP_FE))
3234 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02003235
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003236 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003237 return 0;
3238
Willy Tarreau9e561072016-01-04 19:36:41 +01003239 memset(&stats, 0, sizeof(stats));
3240
3241 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
3242 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
3243 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
3244 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
3245 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
3246 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
3247 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
3248 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
3249 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
3250 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
3251 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
3252 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
3253 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
3254 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
3255 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
3256 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
3257 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
3258 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
3259 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
3260
3261 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3262 if (px->mode == PR_MODE_HTTP) {
3263 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
3264 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
3265 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
3266 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
3267 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
3268 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
3269 }
3270
3271 /* requests : req_rate, req_rate_max, req_tot, */
3272 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
3273 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
3274 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
3275
3276 /* compression: in, out, bypassed, responses */
3277 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
3278 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
3279 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
3280 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
3281
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003282 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003283 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003284 /* name, queue */
3285 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003286
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003287 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003288 /* Column sub-heading for Enable or Disable server */
3289 chunk_appendf(&trash, "<td></td>");
3290 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003291
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003292 chunk_appendf(&trash,
3293 "<td class=ac>"
3294 "<a name=\"%s/Frontend\"></a>"
3295 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
3296 "<td colspan=3></td>"
3297 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003298 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003299
Willy Tarreau466c9b52012-12-23 02:25:03 +01003300 chunk_appendf(&trash,
3301 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003302 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003303 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
3304 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
3305 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003306 U2H(stats[ST_F_RATE].u.u32),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003307 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003308 U2H(stats[ST_F_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003309
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003310 if (px->mode == PR_MODE_HTTP)
3311 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003312 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003313 U2H(stats[ST_F_REQ_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003314
3315 chunk_appendf(&trash,
3316 "</table></div></u></td>"
3317 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003318 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003319 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
3320 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
3321 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003322 U2H(stats[ST_F_RATE_MAX].u.u32),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003323 U2H(px->fe_counters.cps_max),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003324 U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003325
3326 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003327 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003328 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003329 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003330
3331 chunk_appendf(&trash,
3332 "</table></div></u></td>"
3333 /* sessions rate : limit */
3334 "<td>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003335 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003336
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003337 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003338 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003339 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003340 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003341 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
3342 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003343 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003344 U2H(stats[ST_F_SCUR].u.u32), U2H(px->fe_counters.conn_max), U2H(stats[ST_F_SLIM].u.u32),
3345 U2H(stats[ST_F_STOT].u.u64),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003346 U2H(px->fe_counters.cum_conn),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003347 U2H(stats[ST_F_STOT].u.u64));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003348
Willy Tarreau466c9b52012-12-23 02:25:03 +01003349 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003350 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003351 chunk_appendf(&trash,
3352 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3353 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3354 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3355 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3356 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3357 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3358 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3359 "<tr><th>- other responses:</th><td>%s</td></tr>"
3360 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3361 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003362 U2H(stats[ST_F_REQ_TOT].u.u64),
3363 U2H(stats[ST_F_HRSP_1XX].u.u64),
3364 U2H(stats[ST_F_HRSP_2XX].u.u64),
3365 U2H(stats[ST_F_COMP_RSP].u.u64),
3366 stats[ST_F_HRSP_2XX].u.u64 ?
3367 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3368 U2H(stats[ST_F_HRSP_3XX].u.u64),
3369 U2H(stats[ST_F_HRSP_4XX].u.u64),
3370 U2H(stats[ST_F_HRSP_5XX].u.u64),
3371 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003372 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003373 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003374
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003375 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003376 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003377 /* sessions: lbtot, lastsess */
3378 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003379 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003380 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003381 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003382 U2H(stats[ST_F_BIN].u.u64));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003383
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003384 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003385 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003386 "<td>%s%s<div class=tips><table class=det>"
3387 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3388 "<tr><th>Compression in:</th><td>%s</td></tr>"
3389 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3390 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3391 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3392 "</table></div>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003393 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3394 U2H(stats[ST_F_BOUT].u.u64),
3395 U2H(stats[ST_F_BOUT].u.u64),
3396 U2H(stats[ST_F_COMP_IN].u.u64),
3397 U2H(stats[ST_F_COMP_OUT].u.u64),
3398 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3399 U2H(stats[ST_F_COMP_BYP].u.u64),
3400 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
3401 stats[ST_F_BOUT].u.u64 ? (int)((stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64) * 100 / stats[ST_F_BOUT].u.u64) : 0,
3402 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003403
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003404 chunk_appendf(&trash,
3405 /* denied: req, resp */
3406 "<td>%s</td><td>%s</td>"
3407 /* errors : request, connect, response */
3408 "<td>%s</td><td></td><td></td>"
3409 /* warnings: retries, redispatches */
3410 "<td></td><td></td>"
3411 /* server status : reflect frontend status */
3412 "<td class=ac>%s</td>"
3413 /* rest of server: nothing */
3414 "<td class=ac colspan=8></td></tr>"
3415 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003416 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3417 U2H(stats[ST_F_EREQ].u.u64),
3418 field_str(stats, ST_F_STATUS));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003419 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003420 else { /* CSV mode */
Willy Tarreau9e561072016-01-04 19:36:41 +01003421 /* dump everything */
3422 stats_dump_fields_csv(&trash, stats);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003423 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003424 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003425}
3426
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003427/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
3428 * from stream interface <si>, and stats flags <flags>. The caller is responsible
3429 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
3430 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02003431 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003432static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02003433{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003434 struct appctx *appctx = __objt_appctx(si->end);
3435
3436 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003437 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003438 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003439 /* Column sub-heading for Enable or Disable server */
3440 chunk_appendf(&trash, "<td></td>");
3441 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003442 chunk_appendf(&trash,
3443 /* frontend name, listener name */
3444 "<td class=ac><a name=\"%s/+%s\"></a>%s"
3445 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
3446 "",
3447 px->id, l->name,
3448 (flags & ST_SHLGNDS)?"<u>":"",
3449 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02003450
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003451 if (flags & ST_SHLGNDS) {
3452 char str[INET6_ADDRSTRLEN];
3453 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02003454
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003455 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02003456
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003457 port = get_host_port(&l->addr);
3458 switch (addr_to_str(&l->addr, str, sizeof(str))) {
3459 case AF_INET:
3460 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
3461 break;
3462 case AF_INET6:
3463 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
3464 break;
3465 case AF_UNIX:
3466 chunk_appendf(&trash, "unix, ");
3467 break;
3468 case -1:
3469 chunk_appendf(&trash, "(%s), ", strerror(errno));
3470 break;
3471 }
Willy Tarreau91861262007-10-17 17:06:05 +02003472
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003473 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003474 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003475 }
Willy Tarreau91861262007-10-17 17:06:05 +02003476
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003477 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003478 /* queue */
3479 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003480 /* sessions rate: current, max, limit */
3481 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003482 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003483 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003484 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003485 /* bytes: in, out */
3486 "<td>%s</td><td>%s</td>"
3487 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003488 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003489 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
3490 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02003491
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003492 chunk_appendf(&trash,
3493 /* denied: req, resp */
3494 "<td>%s</td><td>%s</td>"
3495 /* errors: request, connect, response */
3496 "<td>%s</td><td></td><td></td>"
3497 /* warnings: retries, redispatches */
3498 "<td></td><td></td>"
3499 /* server status: reflect listener status */
3500 "<td class=ac>%s</td>"
3501 /* rest of server: nothing */
3502 "<td class=ac colspan=8></td></tr>"
3503 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003504 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
3505 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003506 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
3507 }
3508 else { /* CSV mode */
3509 chunk_appendf(&trash,
3510 /* pxid, name, queue cur, queue max, */
3511 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003512 /* sessions: current, max, limit, total */
3513 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003514 /* bytes: in, out */
3515 "%lld,%lld,"
3516 /* denied: req, resp */
3517 "%lld,%lld,"
3518 /* errors: request, connect, response */
3519 "%lld,,,"
3520 /* warnings: retries, redispatches */
3521 ",,"
3522 /* server status: reflect listener status */
3523 "%s,"
3524 /* rest of server: nothing */
3525 ",,,,,,,,"
3526 /* pid, iid, sid, throttle, lbtot, tracked, type */
3527 "%d,%d,%d,,,,%d,"
3528 /* rate, rate_lim, rate_max */
3529 ",,,"
3530 /* check_status, check_code, check_duration */
3531 ",,,"
3532 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3533 ",,,,,,"
3534 /* failed health analyses */
3535 ","
3536 /* requests : req_rate, req_rate_max, req_tot, */
3537 ",,,"
3538 /* errors: cli_aborts, srv_aborts */
3539 ",,"
3540 /* compression: in, out, bypassed, comp_rsp */
3541 ",,,,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003542 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
3543 ",,,,,,,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003544 "\n",
3545 px->id, l->name,
3546 l->nbconn, l->counters->conn_max,
3547 l->maxconn, l->counters->cum_conn,
3548 l->counters->bytes_in, l->counters->bytes_out,
3549 l->counters->denied_req, l->counters->denied_resp,
3550 l->counters->failed_req,
3551 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
3552 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
3553 }
3554 return 1;
3555}
Willy Tarreau91861262007-10-17 17:06:05 +02003556
Simon Horman4d2eab62015-04-23 14:51:26 +09003557enum srv_stats_state {
3558 SRV_STATS_STATE_DOWN = 0,
3559 SRV_STATS_STATE_DOWN_AGENT,
3560 SRV_STATS_STATE_GOING_UP,
3561 SRV_STATS_STATE_UP_GOING_DOWN,
3562 SRV_STATS_STATE_UP,
3563 SRV_STATS_STATE_NOLB_GOING_DOWN,
3564 SRV_STATS_STATE_NOLB,
3565 SRV_STATS_STATE_DRAIN_GOING_DOWN,
3566 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09003567 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09003568 SRV_STATS_STATE_NO_CHECK,
3569
3570 SRV_STATS_STATE_COUNT, /* Must be last */
3571};
3572
Simon Horman837bfa72015-04-23 14:51:27 +09003573enum srv_stats_colour {
3574 SRV_STATS_COLOUR_DOWN = 0,
3575 SRV_STATS_COLOUR_GOING_UP,
3576 SRV_STATS_COLOUR_GOING_DOWN,
3577 SRV_STATS_COLOUR_UP,
3578 SRV_STATS_COLOUR_NOLB,
3579 SRV_STATS_COLOUR_DRAINING,
3580 SRV_STATS_COLOUR_NO_CHECK,
3581
3582 SRV_STATS_COLOUR_COUNT, /* Must be last */
3583};
3584
3585static const char *srv_stats_colour_st[SRV_STATS_COLOUR_COUNT] = {
3586 [SRV_STATS_COLOUR_DOWN] = "down",
3587 [SRV_STATS_COLOUR_GOING_UP] = "going_up",
3588 [SRV_STATS_COLOUR_GOING_DOWN] = "going_down",
3589 [SRV_STATS_COLOUR_UP] = "up",
3590 [SRV_STATS_COLOUR_NOLB] = "nolb",
3591 [SRV_STATS_COLOUR_DRAINING] = "draining",
3592 [SRV_STATS_COLOUR_NO_CHECK] = "no_check",
3593};
3594
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003595/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
3596 * from stream interface <si>, stats flags <flags>, and server state <state>.
3597 * The caller is responsible for clearing the trash if needed. Returns non-zero
Simon Horman4d2eab62015-04-23 14:51:26 +09003598 * if it emits anything, zero otherwise.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003599 */
Simon Horman4d2eab62015-04-23 14:51:26 +09003600static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv,
Simon Horman837bfa72015-04-23 14:51:27 +09003601 enum srv_stats_state state, enum srv_stats_colour colour)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003602{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003603 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02003604 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003605 char str[INET6_ADDRSTRLEN];
3606 struct chunk src;
3607 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02003608
Willy Tarreau32091232014-05-16 13:52:00 +02003609 /* we have "via" which is the tracked server as described in the configuration,
3610 * and "ref" which is the checked server and the end of the chain.
3611 */
3612 via = sv->track ? sv->track : sv;
3613 ref = via;
3614 while (ref->track)
3615 ref = ref->track;
3616
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003617 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003618 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3619 [SRV_STATS_STATE_DOWN] = "DOWN",
3620 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
3621 [SRV_STATS_STATE_GOING_UP] = "DN %d/%d &uarr;",
3622 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d &darr;",
3623 [SRV_STATS_STATE_UP] = "UP",
3624 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d &darr;",
3625 [SRV_STATS_STATE_NOLB] = "NOLB",
3626 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d &darr;",
3627 [SRV_STATS_STATE_DRAIN] = "DRAIN",
Simon Hormanb167b6b2015-04-23 14:51:29 +09003628 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
Simon Horman4d2eab62015-04-23 14:51:26 +09003629 [SRV_STATS_STATE_NO_CHECK] = "<i>no check</i>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003630 };
Willy Tarreau91861262007-10-17 17:06:05 +02003631
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003632 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003633 chunk_appendf(&trash, "<tr class=\"maintain\">");
3634 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003635 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09003636 "<tr class=\"%s_%s\">",
3637 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", srv_stats_colour_st[colour]);
Willy Tarreau91861262007-10-17 17:06:05 +02003638
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003639 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003640 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003641 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3642 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003643
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003644 chunk_appendf(&trash,
3645 "<td class=ac><a name=\"%s/%s\"></a>%s"
3646 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3647 "",
3648 px->id, sv->id,
3649 (flags & ST_SHLGNDS) ? "<u>" : "",
3650 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02003651
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003652 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003653 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003654
3655 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
3656 case AF_INET:
3657 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
3658 break;
3659 case AF_INET6:
3660 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
3661 break;
3662 case AF_UNIX:
3663 chunk_appendf(&trash, "unix, ");
3664 break;
3665 case -1:
3666 chunk_appendf(&trash, "(%s), ", strerror(errno));
3667 break;
3668 default: /* address family not supported */
3669 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02003670 }
Willy Tarreau91861262007-10-17 17:06:05 +02003671
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003672 /* id */
3673 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02003674
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003675 /* cookie */
3676 if (sv->cookie) {
3677 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02003678
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003679 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
3680 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003681
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003682 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003683 }
3684
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003685 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003686 }
Willy Tarreau91861262007-10-17 17:06:05 +02003687
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003688 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003689 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003690 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003691 /* sessions rate : current, max, limit */
3692 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003693 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003694 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003695 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
3696 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02003697
Willy Tarreau466c9b52012-12-23 02:25:03 +01003698
3699 chunk_appendf(&trash,
3700 /* sessions: current, max, limit, total */
3701 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003702 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003703 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3704 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003705 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
3706 U2H(sv->counters.cum_sess),
3707 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02003708
Willy Tarreau466c9b52012-12-23 02:25:03 +01003709 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3710 if (px->mode == PR_MODE_HTTP) {
3711 unsigned long long tot;
3712 for (tot = i = 0; i < 6; i++)
3713 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003714
Willy Tarreau466c9b52012-12-23 02:25:03 +01003715 chunk_appendf(&trash,
3716 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3717 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3718 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3719 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3720 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3721 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3722 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3723 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003724 U2H(tot),
3725 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
3726 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
3727 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
3728 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
3729 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
3730 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 +02003731 }
Willy Tarreau91861262007-10-17 17:06:05 +02003732
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003733 chunk_appendf(&trash, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3734 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)));
3735 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)));
3736 if (px->mode == PR_MODE_HTTP)
3737 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)));
3738 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)));
3739
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003740 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003741 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003742 /* sessions: lbtot, last */
3743 "<td>%s</td><td>%s</td>",
3744 U2H(sv->counters.cum_lbconn),
3745 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003746
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003747 chunk_appendf(&trash,
3748 /* bytes : in, out */
3749 "<td>%s</td><td>%s</td>"
3750 /* denied: req, resp */
3751 "<td></td><td>%s</td>"
3752 /* errors : request, connect */
3753 "<td></td><td>%s</td>"
3754 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003755 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003756 /* warnings: retries, redispatches */
3757 "<td>%lld</td><td>%lld</td>"
3758 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003759 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
3760 U2H(sv->counters.failed_secu),
3761 U2H(sv->counters.failed_conns),
3762 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003763 sv->counters.cli_aborts,
3764 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003765 sv->counters.retries, sv->counters.redispatches);
3766
3767 /* status, lest check */
3768 chunk_appendf(&trash, "<td class=ac>");
3769
Willy Tarreau20125212014-05-13 19:44:56 +02003770 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003771 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
3772 chunk_appendf(&trash, "MAINT");
3773 }
Simon Horman0766e442014-11-12 15:55:54 +09003774 else if ((ref->agent.state & CHK_ST_ENABLED) && !(sv->agent.health) && (ref->state == SRV_ST_STOPPED)) {
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003775 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
Willy Tarreauac497072014-05-29 01:04:35 +02003776 /* DOWN (agent) */
3777 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 +02003778 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01003779 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003780 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
3781 chunk_appendf(&trash,
3782 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003783 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3784 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02003785 }
Willy Tarreau91861262007-10-17 17:06:05 +02003786
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003787 if ((sv->state == SRV_ST_STOPPED) &&
3788 ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) && !(sv->agent.health)) {
3789 chunk_appendf(&trash,
3790 "</td><td class=ac><u> %s%s",
3791 (sv->agent.state & CHK_ST_INPROGRESS) ? "* " : "",
3792 get_check_status_info(sv->agent.status));
3793
3794 if (sv->agent.status >= HCHK_STATUS_L57DATA)
3795 chunk_appendf(&trash, "/%d", sv->agent.code);
3796
3797 if (sv->agent.status >= HCHK_STATUS_CHECKED && sv->agent.duration >= 0)
3798 chunk_appendf(&trash, " in %lums", sv->agent.duration);
3799
3800 chunk_appendf(&trash, "<div class=tips>%s",
3801 get_check_status_description(sv->agent.status));
3802 if (*sv->agent.desc) {
3803 chunk_appendf(&trash, ": ");
3804 chunk_initlen(&src, sv->agent.desc, 0, strlen(sv->agent.desc));
3805 chunk_htmlencode(&trash, &src);
3806 }
3807 chunk_appendf(&trash, "</div></u>");
3808 }
3809 else if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003810 chunk_appendf(&trash,
3811 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01003812 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003813 get_check_status_info(sv->check.status));
3814
3815 if (sv->check.status >= HCHK_STATUS_L57DATA)
3816 chunk_appendf(&trash, "/%d", sv->check.code);
3817
3818 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003819 chunk_appendf(&trash, " in %lums", sv->check.duration);
3820
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003821 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003822 get_check_status_description(sv->check.status));
3823 if (*sv->check.desc) {
3824 chunk_appendf(&trash, ": ");
3825 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
3826 chunk_htmlencode(&trash, &src);
3827 }
3828 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003829 }
3830 else
3831 chunk_appendf(&trash, "</td><td>");
3832
3833 chunk_appendf(&trash,
3834 /* weight */
3835 "</td><td class=ac>%d</td>"
3836 /* act, bck */
3837 "<td class=ac>%s</td><td class=ac>%s</td>"
3838 "",
3839 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003840 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
3841 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003842
3843 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003844 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003845 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003846
3847 if (ref->observe)
3848 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
3849
3850 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003851 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003852 "<td>%lld</td><td>%s</td>"
3853 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003854 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003855 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
3856 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003857 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
3858 /* tracking a server */
3859 chunk_appendf(&trash,
3860 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02003861 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01003862 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003863 else
3864 chunk_appendf(&trash, "<td colspan=3></td>");
3865
3866 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003867 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003868 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003869 else
3870 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3871 }
3872 else { /* CSV mode */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003873 struct chunk *out = get_trash_chunk();
Simon Horman4d2eab62015-04-23 14:51:26 +09003874 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3875 [SRV_STATS_STATE_DOWN] = "DOWN,",
3876 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent),",
3877 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d,",
3878 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d,",
3879 [SRV_STATS_STATE_UP] = "UP,",
3880 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d,",
3881 [SRV_STATS_STATE_NOLB] = "NOLB,",
3882 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d,",
3883 [SRV_STATS_STATE_DRAIN] = "DRAIN,",
Raghu Udiyar0d6b7a42016-02-05 22:30:11 +05303884 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent),",
Simon Horman4d2eab62015-04-23 14:51:26 +09003885 [SRV_STATS_STATE_NO_CHECK] = "no check,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003886 };
3887
3888 chunk_appendf(&trash,
3889 /* pxid, name */
3890 "%s,%s,"
3891 /* queue : current, max */
3892 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003893 /* sessions : current, max, limit, total */
3894 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003895 /* bytes : in, out */
3896 "%lld,%lld,"
3897 /* denied: req, resp */
3898 ",%lld,"
3899 /* errors : request, connect, response */
3900 ",%lld,%lld,"
3901 /* warnings: retries, redispatches */
3902 "%lld,%lld,"
3903 "",
3904 px->id, sv->id,
3905 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003906 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003907 sv->counters.bytes_in, sv->counters.bytes_out,
3908 sv->counters.failed_secu,
3909 sv->counters.failed_conns, sv->counters.failed_resp,
3910 sv->counters.retries, sv->counters.redispatches);
3911
3912 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003913 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003914 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003915 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003916 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003917 else
3918 chunk_appendf(&trash,
3919 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003920 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3921 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003922
3923 chunk_appendf(&trash,
3924 /* weight, active, backup */
3925 "%d,%d,%d,"
3926 "",
3927 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003928 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3929 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003930
3931 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003932 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003933 chunk_appendf(&trash,
3934 "%lld,%lld,%d,%d,",
3935 sv->counters.failed_checks, sv->counters.down_trans,
3936 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3937 else
3938 chunk_appendf(&trash, ",,,,");
3939
3940 /* queue limit, pid, iid, sid, */
3941 chunk_appendf(&trash,
3942 "%s,"
3943 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003944 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003945 relative_pid, px->uuid, sv->puid);
3946
3947 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003948 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003949 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003950
3951 /* sessions: lbtot */
3952 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3953
3954 /* tracked */
3955 if (sv->track)
3956 chunk_appendf(&trash, "%s/%s,",
3957 sv->track->proxy->id, sv->track->id);
3958 else
3959 chunk_appendf(&trash, ",");
3960
3961 /* type */
3962 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3963
3964 /* rate */
3965 chunk_appendf(&trash, "%u,,%u,",
3966 read_freq_ctr(&sv->sess_per_sec),
3967 sv->counters.sps_max);
3968
Willy Tarreauff5ae352013-12-11 20:36:34 +01003969 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003970 /* check_status */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003971 chunk_appendf(&trash, "%s,", csv_enc(get_check_status_info(sv->check.status), 1, out));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003972
3973 /* check_code */
3974 if (sv->check.status >= HCHK_STATUS_L57DATA)
3975 chunk_appendf(&trash, "%u,", sv->check.code);
3976 else
3977 chunk_appendf(&trash, ",");
3978
3979 /* check_duration */
3980 if (sv->check.status >= HCHK_STATUS_CHECKED)
3981 chunk_appendf(&trash, "%lu,", sv->check.duration);
3982 else
3983 chunk_appendf(&trash, ",");
3984
3985 }
3986 else
3987 chunk_appendf(&trash, ",,,");
3988
3989 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3990 if (px->mode == PR_MODE_HTTP) {
3991 for (i=1; i<6; i++)
3992 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3993
3994 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3995 }
3996 else
3997 chunk_appendf(&trash, ",,,,,,");
3998
3999 /* failed health analyses */
4000 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
4001
4002 /* requests : req_rate, req_rate_max, req_tot, */
4003 chunk_appendf(&trash, ",,,");
4004
4005 /* errors: cli_aborts, srv_aborts */
4006 chunk_appendf(&trash, "%lld,%lld,",
4007 sv->counters.cli_aborts, sv->counters.srv_aborts);
4008
4009 /* compression: in, out, bypassed, comp_rsp */
4010 chunk_appendf(&trash, ",,,,");
4011
Willy Tarreauf522f3d2014-02-10 22:22:49 +01004012 /* lastsess */
4013 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
4014
Willy Tarreaua28df3e2014-06-16 16:40:14 +02004015 /* capture of last check and agent statuses */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02004016 chunk_appendf(&trash, "%s,", ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? csv_enc(cstr(sv->check.desc), 1, out) : "");
4017 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 +02004018
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004019 /* qtime, ctime, rtime, ttime, */
4020 chunk_appendf(&trash, "%u,%u,%u,%u,",
4021 swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES),
4022 swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES),
4023 swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES),
4024 swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
4025
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004026 /* finish with EOL */
4027 chunk_appendf(&trash, "\n");
4028 }
4029 return 1;
4030}
4031
4032/* Dumps a line for backend <px> to the trash for and uses the state from stream
4033 * interface <si> and stats flags <flags>. The caller is responsible for clearing
4034 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
4035 */
4036static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
4037{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004038 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004039 struct chunk src;
4040 int i;
4041
4042 if (!(px->cap & PR_CAP_BE))
4043 return 0;
4044
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004045 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004046 return 0;
4047
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004048 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004049 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004050 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004051 /* Column sub-heading for Enable or Disable server */
4052 chunk_appendf(&trash, "<td></td>");
4053 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004054 chunk_appendf(&trash,
4055 "<td class=ac>"
4056 /* name */
4057 "%s<a name=\"%s/Backend\"></a>"
4058 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
4059 "",
4060 (flags & ST_SHLGNDS)?"<u>":"",
4061 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004062
4063 if (flags & ST_SHLGNDS) {
4064 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004065 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004066 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
4067
4068 /* cookie */
4069 if (px->cookie_name) {
4070 chunk_appendf(&trash, ", cookie: '");
4071 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
4072 chunk_htmlencode(&trash, &src);
4073 chunk_appendf(&trash, "'");
4074 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004075 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004076 }
4077
4078 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004079 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004080 /* queue : current, max */
4081 "<td>%s</td><td>%s</td><td></td>"
4082 /* sessions rate : current, max, limit */
4083 "<td>%s</td><td>%s</td><td></td>"
4084 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004085 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01004086 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
4087 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004088
4089 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004090 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004091 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004092 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004093 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004094 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01004095 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
4096 U2H(px->be_counters.cum_conn),
4097 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004098
Willy Tarreau466c9b52012-12-23 02:25:03 +01004099 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004100 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01004101 chunk_appendf(&trash,
4102 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
4103 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
4104 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
4105 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
4106 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
4107 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
4108 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
4109 "<tr><th>- other responses:</th><td>%s</td></tr>"
4110 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004111 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004112 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01004113 U2H(px->be_counters.p.http.cum_req),
4114 U2H(px->be_counters.p.http.rsp[1]),
4115 U2H(px->be_counters.p.http.rsp[2]),
4116 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004117 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01004118 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01004119 U2H(px->be_counters.p.http.rsp[3]),
4120 U2H(px->be_counters.p.http.rsp[4]),
4121 U2H(px->be_counters.p.http.rsp[5]),
4122 U2H(px->be_counters.p.http.rsp[0]),
4123 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004124 }
4125
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004126 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)));
4127 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)));
4128 if (px->mode == PR_MODE_HTTP)
4129 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)));
4130 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)));
4131
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004132 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01004133 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004134 /* sessions: lbtot, last */
4135 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004136 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004137 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004138 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01004139 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004140 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01004141 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004142
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004143 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004144 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02004145 "<td>%s%s<div class=tips><table class=det>"
4146 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
4147 "<tr><th>Compression in:</th><td>%s</td></tr>"
4148 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
4149 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
4150 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
4151 "</table></div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004152 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01004153 U2H(px->be_counters.bytes_out),
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02004154 U2H(px->be_counters.bytes_out),
4155 U2H(px->be_counters.comp_in),
4156 U2H(px->be_counters.comp_out),
4157 px->be_counters.comp_in ? (int)(px->be_counters.comp_out * 100 / px->be_counters.comp_in) : 0,
4158 U2H(px->be_counters.comp_byp),
4159 U2H(px->be_counters.comp_in - px->be_counters.comp_out),
4160 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 +01004161 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
4162
4163 chunk_appendf(&trash,
4164 /* denied: req, resp */
4165 "<td>%s</td><td>%s</td>"
4166 /* errors : request, connect */
4167 "<td></td><td>%s</td>"
4168 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004169 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004170 /* warnings: retries, redispatches */
4171 "<td>%lld</td><td>%lld</td>"
4172 /* backend status: reflect backend status (up/down): we display UP
4173 * if the backend has known working servers or if it has no server at
4174 * all (eg: for stats). Then we display the total weight, number of
4175 * active and backups. */
4176 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
4177 "<td class=ac>%d</td><td class=ac>%d</td>"
4178 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01004179 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
4180 U2H(px->be_counters.failed_conns),
4181 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004182 px->be_counters.cli_aborts,
4183 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004184 px->be_counters.retries, px->be_counters.redispatches,
4185 human_time(now.tv_sec - px->last_change, 1),
4186 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
4187 "<font color=\"red\"><b>DOWN</b></font>",
4188 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
4189 px->srv_act, px->srv_bck);
4190
4191 chunk_appendf(&trash,
4192 /* rest of backend: nothing, down transitions, total downtime, throttle */
4193 "<td class=ac>&nbsp;</td><td>%d</td>"
4194 "<td>%s</td>"
4195 "<td></td>"
4196 "</tr>",
4197 px->down_trans,
4198 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
4199 }
4200 else { /* CSV mode */
4201 chunk_appendf(&trash,
4202 /* pxid, name */
4203 "%s,BACKEND,"
4204 /* queue : current, max */
4205 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01004206 /* sessions : current, max, limit, total */
4207 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004208 /* bytes : in, out */
4209 "%lld,%lld,"
4210 /* denied: req, resp */
4211 "%lld,%lld,"
4212 /* errors : request, connect, response */
4213 ",%lld,%lld,"
4214 /* warnings: retries, redispatches */
4215 "%lld,%lld,"
4216 /* backend status: reflect backend status (up/down): we display UP
4217 * if the backend has known working servers or if it has no server at
4218 * all (eg: for stats). Then we display the total weight, number of
4219 * active and backups. */
4220 "%s,"
4221 "%d,%d,%d,"
4222 /* rest of backend: nothing, down transitions, last change, total downtime */
4223 ",%d,%d,%d,,"
4224 /* pid, iid, sid, throttle, lbtot, tracked, type */
4225 "%d,%d,0,,%lld,,%d,"
4226 /* rate, rate_lim, rate_max, */
4227 "%u,,%u,"
4228 /* check_status, check_code, check_duration */
4229 ",,,",
4230 px->id,
4231 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
4232 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
4233 px->be_counters.bytes_in, px->be_counters.bytes_out,
4234 px->be_counters.denied_req, px->be_counters.denied_resp,
4235 px->be_counters.failed_conns, px->be_counters.failed_resp,
4236 px->be_counters.retries, px->be_counters.redispatches,
4237 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
4238 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
4239 px->srv_act, px->srv_bck,
4240 px->down_trans, (int)(now.tv_sec - px->last_change),
4241 px->srv?be_downtime(px):0,
4242 relative_pid, px->uuid,
4243 px->be_counters.cum_lbconn, STATS_TYPE_BE,
4244 read_freq_ctr(&px->be_sess_per_sec),
4245 px->be_counters.sps_max);
4246
4247 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4248 if (px->mode == PR_MODE_HTTP) {
4249 for (i=1; i<6; i++)
4250 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
4251 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
4252 }
4253 else
4254 chunk_appendf(&trash, ",,,,,,");
4255
4256 /* failed health analyses */
4257 chunk_appendf(&trash, ",");
4258
4259 /* requests : req_rate, req_rate_max, req_tot, */
4260 chunk_appendf(&trash, ",,,");
4261
4262 /* errors: cli_aborts, srv_aborts */
4263 chunk_appendf(&trash, "%lld,%lld,",
4264 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
4265
4266 /* compression: in, out, bypassed */
4267 chunk_appendf(&trash, "%lld,%lld,%lld,",
4268 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
4269
4270 /* compression: comp_rsp */
4271 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
4272
Willy Tarreaua28df3e2014-06-16 16:40:14 +02004273 /* lastsess, last_chk, last_agt, */
4274 chunk_appendf(&trash, "%d,,,", be_lastsession(px));
Willy Tarreauf522f3d2014-02-10 22:22:49 +01004275
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004276 /* qtime, ctime, rtime, ttime, */
4277 chunk_appendf(&trash, "%u,%u,%u,%u,",
4278 swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES),
4279 swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES),
4280 swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES),
4281 swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
4282
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004283 /* finish with EOL */
4284 chunk_appendf(&trash, "\n");
4285 }
4286 return 1;
4287}
4288
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004289/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004290 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004291 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004292 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004293static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004294{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004295 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004296 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4297
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004298 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004299 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004300
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004301 /* 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 +02004302 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004303 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004304 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004305 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 +01004306 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004307 }
4308
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004309 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004310 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004311 }
4312
4313 /* print a new table */
4314 chunk_appendf(&trash,
4315 "<table class=\"tbl\" width=\"100%%\">\n"
4316 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004317 "<th class=\"pxname\" width=\"10%%\">");
4318
4319 chunk_appendf(&trash,
4320 "<a name=\"%s\"></a>%s"
4321 "<a class=px href=\"#%s\">%s</a>",
4322 px->id,
4323 (uri->flags & ST_SHLGNDS) ? "<u>":"",
4324 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004325
4326 if (uri->flags & ST_SHLGNDS) {
4327 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004328 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004329 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
4330 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004331 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004332 }
4333
4334 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004335 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004336 "<th class=\"%s\" width=\"90%%\">%s</th>"
4337 "</tr>\n"
4338 "</table>\n"
4339 "<table class=\"tbl\" width=\"100%%\">\n"
4340 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004341 (uri->flags & ST_SHLGNDS) ? "</u>":"",
4342 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
4343
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004344 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004345 /* Column heading for Enable or Disable server */
4346 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02004347 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004348
4349 chunk_appendf(&trash,
4350 "<th rowspan=2></th>"
4351 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004352 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004353 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
4354 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
4355 "<th colspan=9>Server</th>"
4356 "</tr>\n"
4357 "<tr class=\"titre\">"
4358 "<th>Cur</th><th>Max</th><th>Limit</th>"
4359 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004360 "<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 +01004361 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
4362 "<th>Resp</th><th>Retr</th><th>Redis</th>"
4363 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
4364 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
4365 "<th>Thrtle</th>\n"
4366 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02004367}
4368
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004369/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004370 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004371 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004372static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004373{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004374 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004375 chunk_appendf(&trash, "</table>");
4376
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004377 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004378 /* close the form used to enable/disable this proxy servers */
4379 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004380 "Choose the action to perform on the checked servers : "
4381 "<select name=action>"
4382 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02004383 "<option value=\"ready\">Set state to READY</option>"
4384 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02004385 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02004386 "<option value=\"dhlth\">Health: disable checks</option>"
4387 "<option value=\"ehlth\">Health: enable checks</option>"
4388 "<option value=\"hrunn\">Health: force UP</option>"
4389 "<option value=\"hnolb\">Health: force NOLB</option>"
4390 "<option value=\"hdown\">Health: force DOWN</option>"
4391 "<option value=\"dagent\">Agent: disable checks</option>"
4392 "<option value=\"eagent\">Agent: enable checks</option>"
4393 "<option value=\"arunn\">Agent: force UP</option>"
4394 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004395 "<option value=\"shutdown\">Kill Sessions</option>"
4396 "</select>"
4397 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
4398 "&nbsp;<input type=\"submit\" value=\"Apply\">"
4399 "</form>",
4400 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004401 }
4402
4403 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004404}
Willy Tarreau91861262007-10-17 17:06:05 +02004405
4406/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004407 * Dumps statistics for a proxy. The output is sent to the stream interface's
4408 * input buffer. Returns 0 if it had to stop dumping data because of lack of
4409 * buffer space, or non-zero if everything completed. This function is used
4410 * both by the CLI and the HTTP entry points, and is able to dump the output
4411 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02004412 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004413static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02004414{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004415 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02004416 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004417 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02004418 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004419 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02004420
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004421 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02004422
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004423 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004424 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02004425 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02004426 if (uri && uri->scope) {
4427 /* we have a limited scope, we have to check the proxy name */
4428 struct stat_scope *scope;
4429 int len;
4430
4431 len = strlen(px->id);
4432 scope = uri->scope;
4433
4434 while (scope) {
4435 /* match exact proxy name */
4436 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
4437 break;
4438
4439 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02004440 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02004441 break;
4442 scope = scope->next;
4443 }
4444
4445 /* proxy name not found : don't dump anything */
4446 if (scope == NULL)
4447 return 1;
4448 }
4449
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004450 /* if the user has requested a limited output and the proxy
4451 * name does not match, skip it.
4452 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004453 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004454 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 +02004455 return 1;
4456
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004457 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
4458 (appctx->ctx.stats.iid != -1) &&
4459 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004460 return 1;
4461
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004462 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02004463 /* fall through */
4464
Willy Tarreau295a8372011-03-10 11:25:07 +01004465 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004466 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004467 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004468 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004469 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02004470 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004471 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004472 }
Willy Tarreau91861262007-10-17 17:06:05 +02004473
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004474 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02004475 /* fall through */
4476
Willy Tarreau295a8372011-03-10 11:25:07 +01004477 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02004478 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004479 if (stats_dump_fe_stats(si, px)) {
4480 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004481 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004482 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004483 }
4484 }
Willy Tarreau91861262007-10-17 17:06:05 +02004485
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004486 appctx->ctx.stats.l = px->conf.listeners.n;
4487 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004488 /* fall through */
4489
Willy Tarreau295a8372011-03-10 11:25:07 +01004490 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004491 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004492 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004493 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004494 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004495 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004496 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004497
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004498 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004499 if (!l->counters)
4500 continue;
4501
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004502 if (appctx->ctx.stats.flags & STAT_BOUND) {
4503 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004504 break;
4505
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004506 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004507 continue;
4508 }
4509
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004510 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004511 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0)) {
4512 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004513 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004514 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004515 }
4516 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004517 }
4518
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004519 appctx->ctx.stats.sv = px->srv; /* may be NULL */
4520 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02004521 /* fall through */
4522
Willy Tarreau295a8372011-03-10 11:25:07 +01004523 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02004524 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004525 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004526 enum srv_stats_state sv_state;
Simon Horman837bfa72015-04-23 14:51:27 +09004527 enum srv_stats_colour sv_colour;
Willy Tarreau91861262007-10-17 17:06:05 +02004528
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004529 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004530 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004531 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004532 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004533
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004534 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02004535
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004536 if (appctx->ctx.stats.flags & STAT_BOUND) {
4537 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004538 break;
4539
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004540 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004541 continue;
4542 }
4543
Willy Tarreau32091232014-05-16 13:52:00 +02004544 svs = sv;
4545 while (svs->track)
4546 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004547
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004548 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004549 if ((svs->check.state & CHK_ST_ENABLED) &&
Simon Horman837bfa72015-04-23 14:51:27 +09004550 (svs->check.health < svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004551 sv_state = SRV_STATS_STATE_UP_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09004552 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
4553 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004554 sv_state = SRV_STATS_STATE_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09004555 sv_colour = SRV_STATS_COLOUR_UP;
4556 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01004557
Simon Horman4463d192015-04-23 14:51:28 +09004558 if (sv_state == SRV_STATS_STATE_UP && !svs->uweight)
4559 sv_colour = SRV_STATS_COLOUR_DRAINING;
4560
4561 if (sv->admin & SRV_ADMF_DRAIN) {
Simon Hormanb167b6b2015-04-23 14:51:29 +09004562 if (svs->agent.state & CHK_ST_ENABLED)
4563 sv_state = SRV_STATS_STATE_DRAIN_AGENT;
4564 else if (sv_state == SRV_STATS_STATE_UP_GOING_DOWN)
Simon Horman4d2eab62015-04-23 14:51:26 +09004565 sv_state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
Simon Horman4463d192015-04-23 14:51:28 +09004566 else
Simon Horman4d2eab62015-04-23 14:51:26 +09004567 sv_state = SRV_STATS_STATE_DRAIN;
4568 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004569
Simon Horman837bfa72015-04-23 14:51:27 +09004570 if (sv_state == SRV_STATS_STATE_UP && !(svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004571 sv_state = SRV_STATS_STATE_NO_CHECK;
Simon Horman837bfa72015-04-23 14:51:27 +09004572 sv_colour = SRV_STATS_COLOUR_NO_CHECK;
4573 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01004574 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004575 else if (sv->state == SRV_ST_STOPPING) {
4576 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
Simon Horman837bfa72015-04-23 14:51:27 +09004577 (svs->check.health == svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004578 sv_state = SRV_STATS_STATE_NOLB;
Simon Horman837bfa72015-04-23 14:51:27 +09004579 sv_colour = SRV_STATS_COLOUR_NOLB;
4580 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004581 sv_state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09004582 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
4583 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004584 }
4585 else { /* stopped */
Simon Horman837bfa72015-04-23 14:51:27 +09004586 if ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004587 sv_state = SRV_STATS_STATE_DOWN_AGENT;
Simon Horman837bfa72015-04-23 14:51:27 +09004588 sv_colour = SRV_STATS_COLOUR_DOWN;
4589 } else if ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004590 sv_state = SRV_STATS_STATE_DOWN; /* DOWN */
Simon Horman837bfa72015-04-23 14:51:27 +09004591 sv_colour = SRV_STATS_COLOUR_DOWN;
4592 } else if ((svs->agent.state & CHK_ST_ENABLED) || (svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004593 sv_state = SRV_STATS_STATE_GOING_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09004594 sv_colour = SRV_STATS_COLOUR_GOING_UP;
4595 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004596 sv_state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Simon Horman837bfa72015-04-23 14:51:27 +09004597 sv_colour = SRV_STATS_COLOUR_DOWN;
4598 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004599 }
Willy Tarreau91861262007-10-17 17:06:05 +02004600
Willy Tarreau9638efa2014-05-23 11:19:57 +02004601 if (((sv_state <= 1) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02004602 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004603 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02004604 continue;
4605 }
4606
Simon Horman837bfa72015-04-23 14:51:27 +09004607 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state, sv_colour)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004608 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004609 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004610 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004611 }
4612 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004613 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004614
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004615 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004616 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004617
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004618 case STAT_PX_ST_BE:
4619 /* print the backend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004620 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0)) {
4621 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004622 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004623 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004624 }
4625 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004626
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004627 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004628 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004629
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004630 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004631 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004632 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004633 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004634 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004635 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004636 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004637 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004638
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004639 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004640 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004641
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004642 case STAT_PX_ST_FIN:
4643 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004644
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004645 default:
4646 /* unknown state, we should put an abort() here ! */
4647 return 1;
4648 }
4649}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004650
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004651/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4652 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004653 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004654static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004655{
4656 /* WARNING! This must fit in the first buffer !!! */
4657 chunk_appendf(&trash,
4658 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4659 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4660 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4661 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4662 "<style type=\"text/css\"><!--\n"
4663 "body {"
4664 " font-family: arial, helvetica, sans-serif;"
4665 " font-size: 12px;"
4666 " font-weight: normal;"
4667 " color: black;"
4668 " background: white;"
4669 "}\n"
4670 "th,td {"
4671 " font-size: 10px;"
4672 "}\n"
4673 "h1 {"
4674 " font-size: x-large;"
4675 " margin-bottom: 0.5em;"
4676 "}\n"
4677 "h2 {"
4678 " font-family: helvetica, arial;"
4679 " font-size: x-large;"
4680 " font-weight: bold;"
4681 " font-style: italic;"
4682 " color: #6020a0;"
4683 " margin-top: 0em;"
4684 " margin-bottom: 0em;"
4685 "}\n"
4686 "h3 {"
4687 " font-family: helvetica, arial;"
4688 " font-size: 16px;"
4689 " font-weight: bold;"
4690 " color: #b00040;"
4691 " background: #e8e8d0;"
4692 " margin-top: 0em;"
4693 " margin-bottom: 0em;"
4694 "}\n"
4695 "li {"
4696 " margin-top: 0.25em;"
4697 " margin-right: 2em;"
4698 "}\n"
4699 ".hr {margin-top: 0.25em;"
4700 " border-color: black;"
4701 " border-bottom-style: solid;"
4702 "}\n"
4703 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4704 ".total {background: #20D0D0;color: #ffff80;}\n"
4705 ".frontend {background: #e8e8d0;}\n"
4706 ".socket {background: #d0d0d0;}\n"
4707 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004708 ".active_down {background: #ff9090;}\n"
4709 ".active_going_up {background: #ffd020;}\n"
4710 ".active_going_down {background: #ffffa0;}\n"
4711 ".active_up {background: #c0ffc0;}\n"
4712 ".active_nolb {background: #20a0ff;}\n"
4713 ".active_draining {background: #20a0FF;}\n"
4714 ".active_no_check {background: #e0e0e0;}\n"
4715 ".backup_down {background: #ff9090;}\n"
4716 ".backup_going_up {background: #ff80ff;}\n"
4717 ".backup_going_down {background: #c060ff;}\n"
4718 ".backup_up {background: #b0d0ff;}\n"
4719 ".backup_nolb {background: #90b0e0;}\n"
4720 ".backup_draining {background: #cc9900;}\n"
4721 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004722 ".maintain {background: #c07820;}\n"
4723 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4724 "\n"
4725 "a.px:link {color: #ffff40; text-decoration: none;}"
4726 "a.px:visited {color: #ffff40; text-decoration: none;}"
4727 "a.px:hover {color: #ffffff; text-decoration: none;}"
4728 "a.lfsb:link {color: #000000; text-decoration: none;}"
4729 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4730 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4731 "\n"
4732 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4733 "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"
4734 "table.tbl td.ac { text-align: center;}\n"
4735 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4736 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4737 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4738 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4739 "\n"
4740 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4741 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4742 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004743 "table.det { border-collapse: collapse; border-style: none; }\n"
4744 "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 +01004745 "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 +01004746 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004747 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004748 " display:block;\n"
4749 " visibility:hidden;\n"
4750 " z-index:2147483647;\n"
4751 " position:absolute;\n"
4752 " padding:2px 4px 3px;\n"
4753 " background:#f0f060; color:#000000;\n"
4754 " border:1px solid #7040c0;\n"
4755 " white-space:nowrap;\n"
4756 " font-style:normal;font-size:11px;font-weight:normal;\n"
4757 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4758 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4759 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004760 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004761 "-->\n"
4762 "</style></head>\n",
4763 (uri->flags & ST_SHNODE) ? " on " : "",
4764 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4765 );
4766}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004767
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004768/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004769 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004770 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004771 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004772static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004773{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004774 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004775 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004776 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004777
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004778 /* WARNING! this has to fit the first packet too.
4779 * We are around 3.5 kB, add adding entries will
4780 * become tricky if we want to support 4kB buffers !
4781 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004782 chunk_appendf(&trash,
4783 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4784 PRODUCT_NAME "%s</a></h1>\n"
4785 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4786 "<hr width=\"100%%\" class=\"hr\">\n"
4787 "<h3>&gt; General process information</h3>\n"
4788 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4789 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4790 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4791 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4792 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4793 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4794 "Running tasks: %d/%d; idle = %d %%<br>\n"
4795 "</td><td align=\"center\" nowrap>\n"
4796 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004797 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4798 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004799 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004800 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4801 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004802 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004803 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
4804 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004805 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004806 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
4807 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004808 "</tr><tr>\n"
4809 "<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 -07004810 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004811 "<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 +01004812 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004813 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004814 "</td>"
4815 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4816 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4817 "",
4818 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4819 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4820 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4821 (uri->flags & ST_SHDESC) ? ": " : "",
4822 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4823 pid, relative_pid, global.nbproc,
4824 up / 86400, (up % 86400) / 3600,
4825 (up % 3600) / 60, (up % 60),
4826 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4827 global.rlimit_memmax ? " MB" : "",
4828 global.rlimit_nofile,
4829 global.maxsock, global.maxconn, global.maxpipes,
4830 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4831 run_queue_cur, nb_tasks_cur, idle_pct
4832 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004833
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004834 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004835 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 +01004836 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004837
4838 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004839 "<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 +01004840 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004841 STAT_SCOPE_TXT_MAXLEN);
4842
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004843 /* 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 +02004844 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004845 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004846 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004847 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 +01004848 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004849 }
4850
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004851 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004852 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004853 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004854 uri->uri_prefix,
4855 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004856 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004857 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004858 else
4859 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004860 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004861 uri->uri_prefix,
4862 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004863 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004864 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004865
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004866 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004867 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004868 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004869 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004870 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004871 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004872 "",
4873 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004874 else
4875 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004876 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004877 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004878 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004879 ";norefresh",
4880 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004881 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004882
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004883 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004884 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004885 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004886 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4887 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004888 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004889
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004890 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004891 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004892 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004893 (uri->refresh > 0) ? ";norefresh" : "",
4894 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004895
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004896 chunk_appendf(&trash,
4897 "</ul></td>"
4898 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4899 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4900 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4901 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4902 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4903 "</ul>"
4904 "</td>"
4905 "</tr></table>\n"
4906 ""
4907 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004908
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004909 if (appctx->ctx.stats.st_code) {
4910 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004911 case STAT_STATUS_DONE:
4912 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004913 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004914 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004915 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02004916 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004917 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4918 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004919 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004920 break;
4921 case STAT_STATUS_NONE:
4922 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004923 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004924 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004925 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02004926 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004927 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4928 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004929 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004930 break;
4931 case STAT_STATUS_PART:
4932 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004933 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004934 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004935 "Action partially processed.<br>"
4936 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004937 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004938 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4939 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004940 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004941 break;
4942 case STAT_STATUS_ERRP:
4943 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004944 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004945 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004946 "Action not processed because of invalid parameters."
4947 "<ul>"
4948 "<li>The action is maybe unknown.</li>"
4949 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
4950 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4951 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004952 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004953 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4954 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004955 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004956 break;
4957 case STAT_STATUS_EXCD:
4958 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004959 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004960 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004961 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4962 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004963 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004964 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4965 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004966 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004967 break;
4968 case STAT_STATUS_DENY:
4969 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004970 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004971 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004972 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004973 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004974 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4975 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004976 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004977 break;
4978 default:
4979 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004980 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004981 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004982 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004983 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004984 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4985 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004986 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004987 }
4988 chunk_appendf(&trash, "<p>\n");
4989 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004990}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004991
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004992/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4993 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004994 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004995static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004996{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004997 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004998}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004999
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005000/* This function dumps statistics onto the stream interface's read buffer in
5001 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02005002 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
5003 * 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 +02005004 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02005005 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005006 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005007static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005008{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005009 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005010 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005011 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01005012
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005013 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005014
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005015 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005016 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005017 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005018 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005019
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005020 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005021 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005022 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01005023 else
5024 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01005025
Willy Tarreaubc18da12015-03-13 14:00:47 +01005026 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005027 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005028 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005029 }
Willy Tarreauae526782010-03-04 20:34:23 +01005030
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005031 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005032 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005033
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005034 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005035 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005036 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01005037 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005038 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005039 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005040 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005041 }
Willy Tarreau91861262007-10-17 17:06:05 +02005042
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005043 appctx->ctx.stats.px = proxy;
5044 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
5045 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02005046 /* fall through */
5047
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005048 case STAT_ST_LIST:
5049 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005050 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005051 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005052 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005053 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005054 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005055
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005056 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005057 /* skip the disabled proxies, global frontend and non-networked ones */
5058 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005059 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005060 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005061
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005062 appctx->ctx.stats.px = px->next;
5063 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005064 }
5065 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005066
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005067 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005068 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005069
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005070 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005071 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005072 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01005073 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005074 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005075 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005076 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005077 }
Willy Tarreau55058a72012-11-21 08:27:21 +01005078
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005079 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005080 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02005081
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005082 case STAT_ST_FIN:
5083 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01005084
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005085 default:
5086 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005087 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005088 return -1;
5089 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005090}
Willy Tarreauae526782010-03-04 20:34:23 +01005091
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005092/* We reached the stats page through a POST request. The appctx is
5093 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01005094 * Parse the posted data and enable/disable servers if necessary.
5095 * Returns 1 if request was parsed or zero if it needs more data.
5096 */
5097static int stats_process_http_post(struct stream_interface *si)
5098{
Willy Tarreau87b09662015-04-03 00:22:06 +02005099 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005100 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005101
5102 struct proxy *px = NULL;
5103 struct server *sv = NULL;
5104
5105 char key[LINESIZE];
5106 int action = ST_ADM_ACTION_NONE;
5107 int reprocess = 0;
5108
5109 int total_servers = 0;
5110 int altered_servers = 0;
5111
5112 char *first_param, *cur_param, *next_param, *end_params;
5113 char *st_cur_param = NULL;
5114 char *st_next_param = NULL;
5115
5116 struct chunk *temp;
5117 int reql;
5118
5119 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02005120 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005121 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005122 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005123 goto out;
5124 }
5125
Willy Tarreaueee5b512015-04-03 23:46:31 +02005126 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 +01005127 if (reql <= 0) {
5128 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005129 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005130 return 0;
5131 }
5132
5133 first_param = temp->str;
5134 end_params = temp->str + reql;
5135 cur_param = next_param = end_params;
5136 *end_params = '\0';
5137
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005138 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005139
5140 /*
5141 * Parse the parameters in reverse order to only store the last value.
5142 * From the html form, the backend and the action are at the end.
5143 */
5144 while (cur_param > first_param) {
5145 char *value;
5146 int poffset, plen;
5147
5148 cur_param--;
5149
5150 if ((*cur_param == '&') || (cur_param == first_param)) {
5151 reprocess_servers:
5152 /* Parse the key */
5153 poffset = (cur_param != first_param ? 1 : 0);
5154 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
5155 if ((plen > 0) && (plen <= sizeof(key))) {
5156 strncpy(key, cur_param + poffset, plen);
5157 key[plen - 1] = '\0';
5158 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005159 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005160 goto out;
5161 }
5162
5163 /* Parse the value */
5164 value = key;
5165 while (*value != '\0' && *value != '=') {
5166 value++;
5167 }
5168 if (*value == '=') {
5169 /* Ok, a value is found, we can mark the end of the key */
5170 *value++ = '\0';
5171 }
5172 if (url_decode(key) < 0 || url_decode(value) < 0)
5173 break;
5174
5175 /* Now we can check the key to see what to do */
5176 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02005177 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005178 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005179 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005180 goto out;
5181 }
5182 }
5183 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02005184 if (strcmp(value, "ready") == 0) {
5185 action = ST_ADM_ACTION_READY;
5186 }
5187 else if (strcmp(value, "drain") == 0) {
5188 action = ST_ADM_ACTION_DRAIN;
5189 }
5190 else if (strcmp(value, "maint") == 0) {
5191 action = ST_ADM_ACTION_MAINT;
5192 }
5193 else if (strcmp(value, "shutdown") == 0) {
5194 action = ST_ADM_ACTION_SHUTDOWN;
5195 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02005196 else if (strcmp(value, "dhlth") == 0) {
5197 action = ST_ADM_ACTION_DHLTH;
5198 }
5199 else if (strcmp(value, "ehlth") == 0) {
5200 action = ST_ADM_ACTION_EHLTH;
5201 }
5202 else if (strcmp(value, "hrunn") == 0) {
5203 action = ST_ADM_ACTION_HRUNN;
5204 }
5205 else if (strcmp(value, "hnolb") == 0) {
5206 action = ST_ADM_ACTION_HNOLB;
5207 }
5208 else if (strcmp(value, "hdown") == 0) {
5209 action = ST_ADM_ACTION_HDOWN;
5210 }
5211 else if (strcmp(value, "dagent") == 0) {
5212 action = ST_ADM_ACTION_DAGENT;
5213 }
5214 else if (strcmp(value, "eagent") == 0) {
5215 action = ST_ADM_ACTION_EAGENT;
5216 }
5217 else if (strcmp(value, "arunn") == 0) {
5218 action = ST_ADM_ACTION_ARUNN;
5219 }
5220 else if (strcmp(value, "adown") == 0) {
5221 action = ST_ADM_ACTION_ADOWN;
5222 }
Willy Tarreaued7df902014-05-22 18:04:49 +02005223 /* else these are the old supported methods */
5224 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005225 action = ST_ADM_ACTION_DISABLE;
5226 }
5227 else if (strcmp(value, "enable") == 0) {
5228 action = ST_ADM_ACTION_ENABLE;
5229 }
5230 else if (strcmp(value, "stop") == 0) {
5231 action = ST_ADM_ACTION_STOP;
5232 }
5233 else if (strcmp(value, "start") == 0) {
5234 action = ST_ADM_ACTION_START;
5235 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005236 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005237 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005238 goto out;
5239 }
5240 }
5241 else if (strcmp(key, "s") == 0) {
5242 if (!(px && action)) {
5243 /*
5244 * Indicates that we'll need to reprocess the parameters
5245 * as soon as backend and action are known
5246 */
5247 if (!reprocess) {
5248 st_cur_param = cur_param;
5249 st_next_param = next_param;
5250 }
5251 reprocess = 1;
5252 }
5253 else if ((sv = findserver(px, value)) != NULL) {
5254 switch (action) {
5255 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005256 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005257 altered_servers++;
5258 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005259 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005260 }
5261 break;
5262 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005263 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005264 altered_servers++;
5265 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005266 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005267 }
5268 break;
5269 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005270 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
5271 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
5272 altered_servers++;
5273 total_servers++;
5274 }
5275 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005276 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005277 if (sv->admin & SRV_ADMF_FDRAIN) {
5278 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
5279 altered_servers++;
5280 total_servers++;
5281 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005282 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02005283 case ST_ADM_ACTION_DHLTH:
5284 if (sv->check.state & CHK_ST_CONFIGURED) {
5285 sv->check.state &= ~CHK_ST_ENABLED;
5286 altered_servers++;
5287 total_servers++;
5288 }
5289 break;
5290 case ST_ADM_ACTION_EHLTH:
5291 if (sv->check.state & CHK_ST_CONFIGURED) {
5292 sv->check.state |= CHK_ST_ENABLED;
5293 altered_servers++;
5294 total_servers++;
5295 }
5296 break;
5297 case ST_ADM_ACTION_HRUNN:
5298 if (!(sv->track)) {
5299 sv->check.health = sv->check.rise + sv->check.fall - 1;
5300 srv_set_running(sv, "changed from Web interface");
5301 altered_servers++;
5302 total_servers++;
5303 }
5304 break;
5305 case ST_ADM_ACTION_HNOLB:
5306 if (!(sv->track)) {
5307 sv->check.health = sv->check.rise + sv->check.fall - 1;
5308 srv_set_stopping(sv, "changed from Web interface");
5309 altered_servers++;
5310 total_servers++;
5311 }
5312 break;
5313 case ST_ADM_ACTION_HDOWN:
5314 if (!(sv->track)) {
5315 sv->check.health = 0;
5316 srv_set_stopped(sv, "changed from Web interface");
5317 altered_servers++;
5318 total_servers++;
5319 }
5320 break;
5321 case ST_ADM_ACTION_DAGENT:
5322 if (sv->agent.state & CHK_ST_CONFIGURED) {
5323 sv->agent.state &= ~CHK_ST_ENABLED;
5324 altered_servers++;
5325 total_servers++;
5326 }
5327 break;
5328 case ST_ADM_ACTION_EAGENT:
5329 if (sv->agent.state & CHK_ST_CONFIGURED) {
5330 sv->agent.state |= CHK_ST_ENABLED;
5331 altered_servers++;
5332 total_servers++;
5333 }
5334 break;
5335 case ST_ADM_ACTION_ARUNN:
5336 if (sv->agent.state & CHK_ST_ENABLED) {
5337 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
5338 srv_set_running(sv, "changed from Web interface");
5339 altered_servers++;
5340 total_servers++;
5341 }
5342 break;
5343 case ST_ADM_ACTION_ADOWN:
5344 if (sv->agent.state & CHK_ST_ENABLED) {
5345 sv->agent.health = 0;
5346 srv_set_stopped(sv, "changed from Web interface");
5347 altered_servers++;
5348 total_servers++;
5349 }
5350 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02005351 case ST_ADM_ACTION_READY:
5352 srv_adm_set_ready(sv);
5353 altered_servers++;
5354 total_servers++;
5355 break;
5356 case ST_ADM_ACTION_DRAIN:
5357 srv_adm_set_drain(sv);
5358 altered_servers++;
5359 total_servers++;
5360 break;
5361 case ST_ADM_ACTION_MAINT:
5362 srv_adm_set_maint(sv);
5363 altered_servers++;
5364 total_servers++;
5365 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005366 case ST_ADM_ACTION_SHUTDOWN:
5367 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005368 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005369
5370 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
5371 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005372 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005373
5374 altered_servers++;
5375 total_servers++;
5376 }
5377 break;
5378 }
5379 } else {
5380 /* the server name is unknown or ambiguous (duplicate names) */
5381 total_servers++;
5382 }
5383 }
5384 if (reprocess && px && action) {
5385 /* Now, we know the backend and the action chosen by the user.
5386 * We can safely restart from the first server parameter
5387 * to reprocess them
5388 */
5389 cur_param = st_cur_param;
5390 next_param = st_next_param;
5391 reprocess = 0;
5392 goto reprocess_servers;
5393 }
5394
5395 next_param = cur_param;
5396 }
5397 }
5398
5399 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005400 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005401 }
5402 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005403 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005404 }
5405 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005406 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005407 }
5408 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005409 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005410 }
5411 out:
5412 return 1;
5413}
5414
5415
5416static int stats_send_http_headers(struct stream_interface *si)
5417{
Willy Tarreau87b09662015-04-03 00:22:06 +02005418 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005419 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005420 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005421
5422 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02005423 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005424 "Cache-Control: no-cache\r\n"
5425 "Connection: close\r\n"
5426 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005427 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005428
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005429 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01005430 chunk_appendf(&trash, "Refresh: %d\r\n",
5431 uri->refresh);
5432
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005433 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
5434
5435 if (appctx->ctx.stats.flags & STAT_CHUNKED)
5436 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
5437 else
5438 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005439
Willy Tarreaueee5b512015-04-03 23:46:31 +02005440 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005441 s->logs.tv_request = now;
5442
Willy Tarreaubc18da12015-03-13 14:00:47 +01005443 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005444 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005445 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005446 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005447
5448 return 1;
5449}
5450
5451static int stats_send_http_redirect(struct stream_interface *si)
5452{
5453 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005454 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005455 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005456 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005457
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005458 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01005459 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005460 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005461 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005462 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 +01005463 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005464 }
5465
5466 /* We don't want to land on the posted stats page because a refresh will
5467 * repost the data. We don't want this to happen on accident so we redirect
5468 * the browse to the stats page with a GET.
5469 */
5470 chunk_printf(&trash,
5471 "HTTP/1.1 303 See Other\r\n"
5472 "Cache-Control: no-cache\r\n"
5473 "Content-Type: text/plain\r\n"
5474 "Connection: close\r\n"
5475 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreaufdfcc9d2016-01-26 13:57:29 +01005476 "Content-length: 0\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005477 "\r\n",
5478 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005479 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
5480 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
5481 stat_status_codes[appctx->ctx.stats.st_code]) ?
5482 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01005483 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005484 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5485 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01005486 scope_txt);
5487
Willy Tarreaueee5b512015-04-03 23:46:31 +02005488 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005489 s->logs.tv_request = now;
5490
Willy Tarreaubc18da12015-03-13 14:00:47 +01005491 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005492 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005493 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005494 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005495
5496 return 1;
5497}
5498
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005499/* This I/O handler runs as an applet embedded in a stream interface. It is
5500 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005501 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005502 * automatically unregisters itself once transfer is complete.
5503 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005504static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005505{
Willy Tarreau00a37f02015-04-13 12:05:19 +02005506 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02005507 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005508 struct channel *req = si_oc(si);
5509 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01005510
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005511 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
5512 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005513
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005514 /* check that the output is not closed */
5515 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005516 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005517
Willy Tarreau347a35d2013-11-22 17:51:09 +01005518 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005519 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005520 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02005521 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005522 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005523 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005524 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005525 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005526 }
5527
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005528 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005529 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005530 unsigned int data_len;
5531 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02005532 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005533
5534 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5535 /* One difficulty we're facing is that we must prevent
5536 * the input data from being automatically forwarded to
5537 * the output area. For this, we temporarily disable
5538 * forwarding on the channel.
5539 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005540 last_fwd = si_ic(si)->to_forward;
5541 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005542 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005543 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005544 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005545 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02005546 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005547 }
5548 }
5549
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005550 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005551 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005552 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005553
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005554 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005555
5556 /* Now we must either adjust or remove the chunk size. This is
5557 * not easy because the chunk size might wrap at the end of the
5558 * buffer, so we pretend we have nothing in the buffer, we write
5559 * the size, then restore the buffer's contents. Note that we can
5560 * only do that because no forwarding is scheduled on the stats
5561 * applet.
5562 */
5563 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005564 si_ic(si)->total -= (last_len - prev_len);
5565 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005566
5567 if (last_len != data_len) {
5568 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005569 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005570 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005571
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005572 si_ic(si)->total += (last_len - data_len);
5573 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005574 }
5575 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005576 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005577 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005578 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005579
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005580 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005581 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005582 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005583 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005584 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005585 }
5586
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005587 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005588 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005589 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005590 }
5591
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005592 if (appctx->st0 == STAT_HTTP_DONE) {
5593 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5594 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005595 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005596 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005597 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005598 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005599 }
5600 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005601 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005602 res->flags |= CF_READ_NULL;
5603 si_shutr(si);
5604 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005605
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005606 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5607 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005608
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005609 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005610 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5611 si_shutr(si);
5612 res->flags |= CF_READ_NULL;
5613 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005614 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005615 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005616 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005617}
5618
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005619
Willy Tarreau909d5172012-11-26 03:04:41 +01005620static inline const char *get_conn_ctrl_name(const struct connection *conn)
5621{
Willy Tarreau3c728722014-01-23 13:50:42 +01005622 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005623 return "NONE";
5624 return conn->ctrl->name;
5625}
5626
5627static inline const char *get_conn_xprt_name(const struct connection *conn)
5628{
5629 static char ptr[17];
5630
Willy Tarreauaad69382014-01-23 14:21:42 +01005631 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005632 return "NONE";
5633
5634 if (conn->xprt == &raw_sock)
5635 return "RAW";
5636
5637#ifdef USE_OPENSSL
5638 if (conn->xprt == &ssl_sock)
5639 return "SSL";
5640#endif
5641 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5642 return ptr;
5643}
5644
5645static inline const char *get_conn_data_name(const struct connection *conn)
5646{
5647 static char ptr[17];
5648
5649 if (!conn->data)
5650 return "NONE";
5651
5652 if (conn->data == &sess_conn_cb)
5653 return "SESS";
5654
5655 if (conn->data == &si_conn_cb)
5656 return "STRM";
5657
5658 if (conn->data == &check_conn_cb)
5659 return "CHCK";
5660
5661 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5662 return ptr;
5663}
5664
Willy Tarreau87b09662015-04-03 00:22:06 +02005665/* This function dumps a complete stream state onto the stream interface's
5666 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005667 * 0 if the output buffer is full and it needs to be called again, otherwise
5668 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005669 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005670static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005671{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005672 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005673 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005674 extern const char *monthname[12];
5675 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005676 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005677 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005678
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005679 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005680
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005681 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005682 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005683 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005684 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005685 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005686 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005687 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005688 appctx->ctx.sess.uid = 0;
5689 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005690 return 1;
5691 }
5692
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005693 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005694 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005695 appctx->ctx.sess.uid = sess->uniq_id;
5696 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005697 /* fall through */
5698
5699 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005700 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005701 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005702 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005703 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005704 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5705 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005706 sess->uniq_id,
Willy Tarreau666f5042015-06-17 19:49:52 +02005707 strm_li(sess) ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005708
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005709 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005710 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005711 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005712 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005713 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005714 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005715 break;
5716 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005717 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005718 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005719 default:
5720 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005721 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005722 break;
5723 }
5724
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005725 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005726 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005727 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005728
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005729 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005730 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005731 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5732 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5733 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005734
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005735 if (conn)
5736 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005737
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005738 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005739 case AF_INET:
5740 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005741 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005742 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005743 break;
5744 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005745 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005746 break;
5747 default:
5748 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005749 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005750 break;
5751 }
5752
Willy Tarreau50943332011-09-02 17:33:05 +02005753 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005754 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005755 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005756 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005757 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5758 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005759 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005760
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005761 conn = objt_conn(sess->si[1].end);
5762 if (conn)
5763 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005764
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005765 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005766 case AF_INET:
5767 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005768 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005769 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005770 break;
5771 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005772 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005773 break;
5774 default:
5775 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005776 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005777 break;
5778 }
5779
5780 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005781 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005782 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005783 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5784 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005785 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005786 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005787
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005788 if (conn)
5789 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005790
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005791 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005792 case AF_INET:
5793 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005794 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005795 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005796 break;
5797 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005798 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005799 break;
5800 default:
5801 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005802 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005803 break;
5804 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005805
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005806 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005807 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005808 sess->task,
5809 sess->task->state,
5810 sess->task->nice, sess->task->calls,
5811 sess->task->expire ?
5812 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5813 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5814 TICKS_TO_MS(1000)) : "<NEVER>",
5815 task_in_rq(sess->task) ? ", running" : "");
5816
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005817 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005818 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005819 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5820
Willy Tarreaueee5b512015-04-03 23:46:31 +02005821 if (sess->txn)
5822 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005823 " 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 +02005824 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
5825 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 +01005826
5827 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005828 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005829 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005830 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005831 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005832 obj_type_name(sess->si[0].end),
5833 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005834 sess->si[0].exp ?
5835 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5836 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5837 TICKS_TO_MS(1000)) : "<NEVER>",
5838 sess->si[0].err_type);
5839
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005840 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005841 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005842 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005843 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005844 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005845 obj_type_name(sess->si[1].end),
5846 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005847 sess->si[1].exp ?
5848 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5849 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5850 TICKS_TO_MS(1000)) : "<NEVER>",
5851 sess->si[1].err_type);
5852
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005853 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005854 chunk_appendf(&trash,
5855 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005856 conn,
5857 get_conn_ctrl_name(conn),
5858 get_conn_xprt_name(conn),
5859 get_conn_data_name(conn),
5860 obj_type_name(conn->target),
5861 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005862
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005863 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005864 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005865 conn->flags,
5866 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005867 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005868 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005869 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005870 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005871 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5872 chunk_appendf(&trash,
5873 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5874 tmpctx,
5875 tmpctx->st0,
5876 tmpctx->st1,
5877 tmpctx->st2,
5878 tmpctx->applet->name);
5879 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005880
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005881 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005882 chunk_appendf(&trash,
5883 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005884 conn,
5885 get_conn_ctrl_name(conn),
5886 get_conn_xprt_name(conn),
5887 get_conn_data_name(conn),
5888 obj_type_name(conn->target),
5889 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005890
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005891 chunk_appendf(&trash,
Willy Tarreauceeafb52016-01-25 15:27:17 +01005892 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005893 conn->flags,
5894 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005895 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005896 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005897 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005898 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005899 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
5900 chunk_appendf(&trash,
5901 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
5902 tmpctx,
5903 tmpctx->st0,
5904 tmpctx->st1,
5905 tmpctx->st2,
5906 tmpctx->applet->name);
5907 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005908
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005909 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005910 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005911 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005912 &sess->req,
5913 sess->req.flags, sess->req.analysers,
5914 sess->req.pipe ? sess->req.pipe->data : 0,
5915 sess->req.to_forward, sess->req.total,
5916 sess->req.analyse_exp ?
5917 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005918 TICKS_TO_MS(1000)) : "<NEVER>");
5919
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005920 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005921 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005922 sess->req.rex ?
5923 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005924 TICKS_TO_MS(1000)) : "<NEVER>");
5925
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005926 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005927 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005928 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005929 sess->req.wex ?
5930 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005931 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005932 sess->req.buf,
5933 sess->req.buf->data, sess->req.buf->o,
5934 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005935 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005936 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005937
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005938 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005939 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005940 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005941 &sess->res,
5942 sess->res.flags, sess->res.analysers,
5943 sess->res.pipe ? sess->res.pipe->data : 0,
5944 sess->res.to_forward, sess->res.total,
5945 sess->res.analyse_exp ?
5946 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005947 TICKS_TO_MS(1000)) : "<NEVER>");
5948
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005949 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005950 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005951 sess->res.rex ?
5952 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005953 TICKS_TO_MS(1000)) : "<NEVER>");
5954
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005955 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005956 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005957 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005958 sess->res.wex ?
5959 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005960 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005961 sess->res.buf,
5962 sess->res.buf->data, sess->res.buf->o,
5963 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005964 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005965 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005966
Willy Tarreaubc18da12015-03-13 14:00:47 +01005967 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005968 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005969 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005970 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005971
5972 /* use other states to dump the contents */
5973 }
5974 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005975 appctx->ctx.sess.uid = 0;
5976 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005977 return 1;
5978}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005979
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02005980#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
5981static int stats_tlskeys_list(struct stream_interface *si) {
5982 struct appctx *appctx = __objt_appctx(si->end);
5983
5984 switch (appctx->st2) {
5985 case STAT_ST_INIT:
5986 /* Display the column headers. If the message cannot be sent,
5987 * quit the fucntion with returning 0. The function is called
5988 * later and restart at the state "STAT_ST_INIT".
5989 */
5990 chunk_reset(&trash);
5991 chunk_appendf(&trash, "# id (file)\n");
5992 if (bi_putchk(si_ic(si), &trash) == -1) {
5993 si_applet_cant_put(si);
5994 return 0;
5995 }
5996
5997 /* Now, we start the browsing of the references lists.
5998 * Note that the following call to LIST_ELEM return bad pointer. The only
5999 * avalaible field of this pointer is <list>. It is used with the function
6000 * tlskeys_list_get_next() for retruning the first avalaible entry
6001 */
6002 appctx->ctx.tlskeys.ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
6003 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
6004
6005 appctx->st2 = STAT_ST_LIST;
6006 /* fall through */
6007
6008 case STAT_ST_LIST:
6009 while (appctx->ctx.tlskeys.ref) {
6010 chunk_reset(&trash);
6011
6012 chunk_appendf(&trash, "%d (%s)\n", appctx->ctx.tlskeys.ref->unique_id,
6013 appctx->ctx.tlskeys.ref->filename);
6014
6015 if (bi_putchk(si_ic(si), &trash) == -1) {
6016 /* let's try again later from this stream. We add ourselves into
6017 * this stream's users so that it can remove us upon termination.
6018 */
6019 si_applet_cant_put(si);
6020 return 0;
6021 }
6022
6023 /* get next list entry and check the end of the list */
6024 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
6025 }
6026
6027 appctx->st2 = STAT_ST_FIN;
6028 /* fall through */
6029
6030 default:
6031 appctx->st2 = STAT_ST_FIN;
6032 return 1;
6033 }
6034 return 0;
6035}
6036#endif
6037
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006038static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006039{
6040 struct appctx *appctx = __objt_appctx(si->end);
6041
6042 switch (appctx->st2) {
6043 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006044 /* Display the column headers. If the message cannot be sent,
6045 * quit the fucntion with returning 0. The function is called
6046 * later and restart at the state "STAT_ST_INIT".
6047 */
6048 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01006049 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006050 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006051 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006052 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006053 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006054
6055 /* Now, we start the browsing of the references lists.
6056 * Note that the following call to LIST_ELEM return bad pointer. The only
6057 * avalaible field of this pointer is <list>. It is used with the function
6058 * pat_list_get_next() for retruning the first avalaible entry
6059 */
6060 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
6061 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6062 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006063 appctx->st2 = STAT_ST_LIST;
6064 /* fall through */
6065
6066 case STAT_ST_LIST:
6067 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006068 chunk_reset(&trash);
6069
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006070 /* Build messages. If the reference is used by another category than
6071 * the listed categorie, display the information in the massage.
6072 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01006073 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01006074 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
6075 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006076
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006077 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006078 /* let's try again later from this stream. We add ourselves into
6079 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006080 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006081 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006082 return 0;
6083 }
6084
6085 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006086 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6087 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006088 }
6089
6090 appctx->st2 = STAT_ST_FIN;
6091 /* fall through */
6092
6093 default:
6094 appctx->st2 = STAT_ST_FIN;
6095 return 1;
6096 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006097 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006098}
6099
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006100static int stats_map_lookup(struct stream_interface *si)
6101{
6102 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006103 struct sample sample;
6104 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006105 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006106
6107 switch (appctx->st2) {
6108 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006109 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01006110 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006111 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006112 appctx->st2 = STAT_ST_LIST;
6113 /* fall through */
6114
6115 case STAT_ST_LIST:
6116 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006117 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006118 /* initialise chunk to build new message */
6119 chunk_reset(&trash);
6120
6121 /* execute pattern matching */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006122 sample.data.type = SMP_T_STR;
Andreas Seltenreich9727cf42016-03-03 19:32:25 +01006123 sample.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006124 sample.data.u.str.len = appctx->ctx.map.chunk.len;
6125 sample.data.u.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01006126 if (appctx->ctx.map.expr->pat_head->match &&
6127 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006128 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
6129 else
6130 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006131
6132 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006133 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
6134 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
6135 break;
6136 if (match_method >= PAT_MATCH_NUM)
6137 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
6138 else
6139 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006140
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006141 /* case sensitive */
6142 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
6143 chunk_appendf(&trash, ", case=insensitive");
6144 else
6145 chunk_appendf(&trash, ", case=sensitive");
6146
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006147 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006148 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006149 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6150 chunk_appendf(&trash, ", found=no");
6151 else
6152 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006153 }
6154
6155 /* Display match and match info */
6156 else {
6157 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006158 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6159 chunk_appendf(&trash, ", found=yes");
6160 else
6161 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006162
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006163 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006164 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006165 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006166 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006167 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006168
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006169 /* display pattern */
6170 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
6171 if (pat->ref && pat->ref->pattern)
6172 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
6173 else
6174 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006175 }
6176 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006177 if (pat->ref && pat->ref->pattern)
6178 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
6179 else
6180 chunk_appendf(&trash, ", pattern=unknown");
6181 }
6182
6183 /* display return value */
6184 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02006185 if (pat->data && pat->ref && pat->ref->sample)
Thierry FOURNIER03d0e452015-07-23 18:33:41 +02006186 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
Thierry FOURNIER7d4335c2015-08-19 09:05:25 +02006187 smp_to_type[pat->data->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006188 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006189 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006190 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006191 }
6192
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006193 chunk_appendf(&trash, "\n");
6194
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006195 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006196 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006197 /* let's try again later from this stream. We add ourselves into
6198 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006199 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006200 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006201 return 0;
6202 }
6203
6204 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006205 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
6206 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006207 }
6208
6209 appctx->st2 = STAT_ST_FIN;
6210 /* fall through */
6211
6212 default:
6213 appctx->st2 = STAT_ST_FIN;
6214 free(appctx->ctx.map.chunk.str);
6215 return 1;
6216 }
6217}
6218
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006219static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006220{
6221 struct appctx *appctx = __objt_appctx(si->end);
6222
6223 switch (appctx->st2) {
6224
6225 case STAT_ST_INIT:
6226 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006227 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
6228 struct pat_ref_elt *, list);
6229 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
6230 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006231 appctx->st2 = STAT_ST_LIST;
6232 /* fall through */
6233
6234 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006235 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006236 chunk_reset(&trash);
6237
6238 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006239 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006240 chunk_appendf(&trash, "%p %s %s\n",
6241 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
6242 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006243 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006244 chunk_appendf(&trash, "%p %s\n",
6245 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006246
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006247 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006248 /* let's try again later from this stream. We add ourselves into
6249 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006250 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006251 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006252 return 0;
6253 }
6254
6255 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006256 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
6257 struct pat_ref_elt *, list);
6258 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006259 break;
6260 }
6261
6262 appctx->st2 = STAT_ST_FIN;
6263 /* fall through */
6264
6265 default:
6266 appctx->st2 = STAT_ST_FIN;
6267 return 1;
6268 }
6269}
6270
Willy Tarreau87b09662015-04-03 00:22:06 +02006271/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006272 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006273 * to be called again, otherwise non-zero. It is designed to be called
6274 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006275 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006276static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006277{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006278 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006279 struct connection *conn;
6280
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006281 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006282 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02006283 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006284 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006285 if (appctx->st2 == STAT_ST_LIST) {
6286 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6287 LIST_DEL(&appctx->ctx.sess.bref.users);
6288 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006289 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006290 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006291 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006292 }
6293
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006294 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006295
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006296 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006297 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006298 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02006299 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006300 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02006301 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006302 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02006303 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006304 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006305 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02006306 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006307 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006308 /* fall through */
6309
Willy Tarreau295a8372011-03-10 11:25:07 +01006310 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02006311 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006312 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6313 LIST_DEL(&appctx->ctx.sess.bref.users);
6314 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006315 }
6316
6317 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02006318 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01006319 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02006320 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006321
Willy Tarreau87b09662015-04-03 00:22:06 +02006322 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006323
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006324 if (appctx->ctx.sess.target) {
6325 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006326 goto next_sess;
6327
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006328 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006329 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01006330 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006331 return 0;
6332
Willy Tarreau87b09662015-04-03 00:22:06 +02006333 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006334 LIST_DEL(&appctx->ctx.sess.bref.users);
6335 LIST_INIT(&appctx->ctx.sess.bref.users);
6336 if (appctx->ctx.sess.target != (void *)-1) {
6337 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01006338 break;
6339 }
6340 else
6341 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006342 }
6343
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006344 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006345 "%p: proto=%s",
6346 curr_sess,
Willy Tarreauf1e02122015-09-23 12:16:43 +02006347 strm_li(curr_sess) ? strm_li(curr_sess)->proto->name : "?");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006348
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006349 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006350 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02006351 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006352 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006353 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006354 " src=%s:%d fe=%s be=%s srv=%s",
6355 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006356 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006357 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006358 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006359 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006360 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006361 break;
6362 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006363 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02006364 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006365 strm_li(curr_sess)->luid,
6366 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006367 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006368 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02006369 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006370 break;
6371 }
6372
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006373 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02006374 " ts=%02x age=%s calls=%d",
6375 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01006376 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
6377 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006378
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006379 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006380 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006381 curr_sess->req.flags,
6382 curr_sess->req.buf->i,
6383 curr_sess->req.analysers,
6384 curr_sess->req.rex ?
6385 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006386 TICKS_TO_MS(1000)) : "");
6387
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006388 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006389 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006390 curr_sess->req.wex ?
6391 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006392 TICKS_TO_MS(1000)) : "");
6393
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006394 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006395 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006396 curr_sess->req.analyse_exp ?
6397 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006398 TICKS_TO_MS(1000)) : "");
6399
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006400 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006401 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006402 curr_sess->res.flags,
6403 curr_sess->res.buf->i,
6404 curr_sess->res.analysers,
6405 curr_sess->res.rex ?
6406 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006407 TICKS_TO_MS(1000)) : "");
6408
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006409 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006410 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006411 curr_sess->res.wex ?
6412 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006413 TICKS_TO_MS(1000)) : "");
6414
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006415 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006416 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006417 curr_sess->res.analyse_exp ?
6418 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006419 TICKS_TO_MS(1000)) : "");
6420
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006421 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006422 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006423 " s0=[%d,%1xh,fd=%d,ex=%s]",
6424 curr_sess->si[0].state,
6425 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006426 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006427 curr_sess->si[0].exp ?
6428 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
6429 TICKS_TO_MS(1000)) : "");
6430
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006431 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006432 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006433 " s1=[%d,%1xh,fd=%d,ex=%s]",
6434 curr_sess->si[1].state,
6435 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006436 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006437 curr_sess->si[1].exp ?
6438 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
6439 TICKS_TO_MS(1000)) : "");
6440
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006441 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006442 " exp=%s",
6443 curr_sess->task->expire ?
6444 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
6445 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01006446 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006447 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006448
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006449 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006450
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006451 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006452 /* let's try again later from this stream. We add ourselves into
6453 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006454 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006455 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006456 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006457 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006458 }
6459
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006460 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006461 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006462 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006463
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006464 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006465 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006466 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006467 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006468 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006469 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006470
Willy Tarreaubc18da12015-03-13 14:00:47 +01006471 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006472 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006473 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006474 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006475
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006476 appctx->ctx.sess.target = NULL;
6477 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006478 return 1;
6479 }
6480
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006481 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006482 /* fall through */
6483
6484 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006485 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006486 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006487 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02006488}
6489
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006490/* This is called when the stream interface is closed. For instance, upon an
6491 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02006492 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006493 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02006494static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006495{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006496 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
6497 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
6498 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006499 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006500 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
6501 free(appctx->ctx.cli.err);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006502 appctx->ctx.cli.err = NULL;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006503 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006504 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
6505 free(appctx->ctx.map.chunk.str);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006506 appctx->ctx.map.chunk.str = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006507 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006508}
6509
Willy Tarreau20e99322013-04-13 09:22:25 +02006510/* This function is used to either dump tables states (when action is set
6511 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02006512 * It returns 0 if the output buffer is full and it needs to be called
6513 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02006514 */
Willy Tarreau20e99322013-04-13 09:22:25 +02006515static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02006516{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006517 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02006518 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006519 struct ebmb_node *eb;
6520 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01006521 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02006522 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006523
6524 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006525 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01006526 * - STAT_ST_INIT : the first call
6527 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02006528 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006529 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02006530 * and the entry pointer points to the next entry to be dumped,
6531 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006532 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02006533 * data though.
6534 */
6535
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006536 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006537 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006538 if (appctx->st2 == STAT_ST_LIST) {
6539 appctx->ctx.table.entry->ref_cnt--;
6540 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02006541 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02006542 return 1;
6543 }
6544
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006545 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006546
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006547 while (appctx->st2 != STAT_ST_FIN) {
6548 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006549 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006550 appctx->ctx.table.proxy = appctx->ctx.table.target;
6551 if (!appctx->ctx.table.proxy)
6552 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006553
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006554 appctx->ctx.table.entry = NULL;
6555 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006556 break;
6557
Willy Tarreau295a8372011-03-10 11:25:07 +01006558 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006559 if (!appctx->ctx.table.proxy ||
6560 (appctx->ctx.table.target &&
6561 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6562 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006563 break;
6564 }
6565
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006566 if (appctx->ctx.table.proxy->table.size) {
6567 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6568 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006569 return 0;
6570
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006571 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006572 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006573 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006574 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006575 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006576 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6577 appctx->ctx.table.entry->ref_cnt++;
6578 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006579 break;
6580 }
6581 }
6582 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006583 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006584 break;
6585
Willy Tarreau295a8372011-03-10 11:25:07 +01006586 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006587 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006588
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006589 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006590 /* we're filtering on some data contents */
6591 void *ptr;
6592 long long data;
6593
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006594 dt = appctx->ctx.table.data_type;
6595 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6596 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006597 dt);
6598
6599 data = 0;
6600 switch (stktable_data_types[dt].std_type) {
6601 case STD_T_SINT:
6602 data = stktable_data_cast(ptr, std_t_sint);
6603 break;
6604 case STD_T_UINT:
6605 data = stktable_data_cast(ptr, std_t_uint);
6606 break;
6607 case STD_T_ULL:
6608 data = stktable_data_cast(ptr, std_t_ull);
6609 break;
6610 case STD_T_FRQP:
6611 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006612 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006613 break;
6614 }
6615
6616 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006617 if ((data < appctx->ctx.table.value &&
6618 (appctx->ctx.table.data_op == STD_OP_EQ ||
6619 appctx->ctx.table.data_op == STD_OP_GT ||
6620 appctx->ctx.table.data_op == STD_OP_GE)) ||
6621 (data == appctx->ctx.table.value &&
6622 (appctx->ctx.table.data_op == STD_OP_NE ||
6623 appctx->ctx.table.data_op == STD_OP_GT ||
6624 appctx->ctx.table.data_op == STD_OP_LT)) ||
6625 (data > appctx->ctx.table.value &&
6626 (appctx->ctx.table.data_op == STD_OP_EQ ||
6627 appctx->ctx.table.data_op == STD_OP_LT ||
6628 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006629 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006630 }
6631
Simon Hormanc88b8872011-06-15 15:18:49 +09006632 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006633 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6634 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006635 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006636
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006637 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006638
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006639 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006640 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006641 struct stksess *old = appctx->ctx.table.entry;
6642 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006643 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006644 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6645 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6646 stksess_kill(&appctx->ctx.table.proxy->table, old);
6647 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006648 break;
6649 }
6650
Simon Hormanc88b8872011-06-15 15:18:49 +09006651
6652 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006653 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6654 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6655 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006656
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006657 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6658 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006659 break;
6660
Willy Tarreau295a8372011-03-10 11:25:07 +01006661 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006662 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006663 break;
6664 }
6665 }
6666 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006667}
6668
Willy Tarreaud426a182010-03-05 14:58:26 +01006669/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006670 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6671 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6672 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6673 * lines are respected within the limit of 70 output chars. Lines that are
6674 * continuation of a previous truncated line begin with "+" instead of " "
6675 * after the offset. The new pointer is returned.
6676 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006677static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6678 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006679{
6680 int end;
6681 unsigned char c;
6682
6683 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006684 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006685 return ptr;
6686
Willy Tarreau77804732012-10-29 16:14:26 +01006687 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006688
Willy Tarreaud426a182010-03-05 14:58:26 +01006689 while (ptr < len && ptr < bsize) {
6690 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006691 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006692 if (out->len > end - 2)
6693 break;
6694 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006695 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006696 if (out->len > end - 3)
6697 break;
6698 out->str[out->len++] = '\\';
6699 switch (c) {
6700 case '\t': c = 't'; break;
6701 case '\n': c = 'n'; break;
6702 case '\r': c = 'r'; break;
6703 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006704 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006705 }
6706 out->str[out->len++] = c;
6707 } else {
6708 if (out->len > end - 5)
6709 break;
6710 out->str[out->len++] = '\\';
6711 out->str[out->len++] = 'x';
6712 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6713 out->str[out->len++] = hextab[c & 0xF];
6714 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006715 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006716 /* we had a line break, let's return now */
6717 out->str[out->len++] = '\n';
6718 *line = ptr;
6719 return ptr;
6720 }
6721 }
6722 /* we have an incomplete line, we return it as-is */
6723 out->str[out->len++] = '\n';
6724 return ptr;
6725}
6726
Baptiste Assmann3863f972015-05-17 00:33:24 +02006727/* This function dumps counters from all resolvers section and associated name servers.
6728 * It returns 0 if the output buffer is full and it needs
6729 * to be called again, otherwise non-zero.
6730 */
6731static int stats_dump_resolvers_to_buffer(struct stream_interface *si)
6732{
6733 struct appctx *appctx = __objt_appctx(si->end);
6734 struct dns_resolvers *presolvers;
6735 struct dns_nameserver *pnameserver;
6736
6737 chunk_reset(&trash);
6738
6739 switch (appctx->st2) {
6740 case STAT_ST_INIT:
6741 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
6742 /* fall through */
6743
6744 case STAT_ST_LIST:
Andrew Hayworth68d05342015-10-02 20:33:01 +00006745 if (LIST_ISEMPTY(&dns_resolvers)) {
6746 chunk_appendf(&trash, "No resolvers found\n");
6747 }
6748 else {
6749 list_for_each_entry(presolvers, &dns_resolvers, list) {
6750 if (appctx->ctx.resolvers.ptr != NULL && appctx->ctx.resolvers.ptr != presolvers)
6751 continue;
6752
6753 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
6754 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
6755 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
6756 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
6757 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
6758 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
6759 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
6760 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
6761 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
6762 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
6763 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
6764 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
6765 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
6766 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
6767 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
6768 chunk_appendf(&trash, " truncated: %ld\n", pnameserver->counters.truncated);
6769 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
6770 }
6771 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02006772 }
6773
6774 /* display response */
6775 if (bi_putchk(si_ic(si), &trash) == -1) {
6776 /* let's try again later from this session. We add ourselves into
6777 * this session's users so that it can remove us upon termination.
6778 */
6779 si->flags |= SI_FL_WAIT_ROOM;
6780 return 0;
6781 }
6782
6783 appctx->st2 = STAT_ST_FIN;
6784 /* fall through */
6785
6786 default:
6787 appctx->st2 = STAT_ST_FIN;
6788 return 1;
6789 }
6790}
6791
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006792/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006793 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006794 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006795 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006796static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006797{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006798 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006799 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006800
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006801 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02006802 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006803
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006804 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006805
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006806 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006807 /* the function had not been called yet, let's prepare the
6808 * buffer for a response.
6809 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006810 struct tm tm;
6811
6812 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006813 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006814 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6815 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6816 error_snapshot_id);
6817
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006818 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01006819 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006820 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01006821 return 0;
6822 }
6823
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006824 appctx->ctx.errors.px = proxy;
6825 appctx->ctx.errors.buf = 0;
6826 appctx->ctx.errors.bol = 0;
6827 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006828 }
6829
6830 /* we have two inner loops here, one for the proxy, the other one for
6831 * the buffer.
6832 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006833 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006834 struct error_snapshot *es;
6835
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006836 if (appctx->ctx.errors.buf == 0)
6837 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006838 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006839 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006840
6841 if (!es->when.tv_sec)
6842 goto next;
6843
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006844 if (appctx->ctx.errors.iid >= 0 &&
6845 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6846 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006847 goto next;
6848
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006849 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006850 /* just print headers now */
6851
6852 char pn[INET6_ADDRSTRLEN];
6853 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006854 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006855
6856 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006857 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006858 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006859 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006860
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006861 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6862 case AF_INET:
6863 case AF_INET6:
6864 port = get_host_port(&es->src);
6865 break;
6866 default:
6867 port = 0;
6868 }
6869
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006870 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006871 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006872 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006873 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006874 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006875 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006876 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
6877 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006878 break;
6879 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006880 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02006881 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006882 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006883 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006884 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006885 break;
6886 }
6887
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006888 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006889 ", server %s (#%d), event #%u\n"
6890 " src %s:%d, session #%d, session flags 0x%08x\n"
6891 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
6892 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
6893 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
6894 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
6895 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
6896 es->ev_id,
6897 pn, port, es->sid, es->s_flags,
6898 es->state, es->m_flags, es->t_flags,
6899 es->m_clen, es->m_blen,
6900 es->b_flags, es->b_out, es->b_tot,
6901 es->len, es->b_wrap, es->pos);
6902
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006903 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006904 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006905 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006906 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006907 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006908 appctx->ctx.errors.ptr = 0;
6909 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006910 }
6911
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006912 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006913 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006914 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006915 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006916 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006917 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006918 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006919 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01006920 goto next;
6921 }
6922
6923 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006924 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006925 int newptr;
6926 int newline;
6927
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006928 newline = appctx->ctx.errors.bol;
6929 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
6930 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02006931 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006932
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006933 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006934 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006935 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006936 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006937 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006938 appctx->ctx.errors.ptr = newptr;
6939 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006940 };
6941 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006942 appctx->ctx.errors.bol = 0;
6943 appctx->ctx.errors.ptr = -1;
6944 appctx->ctx.errors.buf++;
6945 if (appctx->ctx.errors.buf > 1) {
6946 appctx->ctx.errors.buf = 0;
6947 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006948 }
Willy Tarreauae795722016-02-16 11:27:28 +01006949 }
6950
6951 /* dump complete */
6952 return 1;
6953}
6954
6955/* This function dumps all environmnent variables to the buffer. It returns 0
6956 * if the output buffer is full and it needs to be called again, otherwise
6957 * non-zero. Dumps only one entry if st2 == STAT_ST_END.
6958 */
6959static int stats_dump_env_to_buffer(struct stream_interface *si)
6960{
6961 struct appctx *appctx = __objt_appctx(si->end);
6962
6963 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
6964 return 1;
6965
6966 chunk_reset(&trash);
6967
6968 /* we have two inner loops here, one for the proxy, the other one for
6969 * the buffer.
6970 */
6971 while (*appctx->ctx.env.var) {
6972 chunk_printf(&trash, "%s\n", *appctx->ctx.env.var);
6973
6974 if (bi_putchk(si_ic(si), &trash) == -1) {
6975 si_applet_cant_put(si);
6976 return 0;
6977 }
6978 if (appctx->st2 == STAT_ST_END)
6979 break;
6980 appctx->ctx.env.var++;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006981 }
6982
6983 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02006984 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006985}
6986
Willy Tarreaud5781202012-09-22 19:32:35 +02006987/* parse the "level" argument on the bind lines */
6988static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6989{
6990 if (!*args[cur_arg + 1]) {
6991 memprintf(err, "'%s' : missing level", args[cur_arg]);
6992 return ERR_ALERT | ERR_FATAL;
6993 }
6994
6995 if (!strcmp(args[cur_arg+1], "user"))
6996 conf->level = ACCESS_LVL_USER;
6997 else if (!strcmp(args[cur_arg+1], "operator"))
6998 conf->level = ACCESS_LVL_OPER;
6999 else if (!strcmp(args[cur_arg+1], "admin"))
7000 conf->level = ACCESS_LVL_ADMIN;
7001 else {
7002 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
7003 args[cur_arg], args[cur_arg+1]);
7004 return ERR_ALERT | ERR_FATAL;
7005 }
7006
7007 return 0;
7008}
7009
Willy Tarreau30576452015-04-13 13:50:30 +02007010struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007011 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007012 .name = "<STATS>", /* used for logging */
7013 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07007014 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007015};
7016
Willy Tarreau30576452015-04-13 13:50:30 +02007017static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007018 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007019 .name = "<CLI>", /* used for logging */
7020 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01007021 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007022};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01007023
Willy Tarreaudc13c112013-06-21 23:16:39 +02007024static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02007025 { CFG_GLOBAL, "stats", stats_parse_global },
7026 { 0, NULL, NULL },
7027}};
7028
Willy Tarreaud5781202012-09-22 19:32:35 +02007029static struct bind_kw_list bind_kws = { "STAT", { }, {
7030 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
7031 { NULL, NULL, 0 },
7032}};
7033
Willy Tarreau10522fd2008-07-09 20:12:41 +02007034__attribute__((constructor))
7035static void __dumpstats_module_init(void)
7036{
7037 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02007038 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02007039}
7040
Willy Tarreau91861262007-10-17 17:06:05 +02007041/*
7042 * Local variables:
7043 * c-indent-level: 8
7044 * c-basic-offset: 8
7045 * End:
7046 */