blob: b6145a76eb4d44011cd0dbfb999386b2c387f465 [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,
Willy Tarreau7f618842016-01-08 11:40:03 +0100325 ST_F_AGENT_STATUS,
326 ST_F_AGENT_CODE,
327 ST_F_AGENT_DURATION,
Willy Tarreaudd7354b2016-01-08 13:47:26 +0100328 ST_F_CHECK_DESC,
329 ST_F_AGENT_DESC,
Willy Tarreau3141f592016-01-08 14:25:28 +0100330 ST_F_CHECK_RISE,
331 ST_F_CHECK_FALL,
332 ST_F_CHECK_HEALTH,
333 ST_F_AGENT_RISE,
334 ST_F_AGENT_FALL,
335 ST_F_AGENT_HEALTH,
Willy Tarreau3a4ec3a2016-01-08 15:35:43 +0100336 ST_F_ADDR,
Willy Tarreau8e205142016-01-04 17:23:25 +0100337
338 /* must always be the last one */
339 ST_F_TOTAL_FIELDS
340};
341
342/* These are the field names for each ST_F_* field position. Please pay attention
343 * to always use the exact same name except that the strings must be lower case
344 * while the enum entries must be upper case.
345 */
346const char *stat_field_names[ST_F_TOTAL_FIELDS] = {
347 [ST_F_PXNAME] = "pxname",
348 [ST_F_SVNAME] = "svname",
349 [ST_F_QCUR] = "qcur",
350 [ST_F_QMAX] = "qmax",
351 [ST_F_SCUR] = "scur",
352 [ST_F_SMAX] = "smax",
353 [ST_F_SLIM] = "slim",
354 [ST_F_STOT] = "stot",
355 [ST_F_BIN] = "bin",
356 [ST_F_BOUT] = "bout",
357 [ST_F_DREQ] = "dreq",
358 [ST_F_DRESP] = "dresp",
359 [ST_F_EREQ] = "ereq",
360 [ST_F_ECON] = "econ",
361 [ST_F_ERESP] = "eresp",
362 [ST_F_WRETR] = "wretr",
363 [ST_F_WREDIS] = "wredis",
364 [ST_F_STATUS] = "status",
365 [ST_F_WEIGHT] = "weight",
366 [ST_F_ACT] = "act",
367 [ST_F_BCK] = "bck",
368 [ST_F_CHKFAIL] = "chkfail",
369 [ST_F_CHKDOWN] = "chkdown",
370 [ST_F_LASTCHG] = "lastchg",
371 [ST_F_DOWNTIME] = "downtime",
372 [ST_F_QLIMIT] = "qlimit",
373 [ST_F_PID] = "pid",
374 [ST_F_IID] = "iid",
375 [ST_F_SID] = "sid",
376 [ST_F_THROTTLE] = "throttle",
377 [ST_F_LBTOT] = "lbtot",
378 [ST_F_TRACKED] = "tracked",
379 [ST_F_TYPE] = "type",
380 [ST_F_RATE] = "rate",
381 [ST_F_RATE_LIM] = "rate_lim",
382 [ST_F_RATE_MAX] = "rate_max",
383 [ST_F_CHECK_STATUS] = "check_status",
384 [ST_F_CHECK_CODE] = "check_code",
385 [ST_F_CHECK_DURATION] = "check_duration",
386 [ST_F_HRSP_1XX] = "hrsp_1xx",
387 [ST_F_HRSP_2XX] = "hrsp_2xx",
388 [ST_F_HRSP_3XX] = "hrsp_3xx",
389 [ST_F_HRSP_4XX] = "hrsp_4xx",
390 [ST_F_HRSP_5XX] = "hrsp_5xx",
391 [ST_F_HRSP_OTHER] = "hrsp_other",
392 [ST_F_HANAFAIL] = "hanafail",
393 [ST_F_REQ_RATE] = "req_rate",
394 [ST_F_REQ_RATE_MAX] = "req_rate_max",
395 [ST_F_REQ_TOT] = "req_tot",
396 [ST_F_CLI_ABRT] = "cli_abrt",
397 [ST_F_SRV_ABRT] = "srv_abrt",
398 [ST_F_COMP_IN] = "comp_in",
399 [ST_F_COMP_OUT] = "comp_out",
400 [ST_F_COMP_BYP] = "comp_byp",
401 [ST_F_COMP_RSP] = "comp_rsp",
402 [ST_F_LASTSESS] = "lastsess",
403 [ST_F_LAST_CHK] = "last_chk",
404 [ST_F_LAST_AGT] = "last_agt",
405 [ST_F_QTIME] = "qtime",
406 [ST_F_CTIME] = "ctime",
407 [ST_F_RTIME] = "rtime",
408 [ST_F_TTIME] = "ttime",
Willy Tarreau7f618842016-01-08 11:40:03 +0100409 [ST_F_AGENT_STATUS] = "agent_status",
410 [ST_F_AGENT_CODE] = "agent_code",
411 [ST_F_AGENT_DURATION] = "agent_duration",
Willy Tarreaudd7354b2016-01-08 13:47:26 +0100412 [ST_F_CHECK_DESC] = "check_desc",
413 [ST_F_AGENT_DESC] = "agent_desc",
Willy Tarreau3141f592016-01-08 14:25:28 +0100414 [ST_F_CHECK_RISE] = "check_rise",
415 [ST_F_CHECK_FALL] = "check_fall",
416 [ST_F_CHECK_HEALTH] = "check_health",
417 [ST_F_AGENT_RISE] = "agent_rise",
418 [ST_F_AGENT_FALL] = "agent_fall",
419 [ST_F_AGENT_HEALTH] = "agent_health",
Willy Tarreau3a4ec3a2016-01-08 15:35:43 +0100420 [ST_F_ADDR] = "addr",
Willy Tarreau8e205142016-01-04 17:23:25 +0100421};
422
Willy Tarreau82a86022016-01-04 19:04:18 +0100423/* one line of stats */
424static struct field stats[ST_F_TOTAL_FIELDS];
425
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200426static int stats_dump_backend_to_buffer(struct stream_interface *si);
Willy Tarreauae795722016-02-16 11:27:28 +0100427static int stats_dump_env_to_buffer(struct stream_interface *si);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100428static int stats_dump_info_to_buffer(struct stream_interface *si);
Baptiste Assmann28289462015-07-03 08:01:20 +0200429static int stats_dump_servers_state_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100430static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau87b09662015-04-03 00:22:06 +0200431static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900432static int stats_dump_sess_to_buffer(struct stream_interface *si);
433static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100434static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100435static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
436static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Baptiste Assmann3863f972015-05-17 00:33:24 +0200437static int stats_dump_resolvers_to_buffer(struct stream_interface *si);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100438static int stats_pats_list(struct stream_interface *si);
439static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100440static int stats_map_lookup(struct stream_interface *si);
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200441#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
442static int stats_tlskeys_list(struct stream_interface *si);
443#endif
Willy Tarreau00a37f02015-04-13 12:05:19 +0200444static void cli_release_handler(struct appctx *appctx);
Simon Horman9bd2c732011-06-15 15:18:44 +0900445
Baptiste Assmann28289462015-07-03 08:01:20 +0200446static void dump_servers_state(struct proxy *backend, struct chunk *buf);
447
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100448/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100449 * cli_io_handler()
450 * -> stats_dump_sess_to_buffer() // "show sess"
451 * -> stats_dump_errors_to_buffer() // "show errors"
452 * -> stats_dump_info_to_buffer() // "show info"
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200453 * -> stats_dump_backend_to_buffer() // "show backend"
Baptiste Assmann28289462015-07-03 08:01:20 +0200454 * -> stats_dump_servers_state_to_buffer() // "show servers state [<backend name>]"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100455 * -> stats_dump_stat_to_buffer() // "show stat"
Baptiste Assmann3863f972015-05-17 00:33:24 +0200456 * -> stats_dump_resolvers_to_buffer() // "show stat resolver <id>"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100457 * -> stats_dump_csv_header()
458 * -> stats_dump_proxy_to_buffer()
459 * -> stats_dump_fe_stats()
460 * -> stats_dump_li_stats()
461 * -> stats_dump_sv_stats()
462 * -> stats_dump_be_stats()
463 *
464 * http_stats_io_handler()
465 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
466 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
467 * -> stats_dump_html_head() // emits the HTML headers
468 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
469 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
470 * -> stats_dump_html_px_hdr()
471 * -> stats_dump_fe_stats()
472 * -> stats_dump_li_stats()
473 * -> stats_dump_sv_stats()
474 * -> stats_dump_be_stats()
475 * -> stats_dump_html_px_end()
476 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100477 */
478
Willy Tarreau30576452015-04-13 13:50:30 +0200479static struct applet cli_applet;
Simon Horman9bd2c732011-06-15 15:18:44 +0900480
481static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200482 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200483 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200484 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200485 " help : this message\n"
486 " prompt : toggle interactive mode with prompt\n"
487 " quit : disconnect\n"
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200488 " show backend : list backends in the current running config\n"
Willy Tarreauae795722016-02-16 11:27:28 +0100489 " show env [var] : dump environment variables known to the process\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200490 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100491 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200492 " show stat : report counters for each proxy and server\n"
493 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100494 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200495 " show table [id]: report table usage stats or dump this table's contents\n"
Baptiste Assmann28289462015-07-03 08:01:20 +0200496 " show servers state [id]: dump volatile server information (for backend <id>)\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200497 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200498 " set weight : change a server's weight\n"
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200499 " set server : change a server's state, weight or address\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200500 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100501 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200502 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200503 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200504 " disable : put a server or frontend in maintenance mode\n"
505 " enable : re-enable a server or frontend which is in maintenance mode\n"
506 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100507 " show acl [id] : report avalaible acls or dump an acl's contents\n"
508 " get acl : reports the patterns matching a sample for an ACL\n"
509 " add acl : add acl entry\n"
510 " del acl : delete acl entry\n"
511 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100512 " show map [id] : report avalaible maps or dump a map's contents\n"
513 " get map : reports the keys and values matching a sample for a map\n"
514 " set map : modify map entry\n"
515 " add map : add map entry\n"
516 " del map : delete map entry\n"
517 " clear map <id> : clear the content of this map\n"
Emeric Brun4147b2e2014-06-16 18:36:30 +0200518 " set ssl <stmt> : set statement for ssl\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200519 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200520
Simon Horman9bd2c732011-06-15 15:18:44 +0900521static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200522 "Permission denied\n"
523 "";
524
Willy Tarreau295a8372011-03-10 11:25:07 +0100525/* data transmission states for the stats responses */
526enum {
527 STAT_ST_INIT = 0,
528 STAT_ST_HEAD,
529 STAT_ST_INFO,
530 STAT_ST_LIST,
531 STAT_ST_END,
532 STAT_ST_FIN,
533};
534
535/* data transmission states for the stats responses inside a proxy */
536enum {
537 STAT_PX_ST_INIT = 0,
538 STAT_PX_ST_TH,
539 STAT_PX_ST_FE,
540 STAT_PX_ST_LI,
541 STAT_PX_ST_SV,
542 STAT_PX_ST_BE,
543 STAT_PX_ST_END,
544 STAT_PX_ST_FIN,
545};
546
Cyril Bonté19979e12012-04-04 12:57:21 +0200547extern const char *stat_status_codes[];
548
Willy Tarreau07e9e642010-08-17 21:48:17 +0200549/* allocate a new stats frontend named <name>, and return it
550 * (or NULL in case of lack of memory).
551 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200552static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200553{
554 struct proxy *fe;
555
556 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
557 if (!fe)
558 return NULL;
559
Willy Tarreau237250c2011-07-29 01:49:03 +0200560 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200561 fe->next = proxy;
562 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200563 fe->last_change = now.tv_sec;
564 fe->id = strdup("GLOBAL");
565 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200566 fe->maxconn = 10; /* default to 10 concurrent connections */
567 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200568 fe->conf.file = strdup(file);
569 fe->conf.line = line;
Willy Tarreau32b60d42015-03-13 16:14:57 +0100570 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +0100571 fe->default_target = &cli_applet.obj_type;
Willy Tarreau050536d2012-10-04 08:47:34 +0200572
573 /* the stats frontend is the only one able to assign ID #0 */
574 fe->conf.id.key = fe->uuid = 0;
575 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200576 return fe;
577}
578
Willy Tarreaufbee7132007-10-18 13:53:22 +0200579/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200580 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
581 * error message into the <err> buffer which will be preallocated. The trailing
582 * '\n' must not be written. The function must be called with <args> pointing to
583 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200584 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200585static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200586 struct proxy *defpx, const char *file, int line,
587 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200588{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200589 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200590 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200591
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200592 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200593 int cur_arg;
594
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200595 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200596 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 +0200597 return -1;
598 }
599
Willy Tarreau89a63132009-08-16 17:41:45 +0200600 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200601 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200602 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200603 return -1;
604 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200605 }
606
Willy Tarreau4348fad2012-09-20 16:48:07 +0200607 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200608 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200609
Willy Tarreauc53d4222012-09-20 20:19:28 +0200610 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
611 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
612 file, line, args[0], args[1], err && *err ? *err : "error");
613 return -1;
614 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200615
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200616 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200617 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200618 static int bind_dumped;
619 struct bind_kw *kw;
620
621 kw = bind_find_kw(args[cur_arg]);
622 if (kw) {
623 if (!kw->parse) {
624 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
625 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200626 return -1;
627 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200628
Willy Tarreau061b5de2015-10-13 15:06:57 +0200629 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200630 if (err && *err)
631 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
632 else
633 memprintf(err, "'%s %s' : error encountered while processing '%s'",
634 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200635 return -1;
636 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200637
638 cur_arg += 1 + kw->skip;
639 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200640 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200641
642 if (!bind_dumped) {
643 bind_dump_kws(err);
644 indent_msg(err, 4);
645 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200646 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200647
648 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
649 args[0], args[1], args[cur_arg],
650 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
651 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200652 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100653
Willy Tarreauc53d4222012-09-20 20:19:28 +0200654 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
655 l->maxconn = global.stats_fe->maxconn;
656 l->backlog = global.stats_fe->backlog;
Willy Tarreau9903f0e2015-04-04 18:50:31 +0200657 l->accept = session_accept_fd;
Willy Tarreau87b09662015-04-03 00:22:06 +0200658 l->handler = process_stream;
Willy Tarreau10b688f2015-03-13 16:43:12 +0100659 l->default_target = global.stats_fe->default_target;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200660 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
661 l->nice = -64; /* we want to boost priority for local stats */
662 global.maxsock += l->maxconn;
663 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200664 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200665 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100666 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200667 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100668
669 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200670 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100671 return -1;
672 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200673
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100674 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200675 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200676 return -1;
677 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200678 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200679 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200680 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200681 return -1;
682 }
683 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200684 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200685 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200686 else if (!strcmp(args[1], "maxconn")) {
687 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200688
689 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200690 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200691 return -1;
692 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200693
694 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200695 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200696 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200697 return -1;
698 }
699 }
700 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200701 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200702 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
703 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100704 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200705
Willy Tarreau91319572013-04-20 09:48:50 +0200706 if (!global.stats_fe) {
707 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
708 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
709 return -1;
710 }
711 }
712
Willy Tarreau35b7b162012-10-22 23:17:18 +0200713 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100714 unsigned int low, high;
715
Willy Tarreau35b7b162012-10-22 23:17:18 +0200716 if (strcmp(args[cur_arg], "all") == 0) {
717 set = 0;
718 break;
719 }
720 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100721 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200722 }
723 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100724 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200725 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100726 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100727 char *dash = strchr(args[cur_arg], '-');
728
729 low = high = str2uic(args[cur_arg]);
730 if (dash)
731 high = str2uic(dash + 1);
732
733 if (high < low) {
734 unsigned int swap = low;
735 low = high;
736 high = swap;
737 }
738
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100739 if (low < 1 || high > LONGBITS) {
740 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
741 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200742 return -1;
743 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100744 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100745 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100746 }
747 else {
748 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100749 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
750 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100751 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200752 }
753 cur_arg++;
754 }
755 global.stats_fe->bind_proc = set;
756 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200757 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200758 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200759 return -1;
760 }
761 return 0;
762}
763
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100764/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
765 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100766 * NOTE: Some tools happen to rely on the field position instead of its name,
767 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100768 */
769static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100770{
Willy Tarreauf6142292016-01-04 17:24:29 +0100771 int field;
772
773 chunk_appendf(&trash, "# ");
774 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
775 chunk_appendf(&trash, "%s,", stat_field_names[field]);
776
777 chunk_appendf(&trash, "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100778}
779
Simon Hormand9366582011-06-15 15:18:45 +0900780/* print a string of text buffer to <out>. The format is :
781 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
782 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
783 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
784 */
785static int dump_text(struct chunk *out, const char *buf, int bsize)
786{
787 unsigned char c;
788 int ptr = 0;
789
790 while (buf[ptr] && ptr < bsize) {
791 c = buf[ptr];
792 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
793 if (out->len > out->size - 1)
794 break;
795 out->str[out->len++] = c;
796 }
797 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
798 if (out->len > out->size - 2)
799 break;
800 out->str[out->len++] = '\\';
801 switch (c) {
802 case ' ': c = ' '; break;
803 case '\t': c = 't'; break;
804 case '\n': c = 'n'; break;
805 case '\r': c = 'r'; break;
806 case '\e': c = 'e'; break;
807 case '\\': c = '\\'; break;
808 }
809 out->str[out->len++] = c;
810 }
811 else {
812 if (out->len > out->size - 4)
813 break;
814 out->str[out->len++] = '\\';
815 out->str[out->len++] = 'x';
816 out->str[out->len++] = hextab[(c >> 4) & 0xF];
817 out->str[out->len++] = hextab[c & 0xF];
818 }
819 ptr++;
820 }
821
822 return ptr;
823}
824
825/* print a buffer in hexa.
826 * Print stopped if <bsize> is reached, or if no more place in the chunk.
827 */
828static int dump_binary(struct chunk *out, const char *buf, int bsize)
829{
830 unsigned char c;
831 int ptr = 0;
832
833 while (ptr < bsize) {
834 c = buf[ptr];
835
836 if (out->len > out->size - 2)
837 break;
838 out->str[out->len++] = hextab[(c >> 4) & 0xF];
839 out->str[out->len++] = hextab[c & 0xF];
840
841 ptr++;
842 }
843 return ptr;
844}
845
846/* Dump the status of a table to a stream interface's
847 * read buffer. It returns 0 if the output buffer is full
848 * and needs to be called again, otherwise non-zero.
849 */
850static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
851 struct proxy *proxy, struct proxy *target)
852{
Willy Tarreau87b09662015-04-03 00:22:06 +0200853 struct stream *s = si_strm(si);
Simon Hormand9366582011-06-15 15:18:45 +0900854
Willy Tarreau77804732012-10-29 16:14:26 +0100855 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900856 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
857
858 /* any other information should be dumped here */
859
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200860 if (target && strm_li(s)->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100861 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900862
Willy Tarreaubc18da12015-03-13 14:00:47 +0100863 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200864 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900865 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100866 }
Simon Hormand9366582011-06-15 15:18:45 +0900867
868 return 1;
869}
870
871/* Dump the a table entry to a stream interface's
872 * read buffer. It returns 0 if the output buffer is full
873 * and needs to be called again, otherwise non-zero.
874 */
875static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
876 struct proxy *proxy, struct stksess *entry)
877{
878 int dt;
879
Willy Tarreau77804732012-10-29 16:14:26 +0100880 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900881
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200882 if (proxy->table.type == SMP_T_IPV4) {
Simon Hormand9366582011-06-15 15:18:45 +0900883 char addr[INET_ADDRSTRLEN];
884 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100885 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900886 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200887 else if (proxy->table.type == SMP_T_IPV6) {
Simon Hormand9366582011-06-15 15:18:45 +0900888 char addr[INET6_ADDRSTRLEN];
889 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100890 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900891 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200892 else if (proxy->table.type == SMP_T_SINT) {
Willy Tarreau77804732012-10-29 16:14:26 +0100893 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900894 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200895 else if (proxy->table.type == SMP_T_STR) {
Willy Tarreau77804732012-10-29 16:14:26 +0100896 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900897 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
898 }
899 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100900 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900901 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
902 }
903
Willy Tarreau77804732012-10-29 16:14:26 +0100904 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900905
906 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
907 void *ptr;
908
909 if (proxy->table.data_ofs[dt] == 0)
910 continue;
911 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100912 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900913 else
Willy Tarreau77804732012-10-29 16:14:26 +0100914 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900915
916 ptr = stktable_data_ptr(&proxy->table, entry, dt);
917 switch (stktable_data_types[dt].std_type) {
918 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100919 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900920 break;
921 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100922 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900923 break;
924 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100925 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900926 break;
927 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100928 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900929 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
930 proxy->table.data_arg[dt].u));
931 break;
932 }
933 }
Willy Tarreau77804732012-10-29 16:14:26 +0100934 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900935
Willy Tarreaubc18da12015-03-13 14:00:47 +0100936 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200937 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900938 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100939 }
Simon Hormand9366582011-06-15 15:18:45 +0900940
941 return 1;
942}
943
Willy Tarreaudec98142012-06-06 23:37:08 +0200944static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900945{
Willy Tarreau87b09662015-04-03 00:22:06 +0200946 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100947 struct appctx *appctx = __objt_appctx(si->end);
948 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900949 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900950 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900951 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200952 long long value;
953 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200954 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200955 void *ptr;
956 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900957
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100958 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900959
960 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100961 appctx->ctx.cli.msg = "Key value expected\n";
962 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900963 return;
964 }
965
Simon Hormanc5b89f62011-06-15 15:18:50 +0900966 switch (px->table.type) {
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200967 case SMP_T_IPV4:
Simon Hormancec9a222011-06-15 15:18:51 +0900968 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100969 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900970 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200971 case SMP_T_IPV6:
Simon Hormanc5b89f62011-06-15 15:18:50 +0900972 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100973 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900974 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200975 case SMP_T_SINT:
Simon Hormancec9a222011-06-15 15:18:51 +0900976 {
977 char *endptr;
978 unsigned long val;
979 errno = 0;
980 val = strtoul(args[4], &endptr, 10);
981 if ((errno == ERANGE && val == ULONG_MAX) ||
982 (errno != 0 && val == 0) || endptr == args[4] ||
983 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100984 appctx->ctx.cli.msg = "Invalid key\n";
985 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900986 return;
987 }
988 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100989 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900990 break;
991 }
992 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200993 case SMP_T_STR:
Willy Tarreau07115412012-10-29 21:56:59 +0100994 static_table_key->key = args[4];
995 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900996 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900997 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200998 switch (action) {
999 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001000 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 +02001001 break;
1002 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001003 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 +02001004 break;
1005 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001006 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +02001007 break;
1008 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001009 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +09001010 return;
1011 }
1012
1013 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001014 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001015 appctx->ctx.cli.msg = stats_permission_denied_msg;
1016 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +09001017 return;
1018 }
1019
Willy Tarreau07115412012-10-29 21:56:59 +01001020 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +09001021
Willy Tarreaudec98142012-06-06 23:37:08 +02001022 switch (action) {
1023 case STAT_CLI_O_TAB:
1024 if (!ts)
1025 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001026 chunk_reset(&trash);
1027 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +09001028 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001029 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +09001030 return;
Willy Tarreaudec98142012-06-06 23:37:08 +02001031
1032 case STAT_CLI_O_CLR:
1033 if (!ts)
1034 return;
1035 if (ts->ref_cnt) {
1036 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001037 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
1038 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +02001039 return;
1040 }
1041 stksess_kill(&px->table, ts);
1042 break;
Simon Horman17bce342011-06-15 15:18:47 +09001043
Willy Tarreau654694e2012-06-07 01:03:16 +02001044 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +02001045 if (ts)
1046 stktable_touch(&px->table, ts, 1);
1047 else {
Willy Tarreau07115412012-10-29 21:56:59 +01001048 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +02001049 if (!ts) {
1050 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001051 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
1052 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +02001053 return;
1054 }
1055 stktable_store(&px->table, ts, 1);
1056 }
1057
Willy Tarreau47060b62013-08-01 21:11:42 +02001058 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
1059 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001060 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
1061 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +02001062 return;
1063 }
1064
1065 data_type = stktable_get_data_type(args[cur_arg] + 5);
1066 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001067 appctx->ctx.cli.msg = "Unknown data type\n";
1068 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +02001069 return;
1070 }
1071
1072 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001073 appctx->ctx.cli.msg = "Data type not stored in this table\n";
1074 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +02001075 return;
1076 }
1077
1078 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001079 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
1080 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +02001081 return;
1082 }
1083
1084 ptr = stktable_data_ptr(&px->table, ts, data_type);
1085
1086 switch (stktable_data_types[data_type].std_type) {
1087 case STD_T_SINT:
1088 stktable_data_cast(ptr, std_t_sint) = value;
1089 break;
1090 case STD_T_UINT:
1091 stktable_data_cast(ptr, std_t_uint) = value;
1092 break;
1093 case STD_T_ULL:
1094 stktable_data_cast(ptr, std_t_ull) = value;
1095 break;
1096 case STD_T_FRQP:
1097 /* We set both the current and previous values. That way
1098 * the reported frequency is stable during all the period
1099 * then slowly fades out. This allows external tools to
1100 * push measures without having to update them too often.
1101 */
1102 frqp = &stktable_data_cast(ptr, std_t_frqp);
1103 frqp->curr_tick = now_ms;
1104 frqp->prev_ctr = 0;
1105 frqp->curr_ctr = value;
1106 break;
1107 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001108 }
1109 break;
1110
Willy Tarreaudec98142012-06-06 23:37:08 +02001111 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001112 appctx->ctx.cli.msg = "Unknown action\n";
1113 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +02001114 break;
Simon Horman121f3052011-06-15 15:18:46 +09001115 }
Simon Horman121f3052011-06-15 15:18:46 +09001116}
1117
Willy Tarreau654694e2012-06-07 01:03:16 +02001118static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001119{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001120 struct appctx *appctx = __objt_appctx(si->end);
1121
Willy Tarreau04b3a192013-04-13 09:41:37 +02001122 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001123 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
1124 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +02001125 return;
1126 }
1127
Simon Hormand5b9fd92011-06-15 15:18:48 +09001128 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001129 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
1130 if (appctx->ctx.table.data_type < 0) {
1131 appctx->ctx.cli.msg = "Unknown data type\n";
1132 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001133 return;
1134 }
1135
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001136 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
1137 appctx->ctx.cli.msg = "Data type not stored in this table\n";
1138 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001139 return;
1140 }
1141
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001142 appctx->ctx.table.data_op = get_std_op(args[4]);
1143 if (appctx->ctx.table.data_op < 0) {
1144 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
1145 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001146 return;
1147 }
1148
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001149 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
1150 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
1151 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001152 return;
1153 }
1154}
1155
Willy Tarreaudec98142012-06-06 23:37:08 +02001156static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001157{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001158 struct appctx *appctx = __objt_appctx(si->end);
1159
1160 appctx->ctx.table.data_type = -1;
1161 appctx->st2 = STAT_ST_INIT;
1162 appctx->ctx.table.target = NULL;
1163 appctx->ctx.table.proxy = NULL;
1164 appctx->ctx.table.entry = NULL;
1165 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001166
1167 if (*args[2]) {
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001168 appctx->ctx.table.target = proxy_tbl_by_name(args[2]);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001169 if (!appctx->ctx.table.target) {
1170 appctx->ctx.cli.msg = "No such table\n";
1171 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001172 return;
1173 }
1174 }
1175 else {
Willy Tarreaudec98142012-06-06 23:37:08 +02001176 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001177 goto err_args;
1178 return;
1179 }
1180
1181 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +02001182 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +09001183 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +02001184 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +09001185 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +09001186 goto err_args;
1187
1188 return;
1189
1190err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +02001191 switch (action) {
1192 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001193 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 +02001194 break;
1195 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001196 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 +02001197 break;
1198 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001199 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +02001200 break;
1201 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001202 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001203}
1204
Willy Tarreau532a4502011-09-07 22:37:44 +02001205/* Expects to find a frontend named <arg> and returns it, otherwise displays various
Willy Tarreau87b09662015-04-03 00:22:06 +02001206 * adequate error messages and returns NULL. This function also expects the stream
Willy Tarreau532a4502011-09-07 22:37:44 +02001207 * level to be admin.
1208 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001209static struct proxy *expect_frontend_admin(struct stream *s, struct stream_interface *si, const char *arg)
Willy Tarreau532a4502011-09-07 22:37:44 +02001210{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001211 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +02001212 struct proxy *px;
1213
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001214 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001215 appctx->ctx.cli.msg = stats_permission_denied_msg;
1216 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001217 return NULL;
1218 }
1219
1220 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001221 appctx->ctx.cli.msg = "A frontend name is expected.\n";
1222 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001223 return NULL;
1224 }
1225
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001226 px = proxy_fe_by_name(arg);
Willy Tarreau532a4502011-09-07 22:37:44 +02001227 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001228 appctx->ctx.cli.msg = "No such frontend.\n";
1229 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001230 return NULL;
1231 }
1232 return px;
1233}
1234
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001235/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
1236 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau87b09662015-04-03 00:22:06 +02001237 * and returns NULL. This function also expects the stream level to be admin. Note:
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001238 * the <arg> is modified to remove the '/'.
1239 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001240static struct server *expect_server_admin(struct stream *s, struct stream_interface *si, char *arg)
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001241{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001242 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001243 struct proxy *px;
1244 struct server *sv;
1245 char *line;
1246
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001247 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001248 appctx->ctx.cli.msg = stats_permission_denied_msg;
1249 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001250 return NULL;
1251 }
1252
1253 /* split "backend/server" and make <line> point to server */
1254 for (line = arg; *line; line++)
1255 if (*line == '/') {
1256 *line++ = '\0';
1257 break;
1258 }
1259
1260 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001261 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1262 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001263 return NULL;
1264 }
1265
1266 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001267 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1268 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001269 return NULL;
1270 }
1271
1272 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001273 appctx->ctx.cli.msg = "Proxy is disabled.\n";
1274 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001275 return NULL;
1276 }
1277
1278 return sv;
1279}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001280
1281/* This function is used with TLS ticket keys management. It permits to browse
1282 * each reference. The variable <getnext> must contain the current node,
1283 * <end> point to the root node.
1284 */
1285#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1286static inline
1287struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
1288{
1289 struct tls_keys_ref *ref = getnext;
1290
1291 while (1) {
1292
1293 /* Get next list entry. */
1294 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
1295
1296 /* If the entry is the last of the list, return NULL. */
1297 if (&ref->list == end)
1298 return NULL;
1299
1300 return ref;
1301 }
1302}
1303
1304static inline
1305struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
1306{
1307 int id;
1308 char *error;
1309
1310 /* If the reference starts by a '#', this is numeric id. */
1311 if (reference[0] == '#') {
1312 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1313 id = strtol(reference + 1, &error, 10);
1314 if (*error != '\0')
1315 return NULL;
1316
1317 /* Perform the unique id lookup. */
1318 return tlskeys_ref_lookupid(id);
1319 }
1320
1321 /* Perform the string lookup. */
1322 return tlskeys_ref_lookup(reference);
1323}
1324#endif
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001325
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001326/* This function is used with map and acl management. It permits to browse
1327 * each reference. The variable <getnext> must contain the current node,
1328 * <end> point to the root node and the <flags> permit to filter required
1329 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001330 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001331static inline
1332struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
1333 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001334{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001335 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001336
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001337 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001338
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001339 /* Get next list entry. */
1340 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001341
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001342 /* If the entry is the last of the list, return NULL. */
1343 if (&ref->list == end)
1344 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001345
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001346 /* If the entry match the flag, return it. */
1347 if (ref->flags & flags)
1348 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001349 }
1350}
1351
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001352static inline
1353struct pat_ref *pat_ref_lookup_ref(const char *reference)
1354{
1355 int id;
1356 char *error;
1357
1358 /* If the reference starts by a '#', this is numeric id. */
1359 if (reference[0] == '#') {
1360 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1361 id = strtol(reference + 1, &error, 10);
1362 if (*error != '\0')
1363 return NULL;
1364
1365 /* Perform the unique id lookup. */
1366 return pat_ref_lookupid(id);
1367 }
1368
1369 /* Perform the string lookup. */
1370 return pat_ref_lookup(reference);
1371}
1372
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001373/* This function is used with map and acl management. It permits to browse
1374 * each reference.
1375 */
1376static inline
1377struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1378{
1379 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001380 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1381 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001382 return NULL;
1383 return expr;
1384}
1385
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001386/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001387 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001388 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001389 * designating the function which will have to process the request, which can
1390 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001391 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001392static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001393{
Willy Tarreau87b09662015-04-03 00:22:06 +02001394 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001395 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001396 char *args[MAX_STATS_ARGS + 1];
1397 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001398 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001399
1400 while (isspace((unsigned char)*line))
1401 line++;
1402
1403 arg = 0;
1404 args[arg] = line;
1405
1406 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001407 if (*line == '\\') {
1408 line++;
1409 if (*line == '\0')
1410 break;
1411 }
1412 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001413 *line++ = '\0';
1414
1415 while (isspace((unsigned char)*line))
1416 line++;
1417
1418 args[++arg] = line;
1419 continue;
1420 }
1421
1422 line++;
1423 }
1424
1425 while (++arg <= MAX_STATS_ARGS)
1426 args[arg] = line;
1427
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001428 /* remove \ */
1429 arg = 0;
1430 while (*args[arg] != '\0') {
1431 j = 0;
1432 for (i=0; args[arg][i] != '\0'; i++) {
1433 if (args[arg][i] == '\\')
1434 continue;
1435 args[arg][j] = args[arg][i];
1436 j++;
1437 }
1438 args[arg][j] = '\0';
1439 arg++;
1440 }
1441
Willy Tarreau6bcb95d2015-05-04 18:07:56 +02001442 appctx->ctx.stats.scope_str = 0;
1443 appctx->ctx.stats.scope_len = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001444 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001445 if (strcmp(args[0], "show") == 0) {
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02001446 if (strcmp(args[1], "backend") == 0) {
1447 appctx->st2 = STAT_ST_INIT;
1448 appctx->st0 = STAT_CLI_O_BACKEND;
1449 }
Willy Tarreauae795722016-02-16 11:27:28 +01001450 else if (strcmp(args[1], "env") == 0) {
1451 extern char **environ;
1452
1453 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
1454 appctx->ctx.cli.msg = stats_permission_denied_msg;
1455 appctx->st0 = STAT_CLI_PRINT;
1456 return 1;
1457 }
1458 appctx->ctx.env.var = environ;
1459 appctx->st2 = STAT_ST_INIT;
1460 appctx->st0 = STAT_CLI_O_ENV; // stats_dump_env_to_buffer
1461
1462 if (*args[2]) {
1463 int len = strlen(args[2]);
1464
1465 for (; *appctx->ctx.env.var; appctx->ctx.env.var++) {
1466 if (strncmp(*appctx->ctx.env.var, args[2], len) == 0 &&
1467 (*appctx->ctx.env.var)[len] == '=')
1468 break;
1469 }
1470 if (!*appctx->ctx.env.var) {
1471 appctx->ctx.cli.msg = "Variable not found\n";
1472 appctx->st0 = STAT_CLI_PRINT;
1473 return 1;
1474 }
1475 appctx->st2 = STAT_ST_END;
1476 }
1477 }
1478 else if (strcmp(args[1], "stat") == 0) {
Baptiste Assmann3863f972015-05-17 00:33:24 +02001479 if (strcmp(args[2], "resolvers") == 0) {
1480 struct dns_resolvers *presolvers;
1481
Andrew Hayworth68d05342015-10-02 20:33:01 +00001482 if (*args[3]) {
1483 appctx->ctx.resolvers.ptr = NULL;
1484 list_for_each_entry(presolvers, &dns_resolvers, list) {
1485 if (strcmp(presolvers->id, args[3]) == 0) {
1486 appctx->ctx.resolvers.ptr = presolvers;
1487 break;
1488 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001489 }
Andrew Hayworth68d05342015-10-02 20:33:01 +00001490 if (appctx->ctx.resolvers.ptr == NULL) {
1491 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1492 appctx->st0 = STAT_CLI_PRINT;
1493 return 1;
1494 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001495 }
1496
1497 appctx->st2 = STAT_ST_INIT;
1498 appctx->st0 = STAT_CLI_O_RESOLVERS;
1499 return 1;
1500 }
1501 else if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001502 appctx->ctx.stats.flags |= STAT_BOUND;
1503 appctx->ctx.stats.iid = atoi(args[2]);
1504 appctx->ctx.stats.type = atoi(args[3]);
1505 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001506 }
1507
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001508 appctx->st2 = STAT_ST_INIT;
1509 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001510 }
1511 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001512 appctx->st2 = STAT_ST_INIT;
1513 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001514 }
Baptiste Assmann28289462015-07-03 08:01:20 +02001515 else if (strcmp(args[1], "servers") == 0 && strcmp(args[2], "state") == 0) {
1516 appctx->ctx.server_state.backend = NULL;
1517
1518 /* check if a backend name has been provided */
1519 if (*args[3]) {
1520 /* read server state from local file */
1521 appctx->ctx.server_state.backend = proxy_be_by_name(args[3]);
1522
1523 if (appctx->ctx.server_state.backend == NULL) {
1524 appctx->ctx.cli.msg = "Can't find backend.\n";
1525 appctx->st0 = STAT_CLI_PRINT;
1526 return 1;
1527 }
1528 }
1529 appctx->st2 = STAT_ST_INIT;
1530 appctx->st0 = STAT_CLI_O_SERVERS_STATE; // stats_dump_servers_state_to_buffer
1531 return 1;
1532 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001533 else if (strcmp(args[1], "pools") == 0) {
1534 appctx->st2 = STAT_ST_INIT;
1535 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1536 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001537 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001538 appctx->st2 = STAT_ST_INIT;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001539 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001540 appctx->ctx.cli.msg = stats_permission_denied_msg;
1541 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001542 return 1;
1543 }
Willy Tarreau76153662012-11-26 01:16:39 +01001544 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001545 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001546 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001547 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001548 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001549 appctx->ctx.sess.target = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001550 appctx->ctx.sess.section = 0; /* start with stream status */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001551 appctx->ctx.sess.pos = 0;
1552 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001553 }
1554 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001555 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001556 appctx->ctx.cli.msg = stats_permission_denied_msg;
1557 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001558 return 1;
1559 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001560 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001561 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001562 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001563 appctx->ctx.errors.iid = -1;
1564 appctx->ctx.errors.px = NULL;
1565 appctx->st2 = STAT_ST_INIT;
1566 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001567 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001568 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001569 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001570 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001571 else if (strcmp(args[1], "tls-keys") == 0) {
1572#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1573 appctx->st2 = STAT_ST_INIT;
1574 appctx->st0 = STAT_CLI_O_TLSK;
1575#else
1576 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1577 "that doesn't support specifying TLS ticket keys\n";
1578 appctx->st0 = STAT_CLI_PRINT;
1579#endif
1580 return 1;
1581 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001582 else if (strcmp(args[1], "map") == 0 ||
1583 strcmp(args[1], "acl") == 0) {
1584
1585 /* Set ACL or MAP flags. */
1586 if (args[1][0] == 'm')
1587 appctx->ctx.map.display_flags = PAT_REF_MAP;
1588 else
1589 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001590
1591 /* no parameter: display all map avalaible */
1592 if (!*args[2]) {
1593 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001594 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001595 return 1;
1596 }
1597
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001598 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001599 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001600 if (!appctx->ctx.map.ref ||
1601 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1602 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001603 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001604 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001605 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001606 appctx->st0 = STAT_CLI_PRINT;
1607 return 1;
1608 }
1609 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001610 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001611 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001612 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001613 return 0;
1614 }
1615 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001616 else if (strcmp(args[0], "clear") == 0) {
1617 if (strcmp(args[1], "counters") == 0) {
1618 struct proxy *px;
1619 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001620 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001621 int clrall = 0;
1622
1623 if (strcmp(args[2], "all") == 0)
1624 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001625
Willy Tarreau6162db22009-10-10 17:13:00 +02001626 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001627 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1628 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001629 appctx->ctx.cli.msg = stats_permission_denied_msg;
1630 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001631 return 1;
1632 }
1633
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001634 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001635 if (clrall) {
1636 memset(&px->be_counters, 0, sizeof(px->be_counters));
1637 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1638 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001639 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001640 px->be_counters.conn_max = 0;
1641 px->be_counters.p.http.rps_max = 0;
1642 px->be_counters.sps_max = 0;
1643 px->be_counters.cps_max = 0;
1644 px->be_counters.nbpend_max = 0;
1645
1646 px->fe_counters.conn_max = 0;
1647 px->fe_counters.p.http.rps_max = 0;
1648 px->fe_counters.sps_max = 0;
1649 px->fe_counters.cps_max = 0;
1650 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001651 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001652
1653 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001654 if (clrall)
1655 memset(&sv->counters, 0, sizeof(sv->counters));
1656 else {
1657 sv->counters.cur_sess_max = 0;
1658 sv->counters.nbpend_max = 0;
1659 sv->counters.sps_max = 0;
1660 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001661
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001662 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001663 if (li->counters) {
1664 if (clrall)
1665 memset(li->counters, 0, sizeof(*li->counters));
1666 else
1667 li->counters->conn_max = 0;
1668 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001669 }
1670
Willy Tarreau81c25d02011-09-07 15:17:21 +02001671 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001672 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001673 return 1;
1674 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001675 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001676 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001677 /* end of processing */
1678 return 1;
1679 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001680 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1681 /* Set ACL or MAP flags. */
1682 if (args[1][0] == 'm')
1683 appctx->ctx.map.display_flags = PAT_REF_MAP;
1684 else
1685 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001686
1687 /* no parameter */
1688 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001689 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1690 appctx->ctx.cli.msg = "Missing map identifier.\n";
1691 else
1692 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001693 appctx->st0 = STAT_CLI_PRINT;
1694 return 1;
1695 }
1696
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001697 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001698 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001699 if (!appctx->ctx.map.ref ||
1700 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1701 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001702 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001703 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001704 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001705 appctx->st0 = STAT_CLI_PRINT;
1706 return 1;
1707 }
1708
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001709 /* Clear all. */
1710 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001711
1712 /* return response */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001713 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001714 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001715 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001716 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001717 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001718 return 0;
1719 }
1720 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001721 else if (strcmp(args[0], "get") == 0) {
1722 if (strcmp(args[1], "weight") == 0) {
1723 struct proxy *px;
1724 struct server *sv;
1725
1726 /* split "backend/server" and make <line> point to server */
1727 for (line = args[2]; *line; line++)
1728 if (*line == '/') {
1729 *line++ = '\0';
1730 break;
1731 }
1732
1733 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001734 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1735 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001736 return 1;
1737 }
1738
1739 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001740 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1741 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001742 return 1;
1743 }
1744
1745 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001746 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001747 if (bi_putstr(si_ic(si), trash.str) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02001748 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001749
Willy Tarreau38338fa2009-10-10 18:37:29 +02001750 return 1;
1751 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001752 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1753 /* Set flags. */
1754 if (args[1][0] == 'm')
1755 appctx->ctx.map.display_flags = PAT_REF_MAP;
1756 else
1757 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001758
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001759 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001760 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001761 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1762 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1763 else
1764 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001765 appctx->st0 = STAT_CLI_PRINT;
1766 return 1;
1767 }
1768
1769 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001770 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001771 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001772 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001773 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001774 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001775 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001776 appctx->st0 = STAT_CLI_PRINT;
1777 return 1;
1778 }
1779
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001780 /* copy input string. The string must be allocated because
1781 * it may be used over multiple iterations. It's released
1782 * at the end and upon abort anyway.
1783 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001784 appctx->ctx.map.chunk.len = strlen(args[3]);
1785 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1786 appctx->ctx.map.chunk.str = strdup(args[3]);
1787 if (!appctx->ctx.map.chunk.str) {
1788 appctx->ctx.cli.msg = "Out of memory error.\n";
1789 appctx->st0 = STAT_CLI_PRINT;
1790 return 1;
1791 }
1792
1793 /* prepare response */
1794 appctx->st2 = STAT_ST_INIT;
1795 appctx->st0 = STAT_CLI_O_MLOOK;
1796 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001797 else { /* not "get weight" */
1798 return 0;
1799 }
1800 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001801 else if (strcmp(args[0], "set") == 0) {
1802 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001803 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001804 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001805
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001806 sv = expect_server_admin(s, si, args[2]);
1807 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001808 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001809
Simon Horman7d09b9a2013-02-12 10:45:51 +09001810 warning = server_parse_weight_change_request(sv, args[3]);
1811 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001812 appctx->ctx.cli.msg = warning;
1813 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001814 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001815 return 1;
1816 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001817 else if (strcmp(args[1], "server") == 0) {
1818 struct server *sv;
1819 const char *warning;
1820
1821 sv = expect_server_admin(s, si, args[2]);
1822 if (!sv)
1823 return 1;
1824
1825 if (strcmp(args[3], "weight") == 0) {
1826 warning = server_parse_weight_change_request(sv, args[4]);
1827 if (warning) {
1828 appctx->ctx.cli.msg = warning;
1829 appctx->st0 = STAT_CLI_PRINT;
1830 }
1831 }
1832 else if (strcmp(args[3], "state") == 0) {
1833 if (strcmp(args[4], "ready") == 0)
1834 srv_adm_set_ready(sv);
1835 else if (strcmp(args[4], "drain") == 0)
1836 srv_adm_set_drain(sv);
1837 else if (strcmp(args[4], "maint") == 0)
1838 srv_adm_set_maint(sv);
1839 else {
1840 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1841 appctx->st0 = STAT_CLI_PRINT;
1842 }
1843 }
1844 else if (strcmp(args[3], "health") == 0) {
1845 if (sv->track) {
1846 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1847 appctx->st0 = STAT_CLI_PRINT;
1848 }
1849 else if (strcmp(args[4], "up") == 0) {
1850 sv->check.health = sv->check.rise + sv->check.fall - 1;
1851 srv_set_running(sv, "changed from CLI");
1852 }
1853 else if (strcmp(args[4], "stopping") == 0) {
1854 sv->check.health = sv->check.rise + sv->check.fall - 1;
1855 srv_set_stopping(sv, "changed from CLI");
1856 }
1857 else if (strcmp(args[4], "down") == 0) {
1858 sv->check.health = 0;
1859 srv_set_stopped(sv, "changed from CLI");
1860 }
1861 else {
1862 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1863 appctx->st0 = STAT_CLI_PRINT;
1864 }
1865 }
1866 else if (strcmp(args[3], "agent") == 0) {
1867 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1868 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1869 appctx->st0 = STAT_CLI_PRINT;
1870 }
1871 else if (strcmp(args[4], "up") == 0) {
1872 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1873 srv_set_running(sv, "changed from CLI");
1874 }
1875 else if (strcmp(args[4], "down") == 0) {
1876 sv->agent.health = 0;
1877 srv_set_stopped(sv, "changed from CLI");
1878 }
1879 else {
1880 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1881 appctx->st0 = STAT_CLI_PRINT;
1882 }
1883 }
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001884 else if (strcmp(args[3], "addr") == 0) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001885 warning = server_parse_addr_change_request(sv, args[4], "stats command");
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001886 if (warning) {
1887 appctx->ctx.cli.msg = warning;
1888 appctx->st0 = STAT_CLI_PRINT;
1889 }
1890 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001891 else {
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001892 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight' and 'addr'.\n";
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001893 appctx->st0 = STAT_CLI_PRINT;
1894 }
1895 return 1;
1896 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001897 else if (strcmp(args[1], "timeout") == 0) {
1898 if (strcmp(args[2], "cli") == 0) {
1899 unsigned timeout;
1900 const char *res;
1901
1902 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001903 appctx->ctx.cli.msg = "Expects an integer value.\n";
1904 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001905 return 1;
1906 }
1907
1908 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1909 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001910 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1911 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001912 return 1;
1913 }
1914
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001915 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001916 return 1;
1917 }
1918 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001919 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1920 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001921 return 1;
1922 }
1923 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001924 else if (strcmp(args[1], "maxconn") == 0) {
1925 if (strcmp(args[2], "frontend") == 0) {
1926 struct proxy *px;
1927 struct listener *l;
1928 int v;
1929
Willy Tarreau532a4502011-09-07 22:37:44 +02001930 px = expect_frontend_admin(s, si, args[3]);
1931 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001932 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001933
1934 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001935 appctx->ctx.cli.msg = "Integer value expected.\n";
1936 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001937 return 1;
1938 }
1939
1940 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001941 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001942 appctx->ctx.cli.msg = "Value out of range.\n";
1943 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001944 return 1;
1945 }
1946
1947 /* OK, the value is fine, so we assign it to the proxy and to all of
1948 * its listeners. The blocked ones will be dequeued.
1949 */
1950 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001951 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001952 l->maxconn = v;
1953 if (l->state == LI_FULL)
1954 resume_listener(l);
1955 }
1956
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001957 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&strm_fe(s)->listener_queue))
1958 dequeue_all_listeners(&strm_fe(s)->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001959
1960 return 1;
1961 }
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001962 else if (strcmp(args[2], "server") == 0) {
1963 struct server *sv;
1964 int v;
1965
1966 sv = expect_server_admin(s, si, args[3]);
1967 if (!sv)
1968 return 1;
1969
1970 if (!*args[4]) {
1971 appctx->ctx.cli.msg = "Integer value expected.\n";
1972 appctx->st0 = STAT_CLI_PRINT;
1973 return 1;
1974 }
1975
1976 v = atoi(args[4]);
1977 if (v < 0) {
1978 appctx->ctx.cli.msg = "Value out of range.\n";
1979 appctx->st0 = STAT_CLI_PRINT;
1980 return 1;
1981 }
1982
1983 if (sv->maxconn == sv->minconn) { // static maxconn
1984 sv->maxconn = sv->minconn = v;
1985 } else { // dynamic maxconn
1986 sv->maxconn = v;
1987 }
1988
1989 if (may_dequeue_tasks(sv, sv->proxy))
1990 process_srv_queue(sv);
1991
1992 return 1;
1993 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001994 else if (strcmp(args[2], "global") == 0) {
1995 int v;
1996
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001997 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001998 appctx->ctx.cli.msg = stats_permission_denied_msg;
1999 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02002000 return 1;
2001 }
2002
2003 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002004 appctx->ctx.cli.msg = "Expects an integer value.\n";
2005 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02002006 return 1;
2007 }
2008
2009 v = atoi(args[3]);
2010 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002011 appctx->ctx.cli.msg = "Value out of range.\n";
2012 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02002013 return 1;
2014 }
2015
2016 /* check for unlimited values */
2017 if (v <= 0)
2018 v = global.hardmaxconn;
2019
2020 global.maxconn = v;
2021
2022 /* Dequeues all of the listeners waiting for a resource */
2023 if (!LIST_ISEMPTY(&global_listener_queue))
2024 dequeue_all_listeners(&global_listener_queue);
2025
2026 return 1;
2027 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02002028 else {
Andrew Hayworthedb93a72015-10-27 21:46:25 +00002029 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend', 'server', and 'global'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002030 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02002031 return 1;
2032 }
2033 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02002034 else if (strcmp(args[1], "rate-limit") == 0) {
2035 if (strcmp(args[2], "connections") == 0) {
2036 if (strcmp(args[3], "global") == 0) {
2037 int v;
2038
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002039 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002040 appctx->ctx.cli.msg = stats_permission_denied_msg;
2041 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002042 return 1;
2043 }
2044
2045 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002046 appctx->ctx.cli.msg = "Expects an integer value.\n";
2047 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002048 return 1;
2049 }
2050
2051 v = atoi(args[4]);
2052 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002053 appctx->ctx.cli.msg = "Value out of range.\n";
2054 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002055 return 1;
2056 }
2057
2058 global.cps_lim = v;
2059
2060 /* Dequeues all of the listeners waiting for a resource */
2061 if (!LIST_ISEMPTY(&global_listener_queue))
2062 dequeue_all_listeners(&global_listener_queue);
2063
2064 return 1;
2065 }
2066 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002067 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
2068 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002069 return 1;
2070 }
2071 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02002072 else if (strcmp(args[2], "sessions") == 0) {
2073 if (strcmp(args[3], "global") == 0) {
2074 int v;
2075
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002076 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02002077 appctx->ctx.cli.msg = stats_permission_denied_msg;
2078 appctx->st0 = STAT_CLI_PRINT;
2079 return 1;
2080 }
2081
2082 if (!*args[4]) {
2083 appctx->ctx.cli.msg = "Expects an integer value.\n";
2084 appctx->st0 = STAT_CLI_PRINT;
2085 return 1;
2086 }
2087
2088 v = atoi(args[4]);
2089 if (v < 0) {
2090 appctx->ctx.cli.msg = "Value out of range.\n";
2091 appctx->st0 = STAT_CLI_PRINT;
2092 return 1;
2093 }
2094
2095 global.sps_lim = v;
2096
2097 /* Dequeues all of the listeners waiting for a resource */
2098 if (!LIST_ISEMPTY(&global_listener_queue))
2099 dequeue_all_listeners(&global_listener_queue);
2100
2101 return 1;
2102 }
2103 else {
2104 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
2105 appctx->st0 = STAT_CLI_PRINT;
2106 return 1;
2107 }
2108 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02002109#ifdef USE_OPENSSL
2110 else if (strcmp(args[2], "ssl-sessions") == 0) {
2111 if (strcmp(args[3], "global") == 0) {
2112 int v;
2113
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002114 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02002115 appctx->ctx.cli.msg = stats_permission_denied_msg;
2116 appctx->st0 = STAT_CLI_PRINT;
2117 return 1;
2118 }
2119
2120 if (!*args[4]) {
2121 appctx->ctx.cli.msg = "Expects an integer value.\n";
2122 appctx->st0 = STAT_CLI_PRINT;
2123 return 1;
2124 }
2125
2126 v = atoi(args[4]);
2127 if (v < 0) {
2128 appctx->ctx.cli.msg = "Value out of range.\n";
2129 appctx->st0 = STAT_CLI_PRINT;
2130 return 1;
2131 }
2132
2133 global.ssl_lim = v;
2134
2135 /* Dequeues all of the listeners waiting for a resource */
2136 if (!LIST_ISEMPTY(&global_listener_queue))
2137 dequeue_all_listeners(&global_listener_queue);
2138
2139 return 1;
2140 }
2141 else {
2142 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
2143 appctx->st0 = STAT_CLI_PRINT;
2144 return 1;
2145 }
2146 }
2147#endif
William Lallemandd85f9172012-11-09 17:05:39 +01002148 else if (strcmp(args[2], "http-compression") == 0) {
2149 if (strcmp(args[3], "global") == 0) {
2150 int v;
2151
Willy Tarreaua1c2b2c2015-11-26 18:32:39 +01002152 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
2153 appctx->ctx.cli.msg = stats_permission_denied_msg;
2154 appctx->st0 = STAT_CLI_PRINT;
2155 return 1;
2156 }
2157
Willy Tarreau85d47f92012-11-21 00:29:50 +01002158 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002159 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
2160 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01002161 return 1;
2162 }
2163
William Lallemandd85f9172012-11-09 17:05:39 +01002164 v = atoi(args[4]);
2165 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
2166 }
2167 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002168 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
2169 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01002170 return 1;
2171 }
2172 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02002173 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02002174 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002175 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002176 return 1;
2177 }
2178 }
Willy Tarreau654694e2012-06-07 01:03:16 +02002179 else if (strcmp(args[1], "table") == 0) {
2180 stats_sock_table_request(si, args, STAT_CLI_O_SET);
2181 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002182 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002183 char *err;
2184
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002185 /* Set flags. */
2186 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002187
2188 /* Expect three parameters: map name, key and new value. */
2189 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01002190 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002191 appctx->st0 = STAT_CLI_PRINT;
2192 return 1;
2193 }
2194
2195 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002196 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002197 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002198 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002199 appctx->st0 = STAT_CLI_PRINT;
2200 return 1;
2201 }
2202
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002203 /* If the entry identifier start with a '#', it is considered as
2204 * pointer id
2205 */
2206 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2207 struct pat_ref_elt *ref;
2208 long long int conv;
2209 char *error;
2210
2211 /* Convert argument to integer value. */
2212 conv = strtoll(&args[3][1], &error, 16);
2213 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002214 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002215 appctx->st0 = STAT_CLI_PRINT;
2216 return 1;
2217 }
2218
2219 /* Convert and check integer to pointer. */
2220 ref = (struct pat_ref_elt *)(long)conv;
2221 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002222 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002223 appctx->st0 = STAT_CLI_PRINT;
2224 return 1;
2225 }
2226
2227 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002228 err = NULL;
2229 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
2230 if (err)
2231 memprintf(&err, "%s.\n", err);
2232 appctx->ctx.cli.err = err;
2233 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002234 return 1;
2235 }
2236 }
2237 else {
2238 /* Else, use the entry identifier as pattern
2239 * string, and update the value.
2240 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002241 err = NULL;
2242 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
2243 if (err)
2244 memprintf(&err, "%s.\n", err);
2245 appctx->ctx.cli.err = err;
2246 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002247 return 1;
2248 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002249 }
2250
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002251 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002252 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002253 return 1;
2254 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002255#ifdef USE_OPENSSL
2256 else if (strcmp(args[1], "ssl") == 0) {
2257 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01002258#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02002259 char *err = NULL;
2260
Emeric Brunaf4ef742014-06-19 14:10:45 +02002261 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02002262 if (!*args[3]) {
2263 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
2264 appctx->st0 = STAT_CLI_PRINT;
2265 return 1;
2266 }
2267
2268 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
2269 if (trash.len < 0) {
2270 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
2271 appctx->st0 = STAT_CLI_PRINT;
2272 return 1;
2273 }
2274
2275 if (ssl_sock_update_ocsp_response(&trash, &err)) {
2276 if (err) {
2277 memprintf(&err, "%s.\n", err);
2278 appctx->ctx.cli.err = err;
2279 appctx->st0 = STAT_CLI_PRINT_FREE;
2280 }
2281 return 1;
2282 }
2283 appctx->ctx.cli.msg = "OCSP Response updated!";
2284 appctx->st0 = STAT_CLI_PRINT;
2285 return 1;
2286#else
2287 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
2288 appctx->st0 = STAT_CLI_PRINT;
2289 return 1;
2290#endif
2291 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002292 else if (strcmp(args[2], "tls-key") == 0) {
2293#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2294 /* Expect two parameters: the filename and the new new TLS key in encoding */
2295 if (!*args[3] || !*args[4]) {
2296 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
2297 appctx->st0 = STAT_CLI_PRINT;
2298 return 1;
2299 }
2300
2301 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[3]);
2302 if(!appctx->ctx.tlskeys.ref) {
2303 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
2304 appctx->st0 = STAT_CLI_PRINT;
2305 return 1;
2306 }
2307
2308 trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size);
2309 if (trash.len != sizeof(struct tls_sess_key)) {
2310 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
2311 appctx->st0 = STAT_CLI_PRINT;
2312 return 1;
2313 }
2314
Pradeep Jindalcc79b002015-08-20 18:25:17 +05302315 memcpy(appctx->ctx.tlskeys.ref->tlskeys + ((appctx->ctx.tlskeys.ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len);
2316 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 +02002317
2318 appctx->ctx.cli.msg = "TLS ticket key updated!";
2319 appctx->st0 = STAT_CLI_PRINT;
2320 return 1;
2321#else
2322 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
2323 "that doesn't support specifying TLS ticket keys\n";
2324 appctx->st0 = STAT_CLI_PRINT;
2325 return 1;
2326#endif
2327 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002328 else {
2329 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
2330 appctx->st0 = STAT_CLI_PRINT;
2331 return 1;
2332 }
2333 }
2334#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01002335 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02002336 return 0;
2337 }
2338 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002339 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002340 if (strcmp(args[1], "agent") == 0) {
2341 struct server *sv;
2342
2343 sv = expect_server_admin(s, si, args[2]);
2344 if (!sv)
2345 return 1;
2346
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002347 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
2348 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
2349 appctx->st0 = STAT_CLI_PRINT;
2350 return 1;
2351 }
2352
2353 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002354 return 1;
2355 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002356 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002357 struct server *sv;
2358
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002359 sv = expect_server_admin(s, si, args[2]);
2360 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002361 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002362
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002363 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
2364 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
2365 appctx->st0 = STAT_CLI_PRINT;
2366 return 1;
2367 }
2368
2369 sv->check.state |= CHK_ST_ENABLED;
2370 return 1;
2371 }
2372 else if (strcmp(args[1], "server") == 0) {
2373 struct server *sv;
2374
2375 sv = expect_server_admin(s, si, args[2]);
2376 if (!sv)
2377 return 1;
2378
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002379 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002380 return 1;
2381 }
2382 else if (strcmp(args[1], "frontend") == 0) {
2383 struct proxy *px;
2384
2385 px = expect_frontend_admin(s, si, args[2]);
2386 if (!px)
2387 return 1;
2388
2389 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002390 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
2391 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002392 return 1;
2393 }
2394
2395 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002396 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
2397 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002398 return 1;
2399 }
2400
2401 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002402 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
2403 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002404 return 1;
2405 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002406 return 1;
2407 }
2408 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002409 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002410 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002411 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002412 }
2413 }
2414 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002415 if (strcmp(args[1], "agent") == 0) {
2416 struct server *sv;
2417
2418 sv = expect_server_admin(s, si, args[2]);
2419 if (!sv)
2420 return 1;
2421
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002422 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002423 return 1;
2424 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002425 else if (strcmp(args[1], "health") == 0) {
2426 struct server *sv;
2427
2428 sv = expect_server_admin(s, si, args[2]);
2429 if (!sv)
2430 return 1;
2431
2432 sv->check.state &= ~CHK_ST_ENABLED;
2433 return 1;
2434 }
Simon Horman671b6f02013-11-25 10:46:39 +09002435 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002436 struct server *sv;
2437
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002438 sv = expect_server_admin(s, si, args[2]);
2439 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002440 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002441
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002442 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002443 return 1;
2444 }
2445 else if (strcmp(args[1], "frontend") == 0) {
2446 struct proxy *px;
2447
2448 px = expect_frontend_admin(s, si, args[2]);
2449 if (!px)
2450 return 1;
2451
2452 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002453 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
2454 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002455 return 1;
2456 }
2457
2458 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002459 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
2460 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002461 return 1;
2462 }
2463
2464 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002465 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
2466 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002467 return 1;
2468 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002469 return 1;
2470 }
2471 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002472 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002473 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002474 return 1;
2475 }
2476 }
2477 else if (strcmp(args[0], "shutdown") == 0) {
2478 if (strcmp(args[1], "frontend") == 0) {
2479 struct proxy *px;
2480
2481 px = expect_frontend_admin(s, si, args[2]);
2482 if (!px)
2483 return 1;
2484
2485 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002486 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
2487 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002488 return 1;
2489 }
2490
2491 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2492 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2493 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2494 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2495 stop_proxy(px);
2496 return 1;
2497 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002498 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002499 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002500
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002501 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002502 appctx->ctx.cli.msg = stats_permission_denied_msg;
2503 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002504 return 1;
2505 }
2506
2507 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002508 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2509 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002510 return 1;
2511 }
2512
2513 ptr = (void *)strtoul(args[2], NULL, 0);
2514
Willy Tarreau87b09662015-04-03 00:22:06 +02002515 /* first, look for the requested stream in the stream table */
2516 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002517 if (sess == ptr)
2518 break;
2519 }
2520
Willy Tarreau87b09662015-04-03 00:22:06 +02002521 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002522 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002523 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2524 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002525 return 1;
2526 }
2527
Willy Tarreaue7dff022015-04-03 01:14:29 +02002528 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002529 return 1;
2530 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002531 else if (strcmp(args[1], "sessions") == 0) {
2532 if (strcmp(args[2], "server") == 0) {
2533 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002534 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002535
2536 sv = expect_server_admin(s, si, args[3]);
2537 if (!sv)
2538 return 1;
2539
Willy Tarreau87b09662015-04-03 00:22:06 +02002540 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002541 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2542 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002543 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002544
2545 return 1;
2546 }
2547 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002548 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2549 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002550 return 1;
2551 }
2552 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002553 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002554 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2555 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002556 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002557 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002558 }
2559 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002560 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2561 if (args[1][0] == 'm')
2562 appctx->ctx.map.display_flags = PAT_REF_MAP;
2563 else
2564 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002565
2566 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002567 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2568 if (!*args[2] || !*args[3]) {
2569 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2570 appctx->st0 = STAT_CLI_PRINT;
2571 return 1;
2572 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002573 }
2574
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002575 else {
2576 if (!*args[2] || !*args[3]) {
2577 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2578 appctx->st0 = STAT_CLI_PRINT;
2579 return 1;
2580 }
2581 }
2582
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002583 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002584 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002585 if (!appctx->ctx.map.ref ||
2586 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002587 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002588 appctx->st0 = STAT_CLI_PRINT;
2589 return 1;
2590 }
2591
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002592 /* If the entry identifier start with a '#', it is considered as
2593 * pointer id
2594 */
2595 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2596 struct pat_ref_elt *ref;
2597 long long int conv;
2598 char *error;
2599
2600 /* Convert argument to integer value. */
2601 conv = strtoll(&args[3][1], &error, 16);
2602 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002603 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002604 appctx->st0 = STAT_CLI_PRINT;
2605 return 1;
2606 }
2607
2608 /* Convert and check integer to pointer. */
2609 ref = (struct pat_ref_elt *)(long)conv;
2610 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002611 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002612 appctx->st0 = STAT_CLI_PRINT;
2613 return 1;
2614 }
2615
2616 /* Try to delete the entry. */
2617 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2618 /* The entry is not found, send message. */
2619 appctx->ctx.cli.msg = "Key not found.\n";
2620 appctx->st0 = STAT_CLI_PRINT;
2621 return 1;
2622 }
2623 }
2624 else {
2625 /* Else, use the entry identifier as pattern
2626 * string and try to delete the entry.
2627 */
2628 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2629 /* The entry is not found, send message. */
2630 appctx->ctx.cli.msg = "Key not found.\n";
2631 appctx->st0 = STAT_CLI_PRINT;
2632 return 1;
2633 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002634 }
2635
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002636 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002637 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002638 return 1;
2639 }
2640 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002641 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002642 appctx->st0 = STAT_CLI_PRINT;
2643 return 1;
2644 }
2645 }
2646 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002647 if (strcmp(args[1], "map") == 0 ||
2648 strcmp(args[1], "acl") == 0) {
2649 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002650 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002651
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002652 /* Set flags. */
2653 if (args[1][0] == 'm')
2654 appctx->ctx.map.display_flags = PAT_REF_MAP;
2655 else
2656 appctx->ctx.map.display_flags = PAT_REF_ACL;
2657
2658 /* If the keywork is "map", we expect three parameters, if it
2659 * is "acl", we expect only two parameters
2660 */
2661 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2662 if (!*args[2] || !*args[3] || !*args[4]) {
2663 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2664 appctx->st0 = STAT_CLI_PRINT;
2665 return 1;
2666 }
2667 }
2668 else {
2669 if (!*args[2] || !*args[3]) {
2670 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2671 appctx->st0 = STAT_CLI_PRINT;
2672 return 1;
2673 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002674 }
2675
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002676 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002677 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002678 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002679 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002680 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002681 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002682 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002683 appctx->st0 = STAT_CLI_PRINT;
2684 return 1;
2685 }
2686
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002687 /* The command "add acl" is prohibited if the reference
2688 * use samples.
2689 */
2690 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2691 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2692 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2693 "You must use the command 'add map' to add values.\n";
2694 appctx->st0 = STAT_CLI_PRINT;
2695 return 1;
2696 }
2697
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002698 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002699 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002700 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002701 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002702 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002703 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002704 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002705 if (err)
2706 memprintf(&err, "%s.\n", err);
2707 appctx->ctx.cli.err = err;
2708 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002709 return 1;
2710 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002711
2712 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002713 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002714 return 1;
2715 }
2716 else { /* unknown "del" parameter */
2717 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2718 appctx->st0 = STAT_CLI_PRINT;
2719 return 1;
2720 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002721 }
2722 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002723 return 0;
2724 }
2725 return 1;
2726}
2727
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002728/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002729 * used to processes I/O from/to the stats unix socket. The system relies on a
2730 * state machine handling requests and various responses. We read a request,
2731 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002732 * Then we can read again. The state is stored in appctx->st0 and is one of the
2733 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002734 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002735 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002736static void cli_io_handler(struct appctx *appctx)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002737{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002738 struct stream_interface *si = appctx->owner;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002739 struct channel *req = si_oc(si);
2740 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002741 int reql;
2742 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002743
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002744 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2745 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002746
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002747 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002748 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002749 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002750 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2751 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002752 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002753 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002754 /* Let's close for real now. We just close the request
2755 * side, the conditions below will complete if needed.
2756 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002757 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002758 break;
2759 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002760 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002761 /* ensure we have some output room left in the event we
2762 * would want to return some info right after parsing.
2763 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002764 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002765 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02002766 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002767 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002768
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002769 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002770 if (reql <= 0) { /* closed or EOL not found */
2771 if (reql == 0)
2772 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002773 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002774 continue;
2775 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002776
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002777 /* seek for a possible semi-colon. If we find one, we
2778 * replace it with an LF and skip only this part.
2779 */
2780 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002781 if (trash.str[len] == ';') {
2782 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002783 reql = len + 1;
2784 break;
2785 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002786
Willy Tarreau816fc222009-10-04 07:36:58 +02002787 /* now it is time to check that we have a full line,
2788 * remove the trailing \n and possibly \r, then cut the
2789 * line.
2790 */
2791 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002792 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002793 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002794 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002795 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002796
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002797 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002798 len--;
2799
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002800 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002801
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002802 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002803 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002804 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002805 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002806 continue;
2807 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002808 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002809 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002810 else if (strcmp(trash.str, "help") == 0 ||
2811 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002812 appctx->ctx.cli.msg = stats_sock_usage_msg;
2813 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002814 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002815 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002816 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002817 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002818 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002819 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002820 /* if prompt is disabled, print help on empty lines,
2821 * so that the user at least knows how to enable
2822 * prompt and find help.
2823 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002824 appctx->ctx.cli.msg = stats_sock_usage_msg;
2825 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002826 }
2827
2828 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002829 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002830 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002831 }
Willy Tarreau68c00c72015-09-25 19:21:19 +02002832 else { /* output functions */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002833 switch (appctx->st0) {
Andrew Hayworthe32d1862015-10-02 15:08:10 +00002834 case STAT_CLI_PROMPT:
2835 break;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002836 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002837 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002838 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002839 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002840 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002841 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002842 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002843 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002844 free(appctx->ctx.cli.err);
2845 appctx->st0 = STAT_CLI_PROMPT;
2846 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002847 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002848 si_applet_cant_put(si);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002849 break;
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02002850 case STAT_CLI_O_BACKEND:
2851 if (stats_dump_backend_to_buffer(si))
2852 appctx->st0 = STAT_CLI_PROMPT;
2853 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002854 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002855 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002856 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002857 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02002858 case STAT_CLI_O_SERVERS_STATE:
2859 if (stats_dump_servers_state_to_buffer(si))
2860 appctx->st0 = STAT_CLI_PROMPT;
2861 break;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002862 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002863 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002864 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002865 break;
Baptiste Assmann3863f972015-05-17 00:33:24 +02002866 case STAT_CLI_O_RESOLVERS:
2867 if (stats_dump_resolvers_to_buffer(si))
2868 appctx->st0 = STAT_CLI_PROMPT;
2869 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002870 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002871 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002872 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002873 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002874 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002875 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002876 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002877 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002878 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002879 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002880 if (stats_table_request(si, appctx->st0))
2881 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002882 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002883 case STAT_CLI_O_PATS:
2884 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002885 appctx->st0 = STAT_CLI_PROMPT;
2886 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002887 case STAT_CLI_O_PAT:
2888 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002889 appctx->st0 = STAT_CLI_PROMPT;
2890 break;
2891 case STAT_CLI_O_MLOOK:
2892 if (stats_map_lookup(si))
2893 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002894 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002895 case STAT_CLI_O_POOLS:
2896 if (stats_dump_pools_to_buffer(si))
2897 appctx->st0 = STAT_CLI_PROMPT;
2898 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002899#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2900 case STAT_CLI_O_TLSK:
2901 if (stats_tlskeys_list(si))
2902 appctx->st0 = STAT_CLI_PROMPT;
2903 break;
2904#endif
Willy Tarreauae795722016-02-16 11:27:28 +01002905 case STAT_CLI_O_ENV: /* environment dump */
2906 if (stats_dump_env_to_buffer(si))
2907 appctx->st0 = STAT_CLI_PROMPT;
2908 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002909 default: /* abnormal state */
Willy Tarreau5cfa3bc2015-09-25 20:08:51 +02002910 si->flags |= SI_FL_ERR;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002911 break;
2912 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002913
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002914 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002915 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002916 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002917 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002918 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002919 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002920 }
2921
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002922 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002923 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002924 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002925
2926 /* Now we close the output if one of the writers did so,
2927 * or if we're not in interactive mode and the request
2928 * buffer is empty. This still allows pipelined requests
2929 * to be sent in non-interactive mode.
2930 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002931 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2932 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002933 continue;
2934 }
2935
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002936 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002937 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002938 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002939 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002940
Willy Tarreauc9e930a2015-09-25 20:06:08 +02002941 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002942 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2943 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002944 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002945 * and nothing more to consume. This is comparable to a broken pipe, so
2946 * we forward the close to the request side so that it flows upstream to
2947 * the client.
2948 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002949 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002950 }
2951
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002952 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002953 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2954 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2955 /* We have no more processing to do, and nothing more to send, and
2956 * the client side has closed. So we'll forward this state downstream
2957 * on the response buffer.
2958 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002959 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002960 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002961 }
2962
Willy Tarreau828824a2015-04-19 17:20:03 +02002963 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002964 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 +02002965 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002966 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 +02002967}
2968
Willy Tarreau638d40a2016-02-24 23:11:01 +01002969/* Emits a stats field without any surrounding element and properly encoded to
2970 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
2971 */
2972static int stats_emit_raw_data_field(struct chunk *out, const struct field *f)
2973{
2974 switch (field_format(f, 0)) {
2975 case FF_EMPTY: return 1;
2976 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
2977 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
2978 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
2979 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
2980 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
2981 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
2982 }
2983}
2984
Willy Tarreaubf95cba2016-01-11 18:27:29 +01002985/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
2986static int stats_dump_info_fields(struct chunk *out, const struct field *info)
2987{
2988 int field;
2989
2990 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2991 if (!field_format(info, field))
2992 continue;
2993
2994 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
2995 return 0;
2996 if (!stats_emit_raw_data_field(out, &info[field]))
2997 return 0;
2998 if (!chunk_strcat(out, "\n"))
2999 return 0;
3000 }
3001 return 1;
3002}
3003
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003004/* This function dumps information onto the stream interface's read buffer.
3005 * It returns 0 as long as it does not complete, non-zero upon completion.
3006 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003007 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003008static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003009{
3010 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau4529c072016-01-11 18:17:07 +01003011 struct chunk *out = get_trash_chunk();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003012
Willy Tarreau0c9c2722014-05-28 12:28:58 +02003013#ifdef USE_OPENSSL
3014 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3015 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3016 int ssl_reuse = 0;
3017
3018 if (ssl_key_rate < ssl_sess_rate) {
3019 /* count the ssl reuse ratio and avoid overflows in both directions */
3020 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3021 }
3022#endif
3023
Willy Tarreau4529c072016-01-11 18:17:07 +01003024 chunk_reset(out);
3025 memset(&info, 0, sizeof(info));
3026
3027 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
3028 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
3029 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
3030
3031 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3032 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3033 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3034
3035 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3036 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3037
3038 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3039 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3040 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3041 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3042 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3043 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3044 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3045 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3046 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3047 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3048 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3049 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3050#ifdef USE_OPENSSL
3051 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3052 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3053 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3054#endif
3055 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3056 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3057 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3058 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3059 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3060 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3061 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3062 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3063 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3064
3065#ifdef USE_OPENSSL
3066 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3067 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3068 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3069 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3070 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3071 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3072 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3073 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3074 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3075 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3076#endif
3077 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3078 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3079 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3080#ifdef USE_ZLIB
3081 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3082 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3083#endif
3084 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
3085 info[INF_RUN_QUEUE] = mkf_u32(0, run_queue_cur);
3086 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3087 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3088 if (global.desc)
3089 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
3090
Willy Tarreau1b4ba1e2016-01-11 18:29:04 +01003091 chunk_reset(&trash);
3092 stats_dump_info_fields(&trash, info);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003093
Willy Tarreaubc18da12015-03-13 14:00:47 +01003094 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003095 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003096 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003097 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003098
3099 return 1;
3100}
3101
Baptiste Assmann28289462015-07-03 08:01:20 +02003102/* dumps server state information into <buf> for all the servers found in <backend>
3103 * These information are all the parameters which may change during HAProxy runtime.
3104 * By default, we only export to the last known server state file format.
3105 * These information can be used at next startup to recover same level of server state.
3106 */
3107static void dump_servers_state(struct proxy *backend, struct chunk *buf)
3108{
3109 struct server *srv;
3110 char srv_addr[INET6_ADDRSTRLEN + 1];
3111 time_t srv_time_since_last_change;
3112 int bk_f_forced_id, srv_f_forced_id;
3113
3114 /* we don't want to report any state if the backend is not enabled on this process */
3115 if (backend->bind_proc && !(backend->bind_proc & (1UL << (relative_pid - 1))))
3116 return;
3117
3118 srv = backend->srv;
3119
3120 while (srv) {
3121 srv_addr[0] = '\0';
3122 srv_time_since_last_change = 0;
3123 bk_f_forced_id = 0;
3124 srv_f_forced_id = 0;
3125
3126 switch (srv->addr.ss_family) {
3127 case AF_INET:
3128 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
3129 srv_addr, INET_ADDRSTRLEN + 1);
3130 break;
3131 case AF_INET6:
3132 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
3133 srv_addr, INET6_ADDRSTRLEN + 1);
3134 break;
3135 }
3136 srv_time_since_last_change = now.tv_sec - srv->last_change;
3137 bk_f_forced_id = backend->options & PR_O_FORCED_ID ? 1 : 0;
3138 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
3139
3140 chunk_appendf(buf,
3141 "%d %s "
3142 "%d %s %s "
3143 "%d %d %d %d %ld "
3144 "%d %d %d %d %d "
3145 "%d %d"
3146 "\n",
3147 backend->uuid, backend->id,
3148 srv->puid, srv->id, srv_addr,
David Carlier081b3362015-11-18 06:10:22 +00003149 srv->state, srv->admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
Baptiste Assmann28289462015-07-03 08:01:20 +02003150 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
3151 bk_f_forced_id, srv_f_forced_id);
3152
3153 srv = srv->next;
3154 }
3155}
3156
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003157/* Parses backend list and simply report backend names */
3158static int stats_dump_backend_to_buffer(struct stream_interface *si)
3159{
3160 extern struct proxy *proxy;
3161 struct proxy *curproxy;
3162
3163 chunk_reset(&trash);
3164 chunk_printf(&trash, "# name\n");
3165
3166 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3167 /* looking for backends only */
3168 if (!(curproxy->cap & PR_CAP_BE))
3169 continue;
3170
3171 /* we don't want to list a backend which is bound to this process */
3172 if (curproxy->bind_proc && !(curproxy->bind_proc & (1UL << (relative_pid - 1))))
3173 continue;
3174
3175 chunk_appendf(&trash, "%s\n", curproxy->id);
3176 }
3177
3178 if (bi_putchk(si_ic(si), &trash) == -1) {
3179 si_applet_cant_put(si);
3180 return 0;
3181 }
3182
3183 return 1;
3184}
3185
Baptiste Assmann28289462015-07-03 08:01:20 +02003186/* Parses backend list or simply use backend name provided by the user to return
3187 * states of servers to stdout.
3188 */
3189static int stats_dump_servers_state_to_buffer(struct stream_interface *si)
3190{
3191 struct appctx *appctx = __objt_appctx(si->end);
3192 extern struct proxy *proxy;
3193 struct proxy *curproxy;
3194
3195 chunk_reset(&trash);
3196
3197 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
3198
3199 if (appctx->ctx.server_state.backend) {
3200 dump_servers_state(appctx->ctx.server_state.backend, &trash);
3201 }
3202 else {
3203 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3204 /* servers are only in backends */
3205 if (!(curproxy->cap & PR_CAP_BE))
3206 continue;
3207
3208 dump_servers_state(curproxy, &trash);
3209 }
3210 }
3211
3212 if (bi_putchk(si_ic(si), &trash) == -1) {
3213 si_applet_cant_put(si);
3214 return 0;
3215 }
3216
3217 return 1;
3218}
3219
Willy Tarreau12833bb2014-01-28 16:49:56 +01003220/* This function dumps memory usage information onto the stream interface's
3221 * read buffer. It returns 0 as long as it does not complete, non-zero upon
3222 * completion. No state is used.
3223 */
3224static int stats_dump_pools_to_buffer(struct stream_interface *si)
3225{
3226 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01003227 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003228 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01003229 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003230 }
Willy Tarreau82a86022016-01-04 19:04:18 +01003231 return 1;
3232}
3233
3234/* Dump all fields from <stats> into <out> using CSV format */
3235static int stats_dump_fields_csv(struct chunk *out, const struct field *stats)
3236{
3237 int field;
3238
3239 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
3240 if (!stats_emit_raw_data_field(out, &stats[field]))
3241 return 0;
3242 if (!chunk_strcat(out, ","))
3243 return 0;
3244 }
3245 chunk_strcat(&trash, "\n");
Willy Tarreau12833bb2014-01-28 16:49:56 +01003246 return 1;
3247}
3248
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003249/* Dumps a frontend's line to the trash for the current proxy <px> and uses
3250 * the state from stream interface <si>. The caller is responsible for clearing
3251 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02003252 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003253static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02003254{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003255 struct appctx *appctx = __objt_appctx(si->end);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003256
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003257 if (!(px->cap & PR_CAP_FE))
3258 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02003259
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003260 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003261 return 0;
3262
Willy Tarreau9e561072016-01-04 19:36:41 +01003263 memset(&stats, 0, sizeof(stats));
3264
3265 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
3266 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
3267 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
3268 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
3269 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
3270 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
3271 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
3272 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
3273 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
3274 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
3275 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
3276 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
3277 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
3278 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
3279 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
3280 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
3281 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
3282 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
3283 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
3284
3285 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3286 if (px->mode == PR_MODE_HTTP) {
3287 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
3288 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
3289 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
3290 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
3291 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
3292 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
3293 }
3294
3295 /* requests : req_rate, req_rate_max, req_tot, */
3296 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
3297 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
3298 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
3299
3300 /* compression: in, out, bypassed, responses */
3301 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
3302 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
3303 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
3304 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
3305
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003306 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003307 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003308 /* name, queue */
3309 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003310
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003311 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003312 /* Column sub-heading for Enable or Disable server */
3313 chunk_appendf(&trash, "<td></td>");
3314 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003315
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003316 chunk_appendf(&trash,
3317 "<td class=ac>"
3318 "<a name=\"%s/Frontend\"></a>"
3319 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
3320 "<td colspan=3></td>"
3321 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003322 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003323
Willy Tarreau466c9b52012-12-23 02:25:03 +01003324 chunk_appendf(&trash,
3325 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003326 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003327 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
3328 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
3329 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003330 U2H(stats[ST_F_RATE].u.u32),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003331 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003332 U2H(stats[ST_F_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003333
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003334 if (px->mode == PR_MODE_HTTP)
3335 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003336 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003337 U2H(stats[ST_F_REQ_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003338
3339 chunk_appendf(&trash,
3340 "</table></div></u></td>"
3341 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003342 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003343 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
3344 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
3345 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003346 U2H(stats[ST_F_RATE_MAX].u.u32),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003347 U2H(px->fe_counters.cps_max),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003348 U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003349
3350 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003351 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003352 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003353 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003354
3355 chunk_appendf(&trash,
3356 "</table></div></u></td>"
3357 /* sessions rate : limit */
3358 "<td>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003359 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003360
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003361 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003362 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003363 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003364 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003365 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
3366 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003367 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003368 U2H(stats[ST_F_SCUR].u.u32), U2H(px->fe_counters.conn_max), U2H(stats[ST_F_SLIM].u.u32),
3369 U2H(stats[ST_F_STOT].u.u64),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003370 U2H(px->fe_counters.cum_conn),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003371 U2H(stats[ST_F_STOT].u.u64));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003372
Willy Tarreau466c9b52012-12-23 02:25:03 +01003373 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003374 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003375 chunk_appendf(&trash,
3376 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3377 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3378 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3379 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3380 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3381 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3382 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3383 "<tr><th>- other responses:</th><td>%s</td></tr>"
3384 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3385 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003386 U2H(stats[ST_F_REQ_TOT].u.u64),
3387 U2H(stats[ST_F_HRSP_1XX].u.u64),
3388 U2H(stats[ST_F_HRSP_2XX].u.u64),
3389 U2H(stats[ST_F_COMP_RSP].u.u64),
3390 stats[ST_F_HRSP_2XX].u.u64 ?
3391 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3392 U2H(stats[ST_F_HRSP_3XX].u.u64),
3393 U2H(stats[ST_F_HRSP_4XX].u.u64),
3394 U2H(stats[ST_F_HRSP_5XX].u.u64),
3395 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003396 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003397 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003398
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003399 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003400 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003401 /* sessions: lbtot, lastsess */
3402 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003403 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003404 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003405 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003406 U2H(stats[ST_F_BIN].u.u64));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003407
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003408 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003409 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003410 "<td>%s%s<div class=tips><table class=det>"
3411 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3412 "<tr><th>Compression in:</th><td>%s</td></tr>"
3413 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3414 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3415 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3416 "</table></div>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003417 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3418 U2H(stats[ST_F_BOUT].u.u64),
3419 U2H(stats[ST_F_BOUT].u.u64),
3420 U2H(stats[ST_F_COMP_IN].u.u64),
3421 U2H(stats[ST_F_COMP_OUT].u.u64),
3422 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3423 U2H(stats[ST_F_COMP_BYP].u.u64),
3424 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
3425 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,
3426 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003427
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003428 chunk_appendf(&trash,
3429 /* denied: req, resp */
3430 "<td>%s</td><td>%s</td>"
3431 /* errors : request, connect, response */
3432 "<td>%s</td><td></td><td></td>"
3433 /* warnings: retries, redispatches */
3434 "<td></td><td></td>"
3435 /* server status : reflect frontend status */
3436 "<td class=ac>%s</td>"
3437 /* rest of server: nothing */
3438 "<td class=ac colspan=8></td></tr>"
3439 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003440 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3441 U2H(stats[ST_F_EREQ].u.u64),
3442 field_str(stats, ST_F_STATUS));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003443 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003444 else { /* CSV mode */
Willy Tarreau9e561072016-01-04 19:36:41 +01003445 /* dump everything */
3446 stats_dump_fields_csv(&trash, stats);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003447 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003448 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003449}
3450
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003451/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
3452 * from stream interface <si>, and stats flags <flags>. The caller is responsible
3453 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
3454 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02003455 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003456static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02003457{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003458 struct appctx *appctx = __objt_appctx(si->end);
3459
Willy Tarreau4607fad2016-01-06 15:41:29 +01003460 memset(&stats, 0, sizeof(stats));
3461
3462 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
3463 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
3464 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
3465 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
3466 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
3467 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
3468 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
3469 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
3470 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
3471 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
3472 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
3473 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (l->nbconn < l->maxconn) ? "OPEN" : "FULL");
3474 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
3475 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
3476 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
3477 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
3478
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003479 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003480 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003481 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003482 /* Column sub-heading for Enable or Disable server */
3483 chunk_appendf(&trash, "<td></td>");
3484 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003485 chunk_appendf(&trash,
3486 /* frontend name, listener name */
3487 "<td class=ac><a name=\"%s/+%s\"></a>%s"
3488 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
3489 "",
Willy Tarreauf7e27232016-01-06 15:49:26 +01003490 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003491 (flags & ST_SHLGNDS)?"<u>":"",
Willy Tarreauf7e27232016-01-06 15:49:26 +01003492 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
Willy Tarreau91861262007-10-17 17:06:05 +02003493
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003494 if (flags & ST_SHLGNDS) {
3495 char str[INET6_ADDRSTRLEN];
3496 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02003497
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003498 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02003499
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003500 port = get_host_port(&l->addr);
3501 switch (addr_to_str(&l->addr, str, sizeof(str))) {
3502 case AF_INET:
3503 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
3504 break;
3505 case AF_INET6:
3506 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
3507 break;
3508 case AF_UNIX:
3509 chunk_appendf(&trash, "unix, ");
3510 break;
3511 case -1:
3512 chunk_appendf(&trash, "(%s), ", strerror(errno));
3513 break;
3514 }
Willy Tarreau91861262007-10-17 17:06:05 +02003515
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003516 /* id */
Willy Tarreauf7e27232016-01-06 15:49:26 +01003517 chunk_appendf(&trash, "id: %d</div>", stats[ST_F_SID].u.u32);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003518 }
Willy Tarreau91861262007-10-17 17:06:05 +02003519
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003520 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003521 /* queue */
3522 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003523 /* sessions rate: current, max, limit */
3524 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003525 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003526 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003527 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003528 /* bytes: in, out */
3529 "<td>%s</td><td>%s</td>"
3530 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003531 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauf7e27232016-01-06 15:49:26 +01003532 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3533 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
Willy Tarreau56a560a2009-09-22 19:27:35 +02003534
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003535 chunk_appendf(&trash,
3536 /* denied: req, resp */
3537 "<td>%s</td><td>%s</td>"
3538 /* errors: request, connect, response */
3539 "<td>%s</td><td></td><td></td>"
3540 /* warnings: retries, redispatches */
3541 "<td></td><td></td>"
3542 /* server status: reflect listener status */
3543 "<td class=ac>%s</td>"
3544 /* rest of server: nothing */
3545 "<td class=ac colspan=8></td></tr>"
3546 "",
Willy Tarreauf7e27232016-01-06 15:49:26 +01003547 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3548 U2H(stats[ST_F_EREQ].u.u64),
3549 (stats[ST_F_SCUR].u.u32 < stats[ST_F_SLIM].u.u32) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003550 }
3551 else { /* CSV mode */
Willy Tarreau4607fad2016-01-06 15:41:29 +01003552 /* dump everything */
3553 stats_dump_fields_csv(&trash, stats);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003554 }
3555 return 1;
3556}
Willy Tarreau91861262007-10-17 17:06:05 +02003557
Simon Horman4d2eab62015-04-23 14:51:26 +09003558enum srv_stats_state {
3559 SRV_STATS_STATE_DOWN = 0,
3560 SRV_STATS_STATE_DOWN_AGENT,
3561 SRV_STATS_STATE_GOING_UP,
3562 SRV_STATS_STATE_UP_GOING_DOWN,
3563 SRV_STATS_STATE_UP,
3564 SRV_STATS_STATE_NOLB_GOING_DOWN,
3565 SRV_STATS_STATE_NOLB,
3566 SRV_STATS_STATE_DRAIN_GOING_DOWN,
3567 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09003568 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09003569 SRV_STATS_STATE_NO_CHECK,
3570
3571 SRV_STATS_STATE_COUNT, /* Must be last */
3572};
3573
Willy Tarreau2b96cf12016-01-06 19:25:38 +01003574static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3575 [SRV_STATS_STATE_DOWN] = "DOWN",
3576 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
3577 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
3578 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
3579 [SRV_STATS_STATE_UP] = "UP",
3580 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
3581 [SRV_STATS_STATE_NOLB] = "NOLB",
3582 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
3583 [SRV_STATS_STATE_DRAIN] = "DRAIN",
3584 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
3585 [SRV_STATS_STATE_NO_CHECK] = "no check"
3586};
3587
Simon Horman837bfa72015-04-23 14:51:27 +09003588enum srv_stats_colour {
3589 SRV_STATS_COLOUR_DOWN = 0,
3590 SRV_STATS_COLOUR_GOING_UP,
3591 SRV_STATS_COLOUR_GOING_DOWN,
3592 SRV_STATS_COLOUR_UP,
3593 SRV_STATS_COLOUR_NOLB,
3594 SRV_STATS_COLOUR_DRAINING,
3595 SRV_STATS_COLOUR_NO_CHECK,
3596
3597 SRV_STATS_COLOUR_COUNT, /* Must be last */
3598};
3599
3600static const char *srv_stats_colour_st[SRV_STATS_COLOUR_COUNT] = {
3601 [SRV_STATS_COLOUR_DOWN] = "down",
3602 [SRV_STATS_COLOUR_GOING_UP] = "going_up",
3603 [SRV_STATS_COLOUR_GOING_DOWN] = "going_down",
3604 [SRV_STATS_COLOUR_UP] = "up",
3605 [SRV_STATS_COLOUR_NOLB] = "nolb",
3606 [SRV_STATS_COLOUR_DRAINING] = "draining",
3607 [SRV_STATS_COLOUR_NO_CHECK] = "no_check",
3608};
3609
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003610/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
3611 * from stream interface <si>, stats flags <flags>, and server state <state>.
3612 * The caller is responsible for clearing the trash if needed. Returns non-zero
Simon Horman4d2eab62015-04-23 14:51:26 +09003613 * if it emits anything, zero otherwise.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003614 */
Willy Tarreauba2f2642016-01-07 09:54:40 +01003615static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003616{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003617 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02003618 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003619 char str[INET6_ADDRSTRLEN];
3620 struct chunk src;
Willy Tarreau2b96cf12016-01-06 19:25:38 +01003621 struct chunk *out = get_trash_chunk();
Willy Tarreauba2f2642016-01-07 09:54:40 +01003622 enum srv_stats_state state;
3623 enum srv_stats_colour colour;
Willy Tarreau2b96cf12016-01-06 19:25:38 +01003624 char *fld_status;
Willy Tarreau32091232014-05-16 13:52:00 +02003625 /* we have "via" which is the tracked server as described in the configuration,
3626 * and "ref" which is the checked server and the end of the chain.
3627 */
3628 via = sv->track ? sv->track : sv;
3629 ref = via;
3630 while (ref->track)
3631 ref = ref->track;
3632
Willy Tarreauba2f2642016-01-07 09:54:40 +01003633 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
3634 if ((ref->check.state & CHK_ST_ENABLED) &&
3635 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
3636 state = SRV_STATS_STATE_UP_GOING_DOWN;
3637 colour = SRV_STATS_COLOUR_GOING_DOWN;
3638 } else {
3639 state = SRV_STATS_STATE_UP;
3640 colour = SRV_STATS_COLOUR_UP;
3641 }
3642
3643 if (state == SRV_STATS_STATE_UP && !ref->uweight)
3644 colour = SRV_STATS_COLOUR_DRAINING;
3645
3646 if (sv->admin & SRV_ADMF_DRAIN) {
3647 if (ref->agent.state & CHK_ST_ENABLED)
3648 state = SRV_STATS_STATE_DRAIN_AGENT;
3649 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
3650 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
3651 else
3652 state = SRV_STATS_STATE_DRAIN;
3653 }
3654
3655 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
3656 state = SRV_STATS_STATE_NO_CHECK;
3657 colour = SRV_STATS_COLOUR_NO_CHECK;
3658 }
3659 }
3660 else if (sv->state == SRV_ST_STOPPING) {
3661 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
3662 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
3663 state = SRV_STATS_STATE_NOLB;
3664 colour = SRV_STATS_COLOUR_NOLB;
3665 } else {
3666 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
3667 colour = SRV_STATS_COLOUR_GOING_DOWN;
3668 }
3669 }
3670 else { /* stopped */
3671 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
3672 state = SRV_STATS_STATE_DOWN_AGENT;
3673 colour = SRV_STATS_COLOUR_DOWN;
3674 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
3675 state = SRV_STATS_STATE_DOWN; /* DOWN */
3676 colour = SRV_STATS_COLOUR_DOWN;
3677 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
3678 state = SRV_STATS_STATE_GOING_UP;
3679 colour = SRV_STATS_COLOUR_GOING_UP;
3680 } else {
3681 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
3682 colour = SRV_STATS_COLOUR_DOWN;
3683 }
3684 }
3685
Willy Tarreau2b96cf12016-01-06 19:25:38 +01003686 chunk_reset(out);
3687 memset(&stats, 0, sizeof(stats));
3688
3689 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
3690 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
3691 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
3692 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
3693 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
3694 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
3695
3696 if (sv->maxconn)
3697 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
3698
3699 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
3700 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
3701 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
3702 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
3703 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
3704 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
3705 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
3706 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
3707
3708 /* status */
3709 fld_status = chunk_newstr(out);
3710 if (sv->admin & SRV_ADMF_IMAINT)
3711 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
3712 else if (sv->admin & SRV_ADMF_MAINT)
3713 chunk_appendf(out, "MAINT");
3714 else
3715 chunk_appendf(out,
3716 srv_hlt_st[state],
3717 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3718 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
3719
3720 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
Willy Tarreau164d4a92016-01-06 19:48:21 +01003721 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Willy Tarreau2b96cf12016-01-06 19:25:38 +01003722 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
3723 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
3724 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
3725
3726 /* check failures: unique, fatal; last change, total downtime */
3727 if (sv->check.state & CHK_ST_ENABLED) {
3728 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
3729 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
Willy Tarreau2b96cf12016-01-06 19:25:38 +01003730 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
3731 }
3732
3733 if (sv->maxqueue)
3734 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
3735
3736 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
3737 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
3738 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
3739
3740 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
3741 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
3742
3743 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
3744
3745 if (sv->track) {
3746 char *fld_track = chunk_newstr(out);
3747
3748 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
3749 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
3750 }
3751
3752 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
3753 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
3754 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
3755
Willy Tarreau8a8cce12016-01-08 09:41:59 +01003756 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
Willy Tarreau2b96cf12016-01-06 19:25:38 +01003757 const char *fld_chksts;
3758
3759 fld_chksts = chunk_newstr(out);
Willy Tarreau03fb1c52016-01-08 09:43:54 +01003760 chunk_strcat(out, "* "); // for check in progress
Willy Tarreau2b96cf12016-01-06 19:25:38 +01003761 chunk_strcat(out, get_check_status_info(sv->check.status));
Willy Tarreau03fb1c52016-01-08 09:43:54 +01003762 if (!(sv->check.state & CHK_ST_INPROGRESS))
3763 fld_chksts += 2; // skip "* "
Willy Tarreau2b96cf12016-01-06 19:25:38 +01003764 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
3765
3766 if (sv->check.status >= HCHK_STATUS_L57DATA)
3767 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
3768
3769 if (sv->check.status >= HCHK_STATUS_CHECKED)
3770 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
Willy Tarreaudd7354b2016-01-08 13:47:26 +01003771
3772 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
3773 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
Willy Tarreau3141f592016-01-08 14:25:28 +01003774 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
3775 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
3776 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
Willy Tarreau2b96cf12016-01-06 19:25:38 +01003777 }
3778
Willy Tarreau7f618842016-01-08 11:40:03 +01003779 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
3780 const char *fld_chksts;
3781
3782 fld_chksts = chunk_newstr(out);
3783 chunk_strcat(out, "* "); // for check in progress
3784 chunk_strcat(out, get_check_status_info(sv->agent.status));
3785 if (!(sv->agent.state & CHK_ST_INPROGRESS))
3786 fld_chksts += 2; // skip "* "
3787 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
3788
3789 if (sv->agent.status >= HCHK_STATUS_L57DATA)
3790 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
3791
3792 if (sv->agent.status >= HCHK_STATUS_CHECKED)
3793 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
Willy Tarreaudd7354b2016-01-08 13:47:26 +01003794
3795 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
3796 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
Willy Tarreau3141f592016-01-08 14:25:28 +01003797 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
3798 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
3799 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
Willy Tarreau7f618842016-01-08 11:40:03 +01003800 }
3801
Willy Tarreau2b96cf12016-01-06 19:25:38 +01003802 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3803 if (px->mode == PR_MODE_HTTP) {
3804 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
3805 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
3806 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
3807 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
3808 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
3809 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
3810 }
3811
Willy Tarreau89fa6912016-01-08 15:12:28 +01003812 if (ref->observe)
3813 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
3814
Willy Tarreau2b96cf12016-01-06 19:25:38 +01003815 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
3816 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
3817 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
3818
Willy Tarreau2b96cf12016-01-06 19:25:38 +01003819 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
3820 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
3821 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
3822 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
3823
Willy Tarreau3a4ec3a2016-01-08 15:35:43 +01003824 if (flags & ST_SHLGNDS) {
3825 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
3826 case AF_INET:
3827 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
3828 chunk_appendf(out, "%s:%d", str, get_host_port(&sv->addr));
3829 break;
3830 case AF_INET6:
3831 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
3832 chunk_appendf(out, "[%s]:%d", str, get_host_port(&sv->addr));
3833 break;
3834 case AF_UNIX:
3835 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
3836 break;
3837 case -1:
3838 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
3839 chunk_strcat(out, strerror(errno));
3840 break;
3841 default: /* address family not supported */
3842 break;
3843 }
3844 }
3845
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003846 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau02bc6c22016-01-07 12:59:57 +01003847 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003848 chunk_appendf(&trash, "<tr class=\"maintain\">");
3849 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003850 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09003851 "<tr class=\"%s_%s\">",
Willy Tarreau164d4a92016-01-06 19:48:21 +01003852 (stats[ST_F_BCK].u.u32) ? "backup" : "active", srv_stats_colour_st[colour]);
Willy Tarreau91861262007-10-17 17:06:05 +02003853
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003854 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003855 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003856 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
Willy Tarreau164d4a92016-01-06 19:48:21 +01003857 field_str(stats, ST_F_SVNAME));
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003858
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003859 chunk_appendf(&trash,
3860 "<td class=ac><a name=\"%s/%s\"></a>%s"
3861 "<a class=lfsb href=\"#%s/%s\">%s</a>"
Willy Tarreau164d4a92016-01-06 19:48:21 +01003862 "",
3863 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003864 (flags & ST_SHLGNDS) ? "<u>" : "",
Willy Tarreau164d4a92016-01-06 19:48:21 +01003865 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
Willy Tarreau91861262007-10-17 17:06:05 +02003866
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003867 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003868 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003869
Willy Tarreau3a4ec3a2016-01-08 15:35:43 +01003870 if (isdigit(*field_str(stats, ST_F_ADDR)))
3871 chunk_appendf(&trash, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
3872 else if (*field_str(stats, ST_F_ADDR) == '[')
3873 chunk_appendf(&trash, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
3874 else if (*field_str(stats, ST_F_ADDR))
3875 chunk_appendf(&trash, "%s, ", field_str(stats, ST_F_ADDR));
Willy Tarreau91861262007-10-17 17:06:05 +02003876
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003877 /* id */
Willy Tarreau164d4a92016-01-06 19:48:21 +01003878 chunk_appendf(&trash, "id: %d", stats[ST_F_SID].u.u32);
Willy Tarreau91861262007-10-17 17:06:05 +02003879
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003880 /* cookie */
3881 if (sv->cookie) {
3882 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02003883
Willy Tarreau164d4a92016-01-06 19:48:21 +01003884 chunk_initstr(&src, sv->cookie);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003885 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003886
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003887 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003888 }
3889
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003890 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003891 }
Willy Tarreau91861262007-10-17 17:06:05 +02003892
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003893 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003894 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003895 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003896 /* sessions rate : current, max, limit */
3897 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003898 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003899 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau164d4a92016-01-06 19:48:21 +01003900 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
3901 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003902
3903 chunk_appendf(&trash,
3904 /* sessions: current, max, limit, total */
3905 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003906 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003907 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3908 "",
Willy Tarreau164d4a92016-01-06 19:48:21 +01003909 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
3910 U2H(stats[ST_F_STOT].u.u64),
3911 U2H(stats[ST_F_STOT].u.u64));
Willy Tarreau91861262007-10-17 17:06:05 +02003912
Willy Tarreau466c9b52012-12-23 02:25:03 +01003913 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3914 if (px->mode == PR_MODE_HTTP) {
3915 unsigned long long tot;
Willy Tarreau164d4a92016-01-06 19:48:21 +01003916
3917 tot = stats[ST_F_HRSP_OTHER].u.u64;
3918 tot += stats[ST_F_HRSP_1XX].u.u64;
3919 tot += stats[ST_F_HRSP_2XX].u.u64;
3920 tot += stats[ST_F_HRSP_3XX].u.u64;
3921 tot += stats[ST_F_HRSP_4XX].u.u64;
3922 tot += stats[ST_F_HRSP_5XX].u.u64;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003923
Willy Tarreau466c9b52012-12-23 02:25:03 +01003924 chunk_appendf(&trash,
3925 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3926 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3927 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3928 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3929 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3930 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3931 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3932 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003933 U2H(tot),
Willy Tarreau164d4a92016-01-06 19:48:21 +01003934 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
3935 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
3936 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
3937 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
3938 U2H(stats[ST_F_HRSP_5XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / tot) : 0,
3939 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0);
Willy Tarreau91861262007-10-17 17:06:05 +02003940 }
Willy Tarreau91861262007-10-17 17:06:05 +02003941
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003942 chunk_appendf(&trash, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
Willy Tarreau164d4a92016-01-06 19:48:21 +01003943 chunk_appendf(&trash, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
3944 chunk_appendf(&trash, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003945 if (px->mode == PR_MODE_HTTP)
Willy Tarreau164d4a92016-01-06 19:48:21 +01003946 chunk_appendf(&trash, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3947 chunk_appendf(&trash, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003948
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003949 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003950 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003951 /* sessions: lbtot, last */
3952 "<td>%s</td><td>%s</td>",
Willy Tarreau164d4a92016-01-06 19:48:21 +01003953 U2H(stats[ST_F_LBTOT].u.u64),
3954 human_time(stats[ST_F_LASTSESS].u.s32, 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003955
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003956 chunk_appendf(&trash,
3957 /* bytes : in, out */
3958 "<td>%s</td><td>%s</td>"
3959 /* denied: req, resp */
3960 "<td></td><td>%s</td>"
3961 /* errors : request, connect */
3962 "<td></td><td>%s</td>"
3963 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003964 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003965 /* warnings: retries, redispatches */
3966 "<td>%lld</td><td>%lld</td>"
3967 "",
Willy Tarreau164d4a92016-01-06 19:48:21 +01003968 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
3969 U2H(stats[ST_F_DRESP].u.u64),
3970 U2H(stats[ST_F_ECON].u.u64),
3971 U2H(stats[ST_F_ERESP].u.u64),
3972 (long long)stats[ST_F_CLI_ABRT].u.u64,
3973 (long long)stats[ST_F_SRV_ABRT].u.u64,
3974 (long long)stats[ST_F_WRETR].u.u64,
3975 (long long)stats[ST_F_WREDIS].u.u64);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003976
Willy Tarreau164d4a92016-01-06 19:48:21 +01003977 /* status, last change */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003978 chunk_appendf(&trash, "<td class=ac>");
3979
Willy Tarreau164d4a92016-01-06 19:48:21 +01003980 /* FIXME!!!!
3981 * LASTCHG should contain the last change for *this* server and must be computed
3982 * properly above, as was done below, ie: this server if maint, otherwise ref server
3983 * if tracking. Note that ref is either local or remote depending on tracking.
3984 */
3985
3986
Willy Tarreau02bc6c22016-01-07 12:59:57 +01003987 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
3988 chunk_appendf(&trash, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003989 }
Willy Tarreau30d33732016-01-08 14:57:09 +01003990 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
3991 chunk_strcat(&trash, "<i>no check</i>");
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003992 }
Willy Tarreau30d33732016-01-08 14:57:09 +01003993 else {
3994 chunk_appendf(&trash, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
3995 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
3996 if (stats[ST_F_CHECK_HEALTH].u.u32)
3997 chunk_strcat(&trash, " &uarr;");
3998 }
3999 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
4000 chunk_strcat(&trash, " &darr;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02004001 }
Willy Tarreau91861262007-10-17 17:06:05 +02004002
Willy Tarreau8485f4e2016-01-07 13:59:10 +01004003 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
Willy Tarreau3141f592016-01-08 14:25:28 +01004004 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004005 chunk_appendf(&trash,
Willy Tarreau7f618842016-01-08 11:40:03 +01004006 "</td><td class=ac><u> %s",
4007 field_str(stats, ST_F_AGENT_STATUS));
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004008
Willy Tarreau7f618842016-01-08 11:40:03 +01004009 if (stats[ST_F_AGENT_CODE].type)
4010 chunk_appendf(&trash, "/%d", stats[ST_F_AGENT_CODE].u.u32);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004011
Willy Tarreau7f618842016-01-08 11:40:03 +01004012 if (stats[ST_F_AGENT_DURATION].type && stats[ST_F_AGENT_DURATION].u.u64 >= 0)
4013 chunk_appendf(&trash, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004014
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004015 chunk_appendf(&trash, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
4016
Willy Tarreau164d4a92016-01-06 19:48:21 +01004017 if (*field_str(stats, ST_F_LAST_AGT)) {
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004018 chunk_appendf(&trash, ": ");
Willy Tarreau164d4a92016-01-06 19:48:21 +01004019 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004020 chunk_htmlencode(&trash, &src);
4021 }
4022 chunk_appendf(&trash, "</div></u>");
4023 }
Willy Tarreauce1dd442016-01-08 09:48:26 +01004024 else if (stats[ST_F_CHECK_STATUS].type) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004025 chunk_appendf(&trash,
Willy Tarreau03fb1c52016-01-08 09:43:54 +01004026 "</td><td class=ac><u> %s",
Willy Tarreau164d4a92016-01-06 19:48:21 +01004027 field_str(stats, ST_F_CHECK_STATUS));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004028
Willy Tarreau164d4a92016-01-06 19:48:21 +01004029 if (stats[ST_F_CHECK_CODE].type)
4030 chunk_appendf(&trash, "/%d", stats[ST_F_CHECK_CODE].u.u32);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004031
Willy Tarreau164d4a92016-01-06 19:48:21 +01004032 if (stats[ST_F_CHECK_DURATION].type && stats[ST_F_CHECK_DURATION].u.u64 >= 0)
4033 chunk_appendf(&trash, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004034
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004035 chunk_appendf(&trash, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
4036
Willy Tarreau164d4a92016-01-06 19:48:21 +01004037 if (*field_str(stats, ST_F_LAST_CHK)) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004038 chunk_appendf(&trash, ": ");
Willy Tarreau164d4a92016-01-06 19:48:21 +01004039 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004040 chunk_htmlencode(&trash, &src);
4041 }
4042 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004043 }
4044 else
4045 chunk_appendf(&trash, "</td><td>");
4046
4047 chunk_appendf(&trash,
4048 /* weight */
4049 "</td><td class=ac>%d</td>"
4050 /* act, bck */
4051 "<td class=ac>%s</td><td class=ac>%s</td>"
4052 "",
Willy Tarreau164d4a92016-01-06 19:48:21 +01004053 stats[ST_F_WEIGHT].u.u32,
4054 stats[ST_F_BCK].u.u32 ? "-" : "Y",
4055 stats[ST_F_BCK].u.u32 ? "Y" : "-");
4056
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004057 /* check failures: unique, fatal, down time */
Willy Tarreau89fa6912016-01-08 15:12:28 +01004058 if (stats[ST_F_CHKFAIL].type) {
Willy Tarreau164d4a92016-01-06 19:48:21 +01004059 chunk_appendf(&trash, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004060
Willy Tarreau89fa6912016-01-08 15:12:28 +01004061 if (stats[ST_F_HANAFAIL].type)
Willy Tarreau164d4a92016-01-06 19:48:21 +01004062 chunk_appendf(&trash, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004063
4064 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004065 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004066 "<td>%lld</td><td>%s</td>"
4067 "",
Willy Tarreau89fa6912016-01-08 15:12:28 +01004068 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
Willy Tarreau164d4a92016-01-06 19:48:21 +01004069 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004070 }
Willy Tarreau02bc6c22016-01-07 12:59:57 +01004071 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
4072 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02004073 chunk_appendf(&trash,
Willy Tarreau164d4a92016-01-06 19:48:21 +01004074 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
4075 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
Willy Tarreauf4659942013-11-28 10:50:06 +01004076 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004077 else
4078 chunk_appendf(&trash, "<td colspan=3></td>");
4079
4080 /* throttle */
Willy Tarreau164d4a92016-01-06 19:48:21 +01004081 if (stats[ST_F_THROTTLE].type)
4082 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004083 else
4084 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
4085 }
4086 else { /* CSV mode */
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004087 /* dump everything */
4088 stats_dump_fields_csv(&trash, stats);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004089 }
4090 return 1;
4091}
4092
4093/* Dumps a line for backend <px> to the trash for and uses the state from stream
4094 * interface <si> and stats flags <flags>. The caller is responsible for clearing
4095 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
4096 */
4097static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
4098{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004099 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004100 struct chunk src;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004101
4102 if (!(px->cap & PR_CAP_BE))
4103 return 0;
4104
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004105 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004106 return 0;
4107
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004108 memset(&stats, 0, sizeof(stats));
4109
4110 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4111 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
4112 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
4113 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
4114 stats[ST_F_SCUR] = mkf_u32(FO_CONFIG|FN_LIMIT, px->beconn);
4115 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
4116 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
4117 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
4118 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
4119 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
4120 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
4121 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
4122 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
4123 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
4124 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
4125 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
4126 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
4127 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4128 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
4129 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
4130 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
4131 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
Willy Tarreau7344f472016-01-11 12:04:02 +01004132 if (px->srv)
4133 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
4134
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004135 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4136 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4137 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
4138 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
4139 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
4140 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
4141 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
4142
4143 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4144 if (px->mode == PR_MODE_HTTP) {
4145 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
4146 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
4147 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
4148 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
4149 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
4150 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
4151 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
4152 }
4153
4154 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
4155 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
4156
4157 /* compression: in, out, bypassed, responses */
4158 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
4159 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
4160 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
4161 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
4162 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
4163
4164 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
4165 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
4166 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
4167 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
4168
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004169 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004170 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004171 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004172 /* Column sub-heading for Enable or Disable server */
4173 chunk_appendf(&trash, "<td></td>");
4174 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004175 chunk_appendf(&trash,
4176 "<td class=ac>"
4177 /* name */
4178 "%s<a name=\"%s/Backend\"></a>"
4179 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
4180 "",
4181 (flags & ST_SHLGNDS)?"<u>":"",
Willy Tarreau770c2172016-01-06 17:14:42 +01004182 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004183
4184 if (flags & ST_SHLGNDS) {
4185 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004186 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004187 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
4188
4189 /* cookie */
4190 if (px->cookie_name) {
4191 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau164d4a92016-01-06 19:48:21 +01004192 chunk_initstr(&src, px->cookie_name);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004193 chunk_htmlencode(&trash, &src);
4194 chunk_appendf(&trash, "'");
4195 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004196 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004197 }
4198
4199 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004200 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004201 /* queue : current, max */
4202 "<td>%s</td><td>%s</td><td></td>"
4203 /* sessions rate : current, max, limit */
4204 "<td>%s</td><td>%s</td><td></td>"
4205 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004206 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau770c2172016-01-06 17:14:42 +01004207 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
4208 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004209
4210 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004211 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004212 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004213 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004214 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004215 "",
Willy Tarreau770c2172016-01-06 17:14:42 +01004216 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SLIM].u.u32),
4217 U2H(stats[ST_F_STOT].u.u64),
4218 U2H(stats[ST_F_STOT].u.u64));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004219
Willy Tarreau466c9b52012-12-23 02:25:03 +01004220 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004221 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01004222 chunk_appendf(&trash,
4223 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
4224 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
4225 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
4226 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
4227 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
4228 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
4229 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
4230 "<tr><th>- other responses:</th><td>%s</td></tr>"
4231 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004232 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004233 "",
Willy Tarreau770c2172016-01-06 17:14:42 +01004234 U2H(stats[ST_F_REQ_TOT].u.u64),
4235 U2H(stats[ST_F_HRSP_1XX].u.u64),
4236 U2H(stats[ST_F_HRSP_2XX].u.u64),
4237 U2H(stats[ST_F_COMP_RSP].u.u64),
4238 stats[ST_F_HRSP_2XX].u.u64 ?
4239 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
4240 U2H(stats[ST_F_HRSP_3XX].u.u64),
4241 U2H(stats[ST_F_HRSP_4XX].u.u64),
4242 U2H(stats[ST_F_HRSP_5XX].u.u64),
4243 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau56adcf22012-12-23 18:00:29 +01004244 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004245 }
4246
Willy Tarreau770c2172016-01-06 17:14:42 +01004247 chunk_appendf(&trash, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
4248 chunk_appendf(&trash, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004249 if (px->mode == PR_MODE_HTTP)
Willy Tarreau770c2172016-01-06 17:14:42 +01004250 chunk_appendf(&trash, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
4251 chunk_appendf(&trash, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004252
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004253 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01004254 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004255 /* sessions: lbtot, last */
4256 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004257 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004258 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004259 "",
Willy Tarreau770c2172016-01-06 17:14:42 +01004260 U2H(stats[ST_F_LBTOT].u.u64),
4261 human_time(stats[ST_F_LASTSESS].u.s32, 1),
4262 U2H(stats[ST_F_BIN].u.u64));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004263
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004264 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004265 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02004266 "<td>%s%s<div class=tips><table class=det>"
4267 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
4268 "<tr><th>Compression in:</th><td>%s</td></tr>"
4269 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
4270 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
4271 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
4272 "</table></div>%s</td>",
Willy Tarreau770c2172016-01-06 17:14:42 +01004273 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
4274 U2H(stats[ST_F_BOUT].u.u64),
4275 U2H(stats[ST_F_BOUT].u.u64),
4276 U2H(stats[ST_F_COMP_IN].u.u64),
4277 U2H(stats[ST_F_COMP_OUT].u.u64),
4278 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
4279 U2H(stats[ST_F_COMP_BYP].u.u64),
4280 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_BYP].u.u64),
4281 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,
4282 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004283
4284 chunk_appendf(&trash,
4285 /* denied: req, resp */
4286 "<td>%s</td><td>%s</td>"
4287 /* errors : request, connect */
4288 "<td></td><td>%s</td>"
4289 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004290 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004291 /* warnings: retries, redispatches */
4292 "<td>%lld</td><td>%lld</td>"
4293 /* backend status: reflect backend status (up/down): we display UP
4294 * if the backend has known working servers or if it has no server at
4295 * all (eg: for stats). Then we display the total weight, number of
4296 * active and backups. */
4297 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
4298 "<td class=ac>%d</td><td class=ac>%d</td>"
4299 "",
Willy Tarreau770c2172016-01-06 17:14:42 +01004300 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
4301 U2H(stats[ST_F_ECON].u.u64),
4302 U2H(stats[ST_F_ERESP].u.u64),
4303 (long long)stats[ST_F_CLI_ABRT].u.u64,
4304 (long long)stats[ST_F_SRV_ABRT].u.u64,
4305 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
4306 human_time(stats[ST_F_LASTCHG].u.u32, 1),
4307 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
4308 stats[ST_F_WEIGHT].u.u32,
4309 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004310
4311 chunk_appendf(&trash,
4312 /* rest of backend: nothing, down transitions, total downtime, throttle */
4313 "<td class=ac>&nbsp;</td><td>%d</td>"
4314 "<td>%s</td>"
4315 "<td></td>"
4316 "</tr>",
Willy Tarreau770c2172016-01-06 17:14:42 +01004317 stats[ST_F_CHKDOWN].u.u32,
Willy Tarreau7344f472016-01-11 12:04:02 +01004318 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004319 }
4320 else { /* CSV mode */
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004321 /* dump everything */
4322 stats_dump_fields_csv(&trash, stats);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004323 }
4324 return 1;
4325}
4326
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004327/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004328 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004329 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004330 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004331static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004332{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004333 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004334 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4335
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004336 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004337 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004338
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004339 /* 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 +02004340 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004341 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004342 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004343 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 +01004344 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004345 }
4346
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004347 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004348 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004349 }
4350
4351 /* print a new table */
4352 chunk_appendf(&trash,
4353 "<table class=\"tbl\" width=\"100%%\">\n"
4354 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004355 "<th class=\"pxname\" width=\"10%%\">");
4356
4357 chunk_appendf(&trash,
4358 "<a name=\"%s\"></a>%s"
4359 "<a class=px href=\"#%s\">%s</a>",
4360 px->id,
4361 (uri->flags & ST_SHLGNDS) ? "<u>":"",
4362 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004363
4364 if (uri->flags & ST_SHLGNDS) {
4365 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004366 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004367 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
4368 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004369 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004370 }
4371
4372 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004373 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004374 "<th class=\"%s\" width=\"90%%\">%s</th>"
4375 "</tr>\n"
4376 "</table>\n"
4377 "<table class=\"tbl\" width=\"100%%\">\n"
4378 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004379 (uri->flags & ST_SHLGNDS) ? "</u>":"",
4380 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
4381
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004382 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004383 /* Column heading for Enable or Disable server */
4384 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02004385 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004386
4387 chunk_appendf(&trash,
4388 "<th rowspan=2></th>"
4389 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004390 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004391 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
4392 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
4393 "<th colspan=9>Server</th>"
4394 "</tr>\n"
4395 "<tr class=\"titre\">"
4396 "<th>Cur</th><th>Max</th><th>Limit</th>"
4397 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004398 "<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 +01004399 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
4400 "<th>Resp</th><th>Retr</th><th>Redis</th>"
4401 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
4402 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
4403 "<th>Thrtle</th>\n"
4404 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02004405}
4406
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004407/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004408 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004409 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004410static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004411{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004412 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004413 chunk_appendf(&trash, "</table>");
4414
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004415 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004416 /* close the form used to enable/disable this proxy servers */
4417 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004418 "Choose the action to perform on the checked servers : "
4419 "<select name=action>"
4420 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02004421 "<option value=\"ready\">Set state to READY</option>"
4422 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02004423 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02004424 "<option value=\"dhlth\">Health: disable checks</option>"
4425 "<option value=\"ehlth\">Health: enable checks</option>"
4426 "<option value=\"hrunn\">Health: force UP</option>"
4427 "<option value=\"hnolb\">Health: force NOLB</option>"
4428 "<option value=\"hdown\">Health: force DOWN</option>"
4429 "<option value=\"dagent\">Agent: disable checks</option>"
4430 "<option value=\"eagent\">Agent: enable checks</option>"
4431 "<option value=\"arunn\">Agent: force UP</option>"
4432 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004433 "<option value=\"shutdown\">Kill Sessions</option>"
4434 "</select>"
4435 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
4436 "&nbsp;<input type=\"submit\" value=\"Apply\">"
4437 "</form>",
4438 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004439 }
4440
4441 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004442}
Willy Tarreau91861262007-10-17 17:06:05 +02004443
4444/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004445 * Dumps statistics for a proxy. The output is sent to the stream interface's
4446 * input buffer. Returns 0 if it had to stop dumping data because of lack of
4447 * buffer space, or non-zero if everything completed. This function is used
4448 * both by the CLI and the HTTP entry points, and is able to dump the output
4449 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02004450 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004451static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02004452{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004453 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02004454 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004455 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02004456 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004457 struct listener *l;
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004458 unsigned int flags;
4459
4460 if (uri)
4461 flags = uri->flags;
4462 else if (strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER)
4463 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
4464 else
4465 flags = ST_SHNODE | ST_SHDESC;
Willy Tarreau91861262007-10-17 17:06:05 +02004466
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004467 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02004468
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004469 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004470 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02004471 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02004472 if (uri && uri->scope) {
4473 /* we have a limited scope, we have to check the proxy name */
4474 struct stat_scope *scope;
4475 int len;
4476
4477 len = strlen(px->id);
4478 scope = uri->scope;
4479
4480 while (scope) {
4481 /* match exact proxy name */
4482 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
4483 break;
4484
4485 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02004486 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02004487 break;
4488 scope = scope->next;
4489 }
4490
4491 /* proxy name not found : don't dump anything */
4492 if (scope == NULL)
4493 return 1;
4494 }
4495
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004496 /* if the user has requested a limited output and the proxy
4497 * name does not match, skip it.
4498 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004499 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004500 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 +02004501 return 1;
4502
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004503 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
4504 (appctx->ctx.stats.iid != -1) &&
4505 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004506 return 1;
4507
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004508 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02004509 /* fall through */
4510
Willy Tarreau295a8372011-03-10 11:25:07 +01004511 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004512 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004513 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004514 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004515 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02004516 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004517 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004518 }
Willy Tarreau91861262007-10-17 17:06:05 +02004519
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004520 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02004521 /* fall through */
4522
Willy Tarreau295a8372011-03-10 11:25:07 +01004523 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02004524 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004525 if (stats_dump_fe_stats(si, px)) {
4526 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004527 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004528 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004529 }
4530 }
Willy Tarreau91861262007-10-17 17:06:05 +02004531
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004532 appctx->ctx.stats.l = px->conf.listeners.n;
4533 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004534 /* fall through */
4535
Willy Tarreau295a8372011-03-10 11:25:07 +01004536 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004537 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004538 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004539 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004540 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004541 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004542 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004543
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004544 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004545 if (!l->counters)
4546 continue;
4547
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004548 if (appctx->ctx.stats.flags & STAT_BOUND) {
4549 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004550 break;
4551
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004552 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004553 continue;
4554 }
4555
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004556 /* print the frontend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004557 if (stats_dump_li_stats(si, px, l, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004558 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004559 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004560 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004561 }
4562 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004563 }
4564
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004565 appctx->ctx.stats.sv = px->srv; /* may be NULL */
4566 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02004567 /* fall through */
4568
Willy Tarreau295a8372011-03-10 11:25:07 +01004569 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02004570 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004571 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004572 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004573 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004574 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004575 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004576
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004577 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02004578
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004579 if (appctx->ctx.stats.flags & STAT_BOUND) {
4580 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004581 break;
4582
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004583 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004584 continue;
4585 }
4586
Willy Tarreau32091232014-05-16 13:52:00 +02004587 svs = sv;
4588 while (svs->track)
4589 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004590
Willy Tarreauba2f2642016-01-07 09:54:40 +01004591 /* do not report servers which are DOWN and not changing state */
4592 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
4593 ((sv->admin & SRV_ADMF_MAINT) || /* server is in maintenance */
4594 (sv->state == SRV_ST_STOPPED && /* server is down */
4595 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
4596 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
4597 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
Willy Tarreau91861262007-10-17 17:06:05 +02004598 continue;
4599 }
4600
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004601 if (stats_dump_sv_stats(si, px, flags, sv)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004602 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004603 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004604 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004605 }
4606 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004607 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004608
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004609 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004610 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004611
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004612 case STAT_PX_ST_BE:
4613 /* print the backend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004614 if (stats_dump_be_stats(si, px, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004615 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004616 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004617 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004618 }
4619 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004620
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004621 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004622 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004623
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004624 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004625 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004626 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004627 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004628 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004629 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004630 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004631 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004632
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004633 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004634 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004635
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004636 case STAT_PX_ST_FIN:
4637 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004638
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004639 default:
4640 /* unknown state, we should put an abort() here ! */
4641 return 1;
4642 }
4643}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004644
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004645/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4646 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004647 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004648static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004649{
4650 /* WARNING! This must fit in the first buffer !!! */
4651 chunk_appendf(&trash,
4652 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4653 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4654 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4655 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4656 "<style type=\"text/css\"><!--\n"
4657 "body {"
4658 " font-family: arial, helvetica, sans-serif;"
4659 " font-size: 12px;"
4660 " font-weight: normal;"
4661 " color: black;"
4662 " background: white;"
4663 "}\n"
4664 "th,td {"
4665 " font-size: 10px;"
4666 "}\n"
4667 "h1 {"
4668 " font-size: x-large;"
4669 " margin-bottom: 0.5em;"
4670 "}\n"
4671 "h2 {"
4672 " font-family: helvetica, arial;"
4673 " font-size: x-large;"
4674 " font-weight: bold;"
4675 " font-style: italic;"
4676 " color: #6020a0;"
4677 " margin-top: 0em;"
4678 " margin-bottom: 0em;"
4679 "}\n"
4680 "h3 {"
4681 " font-family: helvetica, arial;"
4682 " font-size: 16px;"
4683 " font-weight: bold;"
4684 " color: #b00040;"
4685 " background: #e8e8d0;"
4686 " margin-top: 0em;"
4687 " margin-bottom: 0em;"
4688 "}\n"
4689 "li {"
4690 " margin-top: 0.25em;"
4691 " margin-right: 2em;"
4692 "}\n"
4693 ".hr {margin-top: 0.25em;"
4694 " border-color: black;"
4695 " border-bottom-style: solid;"
4696 "}\n"
4697 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4698 ".total {background: #20D0D0;color: #ffff80;}\n"
4699 ".frontend {background: #e8e8d0;}\n"
4700 ".socket {background: #d0d0d0;}\n"
4701 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004702 ".active_down {background: #ff9090;}\n"
4703 ".active_going_up {background: #ffd020;}\n"
4704 ".active_going_down {background: #ffffa0;}\n"
4705 ".active_up {background: #c0ffc0;}\n"
4706 ".active_nolb {background: #20a0ff;}\n"
4707 ".active_draining {background: #20a0FF;}\n"
4708 ".active_no_check {background: #e0e0e0;}\n"
4709 ".backup_down {background: #ff9090;}\n"
4710 ".backup_going_up {background: #ff80ff;}\n"
4711 ".backup_going_down {background: #c060ff;}\n"
4712 ".backup_up {background: #b0d0ff;}\n"
4713 ".backup_nolb {background: #90b0e0;}\n"
4714 ".backup_draining {background: #cc9900;}\n"
4715 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004716 ".maintain {background: #c07820;}\n"
4717 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4718 "\n"
4719 "a.px:link {color: #ffff40; text-decoration: none;}"
4720 "a.px:visited {color: #ffff40; text-decoration: none;}"
4721 "a.px:hover {color: #ffffff; text-decoration: none;}"
4722 "a.lfsb:link {color: #000000; text-decoration: none;}"
4723 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4724 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4725 "\n"
4726 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4727 "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"
4728 "table.tbl td.ac { text-align: center;}\n"
4729 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4730 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4731 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4732 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4733 "\n"
4734 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4735 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4736 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004737 "table.det { border-collapse: collapse; border-style: none; }\n"
4738 "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 +01004739 "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 +01004740 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004741 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004742 " display:block;\n"
4743 " visibility:hidden;\n"
4744 " z-index:2147483647;\n"
4745 " position:absolute;\n"
4746 " padding:2px 4px 3px;\n"
4747 " background:#f0f060; color:#000000;\n"
4748 " border:1px solid #7040c0;\n"
4749 " white-space:nowrap;\n"
4750 " font-style:normal;font-size:11px;font-weight:normal;\n"
4751 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4752 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4753 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004754 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004755 "-->\n"
4756 "</style></head>\n",
4757 (uri->flags & ST_SHNODE) ? " on " : "",
4758 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4759 );
4760}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004761
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004762/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004763 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004764 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004765 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004766static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004767{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004768 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004769 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004770 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004771
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004772 /* WARNING! this has to fit the first packet too.
4773 * We are around 3.5 kB, add adding entries will
4774 * become tricky if we want to support 4kB buffers !
4775 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004776 chunk_appendf(&trash,
4777 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4778 PRODUCT_NAME "%s</a></h1>\n"
4779 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4780 "<hr width=\"100%%\" class=\"hr\">\n"
4781 "<h3>&gt; General process information</h3>\n"
4782 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4783 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4784 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4785 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4786 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4787 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4788 "Running tasks: %d/%d; idle = %d %%<br>\n"
4789 "</td><td align=\"center\" nowrap>\n"
4790 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004791 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4792 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004793 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004794 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4795 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004796 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004797 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
4798 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004799 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004800 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
4801 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004802 "</tr><tr>\n"
4803 "<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 -07004804 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004805 "<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 +01004806 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004807 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004808 "</td>"
4809 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4810 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4811 "",
4812 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4813 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4814 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4815 (uri->flags & ST_SHDESC) ? ": " : "",
4816 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4817 pid, relative_pid, global.nbproc,
4818 up / 86400, (up % 86400) / 3600,
4819 (up % 3600) / 60, (up % 60),
4820 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4821 global.rlimit_memmax ? " MB" : "",
4822 global.rlimit_nofile,
4823 global.maxsock, global.maxconn, global.maxpipes,
4824 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4825 run_queue_cur, nb_tasks_cur, idle_pct
4826 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004827
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004828 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004829 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 +01004830 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004831
4832 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004833 "<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 +01004834 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004835 STAT_SCOPE_TXT_MAXLEN);
4836
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004837 /* 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 +02004838 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004839 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004840 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004841 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 +01004842 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004843 }
4844
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004845 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004846 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004847 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004848 uri->uri_prefix,
4849 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004850 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004851 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004852 else
4853 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004854 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004855 uri->uri_prefix,
4856 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004857 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004858 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004859
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004860 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004861 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004862 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004863 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004864 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004865 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004866 "",
4867 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004868 else
4869 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004870 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004871 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004872 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004873 ";norefresh",
4874 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004875 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004876
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004877 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004878 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004879 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004880 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4881 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004882 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004883
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004884 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004885 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004886 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004887 (uri->refresh > 0) ? ";norefresh" : "",
4888 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004889
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004890 chunk_appendf(&trash,
4891 "</ul></td>"
4892 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4893 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4894 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4895 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4896 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4897 "</ul>"
4898 "</td>"
4899 "</tr></table>\n"
4900 ""
4901 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004902
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004903 if (appctx->ctx.stats.st_code) {
4904 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004905 case STAT_STATUS_DONE:
4906 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004907 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004908 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004909 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02004910 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004911 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4912 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004913 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004914 break;
4915 case STAT_STATUS_NONE:
4916 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004917 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004918 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004919 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02004920 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004921 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4922 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004923 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004924 break;
4925 case STAT_STATUS_PART:
4926 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004927 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004928 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004929 "Action partially processed.<br>"
4930 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004931 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004932 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4933 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004934 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004935 break;
4936 case STAT_STATUS_ERRP:
4937 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004938 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004939 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004940 "Action not processed because of invalid parameters."
4941 "<ul>"
4942 "<li>The action is maybe unknown.</li>"
4943 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
4944 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4945 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004946 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004947 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4948 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004949 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004950 break;
4951 case STAT_STATUS_EXCD:
4952 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004953 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004954 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004955 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4956 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004957 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004958 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4959 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004960 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004961 break;
4962 case STAT_STATUS_DENY:
4963 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004964 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004965 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004966 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004967 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004968 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4969 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004970 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004971 break;
4972 default:
4973 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004974 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004975 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004976 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004977 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004978 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4979 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004980 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004981 }
4982 chunk_appendf(&trash, "<p>\n");
4983 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004984}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004985
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004986/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4987 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004988 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004989static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004990{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004991 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004992}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004993
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004994/* This function dumps statistics onto the stream interface's read buffer in
4995 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004996 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4997 * 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 +02004998 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02004999 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005000 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005001static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005002{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005003 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005004 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005005 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01005006
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005007 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005008
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005009 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005010 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005011 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005012 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005013
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005014 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005015 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005016 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01005017 else
5018 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01005019
Willy Tarreaubc18da12015-03-13 14:00:47 +01005020 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005021 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005022 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005023 }
Willy Tarreauae526782010-03-04 20:34:23 +01005024
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005025 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005026 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005027
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005028 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005029 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005030 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01005031 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005032 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005033 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005034 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005035 }
Willy Tarreau91861262007-10-17 17:06:05 +02005036
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005037 appctx->ctx.stats.px = proxy;
5038 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
5039 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02005040 /* fall through */
5041
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005042 case STAT_ST_LIST:
5043 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005044 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005045 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005046 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005047 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005048 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005049
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005050 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005051 /* skip the disabled proxies, global frontend and non-networked ones */
5052 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005053 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005054 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005055
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005056 appctx->ctx.stats.px = px->next;
5057 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005058 }
5059 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005060
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005061 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005062 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005063
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005064 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005065 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005066 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01005067 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005068 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005069 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005070 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005071 }
Willy Tarreau55058a72012-11-21 08:27:21 +01005072
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005073 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005074 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02005075
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005076 case STAT_ST_FIN:
5077 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01005078
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005079 default:
5080 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005081 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005082 return -1;
5083 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005084}
Willy Tarreauae526782010-03-04 20:34:23 +01005085
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005086/* We reached the stats page through a POST request. The appctx is
5087 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01005088 * Parse the posted data and enable/disable servers if necessary.
5089 * Returns 1 if request was parsed or zero if it needs more data.
5090 */
5091static int stats_process_http_post(struct stream_interface *si)
5092{
Willy Tarreau87b09662015-04-03 00:22:06 +02005093 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005094 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005095
5096 struct proxy *px = NULL;
5097 struct server *sv = NULL;
5098
5099 char key[LINESIZE];
5100 int action = ST_ADM_ACTION_NONE;
5101 int reprocess = 0;
5102
5103 int total_servers = 0;
5104 int altered_servers = 0;
5105
5106 char *first_param, *cur_param, *next_param, *end_params;
5107 char *st_cur_param = NULL;
5108 char *st_next_param = NULL;
5109
5110 struct chunk *temp;
5111 int reql;
5112
5113 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02005114 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005115 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005116 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005117 goto out;
5118 }
5119
Willy Tarreaueee5b512015-04-03 23:46:31 +02005120 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 +01005121 if (reql <= 0) {
5122 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005123 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005124 return 0;
5125 }
5126
5127 first_param = temp->str;
5128 end_params = temp->str + reql;
5129 cur_param = next_param = end_params;
5130 *end_params = '\0';
5131
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005132 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005133
5134 /*
5135 * Parse the parameters in reverse order to only store the last value.
5136 * From the html form, the backend and the action are at the end.
5137 */
5138 while (cur_param > first_param) {
5139 char *value;
5140 int poffset, plen;
5141
5142 cur_param--;
5143
5144 if ((*cur_param == '&') || (cur_param == first_param)) {
5145 reprocess_servers:
5146 /* Parse the key */
5147 poffset = (cur_param != first_param ? 1 : 0);
5148 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
5149 if ((plen > 0) && (plen <= sizeof(key))) {
5150 strncpy(key, cur_param + poffset, plen);
5151 key[plen - 1] = '\0';
5152 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005153 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005154 goto out;
5155 }
5156
5157 /* Parse the value */
5158 value = key;
5159 while (*value != '\0' && *value != '=') {
5160 value++;
5161 }
5162 if (*value == '=') {
5163 /* Ok, a value is found, we can mark the end of the key */
5164 *value++ = '\0';
5165 }
5166 if (url_decode(key) < 0 || url_decode(value) < 0)
5167 break;
5168
5169 /* Now we can check the key to see what to do */
5170 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02005171 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005172 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005173 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005174 goto out;
5175 }
5176 }
5177 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02005178 if (strcmp(value, "ready") == 0) {
5179 action = ST_ADM_ACTION_READY;
5180 }
5181 else if (strcmp(value, "drain") == 0) {
5182 action = ST_ADM_ACTION_DRAIN;
5183 }
5184 else if (strcmp(value, "maint") == 0) {
5185 action = ST_ADM_ACTION_MAINT;
5186 }
5187 else if (strcmp(value, "shutdown") == 0) {
5188 action = ST_ADM_ACTION_SHUTDOWN;
5189 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02005190 else if (strcmp(value, "dhlth") == 0) {
5191 action = ST_ADM_ACTION_DHLTH;
5192 }
5193 else if (strcmp(value, "ehlth") == 0) {
5194 action = ST_ADM_ACTION_EHLTH;
5195 }
5196 else if (strcmp(value, "hrunn") == 0) {
5197 action = ST_ADM_ACTION_HRUNN;
5198 }
5199 else if (strcmp(value, "hnolb") == 0) {
5200 action = ST_ADM_ACTION_HNOLB;
5201 }
5202 else if (strcmp(value, "hdown") == 0) {
5203 action = ST_ADM_ACTION_HDOWN;
5204 }
5205 else if (strcmp(value, "dagent") == 0) {
5206 action = ST_ADM_ACTION_DAGENT;
5207 }
5208 else if (strcmp(value, "eagent") == 0) {
5209 action = ST_ADM_ACTION_EAGENT;
5210 }
5211 else if (strcmp(value, "arunn") == 0) {
5212 action = ST_ADM_ACTION_ARUNN;
5213 }
5214 else if (strcmp(value, "adown") == 0) {
5215 action = ST_ADM_ACTION_ADOWN;
5216 }
Willy Tarreaued7df902014-05-22 18:04:49 +02005217 /* else these are the old supported methods */
5218 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005219 action = ST_ADM_ACTION_DISABLE;
5220 }
5221 else if (strcmp(value, "enable") == 0) {
5222 action = ST_ADM_ACTION_ENABLE;
5223 }
5224 else if (strcmp(value, "stop") == 0) {
5225 action = ST_ADM_ACTION_STOP;
5226 }
5227 else if (strcmp(value, "start") == 0) {
5228 action = ST_ADM_ACTION_START;
5229 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005230 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005231 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005232 goto out;
5233 }
5234 }
5235 else if (strcmp(key, "s") == 0) {
5236 if (!(px && action)) {
5237 /*
5238 * Indicates that we'll need to reprocess the parameters
5239 * as soon as backend and action are known
5240 */
5241 if (!reprocess) {
5242 st_cur_param = cur_param;
5243 st_next_param = next_param;
5244 }
5245 reprocess = 1;
5246 }
5247 else if ((sv = findserver(px, value)) != NULL) {
5248 switch (action) {
5249 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005250 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005251 altered_servers++;
5252 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005253 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005254 }
5255 break;
5256 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005257 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005258 altered_servers++;
5259 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005260 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005261 }
5262 break;
5263 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005264 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
5265 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
5266 altered_servers++;
5267 total_servers++;
5268 }
5269 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005270 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005271 if (sv->admin & SRV_ADMF_FDRAIN) {
5272 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
5273 altered_servers++;
5274 total_servers++;
5275 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005276 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02005277 case ST_ADM_ACTION_DHLTH:
5278 if (sv->check.state & CHK_ST_CONFIGURED) {
5279 sv->check.state &= ~CHK_ST_ENABLED;
5280 altered_servers++;
5281 total_servers++;
5282 }
5283 break;
5284 case ST_ADM_ACTION_EHLTH:
5285 if (sv->check.state & CHK_ST_CONFIGURED) {
5286 sv->check.state |= CHK_ST_ENABLED;
5287 altered_servers++;
5288 total_servers++;
5289 }
5290 break;
5291 case ST_ADM_ACTION_HRUNN:
5292 if (!(sv->track)) {
5293 sv->check.health = sv->check.rise + sv->check.fall - 1;
5294 srv_set_running(sv, "changed from Web interface");
5295 altered_servers++;
5296 total_servers++;
5297 }
5298 break;
5299 case ST_ADM_ACTION_HNOLB:
5300 if (!(sv->track)) {
5301 sv->check.health = sv->check.rise + sv->check.fall - 1;
5302 srv_set_stopping(sv, "changed from Web interface");
5303 altered_servers++;
5304 total_servers++;
5305 }
5306 break;
5307 case ST_ADM_ACTION_HDOWN:
5308 if (!(sv->track)) {
5309 sv->check.health = 0;
5310 srv_set_stopped(sv, "changed from Web interface");
5311 altered_servers++;
5312 total_servers++;
5313 }
5314 break;
5315 case ST_ADM_ACTION_DAGENT:
5316 if (sv->agent.state & CHK_ST_CONFIGURED) {
5317 sv->agent.state &= ~CHK_ST_ENABLED;
5318 altered_servers++;
5319 total_servers++;
5320 }
5321 break;
5322 case ST_ADM_ACTION_EAGENT:
5323 if (sv->agent.state & CHK_ST_CONFIGURED) {
5324 sv->agent.state |= CHK_ST_ENABLED;
5325 altered_servers++;
5326 total_servers++;
5327 }
5328 break;
5329 case ST_ADM_ACTION_ARUNN:
5330 if (sv->agent.state & CHK_ST_ENABLED) {
5331 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
5332 srv_set_running(sv, "changed from Web interface");
5333 altered_servers++;
5334 total_servers++;
5335 }
5336 break;
5337 case ST_ADM_ACTION_ADOWN:
5338 if (sv->agent.state & CHK_ST_ENABLED) {
5339 sv->agent.health = 0;
5340 srv_set_stopped(sv, "changed from Web interface");
5341 altered_servers++;
5342 total_servers++;
5343 }
5344 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02005345 case ST_ADM_ACTION_READY:
5346 srv_adm_set_ready(sv);
5347 altered_servers++;
5348 total_servers++;
5349 break;
5350 case ST_ADM_ACTION_DRAIN:
5351 srv_adm_set_drain(sv);
5352 altered_servers++;
5353 total_servers++;
5354 break;
5355 case ST_ADM_ACTION_MAINT:
5356 srv_adm_set_maint(sv);
5357 altered_servers++;
5358 total_servers++;
5359 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005360 case ST_ADM_ACTION_SHUTDOWN:
5361 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005362 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005363
5364 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
5365 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005366 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005367
5368 altered_servers++;
5369 total_servers++;
5370 }
5371 break;
5372 }
5373 } else {
5374 /* the server name is unknown or ambiguous (duplicate names) */
5375 total_servers++;
5376 }
5377 }
5378 if (reprocess && px && action) {
5379 /* Now, we know the backend and the action chosen by the user.
5380 * We can safely restart from the first server parameter
5381 * to reprocess them
5382 */
5383 cur_param = st_cur_param;
5384 next_param = st_next_param;
5385 reprocess = 0;
5386 goto reprocess_servers;
5387 }
5388
5389 next_param = cur_param;
5390 }
5391 }
5392
5393 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005394 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005395 }
5396 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005397 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005398 }
5399 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005400 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005401 }
5402 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005403 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005404 }
5405 out:
5406 return 1;
5407}
5408
5409
5410static int stats_send_http_headers(struct stream_interface *si)
5411{
Willy Tarreau87b09662015-04-03 00:22:06 +02005412 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005413 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005414 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005415
5416 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02005417 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005418 "Cache-Control: no-cache\r\n"
5419 "Connection: close\r\n"
5420 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005421 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005422
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005423 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01005424 chunk_appendf(&trash, "Refresh: %d\r\n",
5425 uri->refresh);
5426
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005427 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
5428
5429 if (appctx->ctx.stats.flags & STAT_CHUNKED)
5430 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
5431 else
5432 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005433
Willy Tarreaueee5b512015-04-03 23:46:31 +02005434 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005435 s->logs.tv_request = now;
5436
Willy Tarreaubc18da12015-03-13 14:00:47 +01005437 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005438 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005439 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005440 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005441
5442 return 1;
5443}
5444
5445static int stats_send_http_redirect(struct stream_interface *si)
5446{
5447 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005448 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005449 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005450 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005451
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005452 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01005453 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005454 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005455 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005456 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 +01005457 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005458 }
5459
5460 /* We don't want to land on the posted stats page because a refresh will
5461 * repost the data. We don't want this to happen on accident so we redirect
5462 * the browse to the stats page with a GET.
5463 */
5464 chunk_printf(&trash,
5465 "HTTP/1.1 303 See Other\r\n"
5466 "Cache-Control: no-cache\r\n"
5467 "Content-Type: text/plain\r\n"
5468 "Connection: close\r\n"
5469 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreaufdfcc9d2016-01-26 13:57:29 +01005470 "Content-length: 0\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005471 "\r\n",
5472 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005473 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
5474 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
5475 stat_status_codes[appctx->ctx.stats.st_code]) ?
5476 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01005477 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005478 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5479 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01005480 scope_txt);
5481
Willy Tarreaueee5b512015-04-03 23:46:31 +02005482 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005483 s->logs.tv_request = now;
5484
Willy Tarreaubc18da12015-03-13 14:00:47 +01005485 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005486 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005487 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005488 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005489
5490 return 1;
5491}
5492
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005493/* This I/O handler runs as an applet embedded in a stream interface. It is
5494 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005495 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005496 * automatically unregisters itself once transfer is complete.
5497 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005498static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005499{
Willy Tarreau00a37f02015-04-13 12:05:19 +02005500 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02005501 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005502 struct channel *req = si_oc(si);
5503 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01005504
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005505 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
5506 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005507
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005508 /* check that the output is not closed */
5509 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005510 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005511
Willy Tarreau347a35d2013-11-22 17:51:09 +01005512 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005513 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005514 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02005515 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005516 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005517 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005518 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005519 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005520 }
5521
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005522 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005523 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005524 unsigned int data_len;
5525 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02005526 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005527
5528 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5529 /* One difficulty we're facing is that we must prevent
5530 * the input data from being automatically forwarded to
5531 * the output area. For this, we temporarily disable
5532 * forwarding on the channel.
5533 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005534 last_fwd = si_ic(si)->to_forward;
5535 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005536 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005537 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005538 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005539 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02005540 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005541 }
5542 }
5543
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005544 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005545 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005546 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005547
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005548 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005549
5550 /* Now we must either adjust or remove the chunk size. This is
5551 * not easy because the chunk size might wrap at the end of the
5552 * buffer, so we pretend we have nothing in the buffer, we write
5553 * the size, then restore the buffer's contents. Note that we can
5554 * only do that because no forwarding is scheduled on the stats
5555 * applet.
5556 */
5557 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005558 si_ic(si)->total -= (last_len - prev_len);
5559 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005560
5561 if (last_len != data_len) {
5562 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005563 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005564 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005565
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005566 si_ic(si)->total += (last_len - data_len);
5567 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005568 }
5569 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005570 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005571 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005572 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005573
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005574 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005575 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005576 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005577 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005578 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005579 }
5580
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005581 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005582 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005583 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005584 }
5585
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005586 if (appctx->st0 == STAT_HTTP_DONE) {
5587 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5588 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005589 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005590 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005591 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005592 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005593 }
5594 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005595 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005596 res->flags |= CF_READ_NULL;
5597 si_shutr(si);
5598 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005599
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005600 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5601 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005602
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005603 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005604 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5605 si_shutr(si);
5606 res->flags |= CF_READ_NULL;
5607 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005608 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005609 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005610 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005611}
5612
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005613
Willy Tarreau909d5172012-11-26 03:04:41 +01005614static inline const char *get_conn_ctrl_name(const struct connection *conn)
5615{
Willy Tarreau3c728722014-01-23 13:50:42 +01005616 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005617 return "NONE";
5618 return conn->ctrl->name;
5619}
5620
5621static inline const char *get_conn_xprt_name(const struct connection *conn)
5622{
5623 static char ptr[17];
5624
Willy Tarreauaad69382014-01-23 14:21:42 +01005625 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005626 return "NONE";
5627
5628 if (conn->xprt == &raw_sock)
5629 return "RAW";
5630
5631#ifdef USE_OPENSSL
5632 if (conn->xprt == &ssl_sock)
5633 return "SSL";
5634#endif
5635 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5636 return ptr;
5637}
5638
5639static inline const char *get_conn_data_name(const struct connection *conn)
5640{
5641 static char ptr[17];
5642
5643 if (!conn->data)
5644 return "NONE";
5645
5646 if (conn->data == &sess_conn_cb)
5647 return "SESS";
5648
5649 if (conn->data == &si_conn_cb)
5650 return "STRM";
5651
5652 if (conn->data == &check_conn_cb)
5653 return "CHCK";
5654
5655 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5656 return ptr;
5657}
5658
Willy Tarreau87b09662015-04-03 00:22:06 +02005659/* This function dumps a complete stream state onto the stream interface's
5660 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005661 * 0 if the output buffer is full and it needs to be called again, otherwise
5662 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005663 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005664static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005665{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005666 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005667 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005668 extern const char *monthname[12];
5669 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005670 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005671 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005672
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005673 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005674
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005675 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005676 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005677 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005678 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005679 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005680 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005681 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005682 appctx->ctx.sess.uid = 0;
5683 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005684 return 1;
5685 }
5686
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005687 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005688 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005689 appctx->ctx.sess.uid = sess->uniq_id;
5690 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005691 /* fall through */
5692
5693 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005694 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005695 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005696 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005697 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005698 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5699 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005700 sess->uniq_id,
Willy Tarreau666f5042015-06-17 19:49:52 +02005701 strm_li(sess) ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005702
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005703 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005704 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005705 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005706 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005707 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005708 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005709 break;
5710 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005711 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005712 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005713 default:
5714 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005715 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005716 break;
5717 }
5718
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005719 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005720 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005721 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005722
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005723 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005724 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005725 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5726 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5727 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005728
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005729 if (conn)
5730 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005731
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005732 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005733 case AF_INET:
5734 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005735 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005736 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005737 break;
5738 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005739 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005740 break;
5741 default:
5742 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005743 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005744 break;
5745 }
5746
Willy Tarreau50943332011-09-02 17:33:05 +02005747 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005748 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005749 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005750 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005751 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5752 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005753 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005754
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005755 conn = objt_conn(sess->si[1].end);
5756 if (conn)
5757 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005758
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005759 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005760 case AF_INET:
5761 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005762 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005763 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005764 break;
5765 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005766 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005767 break;
5768 default:
5769 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005770 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005771 break;
5772 }
5773
5774 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005775 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005776 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005777 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5778 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005779 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005780 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005781
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005782 if (conn)
5783 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005784
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005785 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005786 case AF_INET:
5787 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005788 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005789 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005790 break;
5791 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005792 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005793 break;
5794 default:
5795 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005796 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005797 break;
5798 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005799
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005800 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005801 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005802 sess->task,
5803 sess->task->state,
5804 sess->task->nice, sess->task->calls,
5805 sess->task->expire ?
5806 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5807 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5808 TICKS_TO_MS(1000)) : "<NEVER>",
5809 task_in_rq(sess->task) ? ", running" : "");
5810
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005811 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005812 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005813 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5814
Willy Tarreaueee5b512015-04-03 23:46:31 +02005815 if (sess->txn)
5816 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005817 " 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 +02005818 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
5819 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 +01005820
5821 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005822 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005823 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005824 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005825 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005826 obj_type_name(sess->si[0].end),
5827 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005828 sess->si[0].exp ?
5829 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5830 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5831 TICKS_TO_MS(1000)) : "<NEVER>",
5832 sess->si[0].err_type);
5833
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005834 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005835 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005836 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005837 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005838 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005839 obj_type_name(sess->si[1].end),
5840 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005841 sess->si[1].exp ?
5842 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5843 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5844 TICKS_TO_MS(1000)) : "<NEVER>",
5845 sess->si[1].err_type);
5846
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005847 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005848 chunk_appendf(&trash,
5849 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005850 conn,
5851 get_conn_ctrl_name(conn),
5852 get_conn_xprt_name(conn),
5853 get_conn_data_name(conn),
5854 obj_type_name(conn->target),
5855 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005856
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005857 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005858 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005859 conn->flags,
5860 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005861 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005862 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005863 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005864 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005865 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5866 chunk_appendf(&trash,
5867 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5868 tmpctx,
5869 tmpctx->st0,
5870 tmpctx->st1,
5871 tmpctx->st2,
5872 tmpctx->applet->name);
5873 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005874
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005875 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005876 chunk_appendf(&trash,
5877 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005878 conn,
5879 get_conn_ctrl_name(conn),
5880 get_conn_xprt_name(conn),
5881 get_conn_data_name(conn),
5882 obj_type_name(conn->target),
5883 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005884
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005885 chunk_appendf(&trash,
Willy Tarreauceeafb52016-01-25 15:27:17 +01005886 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005887 conn->flags,
5888 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005889 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005890 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005891 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005892 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005893 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
5894 chunk_appendf(&trash,
5895 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
5896 tmpctx,
5897 tmpctx->st0,
5898 tmpctx->st1,
5899 tmpctx->st2,
5900 tmpctx->applet->name);
5901 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005902
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005903 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005904 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005905 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005906 &sess->req,
5907 sess->req.flags, sess->req.analysers,
5908 sess->req.pipe ? sess->req.pipe->data : 0,
5909 sess->req.to_forward, sess->req.total,
5910 sess->req.analyse_exp ?
5911 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005912 TICKS_TO_MS(1000)) : "<NEVER>");
5913
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005914 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005915 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005916 sess->req.rex ?
5917 human_time(TICKS_TO_MS(sess->req.rex - 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 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005922 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005923 sess->req.wex ?
5924 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005925 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005926 sess->req.buf,
5927 sess->req.buf->data, sess->req.buf->o,
5928 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005929 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005930 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005931
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005932 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005933 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005934 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005935 &sess->res,
5936 sess->res.flags, sess->res.analysers,
5937 sess->res.pipe ? sess->res.pipe->data : 0,
5938 sess->res.to_forward, sess->res.total,
5939 sess->res.analyse_exp ?
5940 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005941 TICKS_TO_MS(1000)) : "<NEVER>");
5942
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005943 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005944 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005945 sess->res.rex ?
5946 human_time(TICKS_TO_MS(sess->res.rex - 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 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005951 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005952 sess->res.wex ?
5953 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005954 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005955 sess->res.buf,
5956 sess->res.buf->data, sess->res.buf->o,
5957 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005958 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005959 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005960
Willy Tarreaubc18da12015-03-13 14:00:47 +01005961 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005962 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005963 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005964 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005965
5966 /* use other states to dump the contents */
5967 }
5968 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005969 appctx->ctx.sess.uid = 0;
5970 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005971 return 1;
5972}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005973
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02005974#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
5975static int stats_tlskeys_list(struct stream_interface *si) {
5976 struct appctx *appctx = __objt_appctx(si->end);
5977
5978 switch (appctx->st2) {
5979 case STAT_ST_INIT:
5980 /* Display the column headers. If the message cannot be sent,
5981 * quit the fucntion with returning 0. The function is called
5982 * later and restart at the state "STAT_ST_INIT".
5983 */
5984 chunk_reset(&trash);
5985 chunk_appendf(&trash, "# id (file)\n");
5986 if (bi_putchk(si_ic(si), &trash) == -1) {
5987 si_applet_cant_put(si);
5988 return 0;
5989 }
5990
5991 /* Now, we start the browsing of the references lists.
5992 * Note that the following call to LIST_ELEM return bad pointer. The only
5993 * avalaible field of this pointer is <list>. It is used with the function
5994 * tlskeys_list_get_next() for retruning the first avalaible entry
5995 */
5996 appctx->ctx.tlskeys.ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
5997 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
5998
5999 appctx->st2 = STAT_ST_LIST;
6000 /* fall through */
6001
6002 case STAT_ST_LIST:
6003 while (appctx->ctx.tlskeys.ref) {
6004 chunk_reset(&trash);
6005
6006 chunk_appendf(&trash, "%d (%s)\n", appctx->ctx.tlskeys.ref->unique_id,
6007 appctx->ctx.tlskeys.ref->filename);
6008
6009 if (bi_putchk(si_ic(si), &trash) == -1) {
6010 /* let's try again later from this stream. We add ourselves into
6011 * this stream's users so that it can remove us upon termination.
6012 */
6013 si_applet_cant_put(si);
6014 return 0;
6015 }
6016
6017 /* get next list entry and check the end of the list */
6018 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
6019 }
6020
6021 appctx->st2 = STAT_ST_FIN;
6022 /* fall through */
6023
6024 default:
6025 appctx->st2 = STAT_ST_FIN;
6026 return 1;
6027 }
6028 return 0;
6029}
6030#endif
6031
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006032static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006033{
6034 struct appctx *appctx = __objt_appctx(si->end);
6035
6036 switch (appctx->st2) {
6037 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006038 /* Display the column headers. If the message cannot be sent,
6039 * quit the fucntion with returning 0. The function is called
6040 * later and restart at the state "STAT_ST_INIT".
6041 */
6042 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01006043 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006044 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006045 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006046 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006047 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006048
6049 /* Now, we start the browsing of the references lists.
6050 * Note that the following call to LIST_ELEM return bad pointer. The only
6051 * avalaible field of this pointer is <list>. It is used with the function
6052 * pat_list_get_next() for retruning the first avalaible entry
6053 */
6054 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
6055 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6056 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006057 appctx->st2 = STAT_ST_LIST;
6058 /* fall through */
6059
6060 case STAT_ST_LIST:
6061 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006062 chunk_reset(&trash);
6063
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006064 /* Build messages. If the reference is used by another category than
6065 * the listed categorie, display the information in the massage.
6066 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01006067 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01006068 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
6069 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006070
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006071 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006072 /* let's try again later from this stream. We add ourselves into
6073 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006074 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006075 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006076 return 0;
6077 }
6078
6079 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006080 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6081 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006082 }
6083
6084 appctx->st2 = STAT_ST_FIN;
6085 /* fall through */
6086
6087 default:
6088 appctx->st2 = STAT_ST_FIN;
6089 return 1;
6090 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006091 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006092}
6093
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006094static int stats_map_lookup(struct stream_interface *si)
6095{
6096 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006097 struct sample sample;
6098 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006099 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006100
6101 switch (appctx->st2) {
6102 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006103 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01006104 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006105 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006106 appctx->st2 = STAT_ST_LIST;
6107 /* fall through */
6108
6109 case STAT_ST_LIST:
6110 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006111 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006112 /* initialise chunk to build new message */
6113 chunk_reset(&trash);
6114
6115 /* execute pattern matching */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006116 sample.data.type = SMP_T_STR;
Andreas Seltenreich9727cf42016-03-03 19:32:25 +01006117 sample.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006118 sample.data.u.str.len = appctx->ctx.map.chunk.len;
6119 sample.data.u.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01006120 if (appctx->ctx.map.expr->pat_head->match &&
6121 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006122 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
6123 else
6124 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006125
6126 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006127 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
6128 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
6129 break;
6130 if (match_method >= PAT_MATCH_NUM)
6131 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
6132 else
6133 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006134
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006135 /* case sensitive */
6136 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
6137 chunk_appendf(&trash, ", case=insensitive");
6138 else
6139 chunk_appendf(&trash, ", case=sensitive");
6140
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006141 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006142 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006143 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6144 chunk_appendf(&trash, ", found=no");
6145 else
6146 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006147 }
6148
6149 /* Display match and match info */
6150 else {
6151 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006152 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6153 chunk_appendf(&trash, ", found=yes");
6154 else
6155 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006156
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006157 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006158 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006159 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006160 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006161 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006162
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006163 /* display pattern */
6164 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
6165 if (pat->ref && pat->ref->pattern)
6166 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
6167 else
6168 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006169 }
6170 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006171 if (pat->ref && pat->ref->pattern)
6172 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
6173 else
6174 chunk_appendf(&trash, ", pattern=unknown");
6175 }
6176
6177 /* display return value */
6178 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02006179 if (pat->data && pat->ref && pat->ref->sample)
Thierry FOURNIER03d0e452015-07-23 18:33:41 +02006180 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
Thierry FOURNIER7d4335c2015-08-19 09:05:25 +02006181 smp_to_type[pat->data->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006182 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006183 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006184 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006185 }
6186
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006187 chunk_appendf(&trash, "\n");
6188
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006189 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006190 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006191 /* let's try again later from this stream. We add ourselves into
6192 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006193 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006194 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006195 return 0;
6196 }
6197
6198 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006199 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
6200 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006201 }
6202
6203 appctx->st2 = STAT_ST_FIN;
6204 /* fall through */
6205
6206 default:
6207 appctx->st2 = STAT_ST_FIN;
6208 free(appctx->ctx.map.chunk.str);
6209 return 1;
6210 }
6211}
6212
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006213static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006214{
6215 struct appctx *appctx = __objt_appctx(si->end);
6216
6217 switch (appctx->st2) {
6218
6219 case STAT_ST_INIT:
6220 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006221 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
6222 struct pat_ref_elt *, list);
6223 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
6224 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006225 appctx->st2 = STAT_ST_LIST;
6226 /* fall through */
6227
6228 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006229 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006230 chunk_reset(&trash);
6231
6232 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006233 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006234 chunk_appendf(&trash, "%p %s %s\n",
6235 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
6236 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006237 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006238 chunk_appendf(&trash, "%p %s\n",
6239 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006240
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006241 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006242 /* let's try again later from this stream. We add ourselves into
6243 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006244 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006245 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006246 return 0;
6247 }
6248
6249 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006250 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
6251 struct pat_ref_elt *, list);
6252 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006253 break;
6254 }
6255
6256 appctx->st2 = STAT_ST_FIN;
6257 /* fall through */
6258
6259 default:
6260 appctx->st2 = STAT_ST_FIN;
6261 return 1;
6262 }
6263}
6264
Willy Tarreau87b09662015-04-03 00:22:06 +02006265/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006266 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006267 * to be called again, otherwise non-zero. It is designed to be called
6268 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006269 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006270static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006271{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006272 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006273 struct connection *conn;
6274
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006275 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006276 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02006277 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006278 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006279 if (appctx->st2 == STAT_ST_LIST) {
6280 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6281 LIST_DEL(&appctx->ctx.sess.bref.users);
6282 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006283 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006284 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006285 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006286 }
6287
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006288 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006289
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006290 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006291 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006292 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02006293 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006294 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02006295 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006296 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02006297 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006298 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006299 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02006300 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006301 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006302 /* fall through */
6303
Willy Tarreau295a8372011-03-10 11:25:07 +01006304 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02006305 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006306 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6307 LIST_DEL(&appctx->ctx.sess.bref.users);
6308 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006309 }
6310
6311 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02006312 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01006313 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02006314 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006315
Willy Tarreau87b09662015-04-03 00:22:06 +02006316 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006317
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006318 if (appctx->ctx.sess.target) {
6319 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006320 goto next_sess;
6321
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006322 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006323 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01006324 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006325 return 0;
6326
Willy Tarreau87b09662015-04-03 00:22:06 +02006327 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006328 LIST_DEL(&appctx->ctx.sess.bref.users);
6329 LIST_INIT(&appctx->ctx.sess.bref.users);
6330 if (appctx->ctx.sess.target != (void *)-1) {
6331 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01006332 break;
6333 }
6334 else
6335 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006336 }
6337
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006338 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006339 "%p: proto=%s",
6340 curr_sess,
Willy Tarreauf1e02122015-09-23 12:16:43 +02006341 strm_li(curr_sess) ? strm_li(curr_sess)->proto->name : "?");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006342
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006343 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006344 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02006345 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006346 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006347 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006348 " src=%s:%d fe=%s be=%s srv=%s",
6349 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006350 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006351 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006352 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006353 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006354 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006355 break;
6356 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006357 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02006358 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006359 strm_li(curr_sess)->luid,
6360 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006361 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006362 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02006363 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006364 break;
6365 }
6366
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006367 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02006368 " ts=%02x age=%s calls=%d",
6369 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01006370 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
6371 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006372
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006373 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006374 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006375 curr_sess->req.flags,
6376 curr_sess->req.buf->i,
6377 curr_sess->req.analysers,
6378 curr_sess->req.rex ?
6379 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006380 TICKS_TO_MS(1000)) : "");
6381
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006382 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006383 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006384 curr_sess->req.wex ?
6385 human_time(TICKS_TO_MS(curr_sess->req.wex - 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 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006390 curr_sess->req.analyse_exp ?
6391 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - 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 Tarreau02d6cfc2012-03-01 18:19:58 +01006395 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006396 curr_sess->res.flags,
6397 curr_sess->res.buf->i,
6398 curr_sess->res.analysers,
6399 curr_sess->res.rex ?
6400 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006401 TICKS_TO_MS(1000)) : "");
6402
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006403 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006404 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006405 curr_sess->res.wex ?
6406 human_time(TICKS_TO_MS(curr_sess->res.wex - 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 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006411 curr_sess->res.analyse_exp ?
6412 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006413 TICKS_TO_MS(1000)) : "");
6414
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006415 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006416 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006417 " s0=[%d,%1xh,fd=%d,ex=%s]",
6418 curr_sess->si[0].state,
6419 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006420 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006421 curr_sess->si[0].exp ?
6422 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
6423 TICKS_TO_MS(1000)) : "");
6424
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006425 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006426 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006427 " s1=[%d,%1xh,fd=%d,ex=%s]",
6428 curr_sess->si[1].state,
6429 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006430 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006431 curr_sess->si[1].exp ?
6432 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
6433 TICKS_TO_MS(1000)) : "");
6434
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006435 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006436 " exp=%s",
6437 curr_sess->task->expire ?
6438 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
6439 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01006440 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006441 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006442
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006443 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006444
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006445 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006446 /* let's try again later from this stream. We add ourselves into
6447 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006448 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006449 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006450 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006451 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006452 }
6453
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006454 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006455 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006456 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006457
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006458 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006459 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006460 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006461 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006462 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006463 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006464
Willy Tarreaubc18da12015-03-13 14:00:47 +01006465 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006466 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006467 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006468 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006469
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006470 appctx->ctx.sess.target = NULL;
6471 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006472 return 1;
6473 }
6474
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006475 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006476 /* fall through */
6477
6478 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006479 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006480 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006481 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02006482}
6483
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006484/* This is called when the stream interface is closed. For instance, upon an
6485 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02006486 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006487 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02006488static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006489{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006490 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
6491 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
6492 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006493 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006494 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
6495 free(appctx->ctx.cli.err);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006496 appctx->ctx.cli.err = NULL;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006497 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006498 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
6499 free(appctx->ctx.map.chunk.str);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006500 appctx->ctx.map.chunk.str = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006501 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006502}
6503
Willy Tarreau20e99322013-04-13 09:22:25 +02006504/* This function is used to either dump tables states (when action is set
6505 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02006506 * It returns 0 if the output buffer is full and it needs to be called
6507 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02006508 */
Willy Tarreau20e99322013-04-13 09:22:25 +02006509static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02006510{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006511 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02006512 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006513 struct ebmb_node *eb;
6514 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01006515 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02006516 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006517
6518 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006519 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01006520 * - STAT_ST_INIT : the first call
6521 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02006522 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006523 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02006524 * and the entry pointer points to the next entry to be dumped,
6525 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006526 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02006527 * data though.
6528 */
6529
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006530 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006531 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006532 if (appctx->st2 == STAT_ST_LIST) {
6533 appctx->ctx.table.entry->ref_cnt--;
6534 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02006535 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02006536 return 1;
6537 }
6538
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006539 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006540
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006541 while (appctx->st2 != STAT_ST_FIN) {
6542 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006543 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006544 appctx->ctx.table.proxy = appctx->ctx.table.target;
6545 if (!appctx->ctx.table.proxy)
6546 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006547
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006548 appctx->ctx.table.entry = NULL;
6549 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006550 break;
6551
Willy Tarreau295a8372011-03-10 11:25:07 +01006552 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006553 if (!appctx->ctx.table.proxy ||
6554 (appctx->ctx.table.target &&
6555 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6556 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006557 break;
6558 }
6559
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006560 if (appctx->ctx.table.proxy->table.size) {
6561 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6562 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006563 return 0;
6564
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006565 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006566 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006567 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006568 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006569 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006570 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6571 appctx->ctx.table.entry->ref_cnt++;
6572 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006573 break;
6574 }
6575 }
6576 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006577 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006578 break;
6579
Willy Tarreau295a8372011-03-10 11:25:07 +01006580 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006581 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006582
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006583 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006584 /* we're filtering on some data contents */
6585 void *ptr;
6586 long long data;
6587
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006588 dt = appctx->ctx.table.data_type;
6589 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6590 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006591 dt);
6592
6593 data = 0;
6594 switch (stktable_data_types[dt].std_type) {
6595 case STD_T_SINT:
6596 data = stktable_data_cast(ptr, std_t_sint);
6597 break;
6598 case STD_T_UINT:
6599 data = stktable_data_cast(ptr, std_t_uint);
6600 break;
6601 case STD_T_ULL:
6602 data = stktable_data_cast(ptr, std_t_ull);
6603 break;
6604 case STD_T_FRQP:
6605 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006606 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006607 break;
6608 }
6609
6610 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006611 if ((data < appctx->ctx.table.value &&
6612 (appctx->ctx.table.data_op == STD_OP_EQ ||
6613 appctx->ctx.table.data_op == STD_OP_GT ||
6614 appctx->ctx.table.data_op == STD_OP_GE)) ||
6615 (data == appctx->ctx.table.value &&
6616 (appctx->ctx.table.data_op == STD_OP_NE ||
6617 appctx->ctx.table.data_op == STD_OP_GT ||
6618 appctx->ctx.table.data_op == STD_OP_LT)) ||
6619 (data > appctx->ctx.table.value &&
6620 (appctx->ctx.table.data_op == STD_OP_EQ ||
6621 appctx->ctx.table.data_op == STD_OP_LT ||
6622 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006623 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006624 }
6625
Simon Hormanc88b8872011-06-15 15:18:49 +09006626 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006627 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6628 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006629 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006630
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006631 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006632
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006633 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006634 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006635 struct stksess *old = appctx->ctx.table.entry;
6636 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006637 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006638 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6639 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6640 stksess_kill(&appctx->ctx.table.proxy->table, old);
6641 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006642 break;
6643 }
6644
Simon Hormanc88b8872011-06-15 15:18:49 +09006645
6646 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006647 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6648 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6649 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006650
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006651 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6652 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006653 break;
6654
Willy Tarreau295a8372011-03-10 11:25:07 +01006655 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006656 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006657 break;
6658 }
6659 }
6660 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006661}
6662
Willy Tarreaud426a182010-03-05 14:58:26 +01006663/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006664 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6665 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6666 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6667 * lines are respected within the limit of 70 output chars. Lines that are
6668 * continuation of a previous truncated line begin with "+" instead of " "
6669 * after the offset. The new pointer is returned.
6670 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006671static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6672 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006673{
6674 int end;
6675 unsigned char c;
6676
6677 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006678 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006679 return ptr;
6680
Willy Tarreau77804732012-10-29 16:14:26 +01006681 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006682
Willy Tarreaud426a182010-03-05 14:58:26 +01006683 while (ptr < len && ptr < bsize) {
6684 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006685 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006686 if (out->len > end - 2)
6687 break;
6688 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006689 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006690 if (out->len > end - 3)
6691 break;
6692 out->str[out->len++] = '\\';
6693 switch (c) {
6694 case '\t': c = 't'; break;
6695 case '\n': c = 'n'; break;
6696 case '\r': c = 'r'; break;
6697 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006698 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006699 }
6700 out->str[out->len++] = c;
6701 } else {
6702 if (out->len > end - 5)
6703 break;
6704 out->str[out->len++] = '\\';
6705 out->str[out->len++] = 'x';
6706 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6707 out->str[out->len++] = hextab[c & 0xF];
6708 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006709 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006710 /* we had a line break, let's return now */
6711 out->str[out->len++] = '\n';
6712 *line = ptr;
6713 return ptr;
6714 }
6715 }
6716 /* we have an incomplete line, we return it as-is */
6717 out->str[out->len++] = '\n';
6718 return ptr;
6719}
6720
Baptiste Assmann3863f972015-05-17 00:33:24 +02006721/* This function dumps counters from all resolvers section and associated name servers.
6722 * It returns 0 if the output buffer is full and it needs
6723 * to be called again, otherwise non-zero.
6724 */
6725static int stats_dump_resolvers_to_buffer(struct stream_interface *si)
6726{
6727 struct appctx *appctx = __objt_appctx(si->end);
6728 struct dns_resolvers *presolvers;
6729 struct dns_nameserver *pnameserver;
6730
6731 chunk_reset(&trash);
6732
6733 switch (appctx->st2) {
6734 case STAT_ST_INIT:
6735 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
6736 /* fall through */
6737
6738 case STAT_ST_LIST:
Andrew Hayworth68d05342015-10-02 20:33:01 +00006739 if (LIST_ISEMPTY(&dns_resolvers)) {
6740 chunk_appendf(&trash, "No resolvers found\n");
6741 }
6742 else {
6743 list_for_each_entry(presolvers, &dns_resolvers, list) {
6744 if (appctx->ctx.resolvers.ptr != NULL && appctx->ctx.resolvers.ptr != presolvers)
6745 continue;
6746
6747 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
6748 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
6749 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
6750 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
6751 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
6752 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
6753 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
6754 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
6755 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
6756 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
6757 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
6758 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
6759 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
6760 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
6761 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
6762 chunk_appendf(&trash, " truncated: %ld\n", pnameserver->counters.truncated);
6763 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
6764 }
6765 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02006766 }
6767
6768 /* display response */
6769 if (bi_putchk(si_ic(si), &trash) == -1) {
6770 /* let's try again later from this session. We add ourselves into
6771 * this session's users so that it can remove us upon termination.
6772 */
6773 si->flags |= SI_FL_WAIT_ROOM;
6774 return 0;
6775 }
6776
6777 appctx->st2 = STAT_ST_FIN;
6778 /* fall through */
6779
6780 default:
6781 appctx->st2 = STAT_ST_FIN;
6782 return 1;
6783 }
6784}
6785
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006786/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006787 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006788 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006789 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006790static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006791{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006792 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006793 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006794
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006795 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02006796 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006797
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006798 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006799
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006800 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006801 /* the function had not been called yet, let's prepare the
6802 * buffer for a response.
6803 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006804 struct tm tm;
6805
6806 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006807 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006808 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6809 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6810 error_snapshot_id);
6811
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006812 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01006813 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006814 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01006815 return 0;
6816 }
6817
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006818 appctx->ctx.errors.px = proxy;
6819 appctx->ctx.errors.buf = 0;
6820 appctx->ctx.errors.bol = 0;
6821 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006822 }
6823
6824 /* we have two inner loops here, one for the proxy, the other one for
6825 * the buffer.
6826 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006827 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006828 struct error_snapshot *es;
6829
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006830 if (appctx->ctx.errors.buf == 0)
6831 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006832 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006833 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006834
6835 if (!es->when.tv_sec)
6836 goto next;
6837
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006838 if (appctx->ctx.errors.iid >= 0 &&
6839 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6840 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006841 goto next;
6842
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006843 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006844 /* just print headers now */
6845
6846 char pn[INET6_ADDRSTRLEN];
6847 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006848 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006849
6850 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006851 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006852 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006853 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006854
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006855 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6856 case AF_INET:
6857 case AF_INET6:
6858 port = get_host_port(&es->src);
6859 break;
6860 default:
6861 port = 0;
6862 }
6863
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006864 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006865 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006866 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006867 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006868 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006869 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006870 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
6871 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006872 break;
6873 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006874 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02006875 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006876 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006877 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006878 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006879 break;
6880 }
6881
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006882 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006883 ", server %s (#%d), event #%u\n"
6884 " src %s:%d, session #%d, session flags 0x%08x\n"
6885 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
6886 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
6887 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
6888 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
6889 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
6890 es->ev_id,
6891 pn, port, es->sid, es->s_flags,
6892 es->state, es->m_flags, es->t_flags,
6893 es->m_clen, es->m_blen,
6894 es->b_flags, es->b_out, es->b_tot,
6895 es->len, es->b_wrap, es->pos);
6896
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006897 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006898 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006899 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006900 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006901 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006902 appctx->ctx.errors.ptr = 0;
6903 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006904 }
6905
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006906 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006907 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006908 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006909 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006910 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006911 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006912 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006913 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01006914 goto next;
6915 }
6916
6917 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006918 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006919 int newptr;
6920 int newline;
6921
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006922 newline = appctx->ctx.errors.bol;
6923 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
6924 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02006925 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006926
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006927 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006928 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006929 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006930 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006931 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006932 appctx->ctx.errors.ptr = newptr;
6933 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006934 };
6935 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006936 appctx->ctx.errors.bol = 0;
6937 appctx->ctx.errors.ptr = -1;
6938 appctx->ctx.errors.buf++;
6939 if (appctx->ctx.errors.buf > 1) {
6940 appctx->ctx.errors.buf = 0;
6941 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006942 }
Willy Tarreauae795722016-02-16 11:27:28 +01006943 }
6944
6945 /* dump complete */
6946 return 1;
6947}
6948
6949/* This function dumps all environmnent variables to the buffer. It returns 0
6950 * if the output buffer is full and it needs to be called again, otherwise
6951 * non-zero. Dumps only one entry if st2 == STAT_ST_END.
6952 */
6953static int stats_dump_env_to_buffer(struct stream_interface *si)
6954{
6955 struct appctx *appctx = __objt_appctx(si->end);
6956
6957 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
6958 return 1;
6959
6960 chunk_reset(&trash);
6961
6962 /* we have two inner loops here, one for the proxy, the other one for
6963 * the buffer.
6964 */
6965 while (*appctx->ctx.env.var) {
6966 chunk_printf(&trash, "%s\n", *appctx->ctx.env.var);
6967
6968 if (bi_putchk(si_ic(si), &trash) == -1) {
6969 si_applet_cant_put(si);
6970 return 0;
6971 }
6972 if (appctx->st2 == STAT_ST_END)
6973 break;
6974 appctx->ctx.env.var++;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006975 }
6976
6977 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02006978 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006979}
6980
Willy Tarreaud5781202012-09-22 19:32:35 +02006981/* parse the "level" argument on the bind lines */
6982static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6983{
6984 if (!*args[cur_arg + 1]) {
6985 memprintf(err, "'%s' : missing level", args[cur_arg]);
6986 return ERR_ALERT | ERR_FATAL;
6987 }
6988
6989 if (!strcmp(args[cur_arg+1], "user"))
6990 conf->level = ACCESS_LVL_USER;
6991 else if (!strcmp(args[cur_arg+1], "operator"))
6992 conf->level = ACCESS_LVL_OPER;
6993 else if (!strcmp(args[cur_arg+1], "admin"))
6994 conf->level = ACCESS_LVL_ADMIN;
6995 else {
6996 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
6997 args[cur_arg], args[cur_arg+1]);
6998 return ERR_ALERT | ERR_FATAL;
6999 }
7000
7001 return 0;
7002}
7003
Willy Tarreau30576452015-04-13 13:50:30 +02007004struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007005 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007006 .name = "<STATS>", /* used for logging */
7007 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07007008 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007009};
7010
Willy Tarreau30576452015-04-13 13:50:30 +02007011static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007012 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007013 .name = "<CLI>", /* used for logging */
7014 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01007015 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007016};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01007017
Willy Tarreaudc13c112013-06-21 23:16:39 +02007018static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02007019 { CFG_GLOBAL, "stats", stats_parse_global },
7020 { 0, NULL, NULL },
7021}};
7022
Willy Tarreaud5781202012-09-22 19:32:35 +02007023static struct bind_kw_list bind_kws = { "STAT", { }, {
7024 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
7025 { NULL, NULL, 0 },
7026}};
7027
Willy Tarreau10522fd2008-07-09 20:12:41 +02007028__attribute__((constructor))
7029static void __dumpstats_module_init(void)
7030{
7031 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02007032 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02007033}
7034
Willy Tarreau91861262007-10-17 17:06:05 +02007035/*
7036 * Local variables:
7037 * c-indent-level: 8
7038 * c-basic-offset: 8
7039 * End:
7040 */